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

ReviewService.ts

141 lines4.8 KB

1import * as Context from "effect/Context";
2import * as DateTime from "effect/DateTime";
3import * as Effect from "effect/Effect";
4import * as FileSystem from "effect/FileSystem";
5import * as Layer from "effect/Layer";
6import * as Path from "effect/Path";
7​
8import {
9 VcsRepositoryDetectionError,
10 VcsUnsupportedOperationError,
11 type ReviewDiffFileContentsInput,
12 type ReviewDiffFileContentsResult,
13 type ReviewDiffPreviewError,
14 type ReviewDiffPreviewInput,
15 type ReviewDiffPreviewResult,
16} from "@t3tools/contracts";
17​
18import * as ServerConfig from "../config.ts";
19import * as GitVcsDriver from "../vcs/GitVcsDriver.ts";
20import * as VcsDriverRegistry from "../vcs/VcsDriverRegistry.ts";
21​
22export class ReviewService extends Context.Service<
23 ReviewService,
24 {
25 readonly getDiffPreview: (
26 input: ReviewDiffPreviewInput,
27 ) => Effect.Effect<ReviewDiffPreviewResult, ReviewDiffPreviewError>;
28 readonly getDiffFileContents: (
29 input: ReviewDiffFileContentsInput,
30 ) => Effect.Effect<ReviewDiffFileContentsResult, ReviewDiffPreviewError>;
31 }
32>()("t3/review/ReviewService") {}
33​
34export const make = Effect.gen(function* () {
35 const config = yield* ServerConfig.ServerConfig;
36 const fileSystem = yield* FileSystem.FileSystem;
37 const path = yield* Path.Path;
38 const vcsRegistry = yield* VcsDriverRegistry.VcsDriverRegistry;
39 const git = yield* GitVcsDriver.GitVcsDriver;
40​
41 const canonicalizePath = (value: string) => {
42 const resolvedPath = path.resolve(value);
43 return fileSystem.realPath(resolvedPath).pipe(
44 Effect.catchTags({
45 PlatformError: (cause) =>
46 cause.reason._tag === "NotFound"
47 ? Effect.succeed(resolvedPath)
48 : Effect.fail(
49 new VcsRepositoryDetectionError({
50 operation: "ReviewService.assertWorkspaceBoundCwd.canonicalizePath",
51 cwd: resolvedPath,
52 detail: "Failed to resolve a path while validating the review workspace.",
53 cause,
54 }),
55 ),
56 }),
57 );
58 };
59​
60 const isWithinRoot = (candidate: string, root: string) => {
61 const relative = path.relative(root, candidate);
62 return relative === "" || (!relative.startsWith("..") && !path.isAbsolute(relative));
63 };
64​
65 const assertWorkspaceBoundCwd = Effect.fn("ReviewService.assertWorkspaceBoundCwd")(function* (
66 operation: "ReviewService.getDiffPreview" | "ReviewService.getDiffFileContents",
67 cwd: string,
68 ) {
69 const [candidate, workspaceRoot, worktreesRoot] = yield* Effect.all([
70 canonicalizePath(cwd),
71 canonicalizePath(config.cwd),
72 canonicalizePath(config.worktreesDir),
73 ]);
74​
75 if (isWithinRoot(candidate, workspaceRoot) || isWithinRoot(candidate, worktreesRoot)) {
76 return;
77 }
78​
79 return yield* new VcsRepositoryDetectionError({
80 operation,
81 cwd,
82 detail:
83 operation === "ReviewService.getDiffPreview"
84 ? "Review diff preview cwd must stay within the configured workspace root."
85 : "Review diff file contents cwd must stay within the configured workspace root.",
86 });
87 });
88​
89 const getDiffPreview: ReviewService["Service"]["getDiffPreview"] = Effect.fn(
90 "ReviewService.getDiffPreview",
91 )(function* (input) {
92 yield* assertWorkspaceBoundCwd("ReviewService.getDiffPreview", input.cwd);
93​
94 const handle = yield* vcsRegistry.detect({ cwd: input.cwd, requestedKind: "auto" });
95 if (!handle) {
96 return {
97 cwd: input.cwd,
98 generatedAt: yield* DateTime.now,
99 sources: [],
100 };
101 }
102​
103 const getDriverDiffPreview = handle.driver.getDiffPreview;
104 if (!getDriverDiffPreview) {
105 if (handle.kind === "git") {
106 return yield* git.getReviewDiffPreview(input);
107 }
108 return yield* new VcsUnsupportedOperationError({
109 operation: "ReviewService.getDiffPreview",
110 kind: handle.kind,
111 detail: `The ${handle.kind} VCS driver does not support review diff previews.`,
112 });
113 }
114​
115 return yield* getDriverDiffPreview(input);
116 });
117​
118 const getDiffFileContents: ReviewService["Service"]["getDiffFileContents"] = Effect.fn(
119 "ReviewService.getDiffFileContents",
120 )(function* (input) {
121 yield* assertWorkspaceBoundCwd("ReviewService.getDiffFileContents", input.cwd);
122​
123 const handle = yield* vcsRegistry.detect({ cwd: input.cwd, requestedKind: "auto" });
124 if (handle?.kind !== "git") {
125 return yield* new VcsUnsupportedOperationError({
126 operation: "ReviewService.getDiffFileContents",
127 kind: handle?.kind ?? "unknown",
128 detail: "Unchanged diff expansion currently requires a Git repository.",
129 });
130 }
131​
132 return yield* git.getReviewDiffFileContents(input);
133 });
134​
135 return ReviewService.of({
136 getDiffPreview,
137 getDiffFileContents,
138 });
139});
140​
141export const layer = Layer.effect(ReviewService, make);