|
| 1 | +name: Autoclose PRs without linked issue |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request_target: |
| 5 | + types: [opened, edited, reopened] |
| 6 | + |
| 7 | +permissions: {} |
| 8 | + |
| 9 | +jobs: |
| 10 | + check: |
| 11 | + name: Require a linked, assigned issue |
| 12 | + if: >- |
| 13 | + !github.event.repository.fork && |
| 14 | + github.event.pull_request.state == 'open' && |
| 15 | + github.event.pull_request.user.type != 'Bot' && |
| 16 | + (github.event.action == 'opened' || |
| 17 | + github.event.sender.login == github.event.pull_request.user.login) |
| 18 | + # Job-level, so runs skipped by the condition above don't cancel an in-progress check. |
| 19 | + concurrency: |
| 20 | + group: pr-linked-issue-${{ github.event.pull_request.number }} |
| 21 | + cancel-in-progress: true |
| 22 | + runs-on: ubuntu-latest |
| 23 | + permissions: |
| 24 | + issues: read |
| 25 | + pull-requests: write |
| 26 | + steps: |
| 27 | + - name: Check for a linked issue assigned to the PR author |
| 28 | + uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0 |
| 29 | + with: |
| 30 | + retries: 3 |
| 31 | + script: | |
| 32 | + const { owner, repo } = context.repo; |
| 33 | + const pr = context.payload.pull_request; |
| 34 | + const author = pr.user.login; |
| 35 | +
|
| 36 | + // The permission endpoint reports "read" for anyone on a public repo, |
| 37 | + // so check collaborator status directly (204 if collaborator, 404 if not). |
| 38 | + try { |
| 39 | + await github.rest.repos.checkCollaborator({ owner, repo, username: author }); |
| 40 | + core.info(`${author} is a collaborator, skipping check.`); |
| 41 | + return; |
| 42 | + } catch (error) { |
| 43 | + if (error.status !== 404) throw error; |
| 44 | + } |
| 45 | +
|
| 46 | + // closingIssuesReferences covers closing keywords in the description and |
| 47 | + // issues linked manually from the PR sidebar. |
| 48 | + const query = ` |
| 49 | + query($owner: String!, $repo: String!, $number: Int!) { |
| 50 | + repository(owner: $owner, name: $repo) { |
| 51 | + pullRequest(number: $number) { |
| 52 | + closingIssuesReferences(first: 25) { |
| 53 | + nodes { |
| 54 | + number |
| 55 | + repository { nameWithOwner } |
| 56 | + assignees(first: 25) { nodes { login } } |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | + } |
| 61 | + }`; |
| 62 | + const fullName = `${owner}/${repo}`.toLowerCase(); |
| 63 | + const attempts = 3; |
| 64 | +
|
| 65 | + // Linked issues may not be available immediately after a PR is opened or |
| 66 | + // edited, so retry before closing. |
| 67 | + for (let attempt = 1; attempt <= attempts; attempt++) { |
| 68 | + const result = await github.graphql(query, { owner, repo, number: pr.number }); |
| 69 | + const issues = result.repository.pullRequest.closingIssuesReferences.nodes; |
| 70 | + core.info( |
| 71 | + `Linked issues: ${JSON.stringify(issues.map((i) => `${i.repository.nameWithOwner}#${i.number}`))}`, |
| 72 | + ); |
| 73 | +
|
| 74 | + const match = issues.find( |
| 75 | + (issue) => |
| 76 | + issue.repository.nameWithOwner.toLowerCase() === fullName && |
| 77 | + issue.assignees.nodes.some((a) => a.login === author), |
| 78 | + ); |
| 79 | + if (match) { |
| 80 | + core.info(`Found issue #${match.number} assigned to ${author}.`); |
| 81 | + return; |
| 82 | + } |
| 83 | +
|
| 84 | + if (attempt < attempts) { |
| 85 | + core.info(`No match on attempt ${attempt}, retrying in 15 seconds.`); |
| 86 | + await new Promise((resolve) => setTimeout(resolve, 15000)); |
| 87 | + } |
| 88 | + } |
| 89 | +
|
| 90 | + core.info(`No linked issue assigned to ${author}, closing PR #${pr.number}.`); |
| 91 | + const body = `\ |
| 92 | + Hi @${author}, thanks for your interest in contributing to Pontoon! |
| 93 | +
|
| 94 | + This pull request is being closed automatically because it is not linked to an issue assigned to you. To avoid duplicated or unwanted work, all external contributions need to be tied to an issue that a maintainer has assigned to you. |
| 95 | +
|
| 96 | + To contribute: |
| 97 | +
|
| 98 | + 1. Find an existing issue (or file a new one) and ask in a comment to be assigned to it. |
| 99 | + 2. Once you've been assigned, open a pull request that includes a closing keyword referencing the issue in its description, e.g. \`Fixes #123\`. |
| 100 | +
|
| 101 | + See the [guide to your first contribution](https://pontoon.mozilla.org/docs/dev/first-contribution/) for more information.`; |
| 102 | + await github.rest.issues.createComment({ |
| 103 | + owner, |
| 104 | + repo, |
| 105 | + issue_number: pr.number, |
| 106 | + body, |
| 107 | + }); |
| 108 | + await github.rest.pulls.update({ owner, repo, pull_number: pr.number, state: "closed" }); |
0 commit comments