Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

pullRequestDiffPages.ts

73 lines2.9 KB

1import type { ChangeRequestTerminology } from "@t3tools/client-runtime/source-control-clients";
2import type { PullRequestDiffResult } from "@t3tools/contracts";
3import { buildReviewParsedDiff, type ReviewParsedDiff } from "../review/reviewModel";
4​
5export interface PullRequestDiffPage {
6 readonly cursor: string | null;
7 readonly result: PullRequestDiffResult;
8}
9​
10/** A changed page invalidates later cursors, which belong to the previous patch. */
11export function retainPullRequestDiffPage(
12 pages: readonly PullRequestDiffPage[],
13 cursor: string | null,
14 result: PullRequestDiffResult,
15): readonly PullRequestDiffPage[] {
16 const index = pages.findIndex((page) => page.cursor === cursor);
17 if (index === -1) return [...pages, { cursor, result }];
18 const previous = pages[index]?.result;
19 if (
20 previous === result ||
21 (previous?.patch === result.patch &&
22 previous.truncated === result.truncated &&
23 previous.nextCursor === result.nextCursor &&
24 JSON.stringify(previous.omittedFileStats ?? []) ===
25 JSON.stringify(result.omittedFileStats ?? []))
26 )
27 return pages;
28 return [...pages.slice(0, index), { cursor, result }];
30​
31export function combinePullRequestDiffPages(
32 pages: readonly PullRequestDiffPage[],
33 cache: WeakMap<PullRequestDiffResult, ReviewParsedDiff>,
34 scope: string,
35 terminology: ChangeRequestTerminology,
36): ReviewParsedDiff {
37 const parsed = pages.map((page) => {
38 const cached = cache.get(page.result);
39 if (cached) return cached;
40 let value = buildReviewParsedDiff(page.result.patch, `${scope}:${page.cursor ?? "first"}`);
41 if (value.kind === "files" && page.result.omittedFileStats?.length) {
42 const omittedFileStats = new Map(
43 page.result.omittedFileStats.map((stat) => [stat.path, stat]),
44 );
45 const files = value.files.map((file) => {
46 const stat = omittedFileStats.get(file.path);
47 return stat ? { ...file, additions: stat.additions, deletions: stat.deletions } : file;
48 });
49 value = {
50 ...value,
51 files,
52 additions: files.reduce((total, file) => total + file.additions, 0),
53 deletions: files.reduce((total, file) => total + file.deletions, 0),
54 };
55 }
56 cache.set(page.result, value);
57 return value;
58 });
59 const files = parsed.flatMap((page) => (page.kind === "files" ? page.files : []));
60 if (files.length === 0) return parsed[0] ?? { kind: "empty" };
61 return {
62 kind: "files",
63 files,
64 fileCount: files.length,
65 additions: files.reduce((total, file) => total + file.additions, 0),
66 deletions: files.reduce((total, file) => total + file.deletions, 0),
67 notice:
68 pages.some((page) => page.result.truncated) ||
69 parsed.some((page) => page.kind === "raw" || (page.kind === "files" && page.notice !== null))
70 ? `Some file contents are unavailable. Open the ${terminology.singular} on its provider to see the full changes.`
71 : null,
72 };