Skip to content

Commit

Permalink
Merge pull request #18150 from Homebrew/pr-count-fix
Browse files Browse the repository at this point in the history
utils/github: fix `too_many_open_prs?`
  • Loading branch information
Bo98 authored Aug 24, 2024
2 parents e521f39 + 03d9a3d commit a208b1b
Showing 1 changed file with 22 additions and 22 deletions.
44 changes: 22 additions & 22 deletions Library/Homebrew/utils/github.rb
Original file line number Diff line number Diff line change
Expand Up @@ -890,46 +890,46 @@ def self.too_many_open_prs?(tap)
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.fetch("viewer")
github_user = data.fetch("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"
end
return true if homebrew_prs_count >= MAXIMUM_OPEN_PRS
return false unless more_graphql_data
return false if tap.nil?
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

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") }
data.dig("pullRequests", "pageInfo")
end

repo_prs_count >= MAXIMUM_OPEN_PRS
false
end
end

0 comments on commit a208b1b

Please sign in to comment.