|
1 | | -name:Lint PR |
| 1 | +name:Pull Request |
2 | 2 |
|
3 | 3 | on: |
4 | 4 | pull_request_target: |
5 | 5 | types: |
| 6 | + -labeled |
| 7 | + -unlabeled |
6 | 8 | -opened |
7 | 9 | -reopened |
8 | 10 | -edited |
9 | | - -synchronize |
| 11 | + |
| 12 | +# Only run one instance per PR to ensure in-order execution. |
| 13 | +concurrency:pr-${{ github.ref }} |
10 | 14 |
|
11 | 15 | jobs: |
12 | | -main: |
13 | | -name:Validate PR title |
| 16 | +lint-title: |
| 17 | +name:Lint title |
14 | 18 | runs-on:ubuntu-latest |
15 | 19 | steps: |
16 | 20 | -uses:amannn/action-semantic-pull-request@v5 |
17 | 21 | env: |
18 | 22 | GITHUB_TOKEN:${{ secrets.GITHUB_TOKEN }} |
19 | 23 | with: |
20 | 24 | requireScope:false |
| 25 | + |
| 26 | +release-labels: |
| 27 | +name:Release labels |
| 28 | +runs-on:ubuntu-latest |
| 29 | +# Depend on lint so that title is Conventional Commits-compatible. |
| 30 | +needs:[lint-title] |
| 31 | +# Skip tagging for draft PRs. |
| 32 | +if:${{ success() && !github.event.pull_request.draft }} |
| 33 | +steps: |
| 34 | + -uses:actions/github-script@v6 |
| 35 | +with: |
| 36 | +# This script ensures PR title and labels are in sync: |
| 37 | +# |
| 38 | +# When release/breaking label is: |
| 39 | +# - Added, rename PR title to include ! (e.g. feat!:) |
| 40 | +# - Removed, rename PR title to strip ! (e.g. feat:) |
| 41 | +# |
| 42 | +# When title is: |
| 43 | +# - Renamed (+!), add the release/breaking label |
| 44 | +# - Renamed (-!), remove the release/breaking label |
| 45 | +script:| |
| 46 | + const releaseLabels = { |
| 47 | + breaking: "release/breaking", |
| 48 | + } |
| 49 | +
|
| 50 | + const { action, changes, label, pull_request } = context.payload |
| 51 | + const { title } = pull_request |
| 52 | + const labels = pull_request.labels.map((label) => label.name) |
| 53 | + const isBreakingTitle = isBreaking(title) |
| 54 | +
|
| 55 | + // Debug information. |
| 56 | + console.log("Action: %s", action) |
| 57 | + console.log("Title: %s", title) |
| 58 | + console.log("Labels: %s", labels.join(", ")) |
| 59 | +
|
| 60 | + const params = { |
| 61 | + issue_number: context.issue.number, |
| 62 | + owner: context.repo.owner, |
| 63 | + repo: context.repo.repo, |
| 64 | + } |
| 65 | +
|
| 66 | + if (action === "opened" || action === "reopened") { |
| 67 | + if (isBreakingTitle && !labels.includes(releaseLabels.breaking)) { |
| 68 | + console.log('Add "%s" label', releaseLabels.breaking) |
| 69 | + await github.rest.issues.addLabels({ |
| 70 | + ...params, |
| 71 | + labels: [releaseLabels.breaking], |
| 72 | + }) |
| 73 | + } |
| 74 | + } |
| 75 | +
|
| 76 | + if (action === "edited" && changes.title) { |
| 77 | + if (isBreakingTitle && !labels.includes(releaseLabels.breaking)) { |
| 78 | + console.log('Add "%s" label', releaseLabels.breaking) |
| 79 | + await github.rest.issues.addLabels({ |
| 80 | + ...params, |
| 81 | + labels: [releaseLabels.breaking], |
| 82 | + }) |
| 83 | + } |
| 84 | +
|
| 85 | + if (!isBreakingTitle && labels.includes(releaseLabels.breaking)) { |
| 86 | + const wasBreakingTitle = isBreaking(changes.title.from) |
| 87 | + if (wasBreakingTitle) { |
| 88 | + console.log('Remove "%s" label', releaseLabels.breaking) |
| 89 | + await github.rest.issues.removeLabel({ |
| 90 | + ...params, |
| 91 | + name: releaseLabels.breaking, |
| 92 | + }) |
| 93 | + } else { |
| 94 | + console.log('Rename title from "%s" to "%s"', title, toBreaking(title)) |
| 95 | + await github.rest.issues.update({ |
| 96 | + ...params, |
| 97 | + title: toBreaking(title), |
| 98 | + }) |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | +
|
| 103 | + if (action === "labeled") { |
| 104 | + if (label.name === releaseLabels.breaking && !isBreakingTitle) { |
| 105 | + console.log('Rename title from "%s" to "%s"', title, toBreaking(title)) |
| 106 | + await github.rest.issues.update({ |
| 107 | + ...params, |
| 108 | + title: toBreaking(title), |
| 109 | + }) |
| 110 | + } |
| 111 | + } |
| 112 | +
|
| 113 | + if (action === "unlabeled") { |
| 114 | + if (label.name === releaseLabels.breaking && isBreakingTitle) { |
| 115 | + console.log('Rename title from "%s" to "%s"', title, fromBreaking(title)) |
| 116 | + await github.rest.issues.update({ |
| 117 | + ...params, |
| 118 | + title: fromBreaking(title), |
| 119 | + }) |
| 120 | + } |
| 121 | + } |
| 122 | +
|
| 123 | + function isBreaking(t) { |
| 124 | + return t.split(" ")[0].endsWith("!:") |
| 125 | + } |
| 126 | +
|
| 127 | + function toBreaking(t) { |
| 128 | + const parts = t.split(" ") |
| 129 | + return [parts[0].replace(/:$/, "!:"), ...parts.slice(1)].join(" ") |
| 130 | + } |
| 131 | +
|
| 132 | + function fromBreaking(t) { |
| 133 | + const parts = t.split(" ") |
| 134 | + return [parts[0].replace(/!:$/, ":"), ...parts.slice(1)].join(" ") |
| 135 | + } |