Refactor GitHub artifact downloads out of dev-cmd/pr-pull

I plan to use these in `test-bot` to download built bottles from
previous CI runs.
This commit is contained in:
Carlo Cabrera 2023-05-17 01:23:06 +08:00
parent f7b3225574
commit 05337cbb79
No known key found for this signature in database
GPG Key ID: C74D447FC549A1D0
3 changed files with 58 additions and 55 deletions

View File

@ -1,7 +1,6 @@
# typed: true
# frozen_string_literal: true
require "download_strategy"
require "cli/parser"
require "utils/github"
require "tmpdir"
@ -356,28 +355,6 @@ module Homebrew
formulae + casks
end
def self.download_artifact(url, dir, pull_request)
odie "Credentials must be set to access the Artifacts API" if GitHub::API.credentials_type == :none
token = GitHub::API.credentials
curl_args = ["--header", "Authorization: token #{token}"]
# Download the artifact as a zip file and unpack it into `dir`. This is
# preferred over system `curl` and `tar` as this leverages the Homebrew
# cache to avoid repeated downloads of (possibly large) bottles.
FileUtils.chdir dir do
downloader = GitHubArtifactDownloadStrategy.new(
url,
"artifact",
pull_request,
curl_args: curl_args,
secrets: [token],
)
downloader.fetch
downloader.stage
end
end
def self.pr_check_conflicts(repo, pull_request)
long_build_pr_files = GitHub.issues(
repo: repo, state: "open", labels: "no long build conflict",
@ -505,7 +482,7 @@ module Homebrew
ohai "Downloading bottles for workflow: #{workflow}"
url = GitHub.get_artifact_url(workflow_run)
download_artifact(url, dir, pr)
GitHub.download_artifact(url, pr, dir)
end
next if args.no_upload?
@ -526,34 +503,3 @@ module Homebrew
end
end
end
class GitHubArtifactDownloadStrategy < AbstractFileDownloadStrategy
def fetch(timeout: nil)
ohai "Downloading #{url}"
if cached_location.exist?
puts "Already downloaded: #{cached_location}"
else
begin
curl "--location", "--create-dirs", "--output", temporary_path, url,
*meta.fetch(:curl_args, []),
secrets: meta.fetch(:secrets, []),
timeout: timeout
rescue ErrorDuringExecution
raise CurlDownloadStrategyError, url
end
ignore_interrupts do
cached_location.dirname.mkpath
temporary_path.rename(cached_location)
symlink_location.dirname.mkpath
end
end
FileUtils.ln_s cached_location.relative_path_from(symlink_location.dirname), symlink_location, force: true
end
private
sig { returns(String) }
def resolved_basename
"artifact.zip"
end
end

View File

@ -633,6 +633,40 @@ class CurlGitHubPackagesDownloadStrategy < CurlDownloadStrategy
end
end
# Strategy for downloading an artifact from GitHub Actions.
#
# @api private
class GitHubArtifactDownloadStrategy < AbstractFileDownloadStrategy
def fetch(timeout: nil)
ohai "Downloading #{url}"
if cached_location.exist?
puts "Already downloaded: #{cached_location}"
else
begin
curl "--location", "--create-dirs", "--output", temporary_path, url,
*meta.fetch(:curl_args, []),
secrets: meta.fetch(:secrets, []),
timeout: timeout
rescue ErrorDuringExecution
raise CurlDownloadStrategyError, url
end
ignore_interrupts do
cached_location.dirname.mkpath
temporary_path.rename(cached_location)
symlink_location.dirname.mkpath
end
end
FileUtils.ln_s cached_location.relative_path_from(symlink_location.dirname), symlink_location, force: true
end
private
sig { returns(String) }
def resolved_basename
"artifact.zip"
end
end
# Strategy for downloading a file from an Apache Mirror URL.
#
# @api public

View File

@ -5,6 +5,7 @@ require "uri"
require "utils/github/actions"
require "utils/github/api"
require "download_strategy"
require "system_command"
# Wrapper functions for the GitHub API.
@ -371,6 +372,28 @@ module GitHub
artifact.last["archive_download_url"]
end
def self.download_artifact(url, artifact_id, dir = Pathname.pwd)
odie "Credentials must be set to access the Artifacts API" if API.credentials_type == :none
token = API.credentials
curl_args = ["--header", "Authorization: token #{token}"]
# Download the artifact as a zip file and unpack it into `dir`. This is
# preferred over system `curl` and `tar` as this leverages the Homebrew
# cache to avoid repeated downloads of (possibly large) bottles.
FileUtils.chdir dir do
downloader = GitHubArtifactDownloadStrategy.new(
url,
"artifact",
artifact_id,
curl_args: curl_args,
secrets: [token],
)
downloader.fetch
downloader.stage
end
end
def self.public_member_usernames(org, per_page: 100)
url = "#{API_URL}/orgs/#{org}/public_members"
members = []