Skip to content

Commit

Permalink
Merge pull request #17709 from reitermarkus/from-bottle-loader-downlo…
Browse files Browse the repository at this point in the history
…adable
  • Loading branch information
MikeMcQuaid authored Jul 13, 2024
2 parents 92ef6ef + ae6f439 commit 919530c
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 45 deletions.
52 changes: 52 additions & 0 deletions Library/Homebrew/resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,58 @@ def stage(target, &block)
end
end

# A resource for a bottle manifest.
class BottleManifest < Resource
class Error < RuntimeError; end

attr_reader :bottle

def initialize(bottle)
super("#{bottle.name}_bottle_manifest")
@bottle = bottle
end

def verify_download_integrity(_filename)
# We don't have a checksum, but we can at least try parsing it.
tab
rescue Error => e
raise DownloadError.new(self, e)
end

def tab
json = begin
JSON.parse(cached_download.read)
rescue JSON::ParserError
raise Error, "The downloaded GitHub Packages manifest was corrupted or modified (it is not valid JSON): " \
"\n#{cached_download}"
end

manifests = json["manifests"]
raise Error, "Missing 'manifests' section." if manifests.blank?

manifests_annotations = manifests.filter_map { |m| m["annotations"] }
raise Error, "Missing 'annotations' section." if manifests_annotations.blank?

bottle_digest = bottle.resource.checksum.hexdigest
image_ref = GitHubPackages.version_rebuild(bottle.resource.version, bottle.rebuild, bottle.tag.to_s)
manifest_annotations = manifests_annotations.find do |m|
next if m["sh.brew.bottle.digest"] != bottle_digest

m["org.opencontainers.image.ref.name"] == image_ref
end
raise Error, "Couldn't find manifest matching bottle checksum." if manifest_annotations.blank?

tab = manifest_annotations["sh.brew.tab"]
raise Error, "Couldn't find tab from manifest." if tab.blank?

begin
JSON.parse(tab)
rescue JSON::ParserError
raise Error, "Couldn't parse tab JSON."
end
end
end

# A resource containing a patch.
class PatchResource < Resource
attr_reader :patch_files
Expand Down
48 changes: 3 additions & 45 deletions Library/Homebrew/software_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -392,14 +392,7 @@ def stage
def fetch_tab
return if github_packages_manifest_resource.blank?

# a checksum is used later identifying the correct tab but we do not have the checksum for the manifest/tab
github_packages_manifest_resource.fetch(verify_download_integrity: false)

begin
github_packages_manifest_resource_tab(github_packages_manifest_resource)
rescue RuntimeError => e
raise DownloadError.new(github_packages_manifest_resource, e)
end
github_packages_manifest_resource.fetch
rescue DownloadError
raise unless fallback_on_error

Expand All @@ -415,7 +408,7 @@ def fetch_tab
def tab_attributes
return {} unless github_packages_manifest_resource&.downloaded?

github_packages_manifest_resource_tab(github_packages_manifest_resource)
github_packages_manifest_resource.tab
end

sig { returns(Filename) }
Expand All @@ -425,46 +418,11 @@ def filename

private

def github_packages_manifest_resource_tab(github_packages_manifest_resource)
manifest_json = github_packages_manifest_resource.cached_download.read

json = begin
JSON.parse(manifest_json)
rescue JSON::ParserError
raise "The downloaded GitHub Packages manifest was corrupted or modified (it is not valid JSON): " \
"\n#{github_packages_manifest_resource.cached_download}"
end

manifests = json["manifests"]
raise ArgumentError, "Missing 'manifests' section." if manifests.blank?

manifests_annotations = manifests.filter_map { |m| m["annotations"] }
raise ArgumentError, "Missing 'annotations' section." if manifests_annotations.blank?

bottle_digest = @resource.checksum.hexdigest
image_ref = GitHubPackages.version_rebuild(@resource.version, rebuild, @tag.to_s)
manifest_annotations = manifests_annotations.find do |m|
next if m["sh.brew.bottle.digest"] != bottle_digest

m["org.opencontainers.image.ref.name"] == image_ref
end
raise ArgumentError, "Couldn't find manifest matching bottle checksum." if manifest_annotations.blank?

tab = manifest_annotations["sh.brew.tab"]
raise ArgumentError, "Couldn't find tab from manifest." if tab.blank?

begin
JSON.parse(tab)
rescue JSON::ParserError
raise ArgumentError, "Couldn't parse tab JSON."
end
end

def github_packages_manifest_resource
return if @resource.download_strategy != CurlGitHubPackagesDownloadStrategy

@github_packages_manifest_resource ||= begin
resource = Resource.new("#{name}_bottle_manifest")
resource = Resource::BottleManifest.new(self)

version_rebuild = GitHubPackages.version_rebuild(@resource.version, rebuild)
resource.version(version_rebuild)
Expand Down

0 comments on commit 919530c

Please sign in to comment.