Skip to content

Commit ca2ef60

Browse files
fix(github): Backfill a page again when a webhook was checking one of its PRs
A webhook that holds a pull request's sync claim checks it again without askInLimbo, so the backfill skipped that pull request's limbo ask. The sync now returns the pull requests it could not claim, and the backfill keeps its cursor and runs the page again on the next heartbeat. Co-Authored-By: BYK <[email protected]>
1 parent 43ae985 commit ca2ef60

3 files changed

Lines changed: 79 additions & 9 deletions

File tree

‎packages/junior-github/src/pull-request-action-backfill.ts‎

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111
* person dismissed. A pull request in limbo, which waits on nobody, gets a
1212
* review request for its requester on GitHub, and so a review item.
1313
*
14-
* The heartbeat starts one page at a time until the last page is done. Bump
14+
* The heartbeat starts one page at a time until the last page is done. A
15+
* page with a pull request that a webhook was checking runs again, because
16+
* that webhook does not ask in limbo. Bump
1517
* `VERSION` when a new derivation rule must reach pull requests that went
1618
* quiet before it.
1719
*/
@@ -32,6 +34,8 @@ export const GITHUB_PULL_REQUEST_ACTION_BACKFILL_TASK =
3234
const VERSION = "v1";
3335
const CURSOR_KEY = `pull-request-action-backfill:${VERSION}:cursor`;
3436
const DONE_KEY = `pull-request-action-backfill:${VERSION}:done`;
37+
// How often a page ran again, so its next run has a new task key.
38+
const RERUNS_KEY = `pull-request-action-backfill:${VERSION}:reruns`;
3539

3640
/**
3741
* Pull requests per task run. Most need no GitHub read, because no person
@@ -50,8 +54,9 @@ export async function startGitHubPullRequestActionBackfill(args: {
5054
if (await args.state.get(DONE_KEY)) return;
5155
// One key per page, so a page that is done does not hold back the next.
5256
const after = await args.state.get<number>(CURSOR_KEY);
57+
const reruns = await args.state.get<number>(RERUNS_KEY);
5358
await args.tasks.start({
54-
key: `pull-request-action-backfill:${VERSION}:${after ?? 0}`,
59+
key: `pull-request-action-backfill:${VERSION}:${after ?? 0}:${reruns ?? 0}`,
5560
name: GITHUB_PULL_REQUEST_ACTION_BACKFILL_TASK,
5661
payload: {},
5762
});
@@ -76,7 +81,7 @@ export function githubPullRequestActionBackfillTask(args: {
7681
.filter(({ id }) => id > after)
7782
.sort((a, b) => a.id - b.id)
7883
.slice(0, PAGE_SIZE);
79-
await syncGitHubPullRequestActions({
84+
const busy = await syncGitHubPullRequestActions({
8085
askInLimbo: true,
8186
botEmail: args.botEmail(),
8287
codeChanges: ctx.codeChanges,
@@ -94,7 +99,13 @@ export function githubPullRequestActionBackfillTask(args: {
9499
})),
95100
});
96101
const last = page.at(-1);
97-
if (!last || page.length < PAGE_SIZE) {
102+
if (busy.length > 0) {
103+
// A page that syncs again changes only what is not saved yet.
104+
await ctx.state.set(
105+
RERUNS_KEY,
106+
((await ctx.state.get<number>(RERUNS_KEY)) ?? 0) + 1,
107+
);
108+
} else if (!last || page.length < PAGE_SIZE) {
98109
await ctx.state.set(DONE_KEY, true);
99110
} else {
100111
await ctx.state.set(CURSOR_KEY, last.id);

‎packages/junior-github/src/webhooks/pull-request-action.ts‎

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -592,7 +592,7 @@ async function syncOne(
592592
state: PluginState;
593593
},
594594
target: GitHubPullRequestActionTarget,
595-
): Promise<void> {
595+
): Promise<"busy" | undefined> {
596596
const opener = (
597597
await listGitHubPullRequestOpeners(args.db, {
598598
numbers: [target.number],
@@ -623,7 +623,7 @@ async function syncOne(
623623
now: new Date(),
624624
staleMs: READY_SYNC_STALE_MS,
625625
});
626-
if (!claim) return;
626+
if (!claim) return "busy";
627627
const { id } = claim;
628628
let saved = claim.action;
629629
let requests: number | undefined = claim.requests;
@@ -737,12 +737,15 @@ async function syncOne(
737737
});
738738
}
739739
if (failure !== undefined) throw failure;
740+
return undefined;
740741
}
741742

742743
/**
743744
* Add, change, or close the next-action item of each Junior pull request that
744745
* a webhook delivery can change. The item is optional, so a failure is logged
745-
* and does not fail the delivery.
746+
* and does not fail the delivery. Return the pull requests that another
747+
* webhook was checking. That webhook checks them again, but without
748+
* `askInLimbo`.
746749
*/
747750
export async function syncGitHubPullRequestActions(args: {
748751
/**
@@ -763,12 +766,13 @@ export async function syncGitHubPullRequestActions(args: {
763766
/** Holds the review asks that wait for the required checks. */
764767
state: PluginState;
765768
targets: GitHubPullRequestActionTarget[];
766-
}): Promise<void> {
769+
}): Promise<GitHubPullRequestActionTarget[]> {
770+
const busy: GitHubPullRequestActionTarget[] = [];
767771
await pLimit(PULL_REQUEST_SYNC_CONCURRENCY).map(
768772
args.targets,
769773
async (target) => {
770774
try {
771-
await syncOne(args, target);
775+
if ((await syncOne(args, target)) === "busy") busy.push(target);
772776
} catch (error) {
773777
logFailOpenFailure(
774778
args.log,
@@ -783,4 +787,5 @@ export async function syncGitHubPullRequestActions(args: {
783787
}
784788
},
785789
);
790+
return busy;
786791
}

‎packages/junior-github/tests/pull-request-action.test.ts‎

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import { http, HttpResponse } from "msw";
22
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
33
import { GITHUB_PULL_REQUEST_ACTION_BACKFILL_TASK } from "../src/pull-request-action-backfill";
4+
import {
5+
claimGitHubPullRequestReadySync,
6+
releaseGitHubPullRequestReadySync,
7+
} from "../src/pull-request-outcomes/store";
48
import { createGitHubFixture } from "./github-db";
59
import { mswServer } from "./msw";
610
import { createPluginHarness } from "./plugin-harness";
@@ -632,6 +636,56 @@ describe("next action item", () => {
632636
}
633637
});
634638

639+
it("backfills a page again when a webhook was checking one of its pull requests", async () => {
640+
const fixture = await createGitHubFixture();
641+
const requests: string[] = [];
642+
mockGitHub(
643+
{
644+
draft: false,
645+
mergeableStates: ["blocked"],
646+
merged: false,
647+
reviews: [],
648+
state: "open",
649+
},
650+
requests,
651+
);
652+
try {
653+
await seedJuniorPullRequest(fixture, "slack:C123:1712345.0001");
654+
// A webhook checks the pull request while the backfill runs. It checks
655+
// again for the backfill's request, but does not ask in limbo.
656+
const claim = await claimGitHubPullRequestReadySync(
657+
fixture.db(),
658+
"1001",
659+
{ now: new Date(), staleMs: 60_000 },
660+
);
661+
if (!claim) throw new Error("expected the webhook's claim");
662+
const h = createPluginHarness(fixture, requesterUsers);
663+
const now = Date.parse("2026-07-03T12:00:00.000Z");
664+
await h.heartbeat(now);
665+
await h.runStartedTasks();
666+
expect(requests).toEqual([]);
667+
let held: number | undefined = claim.requests;
668+
while (held !== undefined) {
669+
held = await releaseGitHubPullRequestReadySync(fixture.db(), "1001", {
670+
id: claim.id,
671+
requests: held,
672+
});
673+
}
674+
675+
// The next heartbeat runs the page again, and it asks.
676+
await h.heartbeat(now + 60_000);
677+
await h.runStartedTasks();
678+
expect(
679+
requests.filter((request) => request.startsWith("request")),
680+
).toEqual(["request Requester"]);
681+
expect(h.notices.map(({ notice }) => notice.title)).toEqual([
682+
"Review requested · #946",
683+
]);
684+
} finally {
685+
await fixture.close();
686+
}
687+
});
688+
635689
const pushedAt = "2026-07-02T12:00:00.000Z";
636690
it.each<{ asks: boolean; github: Partial<GitHubState>; name: string }>([
637691
{ asks: true, github: {}, name: "waits on nobody" },

0 commit comments

Comments
 (0)