|
| 1 | +name: Release Labels |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request_target: |
| 5 | + types: [opened, edited, reopened, synchronize] |
| 6 | + |
| 7 | +permissions: |
| 8 | + contents: read |
| 9 | + issues: write |
| 10 | + pull-requests: write |
| 11 | + |
| 12 | +jobs: |
| 13 | + apply-release-labels: |
| 14 | + runs-on: ubuntu-latest |
| 15 | + steps: |
| 16 | + - name: Map PR title to release labels |
| 17 | + uses: actions/github-script@v7 |
| 18 | + with: |
| 19 | + script: | |
| 20 | + const pr = context.payload.pull_request; |
| 21 | + const title = pr.title || ""; |
| 22 | + const match = title.match(/^([a-z]+)(\([^)]+\))?(!)?:\s.+/); |
| 23 | + const type = match ? match[1] : ""; |
| 24 | + const isBreaking = Boolean(match && match[3]); |
| 25 | +
|
| 26 | + const typeToPrimaryLabel = { |
| 27 | + feat: "release:feature", |
| 28 | + fix: "release:fix", |
| 29 | + perf: "release:performance", |
| 30 | + docs: "release:docs", |
| 31 | + refactor: "release:internal", |
| 32 | + test: "release:internal", |
| 33 | + build: "release:internal", |
| 34 | + ci: "release:internal", |
| 35 | + chore: "release:internal", |
| 36 | + revert: "release:other", |
| 37 | + }; |
| 38 | +
|
| 39 | + const labelDefinitions = [ |
| 40 | + { name: "release:feature", color: "1d76db", description: "User-visible feature for release notes" }, |
| 41 | + { name: "release:fix", color: "d73a4a", description: "User-visible bug fix for release notes" }, |
| 42 | + { name: "release:performance", color: "5319e7", description: "Performance improvement for release notes" }, |
| 43 | + { name: "release:docs", color: "0075ca", description: "Documentation change for release notes" }, |
| 44 | + { name: "release:other", color: "fbca04", description: "Other release-noteworthy changes" }, |
| 45 | + { name: "release:internal", color: "cfd3d7", description: "Internal changes excluded from release notes" }, |
| 46 | + { name: "release:maintenance", color: "bfdadc", description: "Maintenance changes for release notes" }, |
| 47 | + { name: "release:breaking", color: "b60205", description: "Breaking change" }, |
| 48 | + { name: "release:skip", color: "ffffff", description: "Explicitly exclude from release notes" }, |
| 49 | + ]; |
| 50 | +
|
| 51 | + for (const label of labelDefinitions) { |
| 52 | + try { |
| 53 | + await github.rest.issues.createLabel({ |
| 54 | + owner: context.repo.owner, |
| 55 | + repo: context.repo.repo, |
| 56 | + name: label.name, |
| 57 | + color: label.color, |
| 58 | + description: label.description, |
| 59 | + }); |
| 60 | + } catch (error) { |
| 61 | + if (error.status !== 422) { |
| 62 | + throw error; |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | +
|
| 67 | + const primaryLabel = typeToPrimaryLabel[type] || "release:other"; |
| 68 | + const desired = [primaryLabel]; |
| 69 | + if (isBreaking) desired.push("release:breaking"); |
| 70 | +
|
| 71 | + const managedLabels = [ |
| 72 | + "release:feature", |
| 73 | + "release:fix", |
| 74 | + "release:performance", |
| 75 | + "release:docs", |
| 76 | + "release:other", |
| 77 | + "release:internal", |
| 78 | + "release:breaking", |
| 79 | + ]; |
| 80 | +
|
| 81 | + const currentLabels = pr.labels.map((label) => label.name); |
| 82 | + const toRemove = managedLabels.filter( |
| 83 | + (label) => currentLabels.includes(label) && !desired.includes(label), |
| 84 | + ); |
| 85 | +
|
| 86 | + for (const label of toRemove) { |
| 87 | + try { |
| 88 | + await github.rest.issues.removeLabel({ |
| 89 | + owner: context.repo.owner, |
| 90 | + repo: context.repo.repo, |
| 91 | + issue_number: pr.number, |
| 92 | + name: label, |
| 93 | + }); |
| 94 | + } catch (error) { |
| 95 | + if (error.status !== 404) { |
| 96 | + throw error; |
| 97 | + } |
| 98 | + } |
| 99 | + } |
| 100 | +
|
| 101 | + const toAdd = desired.filter((label) => !currentLabels.includes(label)); |
| 102 | + if (toAdd.length > 0) { |
| 103 | + await github.rest.issues.addLabels({ |
| 104 | + owner: context.repo.owner, |
| 105 | + repo: context.repo.repo, |
| 106 | + issue_number: pr.number, |
| 107 | + labels: toAdd, |
| 108 | + }); |
| 109 | + } |
0 commit comments