diff --git a/Library/Homebrew/test/utils/github_spec.rb b/Library/Homebrew/test/utils/github_spec.rb index 1c01709361..56a047c5af 100644 --- a/Library/Homebrew/test/utils/github_spec.rb +++ b/Library/Homebrew/test/utils/github_spec.rb @@ -46,10 +46,10 @@ RSpec.describe GitHub do end end - describe "::get_artifact_url", :needs_network do + describe "::get_artifact_urls", :needs_network do it "fails to find a nonexistent workflow" do expect do - described_class.get_artifact_url( + described_class.get_artifact_urls( described_class.get_workflow_run("Homebrew", "homebrew-core", "1"), ) end.to raise_error(/No matching check suite found/) @@ -57,19 +57,27 @@ RSpec.describe GitHub do it "fails to find artifacts that don't exist" do expect do - described_class.get_artifact_url( + described_class.get_artifact_urls( described_class.get_workflow_run("Homebrew", "homebrew-core", "135608", - workflow_id: "triage.yml", artifact_name: "false_artifact"), + workflow_id: "triage.yml", artifact_pattern: "false_artifact"), ) - end.to raise_error(/No artifact .+ was found/) + end.to raise_error(/No artifacts with the pattern .+ were found/) end - it "gets an artifact link" do - url = described_class.get_artifact_url( + it "gets artifact URLs" do + urls = described_class.get_artifact_urls( described_class.get_workflow_run("Homebrew", "homebrew-core", "135608", - workflow_id: "triage.yml", artifact_name: "event_payload"), + workflow_id: "triage.yml", artifact_pattern: "event_payload"), ) - expect(url).to eq("https://api.github.com/repos/Homebrew/homebrew-core/actions/artifacts/781984175/zip") + expect(urls).to eq(["https://api.github.com/repos/Homebrew/homebrew-core/actions/artifacts/781984175/zip"]) + end + + it "supports pattern matching" do + urls = described_class.get_artifact_urls( + described_class.get_workflow_run("Homebrew", "brew", "17068", + workflow_id: "pkg-installer.yml", artifact_pattern: "Homebrew-*.pkg"), + ) + expect(urls).to eq(["https://api.github.com/repos/Homebrew/brew/actions/artifacts/1405050842/zip"]) end end diff --git a/Library/Homebrew/utils/github.rb b/Library/Homebrew/utils/github.rb index 17f6ec40b0..de01f007c6 100644 --- a/Library/Homebrew/utils/github.rb +++ b/Library/Homebrew/utils/github.rb @@ -283,7 +283,7 @@ module GitHub API.open_rest(url, data_binary_path: local_file, request_method: :POST, scopes: CREATE_ISSUE_FORK_OR_PR_SCOPES) end - def self.get_workflow_run(user, repo, pull_request, workflow_id: "tests.yml", artifact_name: "bottles") + def self.get_workflow_run(user, repo, pull_request, workflow_id: "tests.yml", artifact_pattern: "bottles{,-*}") scopes = CREATE_ISSUE_FORK_OR_PR_SCOPES # GraphQL unfortunately has no way to get the workflow yml name, so we need an extra REST call. @@ -333,11 +333,11 @@ module GitHub [] end - [check_suite, user, repo, pull_request, workflow_id, scopes, artifact_name] + [check_suite, user, repo, pull_request, workflow_id, scopes, artifact_pattern] end - def self.get_artifact_url(workflow_array) - check_suite, user, repo, pr, workflow_id, scopes, artifact_name = *workflow_array + def self.get_artifact_urls(workflow_array) + check_suite, user, repo, pr, workflow_id, scopes, artifact_pattern = *workflow_array if check_suite.empty? raise API::Error, <<~EOS No matching check suite found for these criteria! @@ -357,18 +357,18 @@ module GitHub run_id = check_suite.last["workflowRun"]["databaseId"] artifacts = API.open_rest("#{API_URL}/repos/#{user}/#{repo}/actions/runs/#{run_id}/artifacts", scopes:) - artifact = artifacts["artifacts"].select do |art| - art["name"] == artifact_name + matching_artifacts = artifacts["artifacts"].select do |art| + File.fnmatch?(artifact_pattern, art["name"], File::FNM_EXTGLOB) end - if artifact.empty? + if matching_artifacts.empty? raise API::Error, <<~EOS - No artifact with the name `#{artifact_name}` was found! + No artifacts with the pattern `#{artifact_pattern}` were found! #{Formatter.url check_suite.last["workflowRun"]["url"]} EOS end - artifact.last["archive_download_url"] + matching_artifacts.map { |art| art["archive_download_url"] } end def self.public_member_usernames(org, per_page: 100)