|
| 1 | +name: Label platform |
| 2 | + |
| 3 | +# Issue Forms can't set a label from a dropdown answer, so derive the platform |
| 4 | +# label from the rendered "Platform" field of a bug report. Runs on open/edit and |
| 5 | +# keeps exactly one platform:* label in sync with the chosen option. |
| 6 | +on: |
| 7 | + issues: |
| 8 | + types: [opened, edited] |
| 9 | + |
| 10 | +permissions: |
| 11 | + issues: write |
| 12 | + |
| 13 | +jobs: |
| 14 | + label: |
| 15 | + runs-on: ubuntu-latest |
| 16 | + steps: |
| 17 | + - uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7 |
| 18 | + with: |
| 19 | + script: | |
| 20 | + const body = context.payload.issue.body || ""; |
| 21 | + // The bug form renders the dropdown as a "### Platform" section. |
| 22 | + const m = body.match(/#+\s*Platform\s*\n+\s*([^\n]+)/i); |
| 23 | + const answer = m ? m[1].trim().toLowerCase() : ""; |
| 24 | + const map = [ |
| 25 | + { needle: "android", label: "platform:android" }, |
| 26 | + { needle: "linux", label: "platform:linux" }, |
| 27 | + { needle: "windows", label: "platform:windows" }, |
| 28 | + ]; |
| 29 | + const want = map.find((e) => answer.includes(e.needle)); |
| 30 | + const all = map.map((e) => e.label); |
| 31 | +
|
| 32 | + const current = context.payload.issue.labels.map((l) => l.name); |
| 33 | + const stale = current.filter((l) => all.includes(l) && l !== want?.label); |
| 34 | + for (const label of stale) { |
| 35 | + await github.rest.issues.removeLabel({ |
| 36 | + ...context.repo, |
| 37 | + issue_number: context.issue.number, |
| 38 | + name: label, |
| 39 | + }).catch(() => {}); |
| 40 | + } |
| 41 | + if (want && !current.includes(want.label)) { |
| 42 | + await github.rest.issues.addLabels({ |
| 43 | + ...context.repo, |
| 44 | + issue_number: context.issue.number, |
| 45 | + labels: [want.label], |
| 46 | + }); |
| 47 | + } |
0 commit comments