-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Add community PR limit workflow #6229
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
Merged
Merged
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,163 @@ | ||
| // Copyright (c) Microsoft. All rights reserved. | ||
|
|
||
| function getPullRequest(context) { | ||
| const pullRequest = context.payload.pull_request; | ||
| if (!pullRequest?.number || !pullRequest.user?.login) { | ||
| throw new Error('This script must be run from a pull_request_target event.'); | ||
| } | ||
|
|
||
| return { | ||
| author: pullRequest.user.login, | ||
| labels: pullRequest.labels?.map((label) => label.name).filter(Boolean) ?? [], | ||
| number: pullRequest.number, | ||
| }; | ||
| } | ||
|
|
||
| async function ensureLabel({ github, owner, repo, labelName }) { | ||
| try { | ||
| await github.rest.issues.getLabel({ | ||
| owner, | ||
| repo, | ||
| name: labelName, | ||
| }); | ||
| } catch (error) { | ||
| if (error.status !== 404) { | ||
| throw error; | ||
| } | ||
|
|
||
| try { | ||
| await github.rest.issues.createLabel({ | ||
| owner, | ||
| repo, | ||
| name: labelName, | ||
| color: 'd93f0b', | ||
| description: 'Community author has exceeded the open pull request limit.', | ||
| }); | ||
| } catch (createError) { | ||
| if (createError.status !== 422) { | ||
| throw createError; | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| function hasLabel(labels, labelName) { | ||
| if (!labelName) { | ||
| return false; | ||
| } | ||
|
|
||
| return labels.some((label) => label.toLowerCase() === labelName.toLowerCase()); | ||
| } | ||
|
|
||
| function buildLimitMessage({ author, exemptLabelName, maxOpenPrs, openPrCount }) { | ||
| return [ | ||
| `Thank you for your contribution, @${author}.`, | ||
| '', | ||
| `To keep the review queue manageable, we currently limit community contributors to ${maxOpenPrs} ` | ||
| + `open pull requests at a time. This PR would put you at ${openPrCount} open pull requests, ` | ||
| + 'so we are closing it automatically.', | ||
| '', | ||
| 'Please focus on getting your existing PRs reviewed, merged, or closed before opening another one. ' | ||
| + `If a maintainer asked you to open this PR, they can apply the \`${exemptLabelName}\` label and reopen it.`, | ||
| ].join('\n'); | ||
| } | ||
|
|
||
| async function getOpenPrCount({ github, owner, repo, author, pullRequestNumber }) { | ||
| const query = `repo:${owner}/${repo} is:pr is:open author:${author}`; | ||
| const response = await github.rest.search.issuesAndPullRequests({ | ||
| q: query, | ||
| per_page: 100, | ||
| }); | ||
|
|
||
| const indexedPrNumbers = response.data.items.map((item) => item.number); | ||
| const currentPrIsIndexed = indexedPrNumbers.includes(pullRequestNumber); | ||
| if (currentPrIsIndexed || response.data.total_count >= 100) { | ||
| return response.data.total_count; | ||
| } | ||
|
|
||
| return response.data.total_count + 1; | ||
| } | ||
|
|
||
| async function enforcePrLimit({ github, context, core, exemptLabelName, maxOpenPrs, labelName }) { | ||
| const { owner, repo } = context.repo; | ||
| const { author, labels, number } = getPullRequest(context); | ||
|
|
||
| if (hasLabel(labels, exemptLabelName)) { | ||
| core.info(`PR #${number} has the ${exemptLabelName} label; skipping open PR limit enforcement.`); | ||
| return { | ||
| author, | ||
| closed: false, | ||
| exempt: true, | ||
| openPrCount: null, | ||
| }; | ||
| } | ||
|
|
||
| const openPrCount = await getOpenPrCount({ | ||
| github, | ||
| owner, | ||
| repo, | ||
| author, | ||
| pullRequestNumber: number, | ||
| }); | ||
|
|
||
| if (openPrCount <= maxOpenPrs) { | ||
| core.info( | ||
| `${author} has ${openPrCount} open pull request(s), which is within the limit of ${maxOpenPrs}.`, | ||
| ); | ||
| return { | ||
| author, | ||
| closed: false, | ||
| openPrCount, | ||
| }; | ||
| } | ||
|
|
||
| await ensureLabel({ | ||
| github, | ||
| owner, | ||
| repo, | ||
| labelName, | ||
| }); | ||
|
|
||
| await github.rest.issues.addLabels({ | ||
| owner, | ||
| repo, | ||
| issue_number: number, | ||
| labels: [labelName], | ||
| }); | ||
|
|
||
| await github.rest.issues.createComment({ | ||
| owner, | ||
| repo, | ||
| issue_number: number, | ||
| body: buildLimitMessage({ | ||
| author, | ||
| exemptLabelName, | ||
| maxOpenPrs, | ||
| openPrCount, | ||
| }), | ||
| }); | ||
|
|
||
| await github.rest.pulls.update({ | ||
| owner, | ||
| repo, | ||
| pull_number: number, | ||
| state: 'closed', | ||
| }); | ||
|
|
||
| core.info( | ||
| `${author} has ${openPrCount} open pull request(s), which exceeds the limit of ${maxOpenPrs}. ` | ||
| + `Closed PR #${number}.`, | ||
| ); | ||
|
|
||
| return { | ||
| author, | ||
| closed: true, | ||
| openPrCount, | ||
| }; | ||
| } | ||
|
|
||
| module.exports = { | ||
| buildLimitMessage, | ||
| enforcePrLimit, | ||
| getOpenPrCount, | ||
| }; | ||
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.