|
1 | 1 | name: Close issue/PR on adding invalid label |
2 | 2 |
|
3 | 3 | # Closes public issues and PRs when the invalid label is added. |
| 4 | +# Re-closes invalid PRs that someone without write access reopens. |
| 5 | +# Reopens PRs closed as invalid when maintainers add the skip-spammy-check label. |
4 | 6 |
|
5 | 7 | on: |
6 | 8 | issues: |
7 | 9 | types: [labeled] |
8 | | - # pull_request_target lets the workflow close forked PRs after the invalid label is added. |
| 10 | + # pull_request_target lets the workflow close or reopen forked PRs when they're labeled or reopened. |
9 | 11 | pull_request_target: |
10 | | - types: [labeled] |
| 12 | + types: [labeled, reopened] |
11 | 13 |
|
12 | 14 | permissions: |
13 | 15 | contents: read |
14 | | - issues: write |
15 | | - pull-requests: write |
16 | 16 |
|
17 | 17 | jobs: |
18 | 18 | close-on-adding-invalid-label: |
19 | 19 | if: github.repository == 'github/docs' && github.event.label.name == 'invalid' |
20 | 20 | runs-on: ubuntu-latest |
| 21 | + permissions: |
| 22 | + contents: read |
| 23 | + issues: write |
| 24 | + pull-requests: write |
21 | 25 |
|
22 | 26 | steps: |
23 | 27 | - name: Close issue |
|
42 | 46 | if: ${{ failure() && github.event_name != 'pull_request_target' }} |
43 | 47 | with: |
44 | 48 | slack_token: ${{ secrets.SLACK_DOCS_BOT_TOKEN }} |
| 49 | + |
| 50 | + # Authors can reopen PRs they closed themselves, even after the invalid label is added. |
| 51 | + reclose-reopened-invalid-pr: |
| 52 | + if: > |
| 53 | + github.repository == 'github/docs' && github.event_name == 'pull_request_target' && |
| 54 | + github.event.action == 'reopened' && |
| 55 | + contains(github.event.pull_request.labels.*.name, 'invalid') |
| 56 | + runs-on: ubuntu-latest |
| 57 | + permissions: |
| 58 | + pull-requests: write |
| 59 | + steps: |
| 60 | + - name: Re-close PR unless reopened by someone with write access |
| 61 | + env: |
| 62 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 63 | + PR_URL: ${{ github.event.pull_request.html_url }} |
| 64 | + SENDER: ${{ github.event.sender.login }} |
| 65 | + run: | |
| 66 | + if ! permission=$(gh api "repos/$GITHUB_REPOSITORY/collaborators/$SENDER/permission" --jq '.permission'); then |
| 67 | + echo "::warning::Couldn't look up permission for $SENDER, so treating them as read-only." |
| 68 | + permission=none |
| 69 | + fi |
| 70 | + if [[ "$permission" == "admin" || "$permission" == "write" ]]; then |
| 71 | + echo "$SENDER has $permission access, so leaving the PR open." |
| 72 | + exit 0 |
| 73 | + fi |
| 74 | + gh pr close "$PR_URL" --comment 'This pull request is labeled `invalid`, so only maintainers can reopen it. If you think this is incorrect, please mention this in a corresponding issue.' |
| 75 | +
|
| 76 | + # Maintainers add skip-spammy-check to recover a PR that automated checks closed as invalid. |
| 77 | + reopen-on-adding-skip-label: |
| 78 | + if: github.repository == 'github/docs' && github.event_name == 'pull_request_target' && github.event.label.name == 'skip-spammy-check' |
| 79 | + runs-on: ubuntu-latest |
| 80 | + permissions: |
| 81 | + contents: read |
| 82 | + steps: |
| 83 | + - uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0 |
| 84 | + with: |
| 85 | + # A PAT makes the reopen trigger other workflows, such as triage, like a manual reopen. |
| 86 | + github-token: ${{ secrets.DOCS_BOT_PAT_BASE }} |
| 87 | + script: | |
| 88 | + const { owner, repo } = context.repo |
| 89 | + const pull_number = context.payload.pull_request.number |
| 90 | +
|
| 91 | + // The event payload can be stale by the time this job runs, so read the current state. |
| 92 | + const { data: pr } = await github.rest.pulls.get({ owner, repo, pull_number }) |
| 93 | + const labels = pr.labels.map(label => label.name.toLowerCase()) |
| 94 | +
|
| 95 | + if (pr.merged || pr.state !== 'closed') { |
| 96 | + core.info('The pull request is not closed, so there is nothing to reopen.') |
| 97 | + return |
| 98 | + } |
| 99 | +
|
| 100 | + if (!labels.includes('skip-spammy-check')) { |
| 101 | + core.info('The pull request no longer has the skip-spammy-check label.') |
| 102 | + return |
| 103 | + } |
| 104 | +
|
| 105 | + // Automated checks add the invalid label before closing, so PRs without it were closed for another reason. |
| 106 | + if (!labels.includes('invalid')) { |
| 107 | + core.info('The pull request was not closed as invalid, so it stays closed.') |
| 108 | + return |
| 109 | + } |
| 110 | +
|
| 111 | + try { |
| 112 | + await github.rest.pulls.update({ owner, repo, pull_number, state: 'open' }) |
| 113 | + } catch (error) { |
| 114 | + if (error.status !== 422) throw error |
| 115 | +
|
| 116 | + // GitHub refuses to reopen a PR after its head or base branch was deleted or force-pushed. |
| 117 | + core.warning(`Could not reopen the pull request: ${error.message}`) |
| 118 | + await github.rest.issues.createComment({ |
| 119 | + owner, |
| 120 | + repo, |
| 121 | + issue_number: pull_number, |
| 122 | + body: 'I could not reopen this pull request automatically, most likely because its head or base branch was deleted or force-pushed after it was closed. Restore the branch, then remove and re-add the `skip-spammy-check` label to retry automatic reopening and removal of the `invalid` label.', |
| 123 | + }) |
| 124 | + return |
| 125 | + } |
| 126 | +
|
| 127 | + try { |
| 128 | + await github.rest.issues.removeLabel({ |
| 129 | + owner, |
| 130 | + repo, |
| 131 | + issue_number: pull_number, |
| 132 | + name: 'invalid', |
| 133 | + }) |
| 134 | + } catch (error) { |
| 135 | + // A maintainer or an earlier run may have removed the label already. |
| 136 | + if (error.status !== 404) throw error |
| 137 | + } |
| 138 | +
|
| 139 | + await github.rest.issues.createComment({ |
| 140 | + owner, |
| 141 | + repo, |
| 142 | + issue_number: pull_number, |
| 143 | + body: 'The `skip-spammy-check` label was added, so I removed the `invalid` label and reopened this pull request. The spammy pull request check skips it while the label is applied.', |
| 144 | + }) |
0 commit comments