|
| 1 | +name: PR Quota Limit |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request: |
| 5 | + types: [opened, reopened] |
| 6 | + |
| 7 | +permissions: |
| 8 | + contents: read |
| 9 | + pull-requests: read |
| 10 | + |
| 11 | +jobs: |
| 12 | + check-pr-quota: |
| 13 | + runs-on: ubuntu-latest |
| 14 | + permissions: |
| 15 | + pull-requests: write |
| 16 | + issues: write |
| 17 | + steps: |
| 18 | + - name: Check PR quota |
| 19 | + // Use action version v7.0.1 |
| 20 | + uses: actions/github-script@60a0d8304218317a38b4124020f343a0d555a1eb |
| 21 | + with: |
| 22 | + script: | |
| 23 | + try { |
| 24 | + const prAuthor = context.payload.pull_request.user.login; |
| 25 | + const currentPRNumber = context.payload.pull_request.number; |
| 26 | +
|
| 27 | + console.log(`Checking PR quota for user: ${prAuthor}`); |
| 28 | + console.log(`Current PR number: ${currentPRNumber}`); |
| 29 | +
|
| 30 | + // Get all open PRs with pagination support |
| 31 | + let allPRs = []; |
| 32 | + let page = 1; |
| 33 | + let hasMorePages = true; |
| 34 | +
|
| 35 | + while (hasMorePages) { |
| 36 | + const { data: prs } = await github.rest.pulls.list({ |
| 37 | + owner: context.repo.owner, |
| 38 | + repo: context.repo.repo, |
| 39 | + state: 'open', |
| 40 | + per_page: 100, |
| 41 | + page: page |
| 42 | + }); |
| 43 | +
|
| 44 | + allPRs = allPRs.concat(prs); |
| 45 | +
|
| 46 | + // If we got less than 100 PRs, we've reached the last page |
| 47 | + if (prs.length < 100) { |
| 48 | + hasMorePages = false; |
| 49 | + } else { |
| 50 | + page++; |
| 51 | + } |
| 52 | + } |
| 53 | +
|
| 54 | + console.log(`Total open PRs in repository: ${allPRs.length}`); |
| 55 | +
|
| 56 | + // Filter PRs by the same author |
| 57 | + const userPRs = allPRs.filter(pr => pr.user.login === prAuthor); |
| 58 | + const openCount = userPRs.length; |
| 59 | +
|
| 60 | + console.log(`User ${prAuthor} has ${openCount} open PR(s)`); |
| 61 | +
|
| 62 | + // Check if exceeds quota (15 PRs max) |
| 63 | + const maxPRs = 15; |
| 64 | + if (openCount > maxPRs) { |
| 65 | + const currentStatus = `${openCount}/${maxPRs}`; |
| 66 | +
|
| 67 | + const message = `Hi, @${prAuthor}, Thanks for your contribution! To ensure quality reviews, we limit how many concurrent open PRs contributors can open. This pull request will be temporarily closed (Current status: ${currentStatus} open). We encourage you to submit a new PR once the quota policy permits future contributions.`; |
| 68 | +
|
| 69 | + console.log(`Quota exceeded! Closing PR #${currentPRNumber}`); |
| 70 | + console.log(`User has ${openCount} open PRs, which exceeds the limit of ${maxPRs}`); |
| 71 | + console.log(`Message: ${message}`); |
| 72 | +
|
| 73 | + // Add comment to the PR |
| 74 | + try { |
| 75 | + await github.rest.issues.createComment({ |
| 76 | + owner: context.repo.owner, |
| 77 | + repo: context.repo.repo, |
| 78 | + issue_number: currentPRNumber, |
| 79 | + body: message |
| 80 | + }); |
| 81 | + console.log('Comment added successfully'); |
| 82 | + } catch (commentError) { |
| 83 | + console.error('Failed to add comment:', commentError.message); |
| 84 | + } |
| 85 | +
|
| 86 | + // Close the PR |
| 87 | + try { |
| 88 | + await github.rest.pulls.update({ |
| 89 | + owner: context.repo.owner, |
| 90 | + repo: context.repo.repo, |
| 91 | + pull_number: currentPRNumber, |
| 92 | + state: 'closed' |
| 93 | + }); |
| 94 | + console.log(`PR #${currentPRNumber} has been closed due to quota limit.`); |
| 95 | + } catch (closeError) { |
| 96 | + console.error('Failed to close PR:', closeError.message); |
| 97 | + throw closeError; // Re-throw to fail the workflow |
| 98 | + } |
| 99 | + } else { |
| 100 | + const availableSlots = maxPRs - openCount; |
| 101 | + console.log(`Quota check passed. User has ${availableSlots} slot(s) available.`); |
| 102 | + } |
| 103 | + } catch (error) { |
| 104 | + console.error('Error in PR quota check:', error.message); |
| 105 | + throw error; // Re-throw to fail the workflow |
| 106 | + } |
0 commit comments