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