Skip to content

Commit 52f4ec6

Browse files
perf(gitcafe): PR sync makes 2x fewer GitCafe requests for unchanged PRs (pingdotgg#27100)
Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent 2f4da5d commit 52f4ec6

2 files changed

Lines changed: 97 additions & 35 deletions

File tree

‎packages/source-control-gitcafe/src/server/GitCafePullRequestProvider.test.ts‎

Lines changed: 48 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1226,43 +1226,70 @@ describe("GitCafePullRequestProvider", () => {
12261226

12271227
it.effect("recounts summary lines only when the compared commits move", () => {
12281228
const landedBase = "fedcba9876543210fedcba9876543210fedcba98";
1229+
const mainTip = "0123012301230123012301230123012301230123";
12291230
let comparisonBaseOid = baseOid;
1231+
let tips = { targetBranch: "main", observedBaseOid: baseOid };
12301232
let pullReads = 0;
1233+
let listingCut = true;
1234+
let hunksDown = false;
1235+
const hunks = (request: SentRequest) => ({
1236+
version: 4,
1237+
headOid,
1238+
comparisonBaseOid,
1239+
items: (request.body as { paths: ReadonlyArray<string> }).paths.map((path) => ({
1240+
path,
1241+
additions: comparisonBaseOid === baseOid ? 3 : 1,
1242+
deletions: 1,
1243+
})),
1244+
});
12311245
const server = fakeGitCafe({
12321246
// A comment moves the version without moving the compared commits.
1233-
"GET /repos/owner/repo/pulls/7": () => ({ ...pull, version: 4 + pullReads++ }),
1234-
"GET /repos/owner/repo/pulls/7/changes": () => ({
1235-
version: 4,
1236-
headOid,
1237-
comparisonBaseOid,
1238-
items: [{ path: "a.ts" }, { path: "b.ts" }],
1239-
next: null,
1240-
}),
1241-
"POST /repos/owner/repo/pulls/7/diff-files": (request: SentRequest) => ({
1247+
"GET /repos/owner/repo/pulls/7": () => ({ ...pull, ...tips, version: 4 + pullReads++ }),
1248+
"GET /repos/owner/repo/pulls/7/changes": (request: SentRequest) => ({
12421249
version: 4,
12431250
headOid,
12441251
comparisonBaseOid,
1245-
items: (request.body as { paths: ReadonlyArray<string> }).paths.map((path) => ({
1246-
path,
1247-
additions: comparisonBaseOid === baseOid ? 3 : 1,
1248-
deletions: 1,
1249-
})),
1252+
...(!listingCut
1253+
? { items: [{ path: "a.ts" }, { path: "b.ts" }], next: null }
1254+
: request.query.has("after")
1255+
? { items: [], next: "more" }
1256+
: { items: [{ path: "a.ts" }], next: "more" }),
12501257
}),
1258+
get "POST /repos/owner/repo/pulls/7/diff-files"() {
1259+
return hunksDown
1260+
? new Response(encodeJson({ type: "https://cafe.sh/errors/conflict" }), { status: 409 })
1261+
: hunks;
1262+
},
12511263
});
1252-
const hunkReads = () =>
1253-
server.sent.filter((request) => request.path.endsWith("/diff-files")).length;
1264+
const reads = (suffix: string) =>
1265+
server.sent.filter((request) => request.path.endsWith(suffix)).length;
12541266
return Effect.gen(function* () {
12551267
const provider = yield* GitCafePullRequestProvider.make;
1268+
// A listing that stops before its last page holds nothing for its commits.
1269+
yield* provider.getChangeRequestSummary!(target);
1270+
listingCut = false;
1271+
hunksDown = true;
1272+
// Unread hunks leave the lines unknown, and are read again next time.
1273+
const unread = yield* provider.getChangeRequestSummary!(target);
1274+
assert.strictEqual(unread.additions, undefined);
1275+
hunksDown = false;
12561276
const first = yield* provider.getChangeRequestSummary!(target);
12571277
const second = yield* provider.getChangeRequestSummary!(target);
12581278
assert.deepInclude(first, { additions: 6, deletions: 2, changedFiles: 2 });
12591279
assert.deepInclude(second, { additions: 6, deletions: 2, changedFiles: 2 });
1260-
assert.strictEqual(hunkReads(), 1);
1261-
// A landed parent layer moves the comparison base under the same head.
1262-
comparisonBaseOid = landedBase;
1280+
// Neither the head nor the target tip moved, so the file list is not read again.
1281+
assert.deepStrictEqual([reads("/changes"), reads("/diff-files")], [4, 3]);
1282+
// The target branch advancing keeps the merge base, so only the file list is read.
1283+
tips = { ...tips, observedBaseOid: mainTip };
12631284
const third = yield* provider.getChangeRequestSummary!(target);
1264-
assert.deepInclude(third, { additions: 2, deletions: 2, changedFiles: 2 });
1265-
assert.strictEqual(hunkReads(), 2);
1285+
assert.deepInclude(third, { additions: 6, deletions: 2, changedFiles: 2 });
1286+
assert.deepStrictEqual([reads("/changes"), reads("/diff-files")], [5, 3]);
1287+
// A landed parent layer retargets the pull and moves the comparison base under the same head.
1288+
comparisonBaseOid = landedBase;
1289+
tips = { targetBranch: "trunk", observedBaseOid: mainTip };
1290+
const fourth = yield* provider.getChangeRequestSummary!(target);
1291+
assert.deepInclude(fourth, { additions: 2, deletions: 2, changedFiles: 2 });
1292+
assert.deepStrictEqual([reads("/changes"), reads("/diff-files")], [6, 4]);
12661293
}).pipe(Effect.provide(server.layer));
12671294
});
12681295

‎packages/source-control-gitcafe/src/server/GitCafePullRequestProvider.ts‎

Lines changed: 49 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -636,11 +636,19 @@ export const make = Effect.gen(function* () {
636636
// so hunk totals are held per compared pair of commits. The file list that names the pair is
637637
// cheap; a retarget or a landed parent layer moves the base without moving the head.
638638
const lineTotals = new Map<string, { readonly additions: number; readonly deletions: number }>();
639+
// The compared pair follows from the head and the target tip the pull reports, so while neither
640+
// moves the stats are reused without listing the files again.
641+
const statsByTips = new Map<
642+
string,
643+
{ readonly changedFiles: number; readonly additions?: number; readonly deletions?: number }
644+
>();
645+
const remember = <K, V>(cache: Map<K, V>, key: K, value: V) => {
646+
if (cache.size >= LINE_STATS_CACHE_SIZE) cache.delete(cache.keys().next().value!);
647+
cache.set(key, value);
648+
};
639649

640-
const readLineStats = Effect.fn("GitCafePullRequestProvider.readLineStats")(function* (
641-
input: PullRef,
642-
version: number,
643-
) {
650+
// readLineStats owns the span.
651+
const countLines = Effect.fnUntraced(function* (input: PullRef, version: number) {
644652
const path = yield* pullPath("getChangeRequest", input);
645653
const files: Array<Json.GitCafeDiffFile> = [];
646654
let revision: { version: number; headOid: string; baseOid: string } | undefined;
@@ -667,13 +675,23 @@ export const make = Effect.gen(function* () {
667675
after = batch.next;
668676
if (after === null || after === previous || batch.items.length === 0) break;
669677
}
670-
if (revision === undefined) return { changedFiles: 0, additions: 0, deletions: 0 };
678+
if (revision === undefined)
679+
return {
680+
headOid: null,
681+
complete: true,
682+
stats: { changedFiles: 0, additions: 0, deletions: 0 },
683+
};
671684
const key = `${input.host} ${input.repository.toLowerCase()} ${input.number} ${revision.baseOid} ${revision.headOid}`;
672685
const held = lineTotals.get(key);
673-
if (held !== undefined) return { changedFiles: files.length, ...held };
686+
const { headOid } = revision;
687+
// A listing that stopped before its last page counts only part of the diff, so nothing is held.
688+
const listed = after === null;
689+
if (held !== undefined)
690+
return { headOid, complete: listed, stats: { changedFiles: files.length, ...held } };
691+
const tooMany = files.length > LINE_STATS_MAX_FILES;
674692
const details = files.some((file) => file.additions !== undefined)
675693
? Option.some(files)
676-
: files.length > LINE_STATS_MAX_FILES
694+
: tooMany
677695
? Option.none()
678696
: yield* Effect.forEach(
679697
chunk(detailablePaths(files), DIFF_FILES_BATCH),
@@ -684,16 +702,33 @@ export const make = Effect.gen(function* () {
684702
Effect.option,
685703
);
686704
// Unread hunks leave the lines unknown rather than zero; the file count still stands.
687-
if (Option.isNone(details)) return { changedFiles: files.length };
705+
if (Option.isNone(details))
706+
return { headOid, complete: listed && tooMany, stats: { changedFiles: files.length } };
688707
const counted = details.value;
689708
const totals = {
690709
additions: counted.reduce((total, file) => total + (file.additions ?? 0), 0),
691710
deletions: counted.reduce((total, file) => total + (file.deletions ?? 0), 0),
692711
};
693-
if (lineTotals.size >= LINE_STATS_CACHE_SIZE)
694-
lineTotals.delete(lineTotals.keys().next().value!);
695-
lineTotals.set(key, totals);
696-
return { changedFiles: files.length, ...totals };
712+
if (listed) remember(lineTotals, key, totals);
713+
return { headOid, complete: listed, stats: { changedFiles: files.length, ...totals } };
714+
});
715+
716+
const readLineStats = Effect.fn("GitCafePullRequestProvider.readLineStats")(function* (
717+
input: PullRef,
718+
pull: Json.GitCafePullDetail,
719+
) {
720+
const tipsKey =
721+
pull.headOid === null || pull.observedBaseOid == null
722+
? null
723+
: `${input.host} ${input.repository.toLowerCase()} ${input.number} ${pull.targetBranch} ${pull.observedBaseOid} ${pull.headOid}`;
724+
const heldStats = tipsKey === null ? undefined : statsByTips.get(tipsKey);
725+
if (heldStats !== undefined) return heldStats;
726+
const stats = yield* countLines(input, pull.version);
727+
// A failed hunk read is retried next time rather than held as unknown, and a head that moved
728+
// between the two reads would hold another pair's stats.
729+
if (tipsKey !== null && stats.complete && stats.headOid === pull.headOid)
730+
remember(statsByTips, tipsKey, stats.stats);
731+
return stats.stats;
697732
});
698733

699734
const merge = Effect.fn("GitCafePullRequestProvider.merge")(function* (
@@ -870,7 +905,7 @@ export const make = Effect.gen(function* () {
870905
const changes =
871906
pull.headOid === null
872907
? null
873-
: yield* readLineStats(input, pull.version).pipe(Effect.orElseSucceed(() => null));
908+
: yield* readLineStats(input, pull).pipe(Effect.orElseSucceed(() => null));
874909
// A count that could not be read is absent rather than zero.
875910
const {
876911
additions: _additions,
@@ -904,7 +939,7 @@ export const make = Effect.gen(function* () {
904939
),
905940
pull.headOid === null
906941
? Effect.succeed(null)
907-
: readLineStats(input, pull.version).pipe(Effect.orElseSucceed(() => null)),
942+
: readLineStats(input, pull).pipe(Effect.orElseSucceed(() => null)),
908943
// Reviewers need `pr:review:read`, which a contents-only token lacks.
909944
readPages("getChangeRequest", input, `${path}/reviewers`, Json.GitCafeActorEntries).pipe(
910945
// @effect-diagnostics-next-line catchConditionalRefailToCatchIf:off - catchIf widens this paginated result to any in tsc-rs.

0 commit comments

Comments
 (0)