From f873835b97d286b05646493056627d5464dec631 Mon Sep 17 00:00:00 2001 From: Carlo Cabrera <30379873+carlocab@users.noreply.github.com> Date: Sat, 24 Aug 2024 22:51:23 +0800 Subject: [PATCH 1/2] utils/github: fix `too_many_open_prs?` This doesn't count PRs from forks, so it only works correctly for maintainers (and only if they use non-fork branches). Let's fix that, and: - simplify the logic by using `paginate_graphql` - return early if it's impossible for them to have too many open PRs --- Library/Homebrew/utils/github.rb | 44 ++++++++++++++++---------------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/Library/Homebrew/utils/github.rb b/Library/Homebrew/utils/github.rb index 667366fbcb..09cebc3744 100644 --- a/Library/Homebrew/utils/github.rb +++ b/Library/Homebrew/utils/github.rb @@ -890,46 +890,46 @@ module GitHub odie "Cannot count PRs, HOMEBREW_NO_GITHUB_API set!" if Homebrew::EnvConfig.no_github_api? query = <<~EOS - query { + query($after: String) { viewer { login - pullRequests(first: 100, states: OPEN) { + pullRequests(first: 100, states: OPEN, after: $after) { + totalCount nodes { - headRepositoryOwner { - login + baseRepository { + owner { + login + } } } pageInfo { hasNextPage + endCursor } } } } EOS - graphql_result = API.open_graphql(query) puts - github_user = graphql_result.dig("viewer", "login") - odie "Cannot count PRs, cannot get GitHub username from GraphQL API!" if github_user.blank? + homebrew_prs_count = 0 - # BrewTestBot can open as many PRs as it wants. - return false if github_user.casecmp("brewtestbot").zero? + API.paginate_graphql(query) do |result| + data = result["viewer"] + github_user = data["login"] - prs = graphql_result.dig("viewer", "pullRequests", "nodes") - more_graphql_data = graphql_result.dig("viewer", "pullRequests", "pageInfo", "hasNextPage") - return false if !more_graphql_data && prs.length < MAXIMUM_OPEN_PRS + # BrewTestBot can open as many PRs as it wants. + return false if github_user.casecmp("brewtestbot").zero? + return false if data.dig("pullRequests", "totalCount") < MAXIMUM_OPEN_PRS - homebrew_prs_count = graphql_result.dig("viewer", "pullRequests", "nodes").count do |pr| - pr["headRepositoryOwner"]["login"] == "Homebrew" + homebrew_prs_count += data.dig("pullRequests", "nodes").count do |node| + node.dig("baseRepository", "owner", "login").casecmp?("homebrew") + end + return true if homebrew_prs_count >= MAXIMUM_OPEN_PRS + + data.dig("pullRequests", "pageInfo") end - return true if homebrew_prs_count >= MAXIMUM_OPEN_PRS - return false unless more_graphql_data - return false if tap.nil? - url = "#{API_URL}/repos/#{tap.full_name}/issues?state=open&creator=#{github_user}" - rest_result = API.open_rest(url) - repo_prs_count = rest_result.count { |issue_or_pr| issue_or_pr.key?("pull_request") } - - repo_prs_count >= MAXIMUM_OPEN_PRS + false end end From 03d9a3dda5b4e5be5a42b4e229912c63cb2a3f74 Mon Sep 17 00:00:00 2001 From: Carlo Cabrera <30379873+carlocab@users.noreply.github.com> Date: Sat, 24 Aug 2024 23:10:39 +0800 Subject: [PATCH 2/2] utils/github: use `#fetch` Co-authored-by: Markus Reiter --- Library/Homebrew/utils/github.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Library/Homebrew/utils/github.rb b/Library/Homebrew/utils/github.rb index 09cebc3744..2e964309ba 100644 --- a/Library/Homebrew/utils/github.rb +++ b/Library/Homebrew/utils/github.rb @@ -915,8 +915,8 @@ module GitHub homebrew_prs_count = 0 API.paginate_graphql(query) do |result| - data = result["viewer"] - github_user = data["login"] + data = result.fetch("viewer") + github_user = data.fetch("login") # BrewTestBot can open as many PRs as it wants. return false if github_user.casecmp("brewtestbot").zero?