Merge pull request #17138 from Homebrew/artifacts-pagination

utils/github: paginate artifact API result
This commit is contained in:
Bo Anderson 2024-04-23 23:27:26 +01:00 committed by GitHub
commit 04fc95bfdb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 11 additions and 4 deletions

View File

@ -355,10 +355,17 @@ module GitHub
end end
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 = []
per_page = 50
API.paginate_rest("#{API_URL}/repos/#{user}/#{repo}/actions/runs/#{run_id}/artifacts",
per_page:, scopes:) do |result|
result = result["artifacts"]
artifacts.concat(result)
break if result.length < per_page
end
matching_artifacts = matching_artifacts =
artifacts["artifacts"] artifacts
.group_by { |art| art["name"] } .group_by { |art| art["name"] }
.select { |name| File.fnmatch?(artifact_pattern, name, File::FNM_EXTGLOB) } .select { |name| File.fnmatch?(artifact_pattern, name, File::FNM_EXTGLOB) }
.map { |_, arts| arts.last } .map { |_, arts| arts.last }

View File

@ -284,11 +284,11 @@ module GitHub
end end
end end
def self.paginate_rest(url, additional_query_params: nil, per_page: 100) def self.paginate_rest(url, additional_query_params: nil, per_page: 100, scopes: [].freeze)
(1..API_MAX_PAGES).each do |page| (1..API_MAX_PAGES).each do |page|
retry_count = 1 retry_count = 1
result = begin result = begin
API.open_rest("#{url}?per_page=#{per_page}&page=#{page}&#{additional_query_params}") API.open_rest("#{url}?per_page=#{per_page}&page=#{page}&#{additional_query_params}", scopes:)
rescue Error rescue Error
if retry_count < PAGINATE_RETRY_COUNT if retry_count < PAGINATE_RETRY_COUNT
retry_count += 1 retry_count += 1