-
Notifications
You must be signed in to change notification settings - Fork 165
Expand file tree
/
Copy pathmaintainer-approval.js
More file actions
597 lines (532 loc) · 18.3 KB
/
maintainer-approval.js
File metadata and controls
597 lines (532 loc) · 18.3 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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
const path = require("path");
const { execSync } = require("child_process");
const {
parseOwnersFile,
findOwners,
getMaintainers,
getOwnershipGroups,
} = require("../scripts/owners");
// --- Approval helpers ---
/**
* Check if an approver is a member of a GitHub team.
* Requires org read access on the token; falls back to false if unavailable.
*/
async function isTeamMember(github, org, teamSlug, login, core) {
try {
const { data } = await github.rest.teams.getMembershipForUserInOrg({
org,
team_slug: teamSlug,
username: login,
});
return data.state === "active";
} catch (err) {
if (err.status === 404) {
return false;
}
if (core) {
core.warning(
`Could not verify team membership for ${login} in ${org}/${teamSlug} ` +
`(HTTP ${err.status || "unknown"}). Team-based approval may not work ` +
`without a token with org:read scope.`
);
}
return false;
}
}
/**
* Find which approver (if any) satisfies a group's ownership requirement.
* Returns the login of the first matching approver, or null.
*/
async function findGroupApprover(owners, approverLogins, github, org, core) {
const approverSet = new Set(approverLogins.map(l => l.toLowerCase()));
for (const owner of owners) {
if (owner.includes("/")) {
const teamSlug = owner.split("/")[1];
for (const approver of approverLogins) {
if (await isTeamMember(github, org, teamSlug, approver, core)) {
return approver;
}
}
} else if (approverSet.has(owner.toLowerCase())) {
return owner;
}
}
return null;
}
/**
* Per-path approval check. Each ownership group needs at least one
* approval from its owners. Files matching only "*" require a maintainer.
* Returns groups, approvedBy map, and coverage info.
*/
async function checkPerPathApproval(files, rulesWithTeams, approverLogins, github, org, core) {
const groups = getOwnershipGroups(files.map(f => f.filename), rulesWithTeams);
const approvedBy = new Map();
if (groups.has("*")) {
// Still check non-wildcard groups for comment building
for (const [pattern, { owners }] of groups) {
if (pattern === "*") continue;
const approver = await findGroupApprover(owners, approverLogins, github, org, core);
if (approver) approvedBy.set(pattern, approver);
}
return {
allCovered: false,
hasWildcardFiles: true,
wildcardFiles: groups.get("*").files,
groups,
approvedBy,
};
}
const uncovered = [];
for (const [pattern, { owners }] of groups) {
const approver = await findGroupApprover(owners, approverLogins, github, org, core);
if (approver) {
approvedBy.set(pattern, approver);
} else {
uncovered.push({ pattern, owners });
}
}
return { allCovered: uncovered.length === 0, uncovered, groups, approvedBy };
}
// --- Git history & scoring helpers ---
const MENTION_REVIEWERS = true;
const OWNERS_LINK = "[OWNERS](.github/OWNERS)";
const MARKER = "<!-- MAINTAINER_APPROVAL -->";
const STATUS_CONTEXT = "maintainer-approval";
const loginCache = {};
function classifyFile(filepath, totalFiles) {
const base = path.basename(filepath);
if (base.startsWith("out.") || base === "output.txt") {
return 0.01 / Math.max(totalFiles, 1);
}
if (filepath.startsWith("acceptance/") || filepath.startsWith("integration/")) {
return 0.2;
}
if (filepath.endsWith("_test.go")) return 0.3;
return filepath.endsWith(".go") ? 1.0 : 0.5;
}
function gitLog(filepath) {
try {
const out = execSync(
`git log -50 --no-merges --since="12 months ago" --format="%H|%an|%aI" -- "${filepath}"`,
{ encoding: "utf-8" }
);
const entries = [];
for (const line of out.split("\n")) {
const trimmed = line.trim();
if (!trimmed) continue;
const parts = trimmed.split("|", 3);
if (parts.length !== 3) continue;
const date = new Date(parts[2]);
if (isNaN(date.getTime())) continue;
entries.push({ sha: parts[0], name: parts[1], date });
}
return entries;
} catch {
return [];
}
}
async function resolveLogin(github, owner, repo, sha, authorName) {
if (authorName in loginCache) return loginCache[authorName];
try {
const { data } = await github.rest.repos.getCommit({ owner, repo, ref: sha });
const login = data.author?.login || null;
loginCache[authorName] = login;
return login;
} catch {
loginCache[authorName] = null;
return null;
}
}
function parseOwnersForFiles(changedFiles, ownersPath) {
const rules = parseOwnersFile(ownersPath, { includeTeams: true });
const allOwners = new Set();
for (const filepath of changedFiles) {
for (const o of findOwners(filepath, rules)) allOwners.add(o);
}
return Array.from(allOwners).sort();
}
async function scoreContributors(files, prAuthor, now, github, owner, repo) {
const scores = {};
const dirScores = {};
let scoredCount = 0;
const authorLogin = (prAuthor || "").toLowerCase();
const totalFiles = files.length;
for (const filepath of files) {
const weight = classifyFile(filepath, totalFiles);
let history = gitLog(filepath);
if (history.length === 0) {
const parent = path.dirname(filepath);
if (parent && parent !== ".") {
history = gitLog(parent);
}
}
if (history.length === 0) continue;
const topDir = path.dirname(filepath) || ".";
let fileContributed = false;
for (const { sha, name, date } of history) {
if (name.endsWith("[bot]")) continue;
const login = await resolveLogin(github, owner, repo, sha, name);
if (!login || login.toLowerCase() === authorLogin) continue;
const daysAgo = Math.max(0, (now - date) / 86400000);
const s = weight * Math.pow(0.5, daysAgo / 150);
scores[login] = (scores[login] || 0) + s;
if (!dirScores[login]) dirScores[login] = {};
dirScores[login][topDir] = (dirScores[login][topDir] || 0) + s;
fileContributed = true;
}
if (fileContributed) scoredCount++;
}
return { scores, dirScores, scoredCount };
}
function topDirs(ds, n = 3) {
return Object.entries(ds || {})
.sort((a, b) => b[1] - a[1])
.slice(0, n)
.map(([d]) => d);
}
function fmtReviewer(login, dirs) {
const mention = MENTION_REVIEWERS ? `@${login}` : login;
const dirList = dirs.map((d) => `\`${d}/\``).join(", ");
return `- ${mention} -- recent work in ${dirList}`;
}
function selectReviewers(ss) {
if (ss.length === 0) return [];
const out = [ss[0]];
if (ss.length >= 2 && ss[0][1] < 1.5 * ss[1][1]) {
out.push(ss[1]);
if (ss.length >= 3 && ss[1][1] < 1.5 * ss[2][1]) {
out.push(ss[2]);
}
}
return out;
}
function fmtEligible(owners) {
if (MENTION_REVIEWERS) return owners.map((o) => `@${o}`).join(", ");
return owners.join(", ");
}
async function countRecentReviews(github, owner, repo, logins, days = 30) {
const since = new Date(Date.now() - days * 86400000)
.toISOString()
.slice(0, 10);
const counts = {};
for (const login of logins) {
try {
const { data } = await github.rest.search.issuesAndPullRequests({
q: `repo:${owner}/${repo} reviewed-by:${login} is:pr created:>${since}`,
});
counts[login] = data.total_count;
} catch {
// skip on error
}
}
return counts;
}
async function selectRoundRobin(github, owner, repo, eligibleOwners, prAuthor) {
const candidates = eligibleOwners
.filter((o) => !o.includes("/") && o.toLowerCase() !== (prAuthor || "").toLowerCase());
if (candidates.length === 0) return null;
const counts = await countRecentReviews(github, owner, repo, candidates);
if (Object.keys(counts).length === 0) {
return candidates[Math.floor(Math.random() * candidates.length)];
}
return candidates.reduce((best, c) =>
(counts[c] || 0) < (counts[best] || 0) ? c : best
);
}
// --- Comment builders ---
function buildPendingPerGroupComment(groups, scores, dirScores, approvedBy, maintainers, prAuthor) {
const authorLower = (prAuthor || "").toLowerCase();
const lines = [MARKER, "## Approval status: pending", ""];
for (const [pattern, { owners, files }] of groups) {
if (pattern === "*") continue;
const approver = approvedBy.get(pattern);
if (approver) {
lines.push(`### \`${pattern}\` - approved by @${approver}`);
} else {
lines.push(`### \`${pattern}\` - needs approval`);
}
lines.push(`Files: ${files.map(f => `\`${f}\``).join(", ")}`);
const teams = owners.filter(o => o.includes("/"));
const individuals = owners.filter(o => !o.includes("/") && o.toLowerCase() !== authorLower);
if (teams.length > 0) {
lines.push(`Teams: ${teams.map(t => `@${t}`).join(", ")}`);
}
if (!approver && individuals.length > 0) {
const scored = individuals.map(o => [o, scores[o] || 0]).sort((a, b) => b[1] - a[1]);
if (scored[0][1] > 0) {
lines.push(`Suggested: @${scored[0][0]}`);
const rest = scored.slice(1).map(([o]) => o);
if (rest.length > 0) {
lines.push(`Also eligible: ${fmtEligible(rest)}`);
}
} else {
lines.push(`Eligible: ${fmtEligible(individuals)}`);
}
}
lines.push("");
}
const starGroup = groups.get("*");
if (starGroup) {
lines.push("### General files (require maintainer)");
lines.push(`Files: ${starGroup.files.map(f => `\`${f}\``).join(", ")}`);
const maintainerSet = new Set(maintainers.map(m => m.toLowerCase()));
const maintainerScores = Object.entries(scores)
.filter(([login]) =>
login.toLowerCase() !== authorLower && maintainerSet.has(login.toLowerCase())
)
.sort((a, b) => b[1] - a[1]);
if (maintainerScores.length > 0 && maintainerScores[0][1] > 0) {
const [login] = maintainerScores[0];
const dirs = topDirs(dirScores[login]);
lines.push("Based on git history:");
lines.push(fmtReviewer(login, dirs));
} else {
lines.push(`Pick a maintainer from ${OWNERS_LINK}.`);
}
lines.push("");
}
const maintainerList = maintainers
.filter(m => m.toLowerCase() !== authorLower)
.map(m => `@${m}`)
.join(", ");
lines.push(
`<sub>Any maintainer (${maintainerList}) can approve all areas.`,
`See ${OWNERS_LINK} for ownership rules.</sub>`
);
return lines.join("\n") + "\n";
}
function buildSingleDomainPendingComment(sortedScores, dirScores, scoredCount, eligibleOwners, prAuthor, roundRobinReviewer) {
const reviewers = selectReviewers(sortedScores);
const suggestedLogins = new Set(reviewers.map(([login]) => login.toLowerCase()));
const eligible = eligibleOwners.filter(
o => o.toLowerCase() !== (prAuthor || "").toLowerCase() && !suggestedLogins.has(o.toLowerCase())
);
const lines = [MARKER, "## Waiting for approval", ""];
if (reviewers.length > 0) {
lines.push("Based on git history, these people are best suited to review:", "");
for (const [login] of reviewers) {
lines.push(fmtReviewer(login, topDirs(dirScores[login])));
}
lines.push("");
} else if (roundRobinReviewer) {
lines.push(
"Could not determine reviewers from git history.",
`Round-robin suggestion: @${roundRobinReviewer}`,
""
);
}
if (eligible.length > 0) {
lines.push(`Eligible reviewers: ${fmtEligible(eligible)}`, "");
}
lines.push(`<sub>Suggestions based on git history. See ${OWNERS_LINK} for ownership rules.</sub>`);
return lines.join("\n") + "\n";
}
// --- Comment management ---
const LEGACY_MARKER = "<!-- REVIEWER_SUGGESTION -->";
/**
* Delete all marker and legacy marker comments from the PR.
* Used on success paths to clean up stale pending comments.
*/
async function deleteMarkerComments(github, owner, repo, prNumber) {
const comments = await github.paginate(github.rest.issues.listComments, {
owner, repo, issue_number: prNumber,
});
for (const c of comments) {
if (c.body && (c.body.includes(MARKER) || c.body.includes(LEGACY_MARKER))) {
await github.rest.issues.deleteComment({
owner, repo, comment_id: c.id,
});
}
}
}
/**
* Create or edit the marker comment. Skips the edit if the body is unchanged.
* Cleans up duplicate or legacy marker comments, keeping only the first one.
*/
async function upsertComment(github, owner, repo, prNumber, newBody) {
const comments = await github.paginate(github.rest.issues.listComments, {
owner, repo, issue_number: prNumber,
});
const markerComments = comments.filter(c =>
c.body && (c.body.includes(MARKER) || c.body.includes(LEGACY_MARKER))
);
if (markerComments.length > 0) {
const existing = markerComments[0];
// Clean up duplicates (legacy or accidental), keep the first.
for (const c of markerComments.slice(1)) {
await github.rest.issues.deleteComment({
owner, repo, comment_id: c.id,
});
}
// Skip if body is unchanged.
if (existing.body === newBody) return;
await github.rest.issues.updateComment({
owner, repo, comment_id: existing.id, body: newBody,
});
return;
}
await github.rest.issues.createComment({
owner, repo, issue_number: prNumber, body: newBody,
});
}
// --- Main ---
module.exports = async ({ github, context, core }) => {
const ownersPath = path.join(
process.env.GITHUB_WORKSPACE,
".github",
"OWNERS"
);
const rulesWithTeams = parseOwnersFile(ownersPath, { includeTeams: true });
const maintainers = getMaintainers(rulesWithTeams);
if (maintainers.length === 0) {
core.setFailed(
"Could not determine maintainers from .github/OWNERS (no * rule found)."
);
return;
}
const { pull_request: pr } = context.payload;
const owner = context.repo.owner;
const repo = context.repo.repo;
const prNumber = context.issue.number;
const authorLogin = pr?.user?.login;
const sha = pr.head.sha;
const statusParams = {
owner: context.repo.owner,
repo: context.repo.repo,
sha,
context: STATUS_CONTEXT,
};
const reviews = await github.paginate(github.rest.pulls.listReviews, {
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: context.issue.number,
});
// Maintainer approval -> success with simple comment
const maintainerApproval = reviews.find(
({ state, user }) =>
state === "APPROVED" && user && maintainers.includes(user.login)
);
if (maintainerApproval) {
const approver = maintainerApproval.user.login;
core.info(`Maintainer approval from @${approver}`);
await github.rest.repos.createCommitStatus({
...statusParams,
state: "success",
description: `Approved by @${approver}`,
});
await deleteMarkerComments(github, owner, repo, prNumber);
return;
}
// Maintainer-authored PR with any approval -> success
if (authorLogin && maintainers.includes(authorLogin)) {
const hasAnyApproval = reviews.some(
({ state, user }) =>
state === "APPROVED" && user && user.login !== authorLogin
);
if (hasAnyApproval) {
core.info(`Maintainer-authored PR approved by a reviewer.`);
await github.rest.repos.createCommitStatus({
...statusParams,
state: "success",
description: "Approved (maintainer-authored PR)",
});
await deleteMarkerComments(github, owner, repo, prNumber);
return;
}
}
// Gather approved logins (excluding the PR author).
const approverLogins = reviews
.filter(
({ state, user }) =>
state === "APPROVED" && user && user.login !== authorLogin
)
.map(({ user }) => user.login);
const files = await github.paginate(github.rest.pulls.listFiles, {
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: context.issue.number,
});
const result = await checkPerPathApproval(
files,
rulesWithTeams,
approverLogins,
github,
context.repo.owner,
core
);
// Set commit status. Approved PRs return early (commit status is sufficient).
if (result.allCovered && approverLogins.length > 0) {
core.info("All ownership groups have per-path approval.");
await github.rest.repos.createCommitStatus({
...statusParams,
state: "success",
description: "All ownership groups approved",
});
await deleteMarkerComments(github, owner, repo, prNumber);
return;
}
if (result.hasWildcardFiles) {
const fileList = result.wildcardFiles.join(", ");
const msg =
`Files need maintainer review: ${fileList}. ` +
`Maintainers: ${maintainers.join(", ")}`;
core.info(msg);
await github.rest.repos.createCommitStatus({
...statusParams,
state: "pending",
description: msg.length > 140 ? msg.slice(0, 137) + "..." : msg,
});
} else if (result.uncovered && result.uncovered.length > 0) {
const groupList = result.uncovered
.map(({ pattern, owners }) => `${pattern} (needs: ${owners.join(", ")})`)
.join("; ");
const msg = `Needs approval: ${groupList}`;
core.info(
`${msg}. Alternatively, any maintainer can approve: ${maintainers.join(", ")}.`
);
await github.rest.repos.createCommitStatus({
...statusParams,
state: "pending",
description: msg.length > 140 ? msg.slice(0, 137) + "..." : msg,
});
} else {
const msg = `Waiting for maintainer approval: ${maintainers.join(", ")}`;
core.info(msg);
await github.rest.repos.createCommitStatus({
...statusParams,
state: "pending",
description: msg.length > 140 ? msg.slice(0, 137) + "..." : msg,
});
}
// Score contributors via git history
const fileNames = files.map(f => f.filename);
const now = new Date();
const { scores, dirScores, scoredCount } = await scoreContributors(
fileNames,
authorLogin,
now,
github,
owner,
repo
);
const sortedScores = Object.entries(scores).sort((a, b) => b[1] - a[1]);
// Build pending comment with reviewer suggestions.
let comment;
const groups = result.groups;
if (groups.size >= 2) {
comment = buildPendingPerGroupComment(
groups, scores, dirScores, result.approvedBy, maintainers, authorLogin
);
} else {
const eligible = parseOwnersForFiles(fileNames, ownersPath);
let roundRobin = null;
if (selectReviewers(sortedScores).length === 0 && eligible.length > 0) {
roundRobin = await selectRoundRobin(github, owner, repo, eligible, authorLogin);
}
comment = buildSingleDomainPendingComment(
sortedScores, dirScores, scoredCount, eligible, authorLogin, roundRobin
);
}
core.info(comment);
await upsertComment(github, owner, repo, prNumber, comment);
};