Merge pull request #3870 from GauthamGoli/bump-formula-gh-api
bump-formula-pr: Replace `hub` with GH API calls
This commit is contained in:
commit
54a594ea23
@ -119,7 +119,7 @@ module Homebrew
|
|||||||
def create_issue(repo, title, body)
|
def create_issue(repo, title, body)
|
||||||
url = "https://api.github.com/repos/#{repo}/issues"
|
url = "https://api.github.com/repos/#{repo}/issues"
|
||||||
data = { "title" => title, "body" => body }
|
data = { "title" => title, "body" => body }
|
||||||
scopes = GitHub::CREATE_ISSUE_SCOPES
|
scopes = GitHub::CREATE_ISSUE_FORK_OR_PR_SCOPES
|
||||||
GitHub.open_api(url, data: data, scopes: scopes)["html_url"]
|
GitHub.open_api(url, data: data, scopes: scopes)["html_url"]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@ -131,6 +131,9 @@ module Homebrew
|
|||||||
ENV[env] = homebrew_env
|
ENV[env] = homebrew_env
|
||||||
end
|
end
|
||||||
|
|
||||||
|
gh_api_errors = [GitHub::AuthenticationFailedError, GitHub::HTTPNotFoundError,
|
||||||
|
GitHub::RateLimitExceededError, GitHub::Error, JSON::ParserError].freeze
|
||||||
|
|
||||||
formula = ARGV.formulae.first
|
formula = ARGV.formulae.first
|
||||||
|
|
||||||
if formula
|
if formula
|
||||||
@ -314,73 +317,41 @@ module Homebrew
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
unless Formula["hub"].any_version_installed?
|
|
||||||
if ARGV.dry_run?
|
|
||||||
ohai "brew install hub"
|
|
||||||
else
|
|
||||||
safe_system "brew", "install", "hub"
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
formula.path.parent.cd do
|
formula.path.parent.cd do
|
||||||
branch = "#{formula.name}-#{new_formula_version}"
|
branch = "#{formula.name}-#{new_formula_version}"
|
||||||
git_dir = Utils.popen_read("git rev-parse --git-dir").chomp
|
git_dir = Utils.popen_read("git rev-parse --git-dir").chomp
|
||||||
shallow = !git_dir.empty? && File.exist?("#{git_dir}/shallow")
|
shallow = !git_dir.empty? && File.exist?("#{git_dir}/shallow")
|
||||||
|
|
||||||
hub_args = []
|
|
||||||
git_final_checkout_args = []
|
|
||||||
if ARGV.include?("--no-browse")
|
|
||||||
git_final_checkout_args << "--quiet"
|
|
||||||
else
|
|
||||||
hub_args << "--browse"
|
|
||||||
end
|
|
||||||
|
|
||||||
if ARGV.dry_run?
|
if ARGV.dry_run?
|
||||||
ohai "hub fork # read $HUB_REMOTE"
|
ohai "fork repository with GitHub API"
|
||||||
ohai "git fetch --unshallow origin" if shallow
|
ohai "git fetch --unshallow origin" if shallow
|
||||||
ohai "git checkout --no-track -b #{branch} origin/master"
|
ohai "git checkout --no-track -b #{branch} origin/master"
|
||||||
ohai "git commit --no-edit --verbose --message='#{formula.name} #{new_formula_version}#{devel_message}' -- #{formula.path}"
|
ohai "git commit --no-edit --verbose --message='#{formula.name} #{new_formula_version}#{devel_message}' -- #{formula.path}"
|
||||||
ohai "git push --set-upstream $HUB_REMOTE #{branch}:#{branch}"
|
ohai "git push --set-upstream $HUB_REMOTE #{branch}:#{branch}"
|
||||||
ohai "hub pull-request #{hub_args.join(" ")} -m '#{formula.name} #{new_formula_version}#{devel_message}'"
|
ohai "create pull request with GitHub API"
|
||||||
ohai "git checkout -"
|
ohai "git checkout -"
|
||||||
else
|
else
|
||||||
reply = IO.popen(["hub", "fork"], "r+", err: "/dev/null") do |io|
|
|
||||||
reader = Thread.new { io.read }
|
|
||||||
sleep 1
|
|
||||||
io.close_write
|
|
||||||
reader.value
|
|
||||||
end
|
|
||||||
|
|
||||||
if reply.to_s.include? "username:"
|
begin
|
||||||
|
response = GitHub.create_fork(formula.tap.full_name)
|
||||||
|
# GitHub API responds immediately but fork takes a few seconds to be ready.
|
||||||
|
sleep 3
|
||||||
|
rescue *gh_api_errors => e
|
||||||
formula.path.atomic_write(backup_file) unless ARGV.dry_run?
|
formula.path.atomic_write(backup_file) unless ARGV.dry_run?
|
||||||
git_path = "$(brew --repo #{formula.tap})" if formula.tap
|
odie "Unable to fork: #{e.message}!"
|
||||||
git_path ||= formula.path.parent
|
|
||||||
odie <<~EOS
|
|
||||||
Retry after configuring hub by running:
|
|
||||||
hub -C "#{git_path}" fork
|
|
||||||
Or setting HOMEBREW_GITHUB_TOKEN with at least 'public_repo' scope.
|
|
||||||
EOS
|
|
||||||
end
|
end
|
||||||
|
|
||||||
remote = reply[/remote:? (\S+)/, 1]
|
remote_url = response.fetch("clone_url")
|
||||||
|
username = response.fetch("owner").fetch("login")
|
||||||
# repeat for hub 2.2 backwards compatibility:
|
|
||||||
remote = Utils.popen_read("hub", "fork", err: :out)[/remote:? (\S+)/, 1] if remote.to_s.empty?
|
|
||||||
|
|
||||||
if remote.to_s.empty?
|
|
||||||
formula.path.atomic_write(backup_file) unless ARGV.dry_run?
|
|
||||||
odie "cannot get remote from 'hub'!"
|
|
||||||
end
|
|
||||||
|
|
||||||
safe_system "git", "fetch", "--unshallow", "origin" if shallow
|
safe_system "git", "fetch", "--unshallow", "origin" if shallow
|
||||||
safe_system "git", "checkout", "--no-track", "-b", branch, "origin/master"
|
safe_system "git", "checkout", "--no-track", "-b", branch, "origin/master"
|
||||||
safe_system "git", "commit", "--no-edit", "--verbose",
|
safe_system "git", "commit", "--no-edit", "--verbose",
|
||||||
"--message=#{formula.name} #{new_formula_version}#{devel_message}",
|
"--message=#{formula.name} #{new_formula_version}#{devel_message}",
|
||||||
"--", formula.path
|
"--", formula.path
|
||||||
safe_system "git", "push", "--set-upstream", remote, "#{branch}:#{branch}"
|
safe_system "git", "push", "--set-upstream", remote_url, "#{branch}:#{branch}"
|
||||||
|
safe_system "git", "checkout", "--quiet", "-"
|
||||||
pr_message = <<~EOS
|
pr_message = <<~EOS
|
||||||
#{formula.name} #{new_formula_version}#{devel_message}
|
|
||||||
|
|
||||||
Created with `brew bump-formula-pr`.
|
Created with `brew bump-formula-pr`.
|
||||||
EOS
|
EOS
|
||||||
user_message = ARGV.value("message")
|
user_message = ARGV.value("message")
|
||||||
@ -391,8 +362,19 @@ module Homebrew
|
|||||||
#{user_message}
|
#{user_message}
|
||||||
EOS
|
EOS
|
||||||
end
|
end
|
||||||
safe_system "hub", "pull-request", *hub_args, "-m", pr_message
|
pr_title = "#{formula.name} #{new_formula_version}#{devel_message}"
|
||||||
safe_system "git", "checkout", *git_final_checkout_args, "-"
|
|
||||||
|
begin
|
||||||
|
url = GitHub.create_pull_request(formula.tap.full_name, pr_title,
|
||||||
|
"#{username}:#{branch}", "master", pr_message)["html_url"]
|
||||||
|
if ARGV.include?("--no-browse")
|
||||||
|
puts url
|
||||||
|
else
|
||||||
|
exec_browser url
|
||||||
|
end
|
||||||
|
rescue *gh_api_errors => e
|
||||||
|
odie "Unable to open pull request: #{e.message}!"
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -7,8 +7,8 @@ module GitHub
|
|||||||
API_URL = "https://api.github.com".freeze
|
API_URL = "https://api.github.com".freeze
|
||||||
|
|
||||||
CREATE_GIST_SCOPES = ["gist"].freeze
|
CREATE_GIST_SCOPES = ["gist"].freeze
|
||||||
CREATE_ISSUE_SCOPES = ["public_repo"].freeze
|
CREATE_ISSUE_FORK_OR_PR_SCOPES = ["public_repo"].freeze
|
||||||
ALL_SCOPES = (CREATE_GIST_SCOPES + CREATE_ISSUE_SCOPES).freeze
|
ALL_SCOPES = (CREATE_GIST_SCOPES + CREATE_ISSUE_FORK_OR_PR_SCOPES).freeze
|
||||||
ALL_SCOPES_URL = Formatter.url("https://github.com/settings/tokens/new?scopes=#{ALL_SCOPES.join(",")}&description=Homebrew").freeze
|
ALL_SCOPES_URL = Formatter.url("https://github.com/settings/tokens/new?scopes=#{ALL_SCOPES.join(",")}&description=Homebrew").freeze
|
||||||
|
|
||||||
Error = Class.new(RuntimeError)
|
Error = Class.new(RuntimeError)
|
||||||
@ -253,6 +253,20 @@ module GitHub
|
|||||||
prs.each { |i| puts "#{i["title"]} (#{i["html_url"]})" }
|
prs.each { |i| puts "#{i["title"]} (#{i["html_url"]})" }
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def create_fork(repo)
|
||||||
|
url = "https://api.github.com/repos/#{repo}/forks"
|
||||||
|
data = {}
|
||||||
|
scopes = CREATE_ISSUE_FORK_OR_PR_SCOPES
|
||||||
|
open_api(url, data: data, scopes: scopes)
|
||||||
|
end
|
||||||
|
|
||||||
|
def create_pull_request(repo, title, head, base, body)
|
||||||
|
url = "https://api.github.com/repos/#{repo}/pulls"
|
||||||
|
data = { title: title, head: head, base: base, body: body }
|
||||||
|
scopes = CREATE_ISSUE_FORK_OR_PR_SCOPES
|
||||||
|
open_api(url, data: data, scopes: scopes)
|
||||||
|
end
|
||||||
|
|
||||||
def private_repo?(full_name)
|
def private_repo?(full_name)
|
||||||
uri = url_to "repos", full_name
|
uri = url_to "repos", full_name
|
||||||
open_api(uri) { |json| json["private"] }
|
open_api(uri) { |json| json["private"] }
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user