| 1 | name: Label issue version |
| 2 | |
| 3 | # Issue forms can't let a reporter set a label directly (that needs triage rights), |
| 4 | # so the bug/feature forms carry a "Version line" dropdown and this workflow reads |
| 5 | # the submitted value and applies the matching v1/v2 label on their behalf. |
| 6 | on: |
| 7 | issues: |
| 8 | types: [opened, edited] |
| 9 | |
| 10 | permissions: |
| 11 | issues: write |
| 12 | |
| 13 | concurrency: |
| 14 | group: issue-version-${{ github.event.issue.number }} |
| 15 | cancel-in-progress: true |
| 16 | |
| 17 | jobs: |
| 18 | label: |
| 19 | runs-on: ubuntu-latest |
| 20 | steps: |
| 21 | - uses: actions/github-script@v9 |
| 22 | with: |
| 23 | script: | |
| 24 | const body = context.payload.issue.body || ''; |
| 25 | const sectionOf = (name) => |
| 26 | body.split(/^###\s+/m).find((s) => new RegExp(`^${name}`, 'i').test(s)) || ''; |
| 27 | |
| 28 | const m = sectionOf('Version line').match(/\bv([12])\b/i); |
| 29 | if (!m) { |
| 30 | core.info('No version line found; nothing to label.'); |
| 31 | return; |
| 32 | } |
| 33 | let choice = 'v' + m[1]; |
| 34 | |
| 35 | // The dropdown is a choice, and v2 sits first, so it gets picked by |
| 36 | // reporters who are actually on 0.x. `Exact version` is pasted from |
| 37 | // `reasonix --version`, so when it parses it is the better evidence: |
| 38 | // 0.x is the TypeScript line, 1.x the Go rewrite. Anything that does |
| 39 | // not parse (a commit sha, "main-v2") leaves the dropdown to decide. |
| 40 | const exact = sectionOf('Exact version').match(/\b(\d+)\.(\d+)\.(\d+)/); |
| 41 | if (exact) { |
| 42 | const fromVersion = Number(exact[1]) === 0 ? 'v1' : 'v2'; |
| 43 | if (fromVersion !== choice) { |
| 44 | core.info(`Version line says ${choice} but ${exact[0]} says ${fromVersion}; trusting the version.`); |
| 45 | choice = fromVersion; |
| 46 | } |
| 47 | } |
| 48 | const other = choice === 'v2' ? 'v1' : 'v2'; |
| 49 | await github.rest.issues.addLabels({ |
| 50 | ...context.repo, |
| 51 | issue_number: context.issue.number, |
| 52 | labels: [choice], |
| 53 | }); |
| 54 | // Keep v1/v2 mutually exclusive in case an edit flipped the choice. |
| 55 | try { |
| 56 | await github.rest.issues.removeLabel({ |
| 57 | ...context.repo, |
| 58 | issue_number: context.issue.number, |
| 59 | name: other, |
| 60 | }); |
| 61 | } catch (e) { |
| 62 | core.info(`No ${other} label to remove.`); |
| 63 | } |
| 64 |