Simplify CurlGitHubPackagesDownloadStrategy usage

This commit is contained in:
Mike McQuaid 2021-03-31 20:26:24 +01:00
parent a50ec38d31
commit 8144fdef78
No known key found for this signature in database
GPG Key ID: 48A898132FD8EE70
2 changed files with 22 additions and 19 deletions

View File

@ -533,20 +533,18 @@ end
# #
# @api public # @api public
class CurlGitHubPackagesDownloadStrategy < CurlDownloadStrategy class CurlGitHubPackagesDownloadStrategy < CurlDownloadStrategy
attr_accessor :checksum, :name attr_writer :resolved_basename
def initialize(url, name, version, **meta)
meta ||= {}
meta[:header] = "Authorization: Bearer"
super(url, name, version, meta)
end
private private
def _fetch(url:, resolved_url:) def resolved_basename
raise CurlDownloadStrategyError, "Empty checksum" if checksum.blank? @resolved_basename.presence || super
raise CurlDownloadStrategyError, "Empty name" if name.blank?
_, org, repo, = *url.match(GitHubPackages::URL_REGEX)
# remove redundant repo prefix for a shorter name
repo = repo.delete_prefix("homebrew-")
blob_url = "#{GitHubPackages::URL_PREFIX}#{org}/#{repo}/#{name}/blobs/sha256:#{checksum}"
curl_download(blob_url, "--header", "Authorization: Bearer", to: temporary_path)
end end
end end

View File

@ -304,9 +304,18 @@ class Bottle
checksum, tag, cellar = spec.checksum_for(Utils::Bottles.tag) checksum, tag, cellar = spec.checksum_for(Utils::Bottles.tag)
filename = Filename.create(formula, tag, spec.rebuild) filename = Filename.create(formula, tag, spec.rebuild).bintray
@resource.url("#{spec.root_url}/#{filename.bintray}",
select_download_strategy(spec.root_url_specs)) # TODO: this will need adjusted when if we use GitHub Packages by default
path, resolved_basename = if (bottle_domain = Homebrew::EnvConfig.bottle_domain.presence) &&
bottle_domain.start_with?(GitHubPackages::URL_PREFIX)
["#{@name}/blobs/sha256:#{checksum}", filename]
else
filename
end
@resource.url("#{spec.root_url}/#{path}", select_download_strategy(spec.root_url_specs))
@resource.downloader.resolved_basename = resolved_basename if resolved_basename.present?
@resource.version = formula.pkg_version @resource.version = formula.pkg_version
@resource.checksum = checksum @resource.checksum = checksum
@prefix = spec.prefix @prefix = spec.prefix
@ -316,16 +325,12 @@ class Bottle
def fetch(verify_download_integrity: true) def fetch(verify_download_integrity: true)
# add the default bottle domain as a fallback mirror # add the default bottle domain as a fallback mirror
# TODO: this may need adjusted when if we use GitHub Packages by default
if @resource.download_strategy == CurlDownloadStrategy && if @resource.download_strategy == CurlDownloadStrategy &&
@resource.url.start_with?(Homebrew::EnvConfig.bottle_domain) @resource.url.start_with?(Homebrew::EnvConfig.bottle_domain)
fallback_url = @resource.url fallback_url = @resource.url
.sub(/^#{Regexp.escape(Homebrew::EnvConfig.bottle_domain)}/, .sub(/^#{Regexp.escape(Homebrew::EnvConfig.bottle_domain)}/,
HOMEBREW_BOTTLE_DEFAULT_DOMAIN) HOMEBREW_BOTTLE_DEFAULT_DOMAIN)
@resource.mirror(fallback_url) if [@resource.url, *@resource.mirrors].exclude?(fallback_url) @resource.mirror(fallback_url) if [@resource.url, *@resource.mirrors].exclude?(fallback_url)
elsif @resource.download_strategy == CurlGitHubPackagesDownloadStrategy
@resource.downloader.name = @name
@resource.downloader.checksum = @resource.checksum.hexdigest
end end
@resource.fetch(verify_download_integrity: verify_download_integrity) @resource.fetch(verify_download_integrity: verify_download_integrity)
end end
@ -380,7 +385,7 @@ class BottleSpecification
def root_url(var = nil, specs = {}) def root_url(var = nil, specs = {})
if var.nil? if var.nil?
@root_url ||= if Homebrew::EnvConfig.bottle_domain.start_with?(GitHubPackages::URL_PREFIX) @root_url ||= if Homebrew::EnvConfig.bottle_domain.start_with?(GitHubPackages::URL_PREFIX)
"#{GitHubPackages::URL_PREFIX}#{tap.full_name}" GitHubPackages.root_url(tap.user, tap.repo).to_s
else else
"#{Homebrew::EnvConfig.bottle_domain}/#{Utils::Bottles::Bintray.repository(tap)}" "#{Homebrew::EnvConfig.bottle_domain}/#{Utils::Bottles::Bintray.repository(tap)}"
end end