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

ReviewService.ts

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