Factor out some code into functions

This commit is contained in:
Maxim Belkin 2019-11-19 13:08:28 -06:00
parent 407baf4bcd
commit 845f65b945
No known key found for this signature in database
GPG Key ID: AC71560D4C5F2338
2 changed files with 27 additions and 22 deletions

View File

@ -341,28 +341,7 @@ module Homebrew
remote_url = Utils.popen_read("git remote get-url --push origin").chomp
username = formula.tap.user
else
begin
response = GitHub.create_fork(formula.tap.full_name)
# GitHub API responds immediately but fork takes a few seconds to be ready.
sleep 3
if system("git", "config", "--local", "--get-regexp", "remote\..*\.url", "git@github.com:.*")
remote_url = response.fetch("ssh_url")
else
remote_url = response.fetch("clone_url")
end
username = response.fetch("owner").fetch("login")
rescue GitHub::AuthenticationFailedError => e
raise unless e.github_message.match?(/forking is disabled/)
# If the repository is private, forking might be disabled.
# Create branches in the repository itself instead.
remote_url = Utils.popen_read("git remote get-url --push origin").chomp
username = formula.tap.user
rescue *GitHub.api_errors => e
formula.path.atomic_write(backup_file) unless args.dry_run?
odie "Unable to fork: #{e.message}!"
end
remote_url, username = forked_repo_info(formula)
end
safe_system "git", "fetch", "--unshallow", "origin" if shallow
@ -401,6 +380,23 @@ module Homebrew
end
end
def forked_repo_info(formula)
response = GitHub.create_fork(formula.tap.full_name)
rescue GitHub::AuthenticationFailedError, *GitHub.api_errors => e
formula.path.atomic_write(backup_file) unless args.dry_run?
odie "Unable to fork: #{e.message}!"
else
# GitHub API responds immediately but fork takes a few seconds to be ready.
sleep 1 until GitHub.check_fork_exists(formula.tap.full_name)
if system("git", "config", "--local", "--get-regexp", "remote\..*\.url", "git@github.com:.*")
remote_url = response.fetch("ssh_url")
else
remote_url = response.fetch("clone_url")
end
username = response.fetch("owner").fetch("login")
[remote_url, username]
end
def inreplace_pairs(path, replacement_pairs)
if args.dry_run?
contents = path.open("r") { |f| Formulary.ensure_utf8_encoding(f).read }

View File

@ -340,6 +340,15 @@ module GitHub
open_api(url, data: data, scopes: scopes)
end
def check_fork_exists(repo)
username = api_credentials[1]
reponame = repo.split("/")[1]
json = open_api(url_to("repos", username, reponame))
return false if json["message"] == "Not Found"
true
end
def create_pull_request(repo, title, head, base, body)
url = "#{API_URL}/repos/#{repo}/pulls"
data = { title: title, head: head, base: base, body: body }