|
| 1 | +name: Remove Label on PR Update |
| 2 | +// with thanks to ChatGPT |
| 3 | + |
| 4 | +on: |
| 5 | + pull_request: |
| 6 | + types: |
| 7 | + - synchronize # Trigger when new commits are pushed to the PR |
| 8 | + |
| 9 | +jobs: |
| 10 | + remove-label-and-comment: |
| 11 | + runs-on: ubuntu-latest |
| 12 | + |
| 13 | + permissions: |
| 14 | + pull-requests: write # Needed to modify PR labels and post comments |
| 15 | + |
| 16 | + steps: |
| 17 | + - name: Remove label and comment |
| 18 | + uses: actions/github-script@v7 |
| 19 | + with: |
| 20 | + script: | |
| 21 | + const labelToRemove = "MergeOnCIPass"; |
| 22 | +
|
| 23 | + const pr = context.payload.pull_request; |
| 24 | + const prNumber = pr.number; |
| 25 | + const labels = pr.labels.map(label => label.name); |
| 26 | +
|
| 27 | + if (labels.includes(labelToRemove)) { |
| 28 | + core.info(`Label "${labelToRemove}" is present. Removing it...`); |
| 29 | + |
| 30 | + // Remove the label |
| 31 | + await github.rest.issues.removeLabel({ |
| 32 | + owner: context.repo.owner, |
| 33 | + repo: context.repo.repo, |
| 34 | + issue_number: prNumber, |
| 35 | + name: labelToRemove |
| 36 | + }); |
| 37 | +
|
| 38 | + // Add a comment explaining why |
| 39 | + const commentBody = `🔄 New commits were pushed to this branch, so the \`${labelToRemove}\` label has been removed. Please re-request review when ready.`; |
| 40 | +
|
| 41 | + await github.rest.issues.createComment({ |
| 42 | + owner: context.repo.owner, |
| 43 | + repo: context.repo.repo, |
| 44 | + issue_number: prNumber, |
| 45 | + body: commentBody |
| 46 | + }); |
| 47 | +
|
| 48 | + core.info("Label removed and comment posted."); |
| 49 | + } else { |
| 50 | + core.info(`Label "${labelToRemove}" is not present. No action taken.`); |
| 51 | + } |
0 commit comments