Skip to content

Commit 92a6f6d

Browse files
t3dotggclaude
andauthored
feat(web): show how much of a PR is code vs tests, mocks, and generated files (#22189)
Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent d18352e commit 92a6f6d

23 files changed

Lines changed: 565 additions & 23 deletions

‎apps/mobile/src/features/pull-requests/PullRequestFiles.tsx‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type { PullRequestChangeBreakdown } from "@t3tools/contracts";
12
import { PullRequestSectionHeading } from "./PullRequestSectionHeading";
23
import { AnimatedLegendList } from "@legendapp/list/reanimated";
34
import Animated from "react-native-reanimated";
@@ -23,6 +24,33 @@ import {
2324
import { usePullRequestFilesViewed } from "./usePullRequestFilesViewed";
2425
import { usePullRequestFileFilter } from "./usePullRequestFileFilter";
2526

27+
const BREAKDOWN_LABELS = {
28+
code: "Code",
29+
tests: "Tests",
30+
mocks: "Mocks",
31+
generated: "Generated",
32+
} satisfies Record<keyof NonNullable<PullRequestChangeBreakdown>, string>;
33+
34+
function PullRequestChangeBreakdownLine(props: {
35+
readonly breakdown: NonNullable<PullRequestChangeBreakdown>;
36+
}) {
37+
const parts = (Object.keys(BREAKDOWN_LABELS) as Array<keyof typeof BREAKDOWN_LABELS>).flatMap(
38+
(kind) => {
39+
const { additions, deletions } = props.breakdown[kind];
40+
return additions === 0 && deletions === 0
41+
? []
42+
: [
43+
`${BREAKDOWN_LABELS[kind]} +${additions.toLocaleString()} −${deletions.toLocaleString()}`,
44+
];
45+
},
46+
);
47+
return (
48+
<Text className="text-2xs tabular-nums text-foreground-muted" numberOfLines={2}>
49+
{parts.join(" · ")}
50+
</Text>
51+
);
52+
}
53+
2654
function fileDirectory(path: string) {
2755
const separator = path.lastIndexOf("/");
2856
return separator < 0 ? "" : path.slice(0, separator);
@@ -37,6 +65,9 @@ export function usePullRequestFilesContent(props: {
3765
const result = usePullRequestDiff(props.params);
3866
const detail = useEnvironmentQuery(composerPullRequests.detail(requestTarget(props.params)));
3967
const viewed = usePullRequestFilesViewed(props.params, props.params.commit ? null : detail.data);
68+
const breakdown = useEnvironmentQuery(
69+
props.params.commit ? null : composerPullRequests.changeBreakdown(requestTarget(props.params)),
70+
).data;
4071
const filter = usePullRequestFileFilter(result.parsed);
4172
const parsed = filter.parsed;
4273
const loading = result.isPending && parsed.kind === "empty" && !result.error;
@@ -209,6 +240,7 @@ export function usePullRequestFilesContent(props: {
209240
? `${filter.hiddenCount} test ${filter.hiddenCount === 1 ? "file" : "files"} hidden`
210241
: "Test files included"}
211242
</Text>
243+
{breakdown ? <PullRequestChangeBreakdownLine breakdown={breakdown} /> : null}
212244
</View>
213245
<PullRequestDiffStats
214246
additions={parsed.additions}

‎apps/mobile/src/features/pull-requests/pullRequestFileFilters.ts‎

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,10 @@
1+
import { classifyChangedFile } from "@t3tools/shared/changedFileKind";
12
import type { ReviewParsedDiff } from "../review/reviewModel";
23

4+
/** Test support such as fixtures and snapshots is hidden along with the tests. */
35
export function isPullRequestTestFile(path: string): boolean {
4-
const segments = path.replaceAll("\\", "/").split("/");
5-
const name = segments.at(-1) ?? "";
6-
return (
7-
segments
8-
.slice(0, -1)
9-
.some((segment) => /^(?:__tests__|__snapshots__|tests?|spec|e2e)$/i.test(segment)) ||
10-
/\.(?:test|spec)\./i.test(name) ||
11-
/\.snap$/i.test(name) ||
12-
/^(?:test_.+|.+_test)\.py$/i.test(name) ||
13-
/_test\.(?:go|rs|rb|c|cc|cpp|h|hpp)$/i.test(name) ||
14-
/(?:Test|Tests|Spec)\.(?:java|kt|kts|swift|cs)$/.test(name) ||
15-
/_spec\.rb$/i.test(name)
16-
);
6+
const kind = classifyChangedFile(path);
7+
return kind === "tests" || kind === "mocks";
178
}
189

1910
export function filterPullRequestTestFiles(

‎apps/server/src/auth/RpcAuthorization.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ export const RPC_REQUIRED_SCOPES = {
133133
[WS_METHODS.pullRequestsThreadComments]: AuthOrchestrationReadScope,
134134
[WS_METHODS.pullRequestsDiffFileContents]: AuthOrchestrationReadScope,
135135
[WS_METHODS.pullRequestsFilesViewed]: AuthOrchestrationReadScope,
136+
[WS_METHODS.pullRequestsChangeBreakdown]: AuthOrchestrationReadScope,
136137
[WS_METHODS.pullRequestsInvalidate]: AuthOrchestrationReadScope,
137138
[WS_METHODS.pullRequestsReportState]: AuthOrchestrationReadScope,
138139
[WS_METHODS.pullRequestsSubscribeRefreshes]: AuthOrchestrationReadScope,

‎apps/server/src/observability/RpcInstrumentation.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ const RPC_AGGREGATES = {
113113
[WS_METHODS.pullRequestsThreadComments]: "pull-requests",
114114
[WS_METHODS.pullRequestsDiffFileContents]: "pull-requests",
115115
[WS_METHODS.pullRequestsFilesViewed]: "pull-requests",
116+
[WS_METHODS.pullRequestsChangeBreakdown]: "pull-requests",
116117
[WS_METHODS.pullRequestsRunAction]: "pull-requests",
117118
[WS_METHODS.pullRequestsUpdate]: "pull-requests",
118119
[WS_METHODS.pullRequestsComment]: "pull-requests",

‎apps/server/src/pullRequest/PullRequestService.ts‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ import {
4040
type PullRequestDiffFileContentsResult,
4141
type PullRequestDiffStat,
4242
type PullRequestDiffInput,
43+
type PullRequestChangeBreakdown,
4344
type PullRequestFilesViewedResult,
4445
type PullRequestDiffResult,
4546
type PullRequestInvalidateInput,
@@ -95,6 +96,7 @@ import {
9596
import * as PullRequestReadCache from "./PullRequestReadCache.ts";
9697
import * as PullRequestProviderRegistry from "./PullRequestProviderRegistry.ts";
9798
import * as ViewedFiles from "./pullRequestViewedFiles.ts";
99+
import { toChangeBreakdown } from "./pullRequestChangeBreakdown.ts";
98100

99101
export interface PullRequestMergeEvent extends PullRequestRef {
100102
readonly mergedAt: string;
@@ -230,6 +232,9 @@ export class PullRequestService extends Context.Service<
230232
readonly setFilesViewed: (
231233
input: PullRequestSetFilesViewedInput,
232234
) => Effect.Effect<void, PullRequestError>;
235+
readonly changeBreakdown: (
236+
input: PullRequestRef,
237+
) => Effect.Effect<PullRequestChangeBreakdown, PullRequestError>;
233238
readonly runAction: (input: PullRequestActionInput) => Effect.Effect<void, PullRequestError>;
234239
readonly update: (input: PullRequestUpdateInput) => Effect.Effect<void, PullRequestError>;
235240
readonly comment: (input: PullRequestCommentInput) => Effect.Effect<void, PullRequestError>;
@@ -535,6 +540,9 @@ function withRateLimitBackoff(
535540
...(api.getFilesViewed === undefined
536541
? {}
537542
: { getFilesViewed: wrap("getFilesViewed", api.getFilesViewed) }),
543+
...(api.getFileStats === undefined
544+
? {}
545+
: { getFileStats: wrap("getFileStats", api.getFileStats) }),
538546
...(api.setFilesViewed === undefined
539547
? {}
540548
: { setFilesViewed: interactive("setFilesViewed", api.setFilesViewed) }),
@@ -2958,6 +2966,32 @@ export const make = Effect.gen(function* () {
29582966
},
29592967
);
29602968

2969+
const changeBreakdownCache = yield* Cache.makeWith(
2970+
(key: string) => {
2971+
const ref = refOfCacheKey(key);
2972+
return requireProject(ref).pipe(
2973+
Effect.flatMap((project) => {
2974+
const read = project.api.getFileStats;
2975+
if (read === undefined) return Effect.succeed(null);
2976+
return read({
2977+
cwd: project.project.workspaceRoot,
2978+
repository: project.repository,
2979+
host: project.host,
2980+
number: ref.number,
2981+
}).pipe(
2982+
Effect.map(toChangeBreakdown),
2983+
Effect.mapError(toPullRequestError("changeBreakdown")),
2984+
);
2985+
}),
2986+
);
2987+
},
2988+
{
2989+
capacity: DETAIL_CACHE_CAPACITY,
2990+
timeToLive: (exit, key) =>
2991+
Exit.isSuccess(exit) ? detailTimeToLive(lastGoodSummary.peek(key)?.state) : Duration.zero,
2992+
},
2993+
);
2994+
29612995
const previewCache = yield* Cache.makeWith(
29622996
(key: string) => {
29632997
return previewUncached(refOfCacheKey(key));
@@ -3366,6 +3400,9 @@ export const make = Effect.gen(function* () {
33663400
diffFileContents,
33673401
filesViewed: credentialCached(filesViewed),
33683402
setFilesViewed,
3403+
changeBreakdown: credentialCached((input) =>
3404+
Cache.get(changeBreakdownCache, refCacheKey(input)),
3405+
),
33693406
runAction: runActionAndInvalidate,
33703407
update: invalidatedByMutation(update),
33713408
comment: invalidatedByMutation(comment),
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { describe, expect, it } from "vite-plus/test";
2+
3+
import { toChangeBreakdown } from "./pullRequestChangeBreakdown.ts";
4+
5+
describe("toChangeBreakdown", () => {
6+
it("sums line counts per kind", () => {
7+
expect(
8+
toChangeBreakdown({
9+
truncated: false,
10+
files: [
11+
{ path: "src/a.ts", additions: 10, deletions: 2 },
12+
{ path: "src/b.ts", additions: 5, deletions: 1 },
13+
{ path: "src/a.test.ts", additions: 40, deletions: 0 },
14+
{ path: "pnpm-lock.yaml", additions: 300, deletions: 120 },
15+
],
16+
}),
17+
).toEqual({
18+
code: { additions: 15, deletions: 3 },
19+
tests: { additions: 40, deletions: 0 },
20+
mocks: { additions: 0, deletions: 0 },
21+
generated: { additions: 300, deletions: 120 },
22+
});
23+
});
24+
25+
it("gives no breakdown for a partial file list", () => {
26+
expect(
27+
toChangeBreakdown({
28+
truncated: true,
29+
files: [{ path: "src/a.ts", additions: 1, deletions: 0 }],
30+
}),
31+
).toBeNull();
32+
});
33+
});
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import type { PullRequestChangeBreakdown } from "@t3tools/contracts";
2+
import { classifyChangedFile } from "@t3tools/shared/changedFileKind";
3+
import type { ProviderFileStats } from "@t3tools/source-control-core/server/PullRequestProvider";
4+
5+
export function toChangeBreakdown(stats: ProviderFileStats): PullRequestChangeBreakdown {
6+
if (stats.truncated) return null;
7+
const empty = () => ({ additions: 0, deletions: 0 });
8+
const breakdown = { code: empty(), tests: empty(), mocks: empty(), generated: empty() };
9+
for (const file of stats.files) {
10+
const bucket = breakdown[classifyChangedFile(file.path)];
11+
bucket.additions += file.additions;
12+
bucket.deletions += file.deletions;
13+
}
14+
return breakdown;
15+
}

‎apps/server/src/ws.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2489,6 +2489,8 @@ export const layerWsRpc = (
24892489
withPullRequestViewer(input, pullRequests.diffFileContents(input)),
24902490
[WS_METHODS.pullRequestsFilesViewed]: (input) =>
24912491
withPullRequestViewer(input, pullRequests.filesViewed(input)),
2492+
[WS_METHODS.pullRequestsChangeBreakdown]: (input) =>
2493+
withPullRequestViewer(input, pullRequests.changeBreakdown(input)),
24922494
[WS_METHODS.pullRequestsSetFilesViewed]: (input) =>
24932495
withPullRequestViewer(input, pullRequests.setFilesViewed(input)),
24942496
[WS_METHODS.pullRequestsRunAction]: (input) =>
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
import { formatDiffCount } from "@t3tools/client-runtime/diff-count";
2+
import type { EnvironmentId, PullRequestChangeBreakdown, PullRequestRef } from "@t3tools/contracts";
3+
import { useState } from "react";
4+
5+
import { pullRequestEnvironment } from "~/state/pullRequests";
6+
import { useEnvironmentQuery } from "~/state/query";
7+
8+
import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip";
9+
import { PullRequestDiffStat } from "./pullRequestPresentation";
10+
11+
const KIND_LABELS = {
12+
code: "Code",
13+
tests: "Tests",
14+
mocks: "Mocks",
15+
generated: "Generated",
16+
} satisfies Record<keyof NonNullable<PullRequestChangeBreakdown>, string>;
17+
18+
/**
19+
* The pull request's diff stat, with a hover split into code, tests, mocks, and generated files.
20+
* The split is read on first pointer or focus intent, so a page nobody hovers costs the host
21+
* nothing.
22+
*/
23+
export function PullRequestChangeBreakdownStat({
24+
environmentId,
25+
reference,
26+
additions,
27+
deletions,
28+
className,
29+
}: {
30+
environmentId: EnvironmentId;
31+
reference: PullRequestRef;
32+
additions: number;
33+
deletions: number;
34+
className: string;
35+
}) {
36+
const [requested, setRequested] = useState(false);
37+
const query = useEnvironmentQuery(
38+
requested ? pullRequestEnvironment.changeBreakdown({ environmentId, input: reference }) : null,
39+
);
40+
const breakdown = query.data;
41+
if (additions === 0 && deletions === 0) return null;
42+
return (
43+
<Tooltip>
44+
<TooltipTrigger
45+
onPointerEnter={() => setRequested(true)}
46+
onFocus={() => setRequested(true)}
47+
render={
48+
<span className="inline-flex">
49+
<PullRequestDiffStat
50+
additions={additions}
51+
deletions={deletions}
52+
className={className}
53+
/>
54+
</span>
55+
}
56+
/>
57+
{breakdown ? (
58+
<TooltipPopup side="bottom">
59+
<div className="grid grid-cols-[auto_auto_auto] gap-x-4 gap-y-1 py-0.5 font-mono tabular-nums">
60+
{(Object.keys(KIND_LABELS) as Array<keyof typeof KIND_LABELS>).map((kind) => {
61+
const { additions, deletions } = breakdown[kind];
62+
return additions === 0 && deletions === 0 ? null : (
63+
<BreakdownRow
64+
key={kind}
65+
label={KIND_LABELS[kind]}
66+
additions={additions}
67+
deletions={deletions}
68+
/>
69+
);
70+
})}
71+
<div className="col-span-3 border-t border-border" />
72+
<BreakdownRow label="Total" additions={additions} deletions={deletions} />
73+
</div>
74+
</TooltipPopup>
75+
) : query.isPending ? (
76+
<TooltipPopup side="bottom">Loading breakdown</TooltipPopup>
77+
) : null}
78+
</Tooltip>
79+
);
80+
}
81+
82+
function BreakdownRow({
83+
label,
84+
additions,
85+
deletions,
86+
}: {
87+
label: string;
88+
additions: number;
89+
deletions: number;
90+
}) {
91+
return (
92+
<>
93+
<span className="font-sans">{label}</span>
94+
<span className="text-right text-diff-addition-foreground">
95+
+{formatDiffCount(additions)}
96+
</span>
97+
<span className="text-right text-diff-deletion">-{formatDiffCount(deletions)}</span>
98+
</>
99+
);
100+
}

‎apps/web/src/components/pullRequest/PullRequestDetailPanel.tsx‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ import {
9797
} from "~/state/pullRequests";
9898
import { useAtomCommand } from "~/state/use-atom-command";
9999
import { PullRequestStackMenu } from "./PullRequestStackMenu";
100+
import { PullRequestChangeBreakdownStat } from "./PullRequestChangeBreakdownStat";
100101
import { sourceControlClients } from "@t3tools/client-runtime/source-control-clients";
101102
import { PullRequestThreadLinks } from "./PullRequestThreadLinks";
102103
import { vcsEnvironment } from "~/state/vcs";
@@ -182,7 +183,6 @@ import {
182183
import { PullRequestChecksPopover } from "./PullRequestChecksPopover";
183184
import {
184185
PullRequestActorLabel,
185-
PullRequestDiffStat,
186186
PullRequestMetaLine,
187187
PullRequestReviewOutcomeIcon,
188188
pullRequestChecksState,
@@ -2430,7 +2430,9 @@ export function PullRequestDetailPanel({
24302430
<FileDiffIcon aria-hidden className="size-3" />
24312431
{detail.changedFiles.toLocaleString()}
24322432
</span>
2433-
<PullRequestDiffStat
2433+
<PullRequestChangeBreakdownStat
2434+
environmentId={environmentId}
2435+
reference={reference}
24342436
additions={detail.additions}
24352437
deletions={detail.deletions}
24362438
className="shrink-0 font-mono text-2xs"
@@ -2604,7 +2606,9 @@ export function PullRequestDetailPanel({
26042606
}
26052607
files={`${detail.changedFiles.toLocaleString()} ${detail.changedFiles === 1 ? "file" : "files"}`}
26062608
diffStat={
2607-
<PullRequestDiffStat
2609+
<PullRequestChangeBreakdownStat
2610+
environmentId={environmentId}
2611+
reference={reference}
26082612
additions={detail.additions}
26092613
deletions={detail.deletions}
26102614
className="shrink-0 font-mono text-xs"

0 commit comments

Comments
 (0)