-
-
Notifications
You must be signed in to change notification settings - Fork 0
285 lines (244 loc) · 10.7 KB
/
pr-labeler.yml
File metadata and controls
285 lines (244 loc) · 10.7 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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
name: PR Auto-Labeler
on:
pull_request:
types: [opened, synchronize, reopened]
permissions:
contents: read
pull-requests: write
issues: write
jobs:
label:
name: Auto-label PR
runs-on: ubuntu-latest
steps:
- name: Harden Runner
uses: step-security/harden-runner@v2
with:
egress-policy: audit
- name: Checkout code
uses: actions/checkout@v5
with:
fetch-depth: 0
ref: ${{ github.event.pull_request.head.sha }}
- name: Analyze commits and add labels
uses: actions/github-script@v7
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const { data: commits } = await github.rest.pulls.listCommits({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: context.issue.number,
});
// Extract commit messages
const messages = commits.map(c => c.commit.message);
// Track which types are present
const types = new Set();
// Analyze commit types
for (const msg of messages) {
const match = msg.match(/^(feat|fix|docs|style|refactor|perf|test|chore|ci|build|revert)(\([a-z0-9_-]+\))?(!)?: /);
if (match) {
types.add(match[1]);
// Check for breaking change
if (match[3] === '!' || msg.includes('BREAKING CHANGE:')) {
types.add('breaking');
}
}
}
// Map commit types to labels
const labelMap = {
'feat': 'feature',
'fix': 'bug',
'docs': 'documentation',
'style': 'style',
'refactor': 'refactor',
'perf': 'performance',
'test': 'testing',
'chore': 'chore',
'ci': 'ci/cd',
'build': 'build',
'revert': 'revert',
'breaking': 'breaking change'
};
// Collect labels to add
const labelsToAdd = [];
for (const type of types) {
if (labelMap[type]) {
labelsToAdd.push(labelMap[type]);
}
}
// Remove duplicates
const uniqueLabels = [...new Set(labelsToAdd)];
if (uniqueLabels.length > 0) {
// Get current labels to avoid re-adding
const { data: currentLabels } = await github.rest.issues.listLabelsOnIssue({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
});
const currentLabelNames = new Set(currentLabels.map(l => l.name));
const labelsToActuallyAdd = uniqueLabels.filter(label => !currentLabelNames.has(label));
if (labelsToActuallyAdd.length > 0) {
console.log(`Adding new labels: ${labelsToActuallyAdd.join(', ')}`);
// Add labels to PR
await github.rest.issues.addLabels({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
labels: labelsToActuallyAdd
});
} else {
console.log('All labels already present, no changes needed');
}
// Only comment when PR is first opened (not on every update)
if (context.payload.action === 'opened') {
// Check if we've already commented
const { data: comments } = await github.rest.issues.listComments({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
});
const botCommentExists = comments.some(comment =>
comment.user.type === 'Bot' &&
comment.body.includes('🏷️ Auto-labeled based on commits:')
);
if (!botCommentExists) {
const typesList = Array.from(types).filter(t => t !== 'breaking').join(', ');
const breakingNote = types.has('breaking') ? '\n\n⚠️ **This PR contains breaking changes!**' : '';
console.log('Posting auto-label summary comment');
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
body: `🏷️ Auto-labeled based on commits: \`${typesList}\`${breakingNote}`
});
} else {
console.log('Auto-label comment already exists, skipping');
}
} else {
console.log('Labels updated (no comment on synchronize/reopened to reduce noise)');
}
} else {
console.log('No conventional commit types found, skipping labeling');
}
- name: Add size label
uses: actions/github-script@v7
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const { data: pr } = await github.rest.pulls.get({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: context.issue.number,
});
const additions = pr.additions;
const deletions = pr.deletions;
const total = additions + deletions;
let sizeLabel = '';
if (total < 10) {
sizeLabel = 'size/XS';
} else if (total < 50) {
sizeLabel = 'size/S';
} else if (total < 200) {
sizeLabel = 'size/M';
} else if (total < 500) {
sizeLabel = 'size/L';
} else {
sizeLabel = 'size/XL';
}
console.log(`PR size: ${total} lines (${additions} additions, ${deletions} deletions) → ${sizeLabel}`);
// Get current labels
const { data: currentLabels } = await github.rest.issues.listLabelsOnIssue({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
});
const currentSizeLabel = currentLabels.find(l => l.name.startsWith('size/'))?.name;
// Only update if size label changed
if (currentSizeLabel === sizeLabel) {
console.log(`Size label ${sizeLabel} already correct, no change needed`);
} else {
// Remove old size labels
for (const label of currentLabels) {
if (label.name.startsWith('size/')) {
await github.rest.issues.removeLabel({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
name: label.name,
}).catch(() => {});
}
}
// Add new size label
console.log(`Updating size label: ${currentSizeLabel || 'none'} → ${sizeLabel}`);
await github.rest.issues.addLabels({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
labels: [sizeLabel]
});
}
- name: Hacktoberfest auto-accept
uses: actions/github-script@v7
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
// Only run during October
const now = new Date();
const month = now.getMonth(); // 0 = January, 9 = October
if (month !== 9) {
console.log('Not October - skipping Hacktoberfest labeling');
return;
}
// Check if PR author is a previous contributor
const author = context.payload.pull_request.user.login;
// Get all merged PRs from this author
const { data: searchResults } = await github.rest.search.issuesAndPullRequests({
q: `repo:${context.repo.owner}/${context.repo.repo} author:${author} type:pr is:merged`,
per_page: 1
});
const hasPreviousContributions = searchResults.total_count > 0;
if (hasPreviousContributions) {
console.log(`${author} is a previous contributor`);
// Check if label already exists
const { data: currentLabels } = await github.rest.issues.listLabelsOnIssue({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
});
const hasHacktoberfestLabel = currentLabels.some(l => l.name === 'hacktoberfest-accepted');
if (!hasHacktoberfestLabel) {
console.log('Adding hacktoberfest-accepted label');
await github.rest.issues.addLabels({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
labels: ['hacktoberfest-accepted']
});
// Check if we've already commented about Hacktoberfest
const { data: comments } = await github.rest.issues.listComments({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
});
const hacktoberfestCommentExists = comments.some(comment =>
comment.user.type === 'Bot' &&
comment.body.includes('Happy Hacktoberfest!')
);
if (!hacktoberfestCommentExists && context.payload.action === 'opened') {
console.log('Posting Hacktoberfest welcome comment');
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
body: '🎃 **Happy Hacktoberfest!** Thank you for being a returning contributor. Your PR has been automatically accepted for Hacktoberfest.'
});
} else {
console.log('Hacktoberfest comment already exists or not first opened, skipping');
}
} else {
console.log('Hacktoberfest label already present');
}
} else {
console.log(`${author} is a new contributor - manual review required for Hacktoberfest`);
}