|
| 1 | +name: Issue Comment |
| 2 | + |
| 3 | +on: |
| 4 | + issue_comment: |
| 5 | + types: [created] |
| 6 | + |
| 7 | +permissions: |
| 8 | + pull-requests: write |
| 9 | + issues: write |
| 10 | + |
| 11 | +jobs: |
| 12 | + reopen: |
| 13 | + # Only run for comments on pull requests AND when the comment starts with "/ci" |
| 14 | + if: > |
| 15 | + github.event.issue.pull_request != null && |
| 16 | + startsWith(github.event.comment.body, '/ci') |
| 17 | + runs-on: ubuntu-latest |
| 18 | + steps: |
| 19 | + - name: Guardrail — allow only members/collaborators |
| 20 | + id: guard |
| 21 | + uses: actions/github-script@v8 |
| 22 | + with: |
| 23 | + script: | |
| 24 | + const allowed = ['MEMBER','OWNER','COLLABORATOR']; |
| 25 | + const assoc = context.payload.comment.author_association; |
| 26 | + if (!allowed.includes(assoc)) { |
| 27 | + core.notice(`Ignoring /ci from ${context.payload.comment.user.login} (author_association=${assoc}).`); |
| 28 | + core.setOutput('skip', 'true'); |
| 29 | + } |
| 30 | +
|
| 31 | + - name: Close and Reopen PR |
| 32 | + if: steps.guard.outputs.skip != 'true' |
| 33 | + uses: actions/github-script@v8 |
| 34 | + with: |
| 35 | + script: | |
| 36 | + const owner = context.repo.owner; |
| 37 | + const repo = context.repo.repo; |
| 38 | + const pull_number = context.payload.issue.number; |
| 39 | +
|
| 40 | + // Fetch current PR state |
| 41 | + const { data: pr } = await github.rest.pulls.get({ owner, repo, pull_number }); |
| 42 | +
|
| 43 | + // Add reaction so folks know we saw it |
| 44 | + try { |
| 45 | + await github.rest.reactions.createForIssueComment({ |
| 46 | + owner, repo, |
| 47 | + comment_id: context.payload.comment.id, |
| 48 | + content: 'rocket' |
| 49 | + }); |
| 50 | + } catch (e) { |
| 51 | + core.info('Could not add reaction (likely due to permissions). Continuing.'); |
| 52 | + } |
| 53 | +
|
| 54 | + if (pr.state === 'open') { |
| 55 | + core.info(`Closing PR #${pull_number}…`); |
| 56 | + await github.rest.pulls.update({ owner, repo, pull_number, state: 'closed' }); |
| 57 | + await new Promise(r => setTimeout(r, 1500)); |
| 58 | + } else { |
| 59 | + core.info(`PR #${pull_number} is already closed; will reopen it.`); |
| 60 | + } |
| 61 | +
|
| 62 | + core.info(`Reopening PR #${pull_number} to trigger CI (pull_request.reopened)…`); |
| 63 | + await github.rest.pulls.update({ owner, repo, pull_number, state: 'open' }); |
| 64 | +
|
| 65 | + - name: Acknowledge in thread (optional) |
| 66 | + if: steps.guard.outputs.skip != 'true' |
| 67 | + uses: actions/github-script@v8 |
| 68 | + with: |
| 69 | + script: | |
| 70 | + const { owner, repo } = context.repo; |
| 71 | + const issue_number = context.payload.issue.number; |
| 72 | + await github.rest.issues.createComment({ |
| 73 | + owner, repo, issue_number, |
| 74 | + body: `✅ CI retrigger requested by @${context.payload.comment.user.login}. Closed & reopened the PR to fire \`pull_request: reopened\`.` |
| 75 | + }); |
0 commit comments