|
| 1 | +name: Auto‑prefix & Label Issues |
| 2 | + |
| 3 | +on: |
| 4 | + issues: |
| 5 | + types: [opened, edited] |
| 6 | + |
| 7 | +jobs: |
| 8 | + prefix_and_label: |
| 9 | + runs-on: ubuntu-latest |
| 10 | + steps: |
| 11 | + - name: Ensure labels exist, then prefix titles & add labels |
| 12 | + uses: actions/github-script@v6 |
| 13 | + with: |
| 14 | + script: | |
| 15 | + const owner = context.repo.owner; |
| 16 | + const repo = context.repo.repo; |
| 17 | +
|
| 18 | + // 1. Ensure required labels exist |
| 19 | + const required = [ |
| 20 | + { name: 'bug', color: 'd73a4a', description: 'Something isn\'t working' }, |
| 21 | + { name: 'enhancement', color: 'a2eeef', description: 'New feature or request' } |
| 22 | + ]; |
| 23 | +
|
| 24 | + // Fetch current labels in the repo |
| 25 | + const { data: existingLabels } = await github.rest.issues.listLabelsForRepo({ |
| 26 | + owner, repo, per_page: 100 |
| 27 | + }); |
| 28 | + const existingNames = new Set(existingLabels.map(l => l.name)); |
| 29 | +
|
| 30 | + // Create any missing labels |
| 31 | + for (const lbl of required) { |
| 32 | + if (!existingNames.has(lbl.name)) { |
| 33 | + await github.rest.issues.createLabel({ |
| 34 | + owner, |
| 35 | + repo, |
| 36 | + name: lbl.name, |
| 37 | + color: lbl.color, |
| 38 | + description: lbl.description |
| 39 | + }); |
| 40 | + console.log(`Created label "${lbl.name}"`); |
| 41 | + } |
| 42 | + } |
| 43 | +
|
| 44 | + // 2. Fetch all open issues |
| 45 | + const issues = await github.paginate( |
| 46 | + github.rest.issues.listForRepo, |
| 47 | + { owner, repo, state: 'open', per_page: 100 } |
| 48 | + ); |
| 49 | +
|
| 50 | + // 3. Keyword sets |
| 51 | + const enhancementWords = ["add", "added", "improve", "improved"]; |
| 52 | + const bugWords = ["bug", "error", "problem", "crash", "failed", "fix", "fixed"]; |
| 53 | +
|
| 54 | + // 4. Process each issue |
| 55 | + for (const issue of issues) { |
| 56 | + const origTitle = issue.title; |
| 57 | + const lower = origTitle.toLowerCase(); |
| 58 | +
|
| 59 | + // skip if already prefixed |
| 60 | + if (/^\[(bug|enhancement)\]/i.test(origTitle)) continue; |
| 61 | +
|
| 62 | + let prefix, labelToAdd; |
| 63 | + if (enhancementWords.some(w => lower.includes(w))) { |
| 64 | + prefix = "[enhancement]"; |
| 65 | + labelToAdd = "enhancement"; |
| 66 | + } else if (bugWords.some(w => lower.includes(w))) { |
| 67 | + prefix = "[bug]"; |
| 68 | + labelToAdd = "bug"; |
| 69 | + } |
| 70 | +
|
| 71 | + if (prefix) { |
| 72 | + // update title |
| 73 | + await github.rest.issues.update({ |
| 74 | + owner, repo, issue_number: issue.number, |
| 75 | + title: `${prefix} ${origTitle}` |
| 76 | + }); |
| 77 | + console.log(`Prefixed title of #${issue.number}`); |
| 78 | +
|
| 79 | + // add label |
| 80 | + await github.rest.issues.addLabels({ |
| 81 | + owner, repo, issue_number: issue.number, |
| 82 | + labels: [labelToAdd] |
| 83 | + }); |
| 84 | + console.log(`Added label "${labelToAdd}" to #${issue.number}`); |
| 85 | + } |
| 86 | + } |
0 commit comments