Merge pull request #11158 from dawidd6/bump-fork-org

bump-formula-pr: add --fork-org flag
This commit is contained in:
Dawid Dziurla 2021-04-17 14:18:39 +02:00 committed by GitHub
commit 4a183cf109
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 8 deletions

View File

@ -53,6 +53,8 @@ module Homebrew
comma_array "--mirror",
description: "Use the specified <URL> as a mirror URL. If <URL> is a comma-separated list "\
"of URLs, multiple mirrors will be added."
flag "--fork-org=",
description: "Use the specified GitHub organization for forking."
flag "--version=",
description: "Use the specified <version> to override the value parsed from the URL or tag. Note "\
"that `--version=0` can be used to delete an existing version override from a "\

View File

@ -115,17 +115,18 @@ module GitHub
puts "No pull requests found for #{query.inspect}" if open_prs.blank? && closed_prs.blank?
end
def create_fork(repo)
def create_fork(repo, org: nil)
url = "#{API_URL}/repos/#{repo}/forks"
data = {}
data[:organization] = org if org
scopes = CREATE_ISSUE_FORK_OR_PR_SCOPES
API.open_rest(url, data: data, scopes: scopes)
end
def check_fork_exists(repo)
def check_fork_exists(repo, org: nil)
_, reponame = repo.split("/")
username = API.open_rest(url_to("user")) { |json| json["login"] }
username = org || API.open_rest(url_to("user")) { |json| json["login"] }
json = API.open_rest(url_to("repos", username, reponame))
return false if json["message"] == "Not Found"
@ -450,10 +451,10 @@ module GitHub
end
end
def forked_repo_info!(tap_remote_repo)
response = create_fork(tap_remote_repo)
def forked_repo_info!(tap_remote_repo, org: nil)
response = create_fork(tap_remote_repo, org: org)
# GitHub API responds immediately but fork takes a few seconds to be ready.
sleep 1 until check_fork_exists(tap_remote_repo)
sleep 1 until check_fork_exists(tap_remote_repo, org: org)
remote_url = if system("git", "config", "--local", "--get-regexp", "remote\..*\.url", "git@github.com:.*")
response.fetch("ssh_url")
else
@ -487,7 +488,11 @@ module GitHub
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?
unless args.no_fork?
fork_message = "try to fork repository with GitHub API" \
"#{" into `#{args.fork_org}` organization" if args.fork_org}"
ohai fork_message
end
ohai "git fetch --unshallow origin" if shallow
ohai "git add #{changed_files.join(" ")}"
ohai "git checkout --no-track -b #{branch} #{remote}/#{remote_branch}"
@ -504,7 +509,7 @@ module GitHub
username = tap.user
else
begin
remote_url, username = forked_repo_info!(tap_remote_repo)
remote_url, username = forked_repo_info!(tap_remote_repo, org: args.fork_org)
rescue *API::ERRORS => e
sourcefile_path.atomic_write(old_contents)
odie "Unable to fork: #{e.message}!"