|
| 1 | +name: PR Labeler |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request: |
| 5 | + types: [opened, reopened, edited] |
| 6 | + pull_request_review: |
| 7 | + types: [submitted] |
| 8 | + |
| 9 | +permissions: |
| 10 | + issues: write |
| 11 | + pull-requests: write |
| 12 | + |
| 13 | +jobs: |
| 14 | + label-type: |
| 15 | + runs-on: ubuntu-latest |
| 16 | + if: github.event_name == 'pull_request' && (github.event.action == 'opened' || github.event.action == 'edited') |
| 17 | + steps: |
| 18 | + - name: Apply type label |
| 19 | + uses: actions/github-script@v7 |
| 20 | + with: |
| 21 | + script: | |
| 22 | + const body = context.payload.pull_request.body || ''; |
| 23 | + const match = body.match(/\|\s*Type\s*\|\s*(feature|fix)\s*\|/i); |
| 24 | + if (!match) return; |
| 25 | +
|
| 26 | + const type = match[1].toLowerCase(); |
| 27 | + const label = type === 'fix' ? 'bug' : 'enhancement'; |
| 28 | + const prNumber = context.payload.pull_request.number; |
| 29 | +
|
| 30 | + const { data: labels } = await github.rest.issues.listLabelsOnIssue({ |
| 31 | + owner: context.repo.owner, |
| 32 | + repo: context.repo.repo, |
| 33 | + issue_number: prNumber, |
| 34 | + }); |
| 35 | +
|
| 36 | + const toRemove = ['bug', 'enhancement'].filter(l => labels.some(e => e.name === l) && l !== label); |
| 37 | + for (const l of toRemove) { |
| 38 | + await github.rest.issues.removeLabel({ |
| 39 | + owner: context.repo.owner, |
| 40 | + repo: context.repo.repo, |
| 41 | + issue_number: prNumber, |
| 42 | + name: l, |
| 43 | + }).catch(() => {}); |
| 44 | + } |
| 45 | +
|
| 46 | + await github.rest.issues.addLabels({ |
| 47 | + owner: context.repo.owner, |
| 48 | + repo: context.repo.repo, |
| 49 | + issue_number: prNumber, |
| 50 | + labels: [label], |
| 51 | + }); |
| 52 | +
|
| 53 | + label-status-add: |
| 54 | + runs-on: ubuntu-latest |
| 55 | + if: github.event_name == 'pull_request' && (github.event.action == 'opened' || github.event.action == 'reopened') |
| 56 | + steps: |
| 57 | + - name: 'Add "status: needs review"' |
| 58 | + uses: actions/github-script@v7 |
| 59 | + with: |
| 60 | + script: | |
| 61 | + await github.rest.issues.addLabels({ |
| 62 | + owner: context.repo.owner, |
| 63 | + repo: context.repo.repo, |
| 64 | + issue_number: context.payload.pull_request.number, |
| 65 | + labels: ['status: needs review'], |
| 66 | + }); |
| 67 | +
|
| 68 | + label-status-remove: |
| 69 | + runs-on: ubuntu-latest |
| 70 | + if: github.event_name == 'pull_request_review' && github.event.review.state == 'approved' |
| 71 | + steps: |
| 72 | + - name: 'Remove "status: needs review" on approval' |
| 73 | + uses: actions/github-script@v7 |
| 74 | + with: |
| 75 | + script: | |
| 76 | + await github.rest.issues.removeLabel({ |
| 77 | + owner: context.repo.owner, |
| 78 | + repo: context.repo.repo, |
| 79 | + issue_number: context.payload.pull_request.number, |
| 80 | + name: 'status: needs review', |
| 81 | + }).catch(() => {}); |
0 commit comments