|
| 1 | +name: PR Auto Label |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request: |
| 5 | + types: [opened, synchronize, reopened] |
| 6 | + |
| 7 | +# Cancel in-progress runs for the same PR |
| 8 | +concurrency: |
| 9 | + group: pr-auto-label-${{ github.event.pull_request.number }} |
| 10 | + cancel-in-progress: true |
| 11 | + |
| 12 | +permissions: |
| 13 | + contents: read |
| 14 | + pull-requests: write |
| 15 | + |
| 16 | +jobs: |
| 17 | + label: |
| 18 | + name: Auto Label PR |
| 19 | + runs-on: ubuntu-latest |
| 20 | + # Don't run on fork PRs (they can't write labels) |
| 21 | + if: github.event.pull_request.head.repo.full_name == github.repository |
| 22 | + timeout-minutes: 5 |
| 23 | + steps: |
| 24 | + - name: Auto-label PR |
| 25 | + uses: actions/github-script@v7 |
| 26 | + with: |
| 27 | + retries: 3 |
| 28 | + retry-exempt-status-codes: 400,401,403,404,422 |
| 29 | + script: | |
| 30 | + const { owner, repo } = context.repo; |
| 31 | + const pr = context.payload.pull_request; |
| 32 | + const prNumber = pr.number; |
| 33 | + const title = pr.title; |
| 34 | +
|
| 35 | + console.log(`::group::PR #${prNumber} - Auto-labeling`); |
| 36 | + console.log(`Title: ${title}`); |
| 37 | +
|
| 38 | + const labelsToAdd = new Set(); |
| 39 | + const labelsToRemove = new Set(); |
| 40 | +
|
| 41 | + // ═══════════════════════════════════════════════════════════════ |
| 42 | + // TYPE LABELS (from PR title - Conventional Commits) |
| 43 | + // ═══════════════════════════════════════════════════════════════ |
| 44 | + const typeMap = { |
| 45 | + 'feat': 'feature', |
| 46 | + 'fix': 'bug', |
| 47 | + 'docs': 'documentation', |
| 48 | + 'refactor': 'refactor', |
| 49 | + 'test': 'test', |
| 50 | + 'ci': 'ci', |
| 51 | + 'chore': 'chore', |
| 52 | + 'perf': 'performance', |
| 53 | + 'style': 'style', |
| 54 | + 'build': 'build' |
| 55 | + }; |
| 56 | +
|
| 57 | + const typeMatch = title.match(/^(\w+)(\(.+?\))?(!)?:/); |
| 58 | + if (typeMatch) { |
| 59 | + const type = typeMatch[1].toLowerCase(); |
| 60 | + const isBreaking = typeMatch[3] === '!'; |
| 61 | +
|
| 62 | + if (typeMap[type]) { |
| 63 | + labelsToAdd.add(typeMap[type]); |
| 64 | + console.log(` 📝 Type: ${type} → ${typeMap[type]}`); |
| 65 | + } |
| 66 | +
|
| 67 | + if (isBreaking) { |
| 68 | + labelsToAdd.add('breaking-change'); |
| 69 | + console.log(` ⚠️ Breaking change detected`); |
| 70 | + } |
| 71 | + } else { |
| 72 | + console.log(` ⚠️ No conventional commit prefix found in title`); |
| 73 | + } |
| 74 | +
|
| 75 | + // ═══════════════════════════════════════════════════════════════ |
| 76 | + // AREA LABELS (from changed files) |
| 77 | + // ═══════════════════════════════════════════════════════════════ |
| 78 | + let files = []; |
| 79 | + try { |
| 80 | + const { data } = await github.rest.pulls.listFiles({ |
| 81 | + owner, |
| 82 | + repo, |
| 83 | + pull_number: prNumber, |
| 84 | + per_page: 100 |
| 85 | + }); |
| 86 | + files = data; |
| 87 | + } catch (e) { |
| 88 | + console.log(` ⚠️ Could not fetch files: ${e.message}`); |
| 89 | + } |
| 90 | +
|
| 91 | + const areas = { |
| 92 | + frontend: false, |
| 93 | + backend: false, |
| 94 | + ci: false, |
| 95 | + docs: false, |
| 96 | + tests: false |
| 97 | + }; |
| 98 | +
|
| 99 | + for (const file of files) { |
| 100 | + const path = file.filename; |
| 101 | + if (path.startsWith('apps/frontend/')) areas.frontend = true; |
| 102 | + if (path.startsWith('apps/backend/')) areas.backend = true; |
| 103 | + if (path.startsWith('.github/')) areas.ci = true; |
| 104 | + if (path.endsWith('.md') || path.startsWith('docs/')) areas.docs = true; |
| 105 | + if (path.startsWith('tests/') || path.includes('.test.') || path.includes('.spec.')) areas.tests = true; |
| 106 | + } |
| 107 | +
|
| 108 | + // Determine area label (mutually exclusive) |
| 109 | + const areaLabels = ['area/frontend', 'area/backend', 'area/fullstack', 'area/ci']; |
| 110 | +
|
| 111 | + if (areas.frontend && areas.backend) { |
| 112 | + labelsToAdd.add('area/fullstack'); |
| 113 | + areaLabels.filter(l => l !== 'area/fullstack').forEach(l => labelsToRemove.add(l)); |
| 114 | + console.log(` 📁 Area: fullstack (${files.length} files)`); |
| 115 | + } else if (areas.frontend) { |
| 116 | + labelsToAdd.add('area/frontend'); |
| 117 | + areaLabels.filter(l => l !== 'area/frontend').forEach(l => labelsToRemove.add(l)); |
| 118 | + console.log(` 📁 Area: frontend (${files.length} files)`); |
| 119 | + } else if (areas.backend) { |
| 120 | + labelsToAdd.add('area/backend'); |
| 121 | + areaLabels.filter(l => l !== 'area/backend').forEach(l => labelsToRemove.add(l)); |
| 122 | + console.log(` 📁 Area: backend (${files.length} files)`); |
| 123 | + } else if (areas.ci) { |
| 124 | + labelsToAdd.add('area/ci'); |
| 125 | + areaLabels.filter(l => l !== 'area/ci').forEach(l => labelsToRemove.add(l)); |
| 126 | + console.log(` 📁 Area: ci (${files.length} files)`); |
| 127 | + } |
| 128 | +
|
| 129 | + // ═══════════════════════════════════════════════════════════════ |
| 130 | + // SIZE LABELS (from lines changed) |
| 131 | + // ═══════════════════════════════════════════════════════════════ |
| 132 | + const additions = pr.additions || 0; |
| 133 | + const deletions = pr.deletions || 0; |
| 134 | + const totalLines = additions + deletions; |
| 135 | +
|
| 136 | + const sizeLabels = ['size/XS', 'size/S', 'size/M', 'size/L', 'size/XL']; |
| 137 | + let sizeLabel; |
| 138 | +
|
| 139 | + if (totalLines < 10) sizeLabel = 'size/XS'; |
| 140 | + else if (totalLines < 100) sizeLabel = 'size/S'; |
| 141 | + else if (totalLines < 500) sizeLabel = 'size/M'; |
| 142 | + else if (totalLines < 1000) sizeLabel = 'size/L'; |
| 143 | + else sizeLabel = 'size/XL'; |
| 144 | +
|
| 145 | + labelsToAdd.add(sizeLabel); |
| 146 | + sizeLabels.filter(l => l !== sizeLabel).forEach(l => labelsToRemove.add(l)); |
| 147 | + console.log(` 📏 Size: ${sizeLabel} (+${additions}/-${deletions} = ${totalLines} lines)`); |
| 148 | +
|
| 149 | + console.log('::endgroup::'); |
| 150 | +
|
| 151 | + // ═══════════════════════════════════════════════════════════════ |
| 152 | + // APPLY LABELS |
| 153 | + // ═══════════════════════════════════════════════════════════════ |
| 154 | + console.log(`::group::Applying labels`); |
| 155 | +
|
| 156 | + // Remove old labels (in parallel) |
| 157 | + const removeArray = [...labelsToRemove].filter(l => !labelsToAdd.has(l)); |
| 158 | + if (removeArray.length > 0) { |
| 159 | + const removePromises = removeArray.map(async (label) => { |
| 160 | + try { |
| 161 | + await github.rest.issues.removeLabel({ |
| 162 | + owner, |
| 163 | + repo, |
| 164 | + issue_number: prNumber, |
| 165 | + name: label |
| 166 | + }); |
| 167 | + console.log(` ✓ Removed: ${label}`); |
| 168 | + } catch (e) { |
| 169 | + if (e.status !== 404) { |
| 170 | + console.log(` ⚠ Could not remove ${label}: ${e.message}`); |
| 171 | + } |
| 172 | + } |
| 173 | + }); |
| 174 | + await Promise.all(removePromises); |
| 175 | + } |
| 176 | +
|
| 177 | + // Add new labels |
| 178 | + const addArray = [...labelsToAdd]; |
| 179 | + if (addArray.length > 0) { |
| 180 | + try { |
| 181 | + await github.rest.issues.addLabels({ |
| 182 | + owner, |
| 183 | + repo, |
| 184 | + issue_number: prNumber, |
| 185 | + labels: addArray |
| 186 | + }); |
| 187 | + console.log(` ✓ Added: ${addArray.join(', ')}`); |
| 188 | + } catch (e) { |
| 189 | + // Some labels might not exist |
| 190 | + if (e.status === 404) { |
| 191 | + core.warning(`Some labels do not exist. Please create them in repository settings.`); |
| 192 | + // Try adding one by one |
| 193 | + for (const label of addArray) { |
| 194 | + try { |
| 195 | + await github.rest.issues.addLabels({ |
| 196 | + owner, |
| 197 | + repo, |
| 198 | + issue_number: prNumber, |
| 199 | + labels: [label] |
| 200 | + }); |
| 201 | + } catch (e2) { |
| 202 | + console.log(` ⚠ Label '${label}' does not exist`); |
| 203 | + } |
| 204 | + } |
| 205 | + } else { |
| 206 | + throw e; |
| 207 | + } |
| 208 | + } |
| 209 | + } |
| 210 | +
|
| 211 | + console.log('::endgroup::'); |
| 212 | +
|
| 213 | + // Summary |
| 214 | + console.log(`✅ PR #${prNumber} labeled: ${addArray.join(', ')}`); |
| 215 | +
|
| 216 | + // Write job summary |
| 217 | + core.summary |
| 218 | + .addHeading(`PR #${prNumber} Auto-Labels`, 3) |
| 219 | + .addTable([ |
| 220 | + [{data: 'Category', header: true}, {data: 'Label', header: true}], |
| 221 | + ['Type', typeMatch ? typeMap[typeMatch[1].toLowerCase()] || 'none' : 'none'], |
| 222 | + ['Area', areas.frontend && areas.backend ? 'fullstack' : areas.frontend ? 'frontend' : areas.backend ? 'backend' : 'other'], |
| 223 | + ['Size', sizeLabel] |
| 224 | + ]) |
| 225 | + .addRaw(`\n**Files changed:** ${files.length}\n`) |
| 226 | + .addRaw(`**Lines:** +${additions} / -${deletions}\n`); |
| 227 | + await core.summary.write(); |
0 commit comments