From 51d167c1220378af039a87fbfdee00c5dc7cb086 Mon Sep 17 00:00:00 2001 From: Seeker Date: Fri, 4 Sep 2020 06:18:34 -0700 Subject: [PATCH] utils/github: add `create_bump_pr` --- Library/Homebrew/dev-cmd/bump-formula-pr.rb | 86 ++++----------------- Library/Homebrew/utils/github.rb | 84 ++++++++++++++++++++ 2 files changed, 97 insertions(+), 73 deletions(-) diff --git a/Library/Homebrew/dev-cmd/bump-formula-pr.rb b/Library/Homebrew/dev-cmd/bump-formula-pr.rb index fefc2831e3..69220109b2 100644 --- a/Library/Homebrew/dev-cmd/bump-formula-pr.rb +++ b/Library/Homebrew/dev-cmd/bump-formula-pr.rb @@ -340,64 +340,19 @@ module Homebrew run_audit(formula, alias_rename, old_contents, args: args) - formula.path.parent.cd do - _, base_branch = origin_branch.split("/") - branch = "bump-#{formula.name}-#{new_formula_version}" - git_dir = Utils.popen_read("git rev-parse --git-dir").chomp - shallow = !git_dir.empty? && File.exist?("#{git_dir}/shallow") - changed_files = [formula.path] - changed_files += alias_rename if alias_rename.present? - - if args.dry_run? || (args.write? && !args.commit?) - ohai "try to fork repository with GitHub API" unless args.no_fork? - ohai "git fetch --unshallow origin" if shallow - ohai "git add #{alias_rename.first} #{alias_rename.last}" if alias_rename.present? - ohai "git checkout --no-track -b #{branch} #{origin_branch}" - ohai "git commit --no-edit --verbose --message='#{formula.name} " \ - "#{new_formula_version}' -- #{changed_files.join(" ")}" - ohai "git push --set-upstream $HUB_REMOTE #{branch}:#{branch}" - ohai "git checkout --quiet #{previous_branch}" - ohai "create pull request with GitHub API (base branch: #{base_branch})" - else - - safe_system "git", "fetch", "--unshallow", "origin" if shallow && !args.commit? - safe_system "git", "add", *alias_rename if alias_rename.present? - safe_system "git", "checkout", "--no-track", "-b", branch, origin_branch unless args.commit? - safe_system "git", "commit", "--no-edit", "--verbose", - "--message=#{formula.name} #{new_formula_version}", - "--", *changed_files - return if args.commit? - - remote_url, username = determine_remote_and_username(formula, tap_full_name, old_contents, args: args) - safe_system "git", "push", "--set-upstream", remote_url, "#{branch}:#{branch}" - safe_system "git", "checkout", "--quiet", previous_branch - pr_message = <<~EOS - Created with `brew bump-formula-pr`. - EOS - user_message = args.message - if user_message - pr_message += <<~EOS - - --- - - #{user_message} - EOS - end - pr_title = "#{formula.name} #{new_formula_version}" - - begin - url = GitHub.create_pull_request(tap_full_name, pr_title, - "#{username}:#{branch}", base_branch, pr_message)["html_url"] - if args.no_browse? - puts url - else - exec_browser url - end - rescue *GitHub.api_errors => e - odie "Unable to open pull request: #{e.message}!" - end - end - end + pr_info = { + sourcefile_path: formula.path, + old_contents: old_contents, + additional_files: alias_rename, + origin_branch: origin_branch, + branch_name: "bump-#{formula.name}-#{new_formula_version}", + commit_message: "#{formula.name} #{new_formula_version}", + previous_branch: previous_branch, + tap: formula.tap, + tap_full_name: tap_full_name, + pr_message: "Created with `brew bump-formula-pr`.", + } + GitHub.create_bump_pr(pr_info, args: args) end def determine_formula_from_url(url) @@ -507,19 +462,4 @@ module Homebrew FileUtils.mv alias_rename.last, alias_rename.first if alias_rename.present? odie "`brew audit` failed!" end - - def determine_remote_and_username(formula, tap_full_name, old_contents, args:) - if args.no_fork? - remote_url = Utils.popen_read("git remote get-url --push origin").chomp - username = formula.tap.user - [remote_url, username] - else - begin - GitHub.forked_repo_info!(tap_full_name) - rescue *GitHub.api_errors => e - formula.path.atomic_write(old_contents) - odie "Unable to fork: #{e.message}!" - end - end - end end diff --git a/Library/Homebrew/utils/github.rb b/Library/Homebrew/utils/github.rb index c9d0dc77f3..0d6f66eb51 100644 --- a/Library/Homebrew/utils/github.rb +++ b/Library/Homebrew/utils/github.rb @@ -630,4 +630,88 @@ module GitHub username = response.fetch("owner").fetch("login") [remote_url, username] end + + def create_bump_pr(info, args:) + sourcefile_path = info[:sourcefile_path] + old_contents = info[:old_contents] + additional_files = info[:additional_files] || [] + origin_branch = info[:origin_branch] + branch = info[:branch_name] + commit_message = info[:commit_message] + previous_branch = info[:previous_branch] + tap = info[:tap] + tap_full_name = info[:tap_full_name] + pr_message = info[:pr_message] + + sourcefile_path.parent.cd do + _, base_branch = origin_branch.split("/") + git_dir = Utils.popen_read("git rev-parse --git-dir").chomp + shallow = !git_dir.empty? && File.exist?("#{git_dir}/shallow") + changed_files = [sourcefile_path] + changed_files += additional_files if additional_files.present? + + if args.dry_run? || (args.write? && !args.commit?) + ohai "try to fork repository with GitHub API" unless args.no_fork? + ohai "git fetch --unshallow origin" if shallow + ohai "git add #{changed_files.join(" ")}" + ohai "git checkout --no-track -b #{branch} #{origin_branch}" + ohai "git commit --no-edit --verbose --message='#{commit_message}'" \ + " -- #{changed_files.join(" ")}" + ohai "git push --set-upstream $HUB_REMOTE #{branch}:#{branch}" + ohai "git checkout --quiet #{previous_branch}" + ohai "create pull request with GitHub API (base branch: #{base_branch})" + else + + unless args.commit? + if args.no_fork? + remote_url = Utils.popen_read("git remote get-url --push origin").chomp + username = tap.user + else + begin + remote_url, username = GitHub.forked_repo_info!(tap_full_name) + rescue *GitHub.api_errors => e + sourcefile_path.atomic_write(old_contents) + odie "Unable to fork: #{e.message}!" + end + end + + safe_system "git", "fetch", "--unshallow", "origin" if shallow + end + + safe_system "git", "add", *changed_files + safe_system "git", "checkout", "--no-track", "-b", branch, origin_branch unless args.commit? + safe_system "git", "commit", "--no-edit", "--verbose", + "--message=#{commit_message}", + "--", *changed_files + return if args.commit? + + safe_system "git", "push", "--set-upstream", remote_url, "#{branch}:#{branch}" + safe_system "git", "checkout", "--quiet", previous_branch + pr_message = <<~EOS + #{pr_message} + EOS + user_message = args.message + if user_message + pr_message += <<~EOS + + --- + + #{user_message} + EOS + end + + begin + url = GitHub.create_pull_request(tap_full_name, commit_message, + "#{username}:#{branch}", base_branch, pr_message)["html_url"] + if args.no_browse? + puts url + else + exec_browser url + end + rescue *GitHub.api_errors => e + odie "Unable to open pull request: #{e.message}!" + end + end + end + end end