Skip to content

Commit e4d6ca9

Browse files
fix(github): Give every link to a timed-out pull request its outcome
A page that linked one pull request twice, such as /pull/12 and /pull/12#discussion_r1, gave out_of_time to the last link only. The other link then asked GitHub after the page's budget ran out. Co-Authored-By: BYK <[email protected]>
1 parent 3a12490 commit e4d6ca9

2 files changed

Lines changed: 23 additions & 10 deletions

File tree

‎packages/junior-github/src/issue-links.ts‎

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -231,21 +231,30 @@ async function prefetchLinks(
231231
ctx: ObjectLinkContext,
232232
urls: URL[],
233233
): Promise<Map<string, ObjectLinkPrefetchOutcome>> {
234-
const hrefs = new Map<string, string>();
234+
// A page can link one pull request more than once, such as `/pull/12`
235+
// and `/pull/12#discussion_r1`. Each link gets the outcome.
236+
const hrefs = new Map<string, string[]>();
235237
const refs = urls.flatMap((url) => {
236238
const link = parseLink(url);
237239
if (link?.kind !== "pull_request") return [];
238-
hrefs.set(`${link.owner}/${link.name}#${link.number}`, url.href);
240+
const key = `${link.owner}/${link.name}#${link.number}`;
241+
const known = hrefs.get(key);
242+
if (known) {
243+
known.push(url.href);
244+
return [];
245+
}
246+
hrefs.set(key, [url.href]);
239247
return [
240248
{ name: link.name, number: Number(link.number), owner: link.owner },
241249
];
242250
});
243251
const outOfTime = await prefetchGitHubPullRequests(ctx, refs);
244252
return new Map(
245-
outOfTime.flatMap((ref) => {
246-
const href = hrefs.get(`${ref.owner}/${ref.name}#${ref.number}`);
247-
return href ? [[href, "out_of_time" as const]] : [];
248-
}),
253+
outOfTime.flatMap((ref) =>
254+
(hrefs.get(`${ref.owner}/${ref.name}#${ref.number}`) ?? []).map(
255+
(href) => [href, "out_of_time" as const] as const,
256+
),
257+
),
249258
);
250259
}
251260

‎packages/junior/tests/integration/api/github-page-budget.test.ts‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ describe("GitHub card reads after a page runs out of time", () => {
7070
await closeDb();
7171
});
7272

73-
it("reads GitHub when an uncached pull request is attached right after its page ran out of time", async () => {
73+
it("skips GitHub for every link to a pull request on a page that ran out of time, and reads it on Attach", async () => {
7474
stubGitHubApp();
7575
const reads: string[] = [];
7676
mswServer.use(
@@ -103,6 +103,7 @@ describe("GitHub card reads after a page runs out of time", () => {
103103
// The page's card query answers late, and fails.
104104
mswServer.use(
105105
http.post("https://api.github.com/graphql", async () => {
106+
reads.push("graphql");
106107
reachGitHub();
107108
await answered;
108109
return HttpResponse.json({ message: "Server Error" }, { status: 502 });
@@ -113,13 +114,16 @@ describe("GitHub card reads after a page runs out of time", () => {
113114
await linkGitHubMember();
114115
const read = await viewerApi("[email protected]");
115116
vi.useFakeTimers({ toFake: ["Date"] });
116-
const page = read(PULL_URL);
117+
// The page links the pull request twice.
118+
const page = read(PULL_URL, `${PULL_URL}#discussion_r1`);
117119
await reachedGitHub;
118120
vi.setSystemTime(Date.now() + 21_000);
119121
answerGitHub();
120122
// The page shows no card yet, and makes no read of its own.
121-
expect((await page).objects.has(PULL_URL)).toBe(false);
122-
expect(reads).toEqual([]);
123+
const { objects } = await page;
124+
expect(objects.has(PULL_URL)).toBe(false);
125+
expect(objects.has(`${PULL_URL}#discussion_r1`)).toBe(false);
126+
expect(reads).toEqual(["graphql"]);
123127

124128
// An Attach is not part of the page, so it asks GitHub.
125129
expect(await attachAsAlice(PULL_URL)).toBe(200);

0 commit comments

Comments
 (0)