|
| 1 | +name: 'Label CI Status on PRs' |
| 2 | +on: |
| 3 | + check_suite: |
| 4 | + types: [completed] |
| 5 | + |
| 6 | +permissions: |
| 7 | + pull-requests: write |
| 8 | + |
| 9 | +jobs: |
| 10 | + label-ci-status: |
| 11 | + runs-on: ubuntu-latest |
| 12 | + # Only run for PRs, not pushes to branches |
| 13 | + if: github.event.check_suite.pull_requests[0] != null |
| 14 | + steps: |
| 15 | + - name: Add or remove ci-failed label |
| 16 | + uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1 |
| 17 | + with: |
| 18 | + script: | |
| 19 | + const pullRequests = context.payload.check_suite.pull_requests; |
| 20 | + const conclusion = context.payload.check_suite.conclusion; |
| 21 | + const label = 'ci-failed'; |
| 22 | +
|
| 23 | + for (const pr of pullRequests) { |
| 24 | + const prNumber = pr.number; |
| 25 | +
|
| 26 | + // Get current labels |
| 27 | + const { data: currentLabels } = await github.rest.issues.listLabelsOnIssue({ |
| 28 | + owner: context.repo.owner, |
| 29 | + repo: context.repo.repo, |
| 30 | + issue_number: prNumber, |
| 31 | + }); |
| 32 | + const hasLabel = currentLabels.some(l => l.name === label); |
| 33 | +
|
| 34 | + if (conclusion === 'failure' && !hasLabel) { |
| 35 | + console.log(`PR #${prNumber}: CI failed → adding '${label}' label`); |
| 36 | + await github.rest.issues.addLabels({ |
| 37 | + owner: context.repo.owner, |
| 38 | + repo: context.repo.repo, |
| 39 | + issue_number: prNumber, |
| 40 | + labels: [label], |
| 41 | + }); |
| 42 | + } else if (conclusion === 'success' && hasLabel) { |
| 43 | + console.log(`PR #${prNumber}: CI passed → removing '${label}' label`); |
| 44 | + await github.rest.issues.removeLabel({ |
| 45 | + owner: context.repo.owner, |
| 46 | + repo: context.repo.repo, |
| 47 | + issue_number: prNumber, |
| 48 | + name: label, |
| 49 | + }); |
| 50 | + } else { |
| 51 | + console.log(`PR #${prNumber}: conclusion=${conclusion}, hasLabel=${hasLabel} → no action`); |
| 52 | + } |
| 53 | + } |
0 commit comments