| 1 | name: Label PR version |
| 2 | |
| 3 | # Contributors (especially from forks) can't set labels themselves — that needs |
| 4 | # triage rights. A PR's version line is fully determined by its base branch, so |
| 5 | # this reads base.ref and applies v1/v2 on their behalf. pull_request_target runs |
| 6 | # in the base repo's context so it has write access even for fork PRs; it only |
| 7 | # reads trusted metadata (the base ref) and never checks out or runs PR code. |
| 8 | on: |
| 9 | pull_request_target: |
| 10 | types: [opened, reopened, edited] |
| 11 | |
| 12 | permissions: |
| 13 | pull-requests: write |
| 14 | |
| 15 | concurrency: |
| 16 | group: pr-version-${{ github.event.pull_request.number }} |
| 17 | cancel-in-progress: true |
| 18 | |
| 19 | jobs: |
| 20 | label: |
| 21 | runs-on: ubuntu-latest |
| 22 | steps: |
| 23 | - uses: actions/github-script@v9 |
| 24 | with: |
| 25 | script: | |
| 26 | const base = context.payload.pull_request.base.ref; |
| 27 | const label = base === 'v1' ? 'v1' |
| 28 | : base === 'main-v2' ? 'v2' |
| 29 | : null; |
| 30 | if (!label) { |
| 31 | core.info(`Base branch '${base}' isn't a release line; skipping.`); |
| 32 | return; |
| 33 | } |
| 34 | const other = label === 'v2' ? 'v1' : 'v2'; |
| 35 | await github.rest.issues.addLabels({ |
| 36 | ...context.repo, |
| 37 | issue_number: context.payload.pull_request.number, |
| 38 | labels: [label], |
| 39 | }); |
| 40 | // Drop the other line's label if the base was changed after opening. |
| 41 | try { |
| 42 | await github.rest.issues.removeLabel({ |
| 43 | ...context.repo, |
| 44 | issue_number: context.payload.pull_request.number, |
| 45 | name: other, |
| 46 | }); |
| 47 | } catch (e) { |
| 48 | core.info(`No ${other} label to remove.`); |
| 49 | } |
| 50 |