-
Notifications
You must be signed in to change notification settings - Fork 719
207 lines (183 loc) · 7.92 KB
/
pr-triage.yml
File metadata and controls
207 lines (183 loc) · 7.92 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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
name: PR Triage
on:
pull_request_target:
types: [opened, synchronize, reopened]
permissions:
contents: read
pull-requests: write
jobs:
label:
name: Label PR
runs-on: ubuntu-latest
timeout-minutes: 5
steps:
- name: Triage PR
uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd # v8.0.0
with:
script: |
const pr = context.payload.pull_request;
const labels = new Set();
// --- Bot detection ---
const botLogins = ['dependabot[bot]', 'renovate[bot]', 'emdashbot[bot]'];
if (botLogins.includes(pr.user.login)) {
labels.add('bot');
}
// --- Size labels ---
const { data: files } = await github.rest.pulls.listFiles({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: pr.number,
per_page: 100,
});
const linesChanged = files.reduce((sum, f) => sum + f.additions + f.deletions, 0);
const sizeLabels = ['size/XS', 'size/S', 'size/M', 'size/L', 'size/XL'];
let sizeLabel;
if (linesChanged < 10) sizeLabel = 'size/XS';
else if (linesChanged < 50) sizeLabel = 'size/S';
else if (linesChanged < 200) sizeLabel = 'size/M';
else if (linesChanged < 500) sizeLabel = 'size/L';
else sizeLabel = 'size/XL';
labels.add(sizeLabel);
// --- Area labels ---
const areaMap = {
'area/core': (f) => f.startsWith('packages/core/'),
'area/admin': (f) => f.startsWith('packages/admin/'),
'area/plugins': (f) => f.startsWith('packages/plugins/'),
'area/docs': (f) => f.startsWith('docs/'),
'area/templates': (f) => f.startsWith('templates/'),
'area/ci': (f) => f.startsWith('.github/'),
'area/auth': (f) => f.startsWith('packages/auth/'),
'area/cloudflare': (f) => f.startsWith('packages/cloudflare/'),
};
for (const file of files) {
for (const [label, matcher] of Object.entries(areaMap)) {
if (matcher(file.filename)) {
labels.add(label);
}
}
}
// --- Merge conflict detection ---
// mergeable is available on the full PR object (may need a separate fetch
// since the webhook payload sometimes has mergeable=null while computing)
try {
const { data: fullPr } = await github.rest.pulls.get({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: pr.number,
});
if (fullPr.mergeable === false) {
labels.add('needs-rebase');
}
} catch {
// Ignore -- mergeable state may not be computed yet
}
// CLA labels are managed by the CLA workflow (cla.yml), not here.
// --- Ensure all labels exist, then apply ---
const labelColors = {
'bot': 'ededed',
'size/XS': '3cbf00',
'size/S': '5dba3f',
'size/M': 'fbca04',
'size/L': 'ee9b00',
'size/XL': 'd93f0b',
'area/core': '0052cc',
'area/admin': '7057ff',
'area/plugins': '008672',
'area/docs': '0075ca',
'area/templates': 'bfdadc',
'area/ci': '000000',
'area/auth': 'd4c5f9',
'area/cloudflare': 'f9a825',
'needs-rebase': 'e11d48',
};
// Get existing labels on the repo
const existingLabels = new Set();
for await (const response of github.paginate.iterator(
github.rest.issues.listLabelsForRepo,
{ owner: context.repo.owner, repo: context.repo.repo, per_page: 100 }
)) {
for (const label of response.data) {
existingLabels.add(label.name);
}
}
// Create any missing labels
for (const label of labels) {
if (!existingLabels.has(label) && labelColors[label]) {
await github.rest.issues.createLabel({
owner: context.repo.owner,
repo: context.repo.repo,
name: label,
color: labelColors[label],
});
}
}
// Get current labels on the PR to remove stale ones
const currentLabels = new Set(pr.labels.map(l => l.name));
// Helper: remove a label, ignoring 404 if it's already gone
async function safeRemoveLabel(name) {
try {
await github.rest.issues.removeLabel({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: pr.number,
name,
});
} catch (e) {
if (e.status !== 404) throw e;
}
}
// Remove stale size labels (only one size label at a time)
for (const sl of sizeLabels) {
if (sl !== sizeLabel && currentLabels.has(sl)) {
await safeRemoveLabel(sl);
}
}
// Remove needs-rebase if PR is now mergeable
if (!labels.has('needs-rebase') && currentLabels.has('needs-rebase')) {
await safeRemoveLabel('needs-rebase');
}
// Add new labels
const toAdd = [...labels].filter(l => !currentLabels.has(l));
if (toAdd.length > 0) {
await github.rest.issues.addLabels({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: pr.number,
labels: toAdd,
});
}
core.info(`Applied labels: ${[...labels].join(', ')}`);
// --- Scope guard: comment on oversized PRs ---
// Only on open (not synchronize) to avoid spamming on every push
if (context.payload.action === 'opened') {
const warnings = [];
if (linesChanged > 500) {
warnings.push(`This PR changes **${linesChanged.toLocaleString()} lines** across **${files.length} files**. Large PRs are harder to review and more likely to be closed without review.`);
} else if (files.length > 20) {
warnings.push(`This PR touches **${files.length} files**. PRs with a broad scope are harder to review. Please confirm the scope hasn't drifted beyond the intended change.`);
}
// Check for cross-area changes (touching multiple packages)
const areas = Object.keys(areaMap).filter(a => labels.has(a));
if (areas.length > 3) {
warnings.push(`This PR spans ${areas.length} different areas (${areas.join(', ')}). Consider breaking it into smaller, focused PRs.`);
}
if (warnings.length > 0) {
const marker = '<!-- scope-guard -->';
const body = [
marker,
'## Scope check',
'',
...warnings,
'',
'If this scope is intentional, no action needed. A maintainer will review it. If not, please consider splitting this into smaller PRs.',
'',
'See [CONTRIBUTING.md](https://github.com/emdash-cms/emdash/blob/main/CONTRIBUTING.md) for contribution guidelines.',
].join('\n');
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: pr.number,
body,
});
}
}