|
| 1 | +name: Guard Unassigned PRs |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request_target: |
| 5 | + types: [opened] |
| 6 | + |
| 7 | +jobs: |
| 8 | + check-assignee: |
| 9 | + runs-on: ubuntu-latest |
| 10 | + permissions: |
| 11 | + pull-requests: write |
| 12 | + issues: read |
| 13 | + steps: |
| 14 | + - uses: actions/github-script@v7 |
| 15 | + with: |
| 16 | + script: | |
| 17 | + const { owner, repo, number } = context.issue; |
| 18 | + const pr = context.payload.pull_request; |
| 19 | + const author = pr.user.login; |
| 20 | +
|
| 21 | + // Extract linked issue number (e.g., "Closes #12" or "Fixes #5") |
| 22 | + const match = pr.body?.match(/(?:fixes|closes|resolves)\s+#(\d+)/i); |
| 23 | + |
| 24 | + if (!match) { |
| 25 | + await github.rest.issues.createComment({ |
| 26 | + owner, repo, issue_number: number, |
| 27 | + body: `Hi @${author}! Please reference an issue in your PR body (e.g., \`Closes #12\`) and make sure it is assigned to you.` |
| 28 | + }); |
| 29 | + await github.rest.pulls.update({ owner, repo, pull_number: number, state: 'closed' }); |
| 30 | + return; |
| 31 | + } |
| 32 | +
|
| 33 | + const issueNum = parseInt(match[1]); |
| 34 | + const issue = await github.rest.issues.get({ owner, repo, issue_number: issueNum }); |
| 35 | + const isAssigned = issue.data.assignees.some(a => a.login === author); |
| 36 | +
|
| 37 | + if (!isAssigned) { |
| 38 | + await github.rest.issues.createComment({ |
| 39 | + owner, repo, issue_number: number, |
| 40 | + body: `Hi @${author}! This PR was automatically closed because Issue #${issueNum} is not assigned to you. Please comment on the issue to claim it first!` |
| 41 | + }); |
| 42 | + await github.rest.pulls.update({ owner, repo, pull_number: number, state: 'closed' }); |
| 43 | + } |
| 44 | + |
0 commit comments