Open History to see the last commit
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 ReviewDiffPreviewError,
12 type ReviewDiffPreviewInput,
13 type ReviewDiffPreviewResult,
14} from "@v12/contracts";
15
16import * as ServerConfig from "../config.ts";
17import * as GitVcsDriver from "../vcs/GitVcsDriver.ts";
18import * as VcsDriverRegistry from "../vcs/VcsDriverRegistry.ts";
19
20export class ReviewService extends Context.Service<
21 ReviewService,
22 {
23 readonly getDiffPreview: (
24 input: ReviewDiffPreviewInput,
25 ) => Effect.Effect<ReviewDiffPreviewResult, ReviewDiffPreviewError>;
26 }
27>()("v12/review/ReviewService") {}
28
29export const make = Effect.gen(function* () {
30 const config = yield* ServerConfig.ServerConfig;
31 const fileSystem = yield* FileSystem.FileSystem;
32 const path = yield* Path.Path;
33 const vcsRegistry = yield* VcsDriverRegistry.VcsDriverRegistry;
34 const git = yield* GitVcsDriver.GitVcsDriver;
35
36 const canonicalizePath = (value: string) => {
37 const resolvedPath = path.resolve(value);
38 return fileSystem.realPath(resolvedPath).pipe(
39 Effect.catchTags({
40 PlatformError: (cause) =>
41 cause.reason._tag === "NotFound"
42 ? Effect.succeed(resolvedPath)
43 : Effect.fail(
44 new VcsRepositoryDetectionError({
45 operation: "ReviewService.assertWorkspaceBoundCwd.canonicalizePath",
46 cwd: resolvedPath,
47 detail: "Failed to resolve a path while validating the review workspace.",
48 cause,
49 }),
50 ),
51 }),
52 );
53 };
54
55 const isWithinRoot = (candidate: string, root: string) => {
56 const relative = path.relative(root, candidate);
57 return relative === "" || (!relative.startsWith("..") && !path.isAbsolute(relative));
58 };
59
60 const assertWorkspaceBoundCwd = Effect.fn("ReviewService.assertWorkspaceBoundCwd")(function* (
61 cwd: string,
62 ) {
63 const [candidate, workspaceRoot, worktreesRoot] = yield* Effect.all([
64 canonicalizePath(cwd),
65 canonicalizePath(config.cwd),
66 canonicalizePath(config.worktreesDir),
67 ]);
68
69 if (isWithinRoot(candidate, workspaceRoot) || isWithinRoot(candidate, worktreesRoot)) {
70 return;
71 }
72
73 return yield* new VcsRepositoryDetectionError({
74 operation: "ReviewService.getDiffPreview",
75 cwd,
76 detail: "Review diff preview cwd must stay within the configured workspace root.",
77 });
78 });
79
80 const getDiffPreview: ReviewService["Service"]["getDiffPreview"] = Effect.fn(
81 "ReviewService.getDiffPreview",
82 )(function* (input) {
83 yield* assertWorkspaceBoundCwd(input.cwd);
84
85 const handle = yield* vcsRegistry.detect({ cwd: input.cwd, requestedKind: "auto" });
86 if (!handle) {
87 return {
88 cwd: input.cwd,
89 generatedAt: yield* DateTime.now,
90 sources: [],
91 };
92 }
93
94 const getDriverDiffPreview = handle.driver.getDiffPreview;
95 if (!getDriverDiffPreview) {
96 if (handle.kind === "git") {
97 return yield* git.getReviewDiffPreview(input);
98 }
99 return yield* new VcsUnsupportedOperationError({
100 operation: "ReviewService.getDiffPreview",
101 kind: handle.kind,
102 detail: `The ${handle.kind} VCS driver does not support review diff previews.`,
103 });
104 }
105
106 return yield* getDriverDiffPreview(input);
107 });
108
109 return ReviewService.of({
110 getDiffPreview,
111 });
112});
113
114export const layer = Layer.effect(ReviewService, make);