|
| 1 | +name: PR Labels |
| 2 | +# Checks the PR title format and applies the labels that .github/release.yml |
| 3 | +# uses to group release notes. |
| 4 | +# |
| 5 | +# pull_request_target is used so that labels can be applied to PRs from forks. |
| 6 | +# This workflow never checks out or runs code from the PR. |
| 7 | +on: |
| 8 | + pull_request_target: |
| 9 | + types: [opened, edited, reopened, synchronize] |
| 10 | + |
| 11 | +permissions: |
| 12 | + contents: read |
| 13 | + issues: write |
| 14 | + pull-requests: write |
| 15 | + |
| 16 | +concurrency: |
| 17 | + group: pr-labels-${{ github.event.pull_request.number }} |
| 18 | + # Queue rather than cancel, so a run is never stopped part-way through |
| 19 | + # changing labels. |
| 20 | + cancel-in-progress: false |
| 21 | + |
| 22 | +jobs: |
| 23 | + title: |
| 24 | + name: Check title and label by type |
| 25 | + # Runs on pushes too: GitHub keeps only one pending run per concurrency |
| 26 | + # group, so a push can replace a queued run for a title edit. |
| 27 | + runs-on: ubuntu-latest |
| 28 | + steps: |
| 29 | + - uses: actions/github-script@v8 |
| 30 | + with: |
| 31 | + script: | |
| 32 | + const issue_number = context.payload.pull_request.number; |
| 33 | + const { owner, repo } = context.repo; |
| 34 | + // Read the current title. The event payload keeps the title from when the |
| 35 | + // run started, so a re-run of an old run would otherwise apply old labels. |
| 36 | + const { data: pr } = await github.rest.pulls.get({ owner, repo, pull_number: issue_number }); |
| 37 | + const title = pr.title; |
| 38 | +
|
| 39 | + // Creates the label if it doesn't exist. A 422 means another run created |
| 40 | + // it first, which is fine. |
| 41 | + async function ensureLabel(name, color) { |
| 42 | + try { |
| 43 | + await github.rest.issues.getLabel({ owner, repo, name }); |
| 44 | + } catch (e) { |
| 45 | + if (e.status !== 404) throw e; |
| 46 | + try { |
| 47 | + await github.rest.issues.createLabel({ owner, repo, name, color }); |
| 48 | + } catch (e2) { |
| 49 | + if (e2.status !== 422) throw e2; |
| 50 | + } |
| 51 | + } |
| 52 | + } |
| 53 | +
|
| 54 | + const typeLabels = new Map([ |
| 55 | + ['feat', 'Feature'], |
| 56 | + ['fix', 'Bug'], |
| 57 | + ['perf', 'performance'], |
| 58 | + ['docs', 'Documentation'], |
| 59 | + ['refactor', 'maintenance'], |
| 60 | + ['chore', 'maintenance'], |
| 61 | + ['build', 'maintenance'], |
| 62 | + ['revert', null], |
| 63 | + ['ci', 'ci'], |
| 64 | + ['test', 'test'], |
| 65 | + ]); |
| 66 | + // Scopes for internal-only work, which release notes leave out |
| 67 | + // (e.g. "fix(ci): ..." is labelled ci, not bug). |
| 68 | + const scopeLabels = new Map([['ci', 'ci'], ['test', 'test'], ['tests', 'test']]); |
| 69 | + // Labels this job owns. The title is the source of truth for them: |
| 70 | + // any that don't match the current title are removed. Feature, Bug and |
| 71 | + // Documentation are the repo's existing labels. GitHub label names are |
| 72 | + // case-insensitive, so labels are compared in lowercase. |
| 73 | + const colors = new Map([ |
| 74 | + ['breaking-change', 'b60205'], |
| 75 | + ['Feature', '0e8a16'], |
| 76 | + ['Bug', 'd73a4a'], |
| 77 | + ['performance', 'fbca04'], |
| 78 | + ['Documentation', '0075ca'], |
| 79 | + ['maintenance', 'c5def5'], |
| 80 | + ['ci', 'ededed'], |
| 81 | + ['test', 'ededed'], |
| 82 | + ]); |
| 83 | +
|
| 84 | + const owned = new Set([...colors.keys()].map((n) => n.toLowerCase())); |
| 85 | +
|
| 86 | + let wanted; |
| 87 | + if (/^Revert "/.test(title)) { |
| 88 | + // Title generated by GitHub's "Revert" button. |
| 89 | + wanted = new Set(); |
| 90 | + } else { |
| 91 | + // The type is matched in any case, so "Fix: ..." is labelled like "fix: ...". |
| 92 | + const m = /^([A-Za-z]+)(?:\(([^)]+)\))?(!)?: \S/.exec(title); |
| 93 | + if (!m || !typeLabels.has(m[1].toLowerCase())) { |
| 94 | + core.setFailed( |
| 95 | + `PR title "${title}" must look like "type(scope): description" or ` + |
| 96 | + `"type: description", where type is one of: ` + |
| 97 | + `${[...typeLabels.keys()].join(', ')}. Add "!" after the type or scope ` + |
| 98 | + `for a breaking change, e.g. "feat(plan)!: ...". See CONTRIBUTING.md.` |
| 99 | + ); |
| 100 | + return; |
| 101 | + } |
| 102 | + const [, rawType, scope, bang] = m; |
| 103 | + const type = rawType.toLowerCase(); |
| 104 | + wanted = new Set(); |
| 105 | + if (bang) { |
| 106 | + // Breaking changes always appear in the release notes, so they |
| 107 | + // never get the ci/test labels that exclude a PR. |
| 108 | + wanted.add('breaking-change'); |
| 109 | + const label = typeLabels.get(type); |
| 110 | + if (label && label !== 'ci' && label !== 'test') wanted.add(label); |
| 111 | + } else { |
| 112 | + const label = (scope && scopeLabels.get(scope.toLowerCase())) || typeLabels.get(type); |
| 113 | + if (label) wanted.add(label); |
| 114 | + } |
| 115 | + } |
| 116 | +
|
| 117 | + const { data: live } = await github.rest.issues.listLabelsOnIssue({ |
| 118 | + owner, repo, issue_number, per_page: 100, |
| 119 | + }); |
| 120 | + const current = live.map((l) => l.name); |
| 121 | + const currentLower = current.map((n) => n.toLowerCase()); |
| 122 | + const wantedLower = new Set([...wanted].map((n) => n.toLowerCase())); |
| 123 | +
|
| 124 | + for (const name of current) { |
| 125 | + const lower = name.toLowerCase(); |
| 126 | + if (!owned.has(lower) || wantedLower.has(lower)) continue; |
| 127 | + try { |
| 128 | + await github.rest.issues.removeLabel({ owner, repo, issue_number, name }); |
| 129 | + } catch (e) { |
| 130 | + if (e.status !== 404) throw e; |
| 131 | + } |
| 132 | + } |
| 133 | +
|
| 134 | + const toAdd = [...wanted].filter((name) => !currentLower.includes(name.toLowerCase())); |
| 135 | + for (const name of toAdd) { |
| 136 | + await ensureLabel(name, colors.get(name)); |
| 137 | + } |
| 138 | + if (toAdd.length) { |
| 139 | + await github.rest.issues.addLabels({ owner, repo, issue_number, labels: toAdd }); |
| 140 | + } |
| 141 | +
|
| 142 | + migrations: |
| 143 | + name: Label new state migrations |
| 144 | + runs-on: ubuntu-latest |
| 145 | + steps: |
| 146 | + - uses: actions/github-script@v8 |
| 147 | + with: |
| 148 | + script: | |
| 149 | + const issue_number = context.payload.pull_request.number; |
| 150 | + const { owner, repo } = context.repo; |
| 151 | + const files = await github.paginate(github.rest.pulls.listFiles, { |
| 152 | + owner, repo, pull_number: issue_number, per_page: 100, |
| 153 | + }); |
| 154 | + const added = files.some( |
| 155 | + (f) => f.status === 'added' && /^sqlmesh\/migrations\/v\d+_.*\.py$/.test(f.filename) |
| 156 | + ); |
| 157 | + // Read live labels: a queued run's event payload can be out of date. |
| 158 | + const { data: live } = await github.rest.issues.listLabelsOnIssue({ |
| 159 | + owner, repo, issue_number, per_page: 100, |
| 160 | + }); |
| 161 | + const labels = live.map((l) => l.name); |
| 162 | + if (!added) { |
| 163 | + // The PR no longer adds a migration, e.g. the file was removed. |
| 164 | + if (labels.includes('migration')) { |
| 165 | + try { |
| 166 | + await github.rest.issues.removeLabel({ owner, repo, issue_number, name: 'migration' }); |
| 167 | + } catch (e) { |
| 168 | + if (e.status !== 404) throw e; |
| 169 | + } |
| 170 | + } |
| 171 | + return; |
| 172 | + } |
| 173 | + if (labels.includes('migration')) return; |
| 174 | + try { |
| 175 | + await github.rest.issues.getLabel({ owner, repo, name: 'migration' }); |
| 176 | + } catch (e) { |
| 177 | + if (e.status !== 404) throw e; |
| 178 | + try { |
| 179 | + await github.rest.issues.createLabel({ owner, repo, name: 'migration', color: '5319e7' }); |
| 180 | + } catch (e2) { |
| 181 | + // Another run created it first. |
| 182 | + if (e2.status !== 422) throw e2; |
| 183 | + } |
| 184 | + } |
| 185 | + await github.rest.issues.addLabels({ owner, repo, issue_number, labels: ['migration'] }); |
0 commit comments