|
| 1 | +name: Label PRs |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request: |
| 5 | + types: [opened, edited, synchronize] |
| 6 | + |
| 7 | +permissions: |
| 8 | + contents: read |
| 9 | + pull-requests: write |
| 10 | + |
| 11 | +jobs: |
| 12 | + label: |
| 13 | + # Disabled: fails on fork PRs due to token permissions |
| 14 | + if: false |
| 15 | + runs-on: ubuntu-latest |
| 16 | + steps: |
| 17 | + - name: Checkout |
| 18 | + uses: actions/checkout@v4 |
| 19 | + |
| 20 | + - name: Label PR based on files |
| 21 | + uses: actions/labeler@v6 |
| 22 | + with: |
| 23 | + repo-token: ${{ secrets.GITHUB_TOKEN }} |
| 24 | + configuration-path: .github/labeler.yml |
| 25 | + |
| 26 | + - name: Label PR based on size |
| 27 | + uses: actions/github-script@v8 |
| 28 | + with: |
| 29 | + script: | |
| 30 | + const pr = context.payload.pull_request; |
| 31 | + const additions = pr.additions; |
| 32 | + const deletions = pr.deletions; |
| 33 | + const totalChanges = additions + deletions; |
| 34 | +
|
| 35 | + // Remove existing size labels |
| 36 | + const existingLabels = pr.labels.map(label => label.name); |
| 37 | + const sizeLabels = ['size/XS', 'size/S', 'size/M', 'size/L', 'size/XL']; |
| 38 | + const labelsToRemove = existingLabels.filter(label => sizeLabels.includes(label)); |
| 39 | +
|
| 40 | + for (const label of labelsToRemove) { |
| 41 | + await github.rest.issues.removeLabel({ |
| 42 | + owner: context.repo.owner, |
| 43 | + repo: context.repo.repo, |
| 44 | + issue_number: pr.number, |
| 45 | + name: label |
| 46 | + }); |
| 47 | + } |
| 48 | +
|
| 49 | + // Determine size label |
| 50 | + let sizeLabel; |
| 51 | + if (totalChanges < 10) { |
| 52 | + sizeLabel = 'size/XS'; |
| 53 | + } else if (totalChanges < 50) { |
| 54 | + sizeLabel = 'size/S'; |
| 55 | + } else if (totalChanges < 200) { |
| 56 | + sizeLabel = 'size/M'; |
| 57 | + } else if (totalChanges < 500) { |
| 58 | + sizeLabel = 'size/L'; |
| 59 | + } else { |
| 60 | + sizeLabel = 'size/XL'; |
| 61 | + } |
| 62 | +
|
| 63 | + // Add new size label |
| 64 | + await github.rest.issues.addLabels({ |
| 65 | + owner: context.repo.owner, |
| 66 | + repo: context.repo.repo, |
| 67 | + issue_number: pr.number, |
| 68 | + labels: [sizeLabel] |
| 69 | + }); |
| 70 | +
|
| 71 | + core.info(`Added label: ${sizeLabel} (${totalChanges} changes)`); |
| 72 | +
|
| 73 | + - name: Label PR based on title |
| 74 | + uses: actions/github-script@v8 |
| 75 | + with: |
| 76 | + script: | |
| 77 | + const pr = context.payload.pull_request; |
| 78 | + const title = pr.title.toLowerCase(); |
| 79 | +
|
| 80 | + const labels = []; |
| 81 | +
|
| 82 | + // Conventional commit prefixes (case-insensitive with [] or () brackets) |
| 83 | + const patterns = { |
| 84 | + feat: /^(feat|FEAT)[\[:(]/, |
| 85 | + fix: /^(fix|FIX)[\[:(]/, |
| 86 | + docs: /^(docs|DOCS)[\[:(]/, |
| 87 | + test: /^(test|TEST|tests|TESTS)[\[:(]/, |
| 88 | + chore: /^(chore|CHORE)[\[:(]/, |
| 89 | + refactor: /^(refactor|REFACTOR)[\[:(]/, |
| 90 | + perf: /^(perf|PERF)[\[:(]/, |
| 91 | + style: /^(style|STYLE)[\[:(]/, |
| 92 | + ci: /^(ci|CI)[\[:(]/, |
| 93 | + }; |
| 94 | +
|
| 95 | + if (patterns.feat.test(pr.title)) { |
| 96 | + labels.push('enhancement'); |
| 97 | + } else if (patterns.fix.test(pr.title)) { |
| 98 | + labels.push('bug'); |
| 99 | + } else if (patterns.docs.test(pr.title)) { |
| 100 | + labels.push('documentation'); |
| 101 | + } else if (patterns.test.test(pr.title)) { |
| 102 | + labels.push('tests'); |
| 103 | + } else if (patterns.chore.test(pr.title)) { |
| 104 | + labels.push('maintenance'); |
| 105 | + } else if (patterns.refactor.test(pr.title)) { |
| 106 | + labels.push('refactor'); |
| 107 | + } else if (patterns.perf.test(pr.title)) { |
| 108 | + labels.push('performance'); |
| 109 | + } else if (patterns.ci.test(pr.title)) { |
| 110 | + labels.push('ci'); |
| 111 | + } |
| 112 | +
|
| 113 | + // Keywords |
| 114 | + if (title.includes('breaking') || title.includes('breaking change')) { |
| 115 | + labels.push('breaking-change'); |
| 116 | + } |
| 117 | +
|
| 118 | + if (title.includes('security')) { |
| 119 | + labels.push('security'); |
| 120 | + } |
| 121 | +
|
| 122 | + // Add labels |
| 123 | + if (labels.length > 0) { |
| 124 | + await github.rest.issues.addLabels({ |
| 125 | + owner: context.repo.owner, |
| 126 | + repo: context.repo.repo, |
| 127 | + issue_number: pr.number, |
| 128 | + labels: labels |
| 129 | + }); |
| 130 | +
|
| 131 | + core.info(`Added labels: ${labels.join(', ')}`); |
| 132 | + } |
0 commit comments