Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dev-cmd/bump*: limit the number of open PRs to 15. #16962

Merged
merged 2 commits into from Mar 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions Library/Homebrew/dev-cmd/bump-cask-pr.rb
Expand Up @@ -93,6 +93,8 @@ def run
#{Formatter.url("#{cask.tap.remote}/blob/master/.github/autobump.txt")}
EOS

odie "You have too many PRs open: close or merge some first!" if GitHub.too_many_open_prs?(cask.tap)

new_version = BumpVersionParser.new(
general: args.version,
intel: args.version_intel,
Expand Down
2 changes: 2 additions & 0 deletions Library/Homebrew/dev-cmd/bump-formula-pr.rb
Expand Up @@ -120,6 +120,8 @@ def run
#{Formatter.url("#{formula.tap.remote}/blob/master/.github/autobump.txt")}
EOS

odie "You have too many PRs open: close or merge some first!" if GitHub.too_many_open_prs?(formula.tap)

formula_spec = formula.stable
odie "#{formula}: no stable specification found!" if formula_spec.blank?

Expand Down
4 changes: 4 additions & 0 deletions Library/Homebrew/dev-cmd/bump.rb
Expand Up @@ -499,6 +499,10 @@ def retrieve_and_display_info_and_open_pr(formula_or_cask, name, repositories, a

return unless args.open_pr?

if GitHub.too_many_open_prs?(formula_or_cask.tap)
odie "You have too many PRs open: close or merge some first!"
end

if repology_latest.is_a?(Version) &&
repology_latest > current_version.general &&
repology_latest > new_version.general &&
Expand Down
56 changes: 56 additions & 0 deletions Library/Homebrew/utils/github.rb
Expand Up @@ -863,4 +863,60 @@

[author_count, committer_count]
end

MAXIMUM_OPEN_PRS = 15

sig { params(tap: T.nilable(Tap)).returns(T::Boolean) }
def self.too_many_open_prs?(tap)
# We don't enforce unofficial taps.
return false if tap.nil? || !tap.official?

# BrewTestBot can open as many PRs as it wants.
return false if ENV["HOMEBREW_TEST_BOT_AUTOBUMP"].present?

odie "Cannot count PRs, HOMEBREW_NO_GITHUB_API set!" if Homebrew::EnvConfig.no_github_api?

query = <<~EOS

Check warning on line 879 in Library/Homebrew/utils/github.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/utils/github.rb#L879

Added line #L879 was not covered by tests
query {
viewer {
login
pullRequests(first: 100, states: OPEN) {
nodes {
headRepositoryOwner {
login
}
}
pageInfo {
hasNextPage
}
}
}
}
EOS
graphql_result = API.open_graphql(query)
puts

Check warning on line 897 in Library/Homebrew/utils/github.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/utils/github.rb#L896-L897

Added lines #L896 - L897 were not covered by tests

github_user = graphql_result.dig("viewer", "login")

Check warning on line 899 in Library/Homebrew/utils/github.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/utils/github.rb#L899

Added line #L899 was not covered by tests
odie "Cannot count PRs, cannot get GitHub username from GraphQL API!" if github_user.blank?

# BrewTestBot can open as many PRs as it wants.
return false if github_user.casecmp("brewtestbot").zero?

prs = graphql_result.dig("viewer", "pullRequests", "nodes")
more_graphql_data = graphql_result.dig("viewer", "pullRequests", "pageInfo", "hasNextPage")

Check warning on line 906 in Library/Homebrew/utils/github.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/utils/github.rb#L905-L906

Added lines #L905 - L906 were not covered by tests
return false if !more_graphql_data && prs.length < MAXIMUM_OPEN_PRS

homebrew_prs_count = graphql_result.dig("viewer", "pullRequests", "nodes").count do |pr|
pr["headRepositoryOwner"]["login"] == "Homebrew"

Check warning on line 910 in Library/Homebrew/utils/github.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/utils/github.rb#L909-L910

Added lines #L909 - L910 were not covered by tests
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") }

Check warning on line 918 in Library/Homebrew/utils/github.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/utils/github.rb#L916-L918

Added lines #L916 - L918 were not covered by tests

repo_prs_count >= MAXIMUM_OPEN_PRS

Check warning on line 920 in Library/Homebrew/utils/github.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/utils/github.rb#L920

Added line #L920 was not covered by tests
end
end