Skip to content

Commit db9d59b

Browse files
committed
fix(gitcafe): count lines only for linked pull requests, once per head
1 parent d3b564c commit db9d59b

2 files changed

Lines changed: 12 additions & 37 deletions

File tree

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

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -547,10 +547,12 @@ describe("GitCafePullRequestProvider", () => {
547547
}).pipe(Effect.provide(server.layer));
548548
});
549549

550-
it.effect("summarizes line counts once per revision", () => {
550+
it.effect("summarizes line counts once per head commit", () => {
551551
const snapshot = { version: 4, headOid, comparisonBaseOid: baseOid };
552+
let pullReads = 0;
552553
const server = fakeGitCafe({
553-
"GET /repos/owner/repo/pulls/7": pull,
554+
// A comment moves the version without moving the head.
555+
"GET /repos/owner/repo/pulls/7": () => ({ ...pull, version: 4 + pullReads++ }),
554556
"GET /repos/owner/repo/pulls/7/changes": {
555557
...snapshot,
556558
items: [{ path: "a.ts" }, { path: "b.ts" }],
@@ -569,16 +571,8 @@ describe("GitCafePullRequestProvider", () => {
569571
const provider = yield* GitCafePullRequestProvider.make;
570572
const first = yield* provider.getChangeRequestSummary!(target);
571573
const second = yield* provider.getChangeRequestSummary!(target);
572-
const listed = yield* provider.listChangeRequestStats!({
573-
cwd: target.cwd,
574-
host: target.host,
575-
changeRequests: [{ repository: "owner/repo", number: 7 }],
576-
});
577574
assert.deepInclude(first, { additions: 6, deletions: 2, changedFiles: 2 });
578575
assert.deepInclude(second, { additions: 6, deletions: 2, changedFiles: 2 });
579-
assert.deepStrictEqual(listed, [
580-
{ repository: "owner/repo", number: 7, additions: 6, deletions: 2 },
581-
]);
582576
assert.lengthOf(
583577
server.sent.filter((request) => request.path.endsWith("/changes")),
584578
1,

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

Lines changed: 8 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -400,21 +400,22 @@ export const make = Effect.gen(function* () {
400400
});
401401

402402
// GitCafe's pulls carry no line totals, and linked pull requests are summarized every minute,
403-
// so totals are held per revision rather than recounted from hunks on every read.
404-
const lineStatsByRevision = new Map<string, Effect.Success<ReturnType<typeof readLineStats>>>();
403+
// so totals are held per head commit rather than recounted from hunks on every read. The
404+
// pull's version also moves on comments and reviews, which change no lines.
405+
const lineStatsByHead = new Map<string, Effect.Success<ReturnType<typeof readLineStats>>>();
405406
const lineStats = (
406407
input: PullRef,
407408
pull: { readonly version: number; readonly headOid: string },
408409
) => {
409-
const key = `${input.host} ${input.repository.toLowerCase()} ${input.number} ${pull.version} ${pull.headOid}`;
410-
const held = lineStatsByRevision.get(key);
410+
const key = `${input.host} ${input.repository.toLowerCase()} ${input.number} ${pull.headOid}`;
411+
const held = lineStatsByHead.get(key);
411412
if (held !== undefined) return Effect.succeed(held);
412413
return readLineStats(input, pull.version).pipe(
413414
Effect.tap((stats) =>
414415
Effect.sync(() => {
415-
if (lineStatsByRevision.size >= LINE_STATS_CACHE_SIZE)
416-
lineStatsByRevision.delete(lineStatsByRevision.keys().next().value!);
417-
lineStatsByRevision.set(key, stats);
416+
if (lineStatsByHead.size >= LINE_STATS_CACHE_SIZE)
417+
lineStatsByHead.delete(lineStatsByHead.keys().next().value!);
418+
lineStatsByHead.set(key, stats);
418419
}),
419420
),
420421
);
@@ -547,26 +548,6 @@ export const make = Effect.gen(function* () {
547548
};
548549
},
549550
),
550-
listChangeRequestStats: (input) =>
551-
Effect.forEach(
552-
input.changeRequests,
553-
({ repository, number }) => {
554-
const ref = { cwd: input.cwd, host: input.host, repository, number };
555-
return readPull("listChangeRequestStats", ref).pipe(
556-
Effect.flatMap((pull) =>
557-
pull.headOid === null
558-
? Effect.succeed([])
559-
: lineStats(ref, { version: pull.version, headOid: pull.headOid }).pipe(
560-
Effect.map(({ additions, deletions }) => [
561-
{ repository, number, additions, deletions },
562-
]),
563-
),
564-
),
565-
Effect.orElseSucceed(() => []),
566-
);
567-
},
568-
{ concurrency: 4 },
569-
).pipe(Effect.map((stats) => stats.flat())),
570551
getChangeRequestSummary: Effect.fn("GitCafePullRequestProvider.getChangeRequestSummary")(
571552
function* (input) {
572553
const pull = yield* readPull("getChangeRequestSummary", input);

0 commit comments

Comments
 (0)