utils/github: add forked_repo_info!

This commit is contained in:
Seeker 2020-08-16 10:35:38 -07:00
parent 9a63811e8f
commit d0c7c4fd71
2 changed files with 24 additions and 22 deletions

View File

@ -362,7 +362,9 @@ module Homebrew
remote_url = Utils.popen_read("git remote get-url --push origin").chomp
username = formula.tap.user
else
remote_url, username = forked_repo_info(formula, tap_full_name, old_contents)
remote_url, username = GitHub.forked_repo_info!(tap_full_name) do
formula.path.atomic_write(old_contents)
end
end
safe_system "git", "fetch", "--unshallow", "origin" if shallow
@ -444,27 +446,6 @@ module Homebrew
[resource.fetch, forced_version]
end
def forked_repo_info(formula, tap_full_name, old_contents)
response = GitHub.create_fork(tap_full_name)
rescue GitHub::AuthenticationFailedError, *GitHub.api_errors => e
formula.path.atomic_write(old_contents)
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(tap_full_name)
remote_url = if system("git", "config", "--local", "--get-regexp", "remote\..*\.url", "git@github.com:.*")
response.fetch("ssh_url")
else
url = response.fetch("clone_url")
if (api_token = Homebrew::EnvConfig.github_api_token)
url.gsub!(%r{^https://github\.com/}, "https://#{api_token}@github.com/")
end
url
end
username = response.fetch("owner").fetch("login")
[remote_url, username]
end
def formula_version(formula, spec, contents = nil)
name = formula.name
path = formula.path

View File

@ -603,4 +603,25 @@ module GitHub
EOS
end
end
def forked_repo_info!(tap_full_name)
response = GitHub.create_fork(tap_full_name)
rescue GitHub::AuthenticationFailedError, *GitHub.api_errors => e
yield
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(tap_full_name)
remote_url = if system("git", "config", "--local", "--get-regexp", "remote\..*\.url", "git@github.com:.*")
response.fetch("ssh_url")
else
url = response.fetch("clone_url")
if (api_token = Homebrew::EnvConfig.github_api_token)
url.gsub!(%r{^https://github\.com/}, "https://#{api_token}@github.com/")
end
url
end
username = response.fetch("owner").fetch("login")
[remote_url, username]
end
end