|
| 1 | +name: Reopen mistake PR once |
| 2 | + |
| 3 | +on: |
| 4 | + issue_comment: |
| 5 | + types: [created] |
| 6 | + |
| 7 | +permissions: |
| 8 | + issues: write |
| 9 | + pull-requests: write |
| 10 | + |
| 11 | +jobs: |
| 12 | + reopen: |
| 13 | + runs-on: ubuntu-latest |
| 14 | + steps: |
| 15 | + - name: Handle /not-a-mistake |
| 16 | + uses: actions/github-script@v7 |
| 17 | + with: |
| 18 | + script: | |
| 19 | + const comment = context.payload.comment; |
| 20 | + const issue = context.payload.issue; |
| 21 | +
|
| 22 | + if (!issue.pull_request) return; |
| 23 | + if (comment.body.trim() !== '/not-a-mistake') return; |
| 24 | +
|
| 25 | + const pr = await github.rest.pulls.get({ |
| 26 | + owner: context.repo.owner, |
| 27 | + repo: context.repo.repo, |
| 28 | + pull_number: issue.number |
| 29 | + }); |
| 30 | +
|
| 31 | + const labels = pr.data.labels.map(l => l.name); |
| 32 | +
|
| 33 | + // Only PR author may use this |
| 34 | + if (comment.user.login !== pr.data.user.login) { |
| 35 | + return; |
| 36 | + } |
| 37 | +
|
| 38 | + // Must be closed and labeled mistake-pr |
| 39 | + if (pr.data.state !== 'closed') return; |
| 40 | + if (!labels.includes('mistake-pr')) return; |
| 41 | +
|
| 42 | + // Auto-reopen already disabled |
| 43 | + if (labels.includes('reopen-used') || labels.includes('reopen-locked')) { |
| 44 | + await github.rest.issues.createComment({ |
| 45 | + owner: context.repo.owner, |
| 46 | + repo: context.repo.repo, |
| 47 | + issue_number: issue.number, |
| 48 | + body: |
| 49 | + "Automatic reopening is disabled for this PR.\n\n" + |
| 50 | + "A maintainer may still reopen it manually if needed." |
| 51 | + }); |
| 52 | + return; |
| 53 | + } |
| 54 | +
|
| 55 | + // Mark reopen as used FIRST (prevents race) |
| 56 | + await github.rest.issues.addLabels({ |
| 57 | + owner: context.repo.owner, |
| 58 | + repo: context.repo.repo, |
| 59 | + issue_number: issue.number, |
| 60 | + labels: ['reopen-used'] |
| 61 | + }); |
| 62 | +
|
| 63 | + await github.rest.issues.removeLabel({ |
| 64 | + owner: context.repo.owner, |
| 65 | + repo: context.repo.repo, |
| 66 | + issue_number: issue.number, |
| 67 | + name: 'mistake-pr' |
| 68 | + }); |
| 69 | +
|
| 70 | + await github.rest.pulls.update({ |
| 71 | + owner: context.repo.owner, |
| 72 | + repo: context.repo.repo, |
| 73 | + pull_number: issue.number, |
| 74 | + state: 'open' |
| 75 | + }); |
| 76 | +
|
| 77 | + await github.rest.issues.createComment({ |
| 78 | + owner: context.repo.owner, |
| 79 | + repo: context.repo.repo, |
| 80 | + issue_number: issue.number, |
| 81 | + body: |
| 82 | + "PR reopened.\n\n" + |
| 83 | + "If this PR is closed again, automatic reopening will be disabled." |
| 84 | + }); |
0 commit comments