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