Open History to see the last commit
1import * as Effect from "effect/Effect";
2import * as Option from "effect/Option";
3import * as Path from "effect/Path";
4
5import type { ProjectId } from "@t3tools/contracts";
6import {
7 hasProjectSettingsOverrides,
8 resolveProjectSettings,
9} from "@t3tools/shared/projectSettings";
10
11import * as ProjectStore from "../orchestration-v2/ProjectStore.ts";
12import * as ServerSettings from "../serverSettings.ts";
13import * as GitVcsDriver from "../vcs/GitVcsDriver.ts";
14import { GitHubAccount } from "./GitHubApi.ts";
15
16/**
17 * Runs GitHub work as the project's own `gh` login (its `githubAccount` setting) for paths that
18 * only know a working directory. A worktree belongs to the project whose checkout shares its Git
19 * directory. Without any project overrides this answers at once and runs nothing.
20 */
21export const makeGitHubProjectAccount = Effect.gen(function* () {
22 const serverSettings = yield* ServerSettings.ServerSettingsService;
23 const projects = yield* ProjectStore.ProjectStoreV2;
24 const git = yield* GitVcsDriver.GitVcsDriver;
25 const path = yield* Path.Path;
26 // A worktree's main checkout never changes, so it is asked of Git once.
27 const mainCheckouts = new Map<string, string>();
28
29 const projectAt = (workspaceRoot: string) =>
30 projects.findActiveByWorkspaceRoot(workspaceRoot).pipe(
31 Effect.map((project) => Option.getOrNull(project)?.projectId ?? null),
32 Effect.orElseSucceed(() => null),
33 );
34
35 const mainCheckoutOf = (cwd: string) => {
36 const held = mainCheckouts.get(cwd);
37 if (held !== undefined) return Effect.succeed(Option.some(held));
38 return git
39 .execute({
40 operation: "GitHubProjectAccount.commonDir",
41 cwd,
42 args: ["rev-parse", "--path-format=absolute", "--git-common-dir"],
43 timeoutMs: 5_000,
44 })
45 .pipe(
46 Effect.map((result) => path.dirname(result.stdout.trim())),
47 Effect.tap((root) => Effect.sync(() => mainCheckouts.set(cwd, root))),
48 Effect.option,
49 );
50 };
51
52 const projectOf = Effect.fnUntraced(function* (cwd: string) {
53 const direct = yield* projectAt(cwd);
54 if (direct !== null) return direct;
55 const main = yield* mainCheckoutOf(cwd);
56 return Option.isNone(main) ? null : yield* projectAt(main.value);
57 });
58
59 /** The login the project at `cwd` (or `projectId`, when the caller knows it) chooses, or null. */
60 const accountFor = Effect.fnUntraced(function* (input: {
61 readonly cwd: string;
62 readonly projectId?: ProjectId | null | undefined;
63 }) {
64 const settings = yield* serverSettings.getSettings.pipe(Effect.orElseSucceed(() => null));
65 if (settings === null || !hasProjectSettingsOverrides(settings)) return null;
66 const projectId = input.projectId ?? (yield* projectOf(input.cwd));
67 return projectId === null
68 ? null
69 : resolveProjectSettings(settings, projectId).settings.githubAccount;
70 });
71
72 const actAs = <A, E, R>(
73 input: { readonly cwd: string; readonly projectId?: ProjectId | null | undefined },
74 effect: Effect.Effect<A, E, R>,
75 ) =>
76 accountFor(input).pipe(
77 Effect.flatMap((account) =>
78 account === null ? effect : effect.pipe(Effect.provideService(GitHubAccount, account)),
79 ),
80 );
81
82 return { accountFor, actAs };
83});