From 05337cbb797bab9db3234bd41b01a08477c25df1 Mon Sep 17 00:00:00 2001 From: Carlo Cabrera <30379873+carlocab@users.noreply.github.com> Date: Wed, 17 May 2023 01:23:06 +0800 Subject: [PATCH] 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. --- Library/Homebrew/dev-cmd/pr-pull.rb | 56 +-------------------------- Library/Homebrew/download_strategy.rb | 34 ++++++++++++++++ Library/Homebrew/utils/github.rb | 23 +++++++++++ 3 files changed, 58 insertions(+), 55 deletions(-) diff --git a/Library/Homebrew/dev-cmd/pr-pull.rb b/Library/Homebrew/dev-cmd/pr-pull.rb index 6a106d9c56..69d65eb711 100644 --- a/Library/Homebrew/dev-cmd/pr-pull.rb +++ b/Library/Homebrew/dev-cmd/pr-pull.rb @@ -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 diff --git a/Library/Homebrew/download_strategy.rb b/Library/Homebrew/download_strategy.rb index 8a0edae687..b64f9babf2 100644 --- a/Library/Homebrew/download_strategy.rb +++ b/Library/Homebrew/download_strategy.rb @@ -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 diff --git a/Library/Homebrew/utils/github.rb b/Library/Homebrew/utils/github.rb index 830c6c5a81..21145f00fd 100644 --- a/Library/Homebrew/utils/github.rb +++ b/Library/Homebrew/utils/github.rb @@ -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 = []