-
Notifications
You must be signed in to change notification settings - Fork 22
Add GitHub pull request creation cap support #111
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
Open
potiuk
wants to merge
2
commits into
apache:main
Choose a base branch
from
potiuk:feature/pr-creation-cap
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+414
−1
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| # Licensed to the Apache Software Foundation (ASF) under one | ||
| # or more contributor license agreements. See the NOTICE file | ||
| # distributed with this work for additional information | ||
| # regarding copyright ownership. The ASF licenses this file | ||
| # to you under the Apache License, Version 2.0 (the | ||
| # "License"); you may not use this file except in compliance | ||
| # with the License. You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, | ||
| # software distributed under the License is distributed on an | ||
| # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| # KIND, either express or implied. See the License for the | ||
| # specific language governing permissions and limitations | ||
| # under the License. | ||
|
|
||
| """GitHub pull request creation cap support. | ||
|
|
||
| Limits the number of open pull requests a user without write access may have open | ||
| at one time, via the repository interaction-limits API. See | ||
| https://github.com/community/maintainers/discussions/840 and | ||
| https://docs.github.com/rest/interactions/repos#update-pull-request-creation-cap-for-a-repository | ||
| """ | ||
|
|
||
| import json | ||
| from typing import Any | ||
|
|
||
| from . import directive, ASFGitHubFeature | ||
|
|
||
| # Bounds enforced by the GitHub API for max_open_pull_requests. | ||
| MIN_OPEN_PULL_REQUESTS = 1 | ||
| MAX_OPEN_PULL_REQUESTS = 1000 | ||
|
|
||
|
|
||
| def _creation_cap_url(self: ASFGitHubFeature) -> str: | ||
| return f"/repos/{self.repository.org_id}/{self.repository.name}/interaction-limits/pulls/creation-cap" | ||
|
|
||
|
|
||
| def _check_creation_cap_response(self: ASFGitHubFeature, status: int, body: str) -> None: | ||
| """Raise unless GitHub accepted the creation cap update.""" | ||
| if 200 <= status < 300: | ||
| return | ||
| try: | ||
| parsed = json.loads(body) | ||
| detail = str(parsed.get("errors") or parsed.get("message") or body) | ||
| except (json.JSONDecodeError, AttributeError): | ||
| detail = body | ||
| repo = f"{self.repository.org_id}/{self.repository.name}" | ||
| match status: | ||
| case 403: | ||
| raise Exception(f"Not allowed to set the pull request creation cap on '{repo}': {detail}") | ||
| case 404: | ||
| raise Exception(f"Repository '{repo}' not found or not accessible: {detail}") | ||
| case 422: | ||
| raise Exception(f"Validation failed while setting the pull request creation cap: {detail}") | ||
| case 500: | ||
| raise Exception(f"GitHub server error while setting the pull request creation cap: {detail}") | ||
| case _: | ||
| raise Exception(f"Unexpected response while setting the pull request creation cap: HTTP {status}: {detail}") | ||
|
|
||
|
|
||
| @directive | ||
| def pr_creation_cap(self: ASFGitHubFeature): | ||
| pull_requests = self.yaml.get("pull_requests") or {} | ||
| creation_cap = pull_requests.get("creation_cap") | ||
|
|
||
| previous_yaml = self.previous_yaml if isinstance(self.previous_yaml, dict) else {} | ||
| previous_pull_requests = previous_yaml.get("pull_requests") or {} | ||
| was_previously_configured = "creation_cap" in previous_pull_requests | ||
|
|
||
| if creation_cap: | ||
| enabled = creation_cap.get("enabled", False) | ||
| # Optional: when omitted (None), the key is left out of the payload below and | ||
| # GitHub applies its own default cap. | ||
| max_open_pull_requests = creation_cap.get("max_open_pull_requests") | ||
| elif was_previously_configured: | ||
| # The section was removed; disable the cap that .asf.yaml previously managed. | ||
| enabled = False | ||
| max_open_pull_requests = None | ||
| else: | ||
| return | ||
|
|
||
| if not enabled and not was_previously_configured: | ||
| return | ||
|
|
||
| payload: dict[str, Any] = {"enabled": enabled} | ||
| if enabled and max_open_pull_requests is not None: | ||
| if not MIN_OPEN_PULL_REQUESTS <= max_open_pull_requests <= MAX_OPEN_PULL_REQUESTS: | ||
| raise Exception( | ||
| "github.pull_requests.creation_cap.max_open_pull_requests must be between " | ||
| f"{MIN_OPEN_PULL_REQUESTS} and {MAX_OPEN_PULL_REQUESTS}, got {max_open_pull_requests}" | ||
| ) | ||
| payload["max_open_pull_requests"] = max_open_pull_requests | ||
|
|
||
| if enabled: | ||
| if "max_open_pull_requests" in payload: | ||
| print(f"Setting pull request creation cap to enabled, max {max_open_pull_requests} open per user") | ||
| else: | ||
| print("Setting pull request creation cap to enabled") | ||
| else: | ||
| print("Disabling pull request creation cap") | ||
|
|
||
| if not self.noop("pr_creation_cap"): | ||
| status, _headers, body = self.ghrepo._requester.requestJson("PATCH", _creation_cap_url(self), input=payload) | ||
| _check_creation_cap_response(self, status, body) | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.