Skip to content

Commit bdcb178

Browse files
committed
fix(github): the PR sweep reads author replies and pushes itself before a timed close
A late or failed label-clearing job can no longer cost an author their answer: the sweep clears the label instead of closing. Paging through comments and reviews keeps a finished connection's last cursor so it cannot start over.
1 parent 9928055 commit bdcb178

1 file changed

Lines changed: 28 additions & 13 deletions

File tree

‎.github/scripts/pr-limits.cjs‎

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ const MAX_LOOKUPS_PER_RUN = 300;
1212
const MERGEABLE_ATTEMPTS = 3;
1313
const MERGEABLE_RETRY_MS = 30_000;
1414
const MERGEABLE_LOOKUP_RETRY_MS = 5_000;
15-
const RECENT_ACTIVITY_MS = 15 * 60 * 1000;
1615

1716
const REBASE_LABEL = "needs-rebase";
1817
const CLOSING_LABEL = "triage:closing";
@@ -46,13 +45,15 @@ const PR_DETAIL_QUERY = `
4645
pullRequest(number: $number) {
4746
state
4847
mergeable
49-
updatedAt
48+
author { login }
5049
labels(first: 100) { nodes { name } }
5150
timelineItems(itemTypes: [LABELED_EVENT], last: 100) {
5251
nodes { ... on LabeledEvent { createdAt label { name } } }
5352
}
5453
comments(first: 100) { pageInfo { hasNextPage endCursor } nodes { author { login } } }
5554
reviews(first: 100) { pageInfo { hasNextPage endCursor } nodes { author { login } } }
55+
recentComments: comments(last: 30) { nodes { createdAt author { login } } }
56+
commits(last: 1) { nodes { commit { committedDate } } }
5657
}
5758
}
5859
}`;
@@ -124,23 +125,34 @@ function maintainerOn(activity, exemptions) {
124125
// Reads every page, so a long thread can neither hide a maintainer nor pass for one.
125126
// Each further page spends a lookup; with none left the answer is OUT_OF_LOOKUPS, never "nobody".
126127
async function findMaintainer(github, repo, number, detail, exemptions, spend) {
128+
// A finished connection answers its last cursor with an empty page and no cursor; keep the last
129+
// real one, or the next request would start that connection over from its first page.
130+
const cursors = { comments: null, reviews: null };
127131
for (let activity = detail; ;) {
128132
const login = maintainerOn(activity, exemptions);
129133
if (login) return login;
130134
const { comments, reviews } = activity;
131135
if (!comments.pageInfo.hasNextPage && !reviews.pageInfo.hasNextPage) return undefined;
132136
if (!spend()) return OUT_OF_LOOKUPS;
133-
activity = (
134-
await github.graphql(PR_ACTIVITY_QUERY, {
135-
...repo,
136-
number,
137-
comments: comments.pageInfo.endCursor,
138-
reviews: reviews.pageInfo.endCursor,
139-
})
140-
).repository.pullRequest;
137+
cursors.comments = comments.pageInfo.endCursor ?? cursors.comments;
138+
cursors.reviews = reviews.pageInfo.endCursor ?? cursors.reviews;
139+
activity = (await github.graphql(PR_ACTIVITY_QUERY, { ...repo, number, ...cursors })).repository
140+
.pullRequest;
141141
}
142142
}
143143

144+
// What the author-activity job reacts to, read directly so a late or failed job cannot cost the
145+
// author their answer: a push answers both windows, a reply answers a triage notice.
146+
function answeredSince(detail, since, step) {
147+
const pushedAt = Date.parse(detail.commits.nodes[0]?.commit.committedDate ?? "");
148+
if (pushedAt > since) return true;
149+
if (step !== "closing-window") return false;
150+
const author = detail.author?.login;
151+
return detail.recentComments.nodes.some(
152+
(comment) => comment.author?.login === author && Date.parse(comment.createdAt) > since,
153+
);
154+
}
155+
144156
function labeledAt(detail, name) {
145157
const events = detail.timelineItems.nodes.filter((event) => event.label?.name === name);
146158
const last = events.at(-1);
@@ -278,10 +290,13 @@ async function sweep({ github, context, core, dryRun }) {
278290
core.warning(`#${number}: cannot tell when ${labelOf(step)} was applied, leaving it open.`);
279291
continue;
280292
}
293+
if (answeredSince(detail, since, step)) {
294+
plan.push({ number, action: "clear-answered" });
295+
if (!dryRun) await removeLabel(github, context, number, labelOf(step));
296+
continue;
297+
}
281298
if (now - since < windowOf(step)) continue;
282299
if (closes >= MAX_CLOSES_PER_RUN) continue;
283-
// A reply or push clears the label from another job; give that job time before closing.
284-
if (Date.now() - Date.parse(detail.updatedAt) < RECENT_ACTIVITY_MS) continue;
285300
closes += 1;
286301
plan.push({ number, action: step === "closing-window" ? "close-triage" : "close-conflict" });
287302
if (dryRun) continue;
@@ -307,7 +322,7 @@ async function sweep({ github, context, core, dryRun }) {
307322
core.info(
308323
`${dryRun ? "Dry run. " : ""}${candidates.length} candidates: ` +
309324
`${count("warn-conflict")} conflict warnings, ${count("close-conflict")} conflict closes, ` +
310-
`${count("close-triage")} triage closes, ${count("clear-rebase")} labels cleared, ` +
325+
`${count("close-triage")} triage closes, ${count("clear-rebase") + count("clear-answered")} labels cleared, ` +
311326
`${count("leave-open")} left to maintainers.`,
312327
);
313328
for (const entry of plan) {

0 commit comments

Comments
 (0)