Merge pull request #14112 from hyuraku/move-two-methods-to-utils/github

move two methods from download_strategy to utils/github
This commit is contained in:
Mike McQuaid 2022-11-08 14:11:47 +00:00 committed by GitHub
commit 68b944a94f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 37 additions and 32 deletions

View File

@ -12,6 +12,7 @@ require "mechanize/version"
require "mechanize/http/content_disposition_parser"
require "utils/curl"
require "utils/github"
require "github_packages"
@ -1069,43 +1070,13 @@ class GitHubGitDownloadStrategy < GitDownloadStrategy
@repo = repo
end
def github_last_commit
# TODO: move to Utils::GitHub
return if Homebrew::EnvConfig.no_github_api?
output, _, status = curl_output(
"--silent", "--head", "--location",
"-H", "Accept: application/vnd.github.sha",
"https://api.github.com/repos/#{@user}/#{@repo}/commits/#{@ref}"
)
return unless status.success?
commit = output[/^ETag: "(\h+)"/, 1]
version.update_commit(commit) if commit
commit
end
def multiple_short_commits_exist?(commit)
# TODO: move to Utils::GitHub
return if Homebrew::EnvConfig.no_github_api?
output, _, status = curl_output(
"--silent", "--head", "--location",
"-H", "Accept: application/vnd.github.sha",
"https://api.github.com/repos/#{@user}/#{@repo}/commits/#{commit}"
)
!(status.success? && output && output[/^Status: (200)/, 1] == "200")
end
def commit_outdated?(commit)
@last_commit ||= github_last_commit
@last_commit ||= GitHub.last_commit(@user, @repo, @ref)
if @last_commit
return true unless commit
return true unless @last_commit.start_with?(commit)
if multiple_short_commits_exist?(commit)
if GitHub.multiple_short_commits_exist?(@user, @repo, commit)
true
else
version.update_commit(commit)

View File

@ -666,4 +666,37 @@ module GitHub
pr_data = API.open_rest(url_to("repos", user, repo, "pulls", pr))
pr_data["labels"].map { |label| label["name"] }
end
def last_commit(user, repo, ref)
return if Homebrew::EnvConfig.no_github_api?
output, _, status = curl_output(
"--silent", "--head", "--location",
"--header", "Accept: application/vnd.github.sha",
url_to("repos", user, repo, "commits", ref).to_s
)
return unless status.success?
commit = output[/^ETag: "(\h+)"/, 1]
return if commit.blank?
version.update_commit(commit)
commit
end
def multiple_short_commits_exist?(user, repo, commit)
return if Homebrew::EnvConfig.no_github_api?
output, _, status = curl_output(
"--silent", "--head", "--location",
"--header", "Accept: application/vnd.github.sha",
url_to("repos", user, repo, "commits", commit).to_s
)
return true unless status.success?
return true if output.blank?
output[/^Status: (200)/, 1] != "200"
end
end

View File

@ -3,6 +3,7 @@
require "tempfile"
require "utils/shell"
require "utils/formatter"
module GitHub
API_URL = "https://api.github.com"