utils/github/api: Smarter pagination in paginate_rest

- The `API_MAX_PAGES` value is 50, so for pages 1 to 50, the
  `paginate_rest` method was making an API call even if there was no
  data past, for example, page 8.
- This made `brew contributions --user=issyl0` take 11 minutes, since we
  made 50 API calls _per repo_ even if it was unnecessary, burning down
  our API allowance.
- Instead, stop looping if we detect that there's no data in `result`.
- This probably needs more testing for other parts of Homebrew that rely
  on `paginate_rest` and the different shapes of data it outputs.
This commit is contained in:
Issy Long 2023-02-22 17:53:46 +00:00
parent 1b3fa0bef7
commit dd140ea717
No known key found for this signature in database
GPG Key ID: 8247C390DADC67D4

View File

@ -256,6 +256,8 @@ module GitHub
def paginate_rest(url, additional_query_params: nil, per_page: 100)
(1..API_MAX_PAGES).each do |page|
result = API.open_rest("#{url}?per_page=#{per_page}&page=#{page}&#{additional_query_params}")
break if result.blank?
yield(result, page)
end
end