utils/github: support globbing artifacts

This commit is contained in:
Ruoyu Zhong 2024-04-17 02:57:56 +08:00
parent 5517894523
commit 0df71ea6a3
No known key found for this signature in database
2 changed files with 26 additions and 18 deletions

View File

@ -46,10 +46,10 @@ RSpec.describe GitHub do
end end
end end
describe "::get_artifact_url", :needs_network do describe "::get_artifact_urls", :needs_network do
it "fails to find a nonexistent workflow" do it "fails to find a nonexistent workflow" do
expect do expect do
described_class.get_artifact_url( described_class.get_artifact_urls(
described_class.get_workflow_run("Homebrew", "homebrew-core", "1"), described_class.get_workflow_run("Homebrew", "homebrew-core", "1"),
) )
end.to raise_error(/No matching check suite found/) 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 it "fails to find artifacts that don't exist" do
expect do expect do
described_class.get_artifact_url( described_class.get_artifact_urls(
described_class.get_workflow_run("Homebrew", "homebrew-core", "135608", 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 end
it "gets an artifact link" do it "gets artifact URLs" do
url = described_class.get_artifact_url( urls = described_class.get_artifact_urls(
described_class.get_workflow_run("Homebrew", "homebrew-core", "135608", 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
end end

View File

@ -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) API.open_rest(url, data_binary_path: local_file, request_method: :POST, scopes: CREATE_ISSUE_FORK_OR_PR_SCOPES)
end 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 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. # 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 end
[check_suite, user, repo, pull_request, workflow_id, scopes, artifact_name] [check_suite, user, repo, pull_request, workflow_id, scopes, artifact_pattern]
end end
def self.get_artifact_url(workflow_array) def self.get_artifact_urls(workflow_array)
check_suite, user, repo, pr, workflow_id, scopes, artifact_name = *workflow_array check_suite, user, repo, pr, workflow_id, scopes, artifact_pattern = *workflow_array
if check_suite.empty? if check_suite.empty?
raise API::Error, <<~EOS raise API::Error, <<~EOS
No matching check suite found for these criteria! No matching check suite found for these criteria!
@ -357,18 +357,18 @@ module GitHub
run_id = check_suite.last["workflowRun"]["databaseId"] run_id = check_suite.last["workflowRun"]["databaseId"]
artifacts = API.open_rest("#{API_URL}/repos/#{user}/#{repo}/actions/runs/#{run_id}/artifacts", scopes:) artifacts = API.open_rest("#{API_URL}/repos/#{user}/#{repo}/actions/runs/#{run_id}/artifacts", scopes:)
artifact = artifacts["artifacts"].select do |art| matching_artifacts = artifacts["artifacts"].select do |art|
art["name"] == artifact_name File.fnmatch?(artifact_pattern, art["name"], File::FNM_EXTGLOB)
end end
if artifact.empty? if matching_artifacts.empty?
raise API::Error, <<~EOS 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"]} #{Formatter.url check_suite.last["workflowRun"]["url"]}
EOS EOS
end end
artifact.last["archive_download_url"] matching_artifacts.map { |art| art["archive_download_url"] }
end end
def self.public_member_usernames(org, per_page: 100) def self.public_member_usernames(org, per_page: 100)