Class: Homebrew::DevCmd::MirrorAttestations Private

Inherits:
AbstractCommand show all
Defined in:
dev-cmd/mirror-attestations.rb,
sorbet/rbi/dsl/homebrew/dev_cmd/mirror_attestations.rbi

This class is part of a private API. This class may only be used in the Homebrew/brew repository. Third parties should avoid using this class if possible, as it may be removed or changed without warning.

Defined Under Namespace

Classes: Args, Image, PublishedBottle, Report

Constant Summary collapse

FORMULAE_PER_BATCH =

This constant is part of a private API. This constant may only be used in the Homebrew/brew repository. Third parties should avoid using this constant if possible, as it may be removed or changed without warning.

Formulae read from GitHub Packages at once, whose bottles are few enough for one request to list their attestations.

32
MAX_LIST_ATTEMPTS =

This constant is part of a private API. This constant may only be used in the Homebrew/brew repository. Third parties should avoid using this constant if possible, as it may be removed or changed without warning.

5
PUBLISHED =

This constant is part of a private API. This constant may only be used in the Homebrew/brew repository. Third parties should avoid using this constant if possible, as it may be removed or changed without warning.

"Already published"
MIRRORED =

This constant is part of a private API. This constant may only be used in the Homebrew/brew repository. Third parties should avoid using this constant if possible, as it may be removed or changed without warning.

"Published by this run"
UNPINNED =

This constant is part of a private API. This constant may only be used in the Homebrew/brew repository. Third parties should avoid using this constant if possible, as it may be removed or changed without warning.

"Published by this run but not pinned"
UNPUBLISHED =

This constant is part of a private API. This constant may only be used in the Homebrew/brew repository. Third parties should avoid using this constant if possible, as it may be removed or changed without warning.

"Valid attestation to publish"
UNATTESTED =

This constant is part of a private API. This constant may only be used in the Homebrew/brew repository. Third parties should avoid using this constant if possible, as it may be removed or changed without warning.

"Without a valid attestation"
FAILED =

This constant is part of a private API. This constant may only be used in the Homebrew/brew repository. Third parties should avoid using this constant if possible, as it may be removed or changed without warning.

"Failed"

Instance Method Summary collapse

Methods inherited from AbstractCommand

command, command_name, dev_cmd?, #initialize, parser, ruby_cmd?

Methods included from Utils::Output::Mixin

#issue_reporting_message, #odebug, #odeprecated, #odie, #odisabled, #ofail, #oh1, #oh1_title, #ohai, #ohai_title, #onoe, #opoo, #opoo_once, #opoo_outside_github_actions, #opoo_without_github_actions_annotation, #pretty_cannot_install, #pretty_deprecated, #pretty_disabled, #pretty_duration, #pretty_incompatible, #pretty_install_status, #pretty_installed, #pretty_removed, #pretty_uninstalled, #pretty_unmarked, #pretty_upgradable, #pretty_warning

Constructor Details

This class inherits a constructor from Homebrew::AbstractCommand

Instance Method Details

#args ⇒ Homebrew::DevCmd::MirrorAttestations::Args

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.



10
# File 'sorbet/rbi/dsl/homebrew/dev_cmd/mirror_attestations.rbi', line 10

def args; end

#run ⇒ void

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

This method returns an undefined value.



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'dev-cmd/mirror-attestations.rb', line 76

def run
  tap = CoreTap.instance
  raise TapUnavailableError, tap.name unless tap.installed?

  unless args.dry_run?
    Utils::Git.set_name_email!
    Utils::Git.setup_gpg!
  end
  github_packages = GitHubPackages.new
  report = Report.new
  Homebrew::API.with_no_api_env do
    formulae = (args.named.presence || tap.formula_names).filter_map do |name|
      formula = Formulary.factory("#{tap.name}/#{name.delete_prefix("#{tap.name}/")}")
      formula if formula.bottle_specification.checksums.any?
    end.uniq(&:name)
    formulae.each_slice(FORMULAE_PER_BATCH) do |batch|
      threads = batch.map do |formula|
        # Reads a formula's pinned image index, as an install from the tap does, and
        # the manifest of each bottle in its bottle block, which downloads no bottle.
        Thread.new do
          spec = formula.bottle_specification
          raise "bottle block has no `manifest_sha256`" if spec.manifest_sha256.nil?

          bottles = spec.collector.tags.filter_map { |tag| formula.bottle_for_tag(tag) }
          index = bottles.fetch(0).github_packages_manifest_resource
          raise "bottles are not on GitHub Packages" if index.nil?

          index.fetch(quiet: true)
          manifest_sha256s = index.platform_manifest_sha256s
          name = "homebrew/core/#{GitHubPackages.image_formula_name(formula.name)}"
          authorization = github_packages.authorization(name, dry_run: args.dry_run?)
          Image.new(formula:, name:, tag: index.version.to_s, index: index.cached_download.read,
                    bottles: bottles.map do |bottle|
                      manifest_sha256 = manifest_sha256s.fetch([index.image_ref(bottle.tag),
                                                                bottle.resource.checksum&.hexdigest])
                      manifest = github_packages.manifest(authorization, name, "sha256:#{manifest_sha256}",
                                                          GitHubPackages::MANIFEST_MEDIA_TYPE)
                      bundle = JSON.parse(manifest).fetch("annotations")[GitHubPackages::ATTESTATION_ANNOTATION]
                      # A published bundle that an install would reject fails the formula.
                      Homebrew::Attestation.verify_core_bottle_bundles([bundle], bottle) if bundle
                      PublishedBottle.new(bottle:, manifest_sha256:, manifest:, bundle:)
                    end)
        rescue => e
          [formula, e.message.lines.first.to_s.chomp]
        end
      end
      images = threads.filter_map do |thread|
        result = thread.value
        next result if result.is_a?(Image)

        formula, message = result
        report.add(FAILED, formula.bottle_specification.checksums.count)
        report.problems << "#{formula.name}: #{message}"
        nil
      end
      bundles = listed_bundles(images.flat_map do |image|
        image.bottles.reject(&:bundle).filter_map { |published| published.bottle.resource.checksum&.hexdigest }
      end)
      images.each { |image| mirror(github_packages, image, bundles, report) }
    end
    report.complete = true
  ensure
    summary = summary(report)
    puts summary
    if (github_step_summary = ENV["GITHUB_STEP_SUMMARY"].presence)
      File.open(github_step_summary, "a") { |file| file.puts summary }
    end
  end
  Homebrew.failed = true if report.bottles.key?(FAILED)
end