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

GitWorkflowService.ts

316 lines10.0 KB

1import * as Context from "effect/Context";
2import * as Effect from "effect/Effect";
3import * as Layer from "effect/Layer";
4​
5import {
6 GitManagerError,
7 GitCommandError,
8 type VcsSwitchRefInput,
9 type VcsSwitchRefResult,
10 type VcsCreateRefInput,
11 type VcsCreateRefResult,
12 type VcsCreateWorktreeInput,
13 type VcsCreateWorktreeResult,
14 type VcsListRefsInput,
15 type VcsListRefsResult,
16 type GitManagerServiceError,
17 type GitPreparePullRequestThreadInput,
18 type GitPreparePullRequestThreadResult,
19 type GitPullRequestRefInput,
20 type VcsPullResult,
21 type VcsRemoveWorktreeInput,
22 type GitResolvePullRequestResult,
23 type GitRunStackedActionInput,
24 type GitRunStackedActionResult,
25 type VcsStatusInput,
26 type VcsStatusLocalResult,
27 type VcsStatusRemoteResult,
28 type VcsStatusResult,
29} from "@t3tools/contracts";
30​
31import { GitManager, type GitRunStackedActionOptions } from "./GitManager.ts";
32import { GitVcsDriver } from "../vcs/GitVcsDriver.ts";
33import { VcsDriverRegistry } from "../vcs/VcsDriverRegistry.ts";
34​
35export interface GitWorkflowServiceShape {
36 readonly status: (
37 input: VcsStatusInput,
38 ) => Effect.Effect<VcsStatusResult, GitManagerServiceError>;
39 readonly localStatus: (
40 input: VcsStatusInput,
41 ) => Effect.Effect<VcsStatusLocalResult, GitManagerServiceError>;
42 readonly remoteStatus: (
43 input: VcsStatusInput,
44 ) => Effect.Effect<VcsStatusRemoteResult | null, GitManagerServiceError>;
45 readonly invalidateLocalStatus: (cwd: string) => Effect.Effect<void, never>;
46 readonly invalidateRemoteStatus: (cwd: string) => Effect.Effect<void, never>;
47 readonly invalidateStatus: (cwd: string) => Effect.Effect<void, never>;
48 readonly pullCurrentBranch: (cwd: string) => Effect.Effect<VcsPullResult, GitCommandError>;
49 readonly runStackedAction: (
50 input: GitRunStackedActionInput,
51 options?: GitRunStackedActionOptions,
52 ) => Effect.Effect<GitRunStackedActionResult, GitManagerServiceError>;
53 readonly resolvePullRequest: (
54 input: GitPullRequestRefInput,
55 ) => Effect.Effect<GitResolvePullRequestResult, GitManagerServiceError>;
56 readonly preparePullRequestThread: (
57 input: GitPreparePullRequestThreadInput,
58 ) => Effect.Effect<GitPreparePullRequestThreadResult, GitManagerServiceError>;
59 readonly listRefs: (input: VcsListRefsInput) => Effect.Effect<VcsListRefsResult, GitCommandError>;
60 readonly createWorktree: (
61 input: VcsCreateWorktreeInput,
62 ) => Effect.Effect<VcsCreateWorktreeResult, GitCommandError>;
63 readonly removeWorktree: (input: VcsRemoveWorktreeInput) => Effect.Effect<void, GitCommandError>;
64 readonly createRef: (
65 input: VcsCreateRefInput,
66 ) => Effect.Effect<VcsCreateRefResult, GitCommandError>;
67 readonly switchRef: (
68 input: VcsSwitchRefInput,
69 ) => Effect.Effect<VcsSwitchRefResult, GitCommandError>;
70 readonly renameBranch: (input: {
71 readonly cwd: string;
72 readonly oldBranch: string;
73 readonly newBranch: string;
74 }) => Effect.Effect<{ readonly branch: string }, GitManagerServiceError>;
76​
77export class GitWorkflowService extends Context.Service<
78 GitWorkflowService,
79 GitWorkflowServiceShape
80>()("t3/git/GitWorkflowService") {}
81​
82const unsupportedGitWorkflow = (operation: string, cwd: string, detail: string) =>
83 new GitManagerError({
84 operation,
85 detail: `${detail} (${cwd})`,
86 });
87​
88const unsupportedGitCommand = (operation: string, cwd: string, detail: string) =>
89 new GitCommandError({
90 operation,
91 command: "vcs-route",
92 cwd,
93 detail,
94 });
95​
96function nonRepositoryLocalStatus(): VcsStatusLocalResult {
97 return {
98 isRepo: false,
99 hasPrimaryRemote: false,
100 isDefaultRef: false,
101 refName: null,
102 hasWorkingTreeChanges: false,
103 workingTree: {
104 files: [],
105 insertions: 0,
106 deletions: 0,
107 },
108 };
110​
111function nonRepositoryStatus(): VcsStatusResult {
112 return {
113 ...nonRepositoryLocalStatus(),
114 hasUpstream: false,
115 aheadCount: 0,
116 behindCount: 0,
117 aheadOfDefaultCount: 0,
118 pr: null,
119 };
121​
122function nonRepositoryListRefs(): VcsListRefsResult {
123 return {
124 refs: [],
125 isRepo: false,
126 hasPrimaryRemote: false,
127 nextCursor: null,
128 totalCount: 0,
129 };
131​
132export const make = Effect.fn("makeGitWorkflowService")(function* () {
133 const registry = yield* VcsDriverRegistry;
134 const git = yield* GitVcsDriver;
135 const gitManager = yield* GitManager;
136​
137 const ensureGit = Effect.fn("GitWorkflowService.ensureGit")(function* (
138 operation: string,
139 cwd: string,
140 ) {
141 const handle = yield* registry
142 .resolve({ cwd })
143 .pipe(
144 Effect.mapError((error) =>
145 unsupportedGitWorkflow(
146 operation,
147 cwd,
148 error instanceof Error ? error.message : String(error),
149 ),
150 ),
151 );
152 if (handle.kind !== "git") {
153 return yield* unsupportedGitWorkflow(
154 operation,
155 cwd,
156 `The ${operation} workflow currently supports Git repositories only; detected ${handle.kind}.`,
157 );
158 }
159 });
160​
161 const ensureGitCommand = Effect.fn("GitWorkflowService.ensureGitCommand")(function* (
162 operation: string,
163 cwd: string,
164 ) {
165 const handle = yield* registry
166 .resolve({ cwd })
167 .pipe(
168 Effect.mapError((error) =>
169 unsupportedGitCommand(
170 operation,
171 cwd,
172 error instanceof Error ? error.message : String(error),
173 ),
174 ),
175 );
176 if (handle.kind !== "git") {
177 return yield* unsupportedGitCommand(
178 operation,
179 cwd,
180 `The ${operation} command currently supports Git repositories only; detected ${handle.kind}.`,
181 );
182 }
183 });
184​
185 const detectGitRepositoryForStatus = Effect.fn("GitWorkflowService.detectGitRepositoryForStatus")(
186 function* (operation: string, cwd: string) {
187 const handle = yield* registry
188 .detect({ cwd })
189 .pipe(
190 Effect.mapError((error) =>
191 unsupportedGitWorkflow(
192 operation,
193 cwd,
194 error instanceof Error ? error.message : String(error),
195 ),
196 ),
197 );
198 if (!handle) {
199 return false;
200 }
201 if (handle.kind !== "git") {
202 return yield* unsupportedGitWorkflow(
203 operation,
204 cwd,
205 `The ${operation} workflow currently supports Git repositories only; detected ${handle.kind}.`,
206 );
207 }
208 return true;
209 },
210 );
211​
212 const detectGitRepositoryForCommand = Effect.fn(
213 "GitWorkflowService.detectGitRepositoryForCommand",
214 )(function* (operation: string, cwd: string) {
215 const handle = yield* registry
216 .detect({ cwd })
217 .pipe(
218 Effect.mapError((error) =>
219 unsupportedGitCommand(
220 operation,
221 cwd,
222 error instanceof Error ? error.message : String(error),
223 ),
224 ),
225 );
226 if (!handle) {
227 return false;
228 }
229 if (handle.kind !== "git") {
230 return yield* unsupportedGitCommand(
231 operation,
232 cwd,
233 `The ${operation} command currently supports Git repositories only; detected ${handle.kind}.`,
234 );
235 }
236 return true;
237 });
238​
239 const routeGitManager =
240 <Input extends { readonly cwd: string }, Output>(
241 operation: string,
242 run: (input: Input) => Effect.Effect<Output, GitManagerServiceError>,
243 ) =>
244 (input: Input) =>
245 ensureGit(operation, input.cwd).pipe(Effect.andThen(run(input)));
246​
247 return GitWorkflowService.of({
248 status: (input) =>
249 detectGitRepositoryForStatus("GitWorkflowService.status", input.cwd).pipe(
250 Effect.flatMap((isGitRepository) =>
251 isGitRepository ? gitManager.status(input) : Effect.succeed(nonRepositoryStatus()),
252 ),
253 ),
254 localStatus: (input) =>
255 detectGitRepositoryForStatus("GitWorkflowService.localStatus", input.cwd).pipe(
256 Effect.flatMap((isGitRepository) =>
257 isGitRepository
258 ? gitManager.localStatus(input)
259 : Effect.succeed(nonRepositoryLocalStatus()),
260 ),
261 ),
262 remoteStatus: (input) =>
263 detectGitRepositoryForStatus("GitWorkflowService.remoteStatus", input.cwd).pipe(
264 Effect.flatMap((isGitRepository) =>
265 isGitRepository ? gitManager.remoteStatus(input) : Effect.succeed(null),
266 ),
267 ),
268 invalidateLocalStatus: gitManager.invalidateLocalStatus,
269 invalidateRemoteStatus: gitManager.invalidateRemoteStatus,
270 invalidateStatus: gitManager.invalidateStatus,
271 pullCurrentBranch: (cwd) =>
272 ensureGitCommand("GitWorkflowService.pullCurrentBranch", cwd).pipe(
273 Effect.andThen(git.pullCurrentBranch(cwd)),
274 ),
275 runStackedAction: (input, options) =>
276 ensureGit("GitWorkflowService.runStackedAction", input.cwd).pipe(
277 Effect.andThen(gitManager.runStackedAction(input, options)),
278 ),
279 resolvePullRequest: routeGitManager(
280 "GitWorkflowService.resolvePullRequest",
281 gitManager.resolvePullRequest,
282 ),
283 preparePullRequestThread: routeGitManager(
284 "GitWorkflowService.preparePullRequestThread",
285 gitManager.preparePullRequestThread,
286 ),
287 listRefs: (input) =>
288 detectGitRepositoryForCommand("GitWorkflowService.listRefs", input.cwd).pipe(
289 Effect.flatMap((isGitRepository) =>
290 isGitRepository ? git.listRefs(input) : Effect.succeed(nonRepositoryListRefs()),
291 ),
292 ),
293 createWorktree: (input) =>
294 ensureGitCommand("GitWorkflowService.createWorktree", input.cwd).pipe(
295 Effect.andThen(git.createWorktree(input)),
296 ),
297 removeWorktree: (input) =>
298 ensureGitCommand("GitWorkflowService.removeWorktree", input.cwd).pipe(
299 Effect.andThen(git.removeWorktree(input)),
300 ),
301 createRef: (input) =>
302 ensureGitCommand("GitWorkflowService.createRef", input.cwd).pipe(
303 Effect.andThen(git.createRef(input)),
304 ),
305 switchRef: (input) =>
306 ensureGitCommand("GitWorkflowService.switchRef", input.cwd).pipe(
307 Effect.andThen(Effect.scoped(git.switchRef(input))),
308 ),
309 renameBranch: (input) =>
310 ensureGit("GitWorkflowService.renameBranch", input.cwd).pipe(
311 Effect.andThen(git.renameBranch(input)),
312 ),
313 });
314});
315​
316export const layer = Layer.effect(GitWorkflowService, make());