Skip to content

Commit 7cb28c0

Browse files
Bil0000claude
andcommitted
perf(github): skip a refused reviewer read and fetch commit stats alongside activity pages
Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 1b476dc commit 7cb28c0

2 files changed

Lines changed: 134 additions & 34 deletions

File tree

‎packages/source-control-github/src/server/GitHubPullRequestApi.test.ts‎

Lines changed: 80 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -819,6 +819,74 @@ layer("GitHubPullRequestApi.layer", (it) => {
819819
}),
820820
);
821821

822+
it.effect("reads commit stats alongside later activity pages", () =>
823+
Effect.gen(function* () {
824+
const laterPageStarted = yield* Deferred.make<void>();
825+
const statsStarted = yield* Deferred.make<void>();
826+
const activityPage = (head: boolean) => ({
827+
data: {
828+
repository: {
829+
pullRequest: {
830+
...(head
831+
? {
832+
author: null,
833+
commits: {
834+
nodes: [
835+
{
836+
commit: {
837+
id: "node-regular",
838+
oid: "regular",
839+
messageHeadline: "regular",
840+
committedDate: "2026-07-05T00:00:00Z",
841+
parents: { totalCount: 1 },
842+
authors: { nodes: [] },
843+
},
844+
},
845+
],
846+
},
847+
}
848+
: {}),
849+
comments: {
850+
nodes: [],
851+
pageInfo: head ? { hasNextPage: true, endCursor: "c1" } : lastPage,
852+
},
853+
reviews: { nodes: [], pageInfo: lastPage },
854+
},
855+
},
856+
},
857+
});
858+
mockedExecute.mockImplementation((call) => {
859+
if (matches(call, "PullRequestCommitStats")) {
860+
return Deferred.succeed(statsStarted, undefined).pipe(
861+
Effect.andThen(Deferred.await(laterPageStarted)),
862+
Effect.as(
863+
output(
864+
encodeJson({
865+
data: {
866+
nodes: [{ __typename: "Commit", oid: "regular", additions: 3, deletions: 1 }],
867+
},
868+
}),
869+
),
870+
),
871+
);
872+
}
873+
const head = call.kind === "graphql" && call.variables?.["head"] === true;
874+
return head
875+
? Effect.succeed(output(encodeJson(activityPage(true))))
876+
: Deferred.succeed(laterPageStarted, undefined).pipe(
877+
Effect.andThen(Deferred.await(statsStarted)),
878+
Effect.as(output(encodeJson(activityPage(false)))),
879+
);
880+
});
881+
const cli = yield* GitHubPullRequestApi.GitHubPullRequestApi;
882+
883+
const activity = yield* cli.getPullRequestActivity(pullRequestInput);
884+
885+
expect(activity.commits[0]).toMatchObject({ additions: 3, deletions: 1 });
886+
expect(mockedExecute).toHaveBeenCalledTimes(3);
887+
}),
888+
);
889+
822890
it.effect("loads the complete hover card with one GraphQL request", () =>
823891
Effect.gen(function* () {
824892
mockedExecute.mockReturnValueOnce(
@@ -3268,7 +3336,10 @@ layer("GitHubPullRequestApi.layer", (it) => {
32683336
Effect.succeed(output(reviewThreadsPage([thread("T3", "c3")], null))),
32693337
);
32703338
const cli = yield* GitHubPullRequestApi.GitHubPullRequestApi;
3271-
const read = cli.listReviewThreadComments(pullRequestInput);
3339+
const read = cli.listReviewThreadComments({
3340+
...pullRequestInput,
3341+
repository: `acme/refused-threads-${later}`,
3342+
});
32723343
const conversation = yield* read;
32733344
expect(
32743345
variablesOf("ReviewThreads").map((v) => [v["after"], v["requestedReviewers"]]),
@@ -3296,7 +3367,7 @@ layer("GitHubPullRequestApi.layer", (it) => {
32963367
yield* read;
32973368
expect(variablesOf("ReviewThreads").at(-1)).toMatchObject({
32983369
after: null,
3299-
requestedReviewers: 50,
3370+
requestedReviewers: 0,
33003371
});
33013372
}),
33023373
);
@@ -3429,7 +3500,10 @@ layer("GitHubPullRequestApi.layer", (it) => {
34293500
),
34303501
);
34313502
const cli = yield* GitHubPullRequestApi.GitHubPullRequestApi;
3432-
const read = cli.getPullRequestDetail(pullRequestInput);
3503+
const read = cli.getPullRequestDetail({
3504+
...pullRequestInput,
3505+
repository: `acme/refused-checks-${refused}`,
3506+
});
34333507
const detail = yield* read;
34343508
expect(detail.reviewRequestLogins).toEqual([]);
34353509
expect(detail.reviewRequestTeams).toEqual([]);
@@ -3449,7 +3523,8 @@ layer("GitHubPullRequestApi.layer", (it) => {
34493523
if (refused) {
34503524
mockedExecute.mockReturnValueOnce(Effect.succeed(output(encodeJson(coreResponse()))));
34513525
yield* read;
3452-
expect(variablesOf("PullRequestCore").at(-1)).toMatchObject({ requestedReviewers: 100 });
3526+
expect(variablesOf("PullRequestCore").at(-1)).toMatchObject({ requestedReviewers: 0 });
3527+
expect(mockedExecute).toHaveBeenCalledTimes(4);
34533528
}
34543529
}),
34553530
);
@@ -3483,7 +3558,7 @@ layer("GitHubPullRequestApi.layer", (it) => {
34833558
const error = yield* Effect.flip(
34843559
cli.getPullRequestDetail({
34853560
cwd: "/w",
3486-
repository: "acme/web",
3561+
repository: `acme/head-changed-${refused}`,
34873562
host: "github.com",
34883563
number: 7,
34893564
}),

‎packages/source-control-github/src/server/GitHubPullRequestApi.ts‎

Lines changed: 54 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1392,6 +1392,31 @@ export const make = Effect.gen(function* () {
13921392
};
13931393
});
13941394

1395+
// GitHub refuses a requested-reviewer list holding a team the viewer cannot see. Remember the
1396+
// refusal so later reads of that pull request skip straight to the read without reviewers.
1397+
const refusedReviewers = yield* Cache.make({
1398+
capacity: 512,
1399+
timeToLive: "10 minutes",
1400+
lookup: (_key: string) => Effect.void,
1401+
});
1402+
const readRequestedReviewers = Effect.fn("GitHubPullRequestApi.readRequestedReviewers")(
1403+
function* <A, E, R>(
1404+
input: { readonly host: string; readonly repository: string; readonly number: number },
1405+
wanted: number,
1406+
read: (requestedReviewers: number) => Effect.Effect<A, E, R>,
1407+
) {
1408+
const host = normalizeHost(input.host);
1409+
const { fingerprint } = yield* api.credential(host);
1410+
const key = `${fingerprint}\0${host}\0${input.repository.toLowerCase()}\0${input.number}`;
1411+
if (yield* Cache.has(refusedReviewers, key)) return yield* read(0);
1412+
return yield* read(wanted).pipe(
1413+
Effect.catchIf(isRefusedRequestedReviewersOnly, () =>
1414+
Cache.set(refusedReviewers, key, undefined).pipe(Effect.andThen(read(0))),
1415+
),
1416+
);
1417+
},
1418+
);
1419+
13951420
const getPullRequestDetail: GitHubPullRequestApi["Service"]["getPullRequestDetail"] = (input) => {
13961421
const { owner, name } = parseRepositorySelector(input.repository);
13971422
const operation = "getPullRequestDetail";
@@ -1406,8 +1431,7 @@ export const make = Effect.gen(function* () {
14061431
});
14071432
return SourceControlRateLimit.Interactive.pipe(
14081433
Effect.flatMap((allowReserve) =>
1409-
read(100).pipe(
1410-
Effect.catchIf(isRefusedRequestedReviewersOnly, () => read(0)),
1434+
readRequestedReviewers(input, 100, read).pipe(
14111435
graphql({ ...input, operation, allowReserve }),
14121436
Effect.map(toPullRequestCore),
14131437
Effect.flatMap(found({ ...input, operation })),
@@ -2001,7 +2025,9 @@ export const make = Effect.gen(function* () {
20012025
let withComments = true;
20022026
let withReviews = true;
20032027
const target = { ...input, operation: "getPullRequestActivity" };
2004-
for (let page = 0; page < REVIEW_THREAD_PAGES && (withComments || withReviews); page++) {
2028+
const readPage = Effect.fn("GitHubPullRequestApi.readActivityPage")(function* (
2029+
page: number,
2030+
) {
20052031
const read: GitHubPullRequestActivityPage = yield* client
20062032
.PullRequestActivity({
20072033
owner,
@@ -2028,16 +2054,28 @@ export const make = Effect.gen(function* () {
20282054
reviewsAfter = read.nextReviewsCursor;
20292055
withComments = withComments && commentsAfter !== null;
20302056
withReviews = withReviews && reviewsAfter !== null;
2031-
}
2057+
});
2058+
yield* readPage(0);
2059+
const laterPages = Effect.gen(function* () {
2060+
for (let page = 1; page < REVIEW_THREAD_PAGES; page++) {
2061+
if (!withComments && !withReviews) return;
2062+
yield* readPage(page);
2063+
}
2064+
});
20322065
const emptyStats = toPullRequestCommitStats({ nodes: [] });
2033-
const commitStats =
2034-
nonMergeCommitIds.length === 0
2035-
? emptyStats
2036-
: yield* client.PullRequestCommitStats({ ids: nonMergeCommitIds }).pipe(
2037-
graphql(target),
2038-
Effect.map(toPullRequestCommitStats),
2039-
Effect.orElseSucceed(() => emptyStats),
2040-
);
2066+
const [, commitStats] = yield* Effect.all(
2067+
[
2068+
laterPages,
2069+
nonMergeCommitIds.length === 0
2070+
? Effect.succeed(emptyStats)
2071+
: client.PullRequestCommitStats({ ids: nonMergeCommitIds }).pipe(
2072+
graphql(target),
2073+
Effect.map(toPullRequestCommitStats),
2074+
Effect.orElseSucceed(() => emptyStats),
2075+
),
2076+
],
2077+
{ concurrency: 2 },
2078+
);
20412079
return {
20422080
author,
20432081
comments: remarks.toSorted((left, right) =>
@@ -2127,25 +2165,12 @@ export const make = Effect.gen(function* () {
21272165
function* (input) {
21282166
const { owner, name } = parseRepositorySelector(input.repository);
21292167
const target = { ...input, operation: "listReviewThreadComments" };
2130-
let requestedReviewers = 50;
21312168
const threadPage = (
21322169
after: string | null,
2133-
): Effect.Effect<GitHubReviewThreadPage, GitHubPullRequestApiError> => {
2134-
const read = () =>
2135-
client.ReviewThreads({ owner, name, number: input.number, after, requestedReviewers });
2136-
return read().pipe(
2137-
Effect.catchIf(
2138-
(error) => requestedReviewers > 0 && isRefusedRequestedReviewersOnly(error),
2139-
() => {
2140-
requestedReviewers = 0;
2141-
return read();
2142-
},
2143-
),
2144-
graphql(target),
2145-
Effect.map(toReviewThreadPage),
2146-
Effect.flatMap(found(target)),
2147-
);
2148-
};
2170+
): Effect.Effect<GitHubReviewThreadPage, GitHubPullRequestApiError> =>
2171+
readRequestedReviewers(input, 50, (requestedReviewers) =>
2172+
client.ReviewThreads({ owner, name, number: input.number, after, requestedReviewers }),
2173+
).pipe(graphql(target), Effect.map(toReviewThreadPage), Effect.flatMap(found(target)));
21492174
const entries: GitHubReviewThreadEntry[] = [];
21502175
const avatarsByLogin = new Map<string, string>();
21512176
const botLogins = new Set<string>();

0 commit comments

Comments
 (0)