Open History to see the last commit
1import type { FileDiffContentsLoader } from "@pierre/diffs";
2import {
3 squashAtomCommandFailure,
4 type AtomCommandResult,
5} from "@t3tools/client-runtime/state/runtime";
6import type {
7 EnvironmentId,
8 PullRequestDiffFileContentsInput,
9 PullRequestDiffFileContentsResult,
10 PullRequestRef,
11 ReviewDiffFileContentsInput,
12 ReviewDiffFileContentsResult,
13 ReviewDiffPreviewSourceKind,
14} from "@t3tools/contracts";
15
16import { resolveFileDiffPath } from "./diffRendering";
17
18interface GitDiffFileContentsSource {
19 readonly environmentId: EnvironmentId;
20 readonly cwd: string;
21 readonly sourceKind: ReviewDiffPreviewSourceKind;
22 readonly baseRef: string | null;
23 readonly headRef: string | null;
24 readonly cacheKey: string;
25}
26
27interface PullRequestDiffFileContentsSource {
28 readonly environmentId: EnvironmentId;
29 readonly reference: PullRequestRef;
30 readonly commit: string | null;
31 readonly cacheKey: string;
32}
33
34type GetDiffFileContents<E> = (request: {
35 readonly environmentId: EnvironmentId;
36 readonly input: ReviewDiffFileContentsInput;
37}) => Promise<AtomCommandResult<ReviewDiffFileContentsResult, E>>;
38
39type GetPullRequestDiffFileContents<E> = (request: {
40 readonly environmentId: EnvironmentId;
41 readonly input: PullRequestDiffFileContentsInput;
42}) => Promise<AtomCommandResult<PullRequestDiffFileContentsResult, E>>;
43
44function createDiffFileContentsLoader(
45 load: (input: {
46 readonly changeType: PullRequestDiffFileContentsInput["changeType"];
47 readonly oldPath: string;
48 readonly newPath: string;
49 }) => Promise<{ readonly oldContents: string; readonly newContents: string }>,
50 cacheKey: string,
51): FileDiffContentsLoader {
52 return async (fileDiff) => {
53 const newPath = resolveFileDiffPath(fileDiff);
54 const oldPath = fileDiff.prevName
55 ? resolveFileDiffPath({ ...fileDiff, name: fileDiff.prevName })
56 : newPath;
57 const contents = await load({ changeType: fileDiff.type, oldPath, newPath });
58 const newFile = {
59 name: newPath,
60 contents: contents.newContents,
61 cacheKey: `${cacheKey}:new:${newPath}`,
62 };
63 if (fileDiff.type === "rename-pure") {
64 return { oldFile: null, newFile };
65 }
66 return {
67 oldFile: {
68 name: oldPath,
69 contents: contents.oldContents,
70 cacheKey: `${cacheKey}:old:${oldPath}`,
71 },
72 newFile,
73 };
74 };
75}
76
77export function createGitDiffFileContentsLoader<E>(
78 getDiffFileContents: GetDiffFileContents<E>,
79 source: GitDiffFileContentsSource,
80): FileDiffContentsLoader {
81 return createDiffFileContentsLoader(async ({ changeType, oldPath, newPath }) => {
82 const result = await getDiffFileContents({
83 environmentId: source.environmentId,
84 input: {
85 cwd: source.cwd,
86 sourceKind: source.sourceKind,
87 changeType,
88 baseRef: source.baseRef,
89 headRef: source.headRef,
90 oldPath,
91 newPath,
92 },
93 });
94 if (result._tag !== "Success") {
95 throw squashAtomCommandFailure(result);
96 }
97 return result.value;
98 }, source.cacheKey);
99}
100
101export function createPullRequestDiffFileContentsLoader<E>(
102 getDiffFileContents: GetPullRequestDiffFileContents<E>,
103 source: PullRequestDiffFileContentsSource,
104): FileDiffContentsLoader {
105 return createDiffFileContentsLoader(async ({ changeType, oldPath, newPath }) => {
106 const result = await getDiffFileContents({
107 environmentId: source.environmentId,
108 input: {
109 ...source.reference,
110 ...(source.commit === null ? {} : { commit: source.commit }),
111 changeType,
112 oldPath,
113 newPath,
114 },
115 });
116 if (result._tag !== "Success") {
117 throw squashAtomCommandFailure(result);
118 }
119 return result.value;
120 }, source.cacheKey);
121}