Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create Resource::BottleManifest. #17709

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
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

Check warning on line 278 in Library/Homebrew/resource.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/resource.rb#L277-L278

Added lines #L277 - L278 were not covered by tests
end

def verify_download_integrity(_filename)
# We don't have a checksum, but we can at least try parsing it.
tab

Check warning on line 283 in Library/Homebrew/resource.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/resource.rb#L283

Added line #L283 was not covered by tests
rescue Error => e
raise DownloadError.new(self, e)

Check warning on line 285 in Library/Homebrew/resource.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/resource.rb#L285

Added line #L285 was not covered by tests
end

def tab
json = begin
JSON.parse(cached_download.read)

Check warning on line 290 in Library/Homebrew/resource.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/resource.rb#L290

Added line #L290 was not covered by tests
rescue JSON::ParserError
raise Error, "The downloaded GitHub Packages manifest was corrupted or modified (it is not valid JSON): " \

Check warning on line 292 in Library/Homebrew/resource.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/resource.rb#L292

Added line #L292 was not covered by tests
"\n#{cached_download}"
end

manifests = json["manifests"]

Check warning on line 296 in Library/Homebrew/resource.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/resource.rb#L296

Added line #L296 was not covered by tests
raise Error, "Missing 'manifests' section." if manifests.blank?

manifests_annotations = manifests.filter_map { |m| m["annotations"] }

Check warning on line 299 in Library/Homebrew/resource.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/resource.rb#L299

Added line #L299 was not covered by tests
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|

Check warning on line 304 in Library/Homebrew/resource.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/resource.rb#L302-L304

Added lines #L302 - L304 were not covered by tests
next if m["sh.brew.bottle.digest"] != bottle_digest

m["org.opencontainers.image.ref.name"] == image_ref

Check warning on line 307 in Library/Homebrew/resource.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/resource.rb#L307

Added line #L307 was not covered by tests
end
raise Error, "Couldn't find manifest matching bottle checksum." if manifest_annotations.blank?

tab = manifest_annotations["sh.brew.tab"]

Check warning on line 311 in Library/Homebrew/resource.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/resource.rb#L311

Added line #L311 was not covered by tests
raise Error, "Couldn't find tab from manifest." if tab.blank?

begin
JSON.parse(tab)

Check warning on line 315 in Library/Homebrew/resource.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/resource.rb#L315

Added line #L315 was not covered by tests
rescue JSON::ParserError
raise Error, "Couldn't parse tab JSON."

Check warning on line 317 in Library/Homebrew/resource.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/resource.rb#L317

Added line #L317 was not covered by tests
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 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

Check warning on line 395 in Library/Homebrew/software_spec.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/software_spec.rb#L395

Added line #L395 was not covered by tests
rescue DownloadError
raise unless fallback_on_error

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

github_packages_manifest_resource_tab(github_packages_manifest_resource)
github_packages_manifest_resource.tab

Check warning on line 411 in Library/Homebrew/software_spec.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/software_spec.rb#L411

Added line #L411 was not covered by tests
end

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

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)

Check warning on line 425 in Library/Homebrew/software_spec.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/software_spec.rb#L425

Added line #L425 was not covered by tests

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