Open History to see the last commit
1import type {
2 SourceControlProviderAuth,
3 SourceControlProviderDiscoveryItem,
4 SourceControlProviderKind,
5} from "@t3tools/contracts";
6import * as Effect from "effect/Effect";
7import * as Option from "effect/Option";
8
9import type * as VcsProcess from "../vcs/VcsProcess.ts";
10
11export interface SourceControlAuthProbeInput {
12 readonly stdout: string;
13 readonly stderr: string;
14 readonly exitCode: VcsProcess.VcsProcessOutput["exitCode"];
15}
16
17interface SourceControlDiscoverySpecBase {
18 readonly kind: SourceControlProviderKind;
19 readonly label: string;
20 readonly installHint: string;
21}
22
23export type SourceControlCliDiscoverySpec = SourceControlDiscoverySpecBase & {
24 readonly type: "cli";
25 readonly executable: string;
26 readonly versionArgs: ReadonlyArray<string>;
27 readonly authArgs: ReadonlyArray<string>;
28 readonly parseAuth: (input: SourceControlAuthProbeInput) => SourceControlProviderAuth;
29};
30
31export type SourceControlApiDiscoverySpec = SourceControlDiscoverySpecBase & {
32 readonly type: "api";
33 readonly probeAuth: Effect.Effect<SourceControlProviderAuth, never>;
34};
35
36export type SourceControlProviderDiscoverySpec =
37 | SourceControlCliDiscoverySpec
38 | SourceControlApiDiscoverySpec;
39
40interface DiscoveryProbeResult {
41 readonly kind: SourceControlProviderKind;
42 readonly label: string;
43 readonly executable: string;
44 readonly status: "available" | "missing";
45 readonly version: Option.Option<string>;
46 readonly installHint: string;
47 readonly detail: Option.Option<string>;
48}
49
50export function firstNonEmptyLine(text: string): Option.Option<string> {
51 const line = text
52 .split(/\r?\n/)
53 .map((entry) => entry.trim())
54 .find((entry) => entry.length > 0);
55 return line === undefined ? Option.none() : Option.some(line);
56}
57
58export function detailFromCause(cause: unknown): Option.Option<string> {
59 if (cause instanceof Error && cause.message.trim().length > 0) {
60 return Option.some(cause.message.trim());
61 }
62 return Option.none();
63}
64
65function authAccount(account: string | undefined): Option.Option<string> {
66 const trimmed = account?.trim();
67 return trimmed === undefined || trimmed.length === 0 ? Option.none() : Option.some(trimmed);
68}
69
70function authHost(host: string | undefined): Option.Option<string> {
71 const trimmed = host?.trim();
72 return trimmed === undefined || trimmed.length === 0 ? Option.none() : Option.some(trimmed);
73}
74
75function authDetail(detail: string | undefined): Option.Option<string> {
76 const trimmed = detail?.trim();
77 return trimmed === undefined || trimmed.length === 0 ? Option.none() : Option.some(trimmed);
78}
79
80export function providerAuth(input: {
81 readonly status: SourceControlProviderAuth["status"];
82 readonly account?: string | undefined;
83 readonly host?: string | undefined;
84 readonly detail?: string | undefined;
85}): SourceControlProviderAuth {
86 return {
87 status: input.status,
88 account: authAccount(input.account),
89 host: authHost(input.host),
90 detail: authDetail(input.detail),
91 };
92}
93
94export function unknownAuth(detail?: string): SourceControlProviderAuth {
95 return providerAuth({ status: "unknown", detail });
96}
97
98export function combinedAuthOutput(input: SourceControlAuthProbeInput): string {
99 return [input.stdout, input.stderr].filter((entry) => entry.trim().length > 0).join("\n");
100}
101
102function sanitizedAuthLines(text: string): ReadonlyArray<string> {
103 return text
104 .split(/\r?\n/)
105 .map((entry) => entry.trim())
106 .filter((entry) => entry.length > 0)
107 .filter((entry) => !/^[-\s]*token(?:\s+scopes?)?:/iu.test(entry));
108}
109
110export function firstSafeAuthLine(text: string): string | undefined {
111 return sanitizedAuthLines(text)[0];
112}
113
114export function parseCliHost(text: string): string | undefined {
115 return sanitizedAuthLines(text)
116 .map((line) => line.replace(/^[^a-z0-9]+/iu, ""))
117 .find((line) => /^[a-z0-9][a-z0-9.-]*(?::\d+)?$/iu.test(line));
118}
119
120export function matchFirst(text: string, patterns: ReadonlyArray<RegExp>): string | undefined {
121 for (const pattern of patterns) {
122 const match = pattern.exec(text);
123 const value = match?.[1]?.trim();
124 if (value && value.length > 0) return value;
125 }
126 return undefined;
127}
128
129function probeCli(input: {
130 readonly spec: SourceControlCliDiscoverySpec;
131 readonly process: VcsProcess.VcsProcessShape;
132 readonly cwd: string;
133}): Effect.Effect<DiscoveryProbeResult> {
134 return input.process
135 .run({
136 operation: "source-control.discovery.probe",
137 command: input.spec.executable,
138 args: input.spec.versionArgs,
139 cwd: input.cwd,
140 timeoutMs: 5_000,
141 maxOutputBytes: 8_000,
142 appendTruncationMarker: true,
143 })
144 .pipe(
145 Effect.map(
146 (result) =>
147 ({
148 kind: input.spec.kind,
149 label: input.spec.label,
150 executable: input.spec.executable,
151 status: "available" as const,
152 version: Option.orElse(firstNonEmptyLine(result.stdout), () =>
153 firstNonEmptyLine(result.stderr),
154 ),
155 installHint: input.spec.installHint,
156 detail: Option.none<string>(),
157 }) satisfies DiscoveryProbeResult,
158 ),
159 Effect.catch((cause) =>
160 Effect.succeed({
161 kind: input.spec.kind,
162 label: input.spec.label,
163 executable: input.spec.executable,
164 status: "missing" as const,
165 version: Option.none<string>(),
166 installHint: input.spec.installHint,
167 detail: detailFromCause(cause),
168 } satisfies DiscoveryProbeResult),
169 ),
170 );
171}
172
173export function probeSourceControlProvider(input: {
174 readonly spec: SourceControlProviderDiscoverySpec;
175 readonly process: VcsProcess.VcsProcessShape;
176 readonly cwd: string;
177}): Effect.Effect<SourceControlProviderDiscoveryItem> {
178 if (input.spec.type === "api") {
179 return input.spec.probeAuth.pipe(
180 Effect.map(
181 (auth) =>
182 ({
183 kind: input.spec.kind,
184 label: input.spec.label,
185 status: "available" as const,
186 version: Option.none<string>(),
187 installHint: input.spec.installHint,
188 detail: Option.none<string>(),
189 auth,
190 }) satisfies SourceControlProviderDiscoveryItem,
191 ),
192 );
193 }
194
195 const spec = input.spec;
196
197 return probeCli({
198 spec,
199 process: input.process,
200 cwd: input.cwd,
201 }).pipe(
202 Effect.flatMap((item) => {
203 if (item.status !== "available") {
204 return Effect.succeed({
205 ...item,
206 auth: unknownAuth("Hosting integration command was not found on the server PATH."),
207 } satisfies SourceControlProviderDiscoveryItem);
208 }
209
210 return input.process
211 .run({
212 operation: "source-control.discovery.auth",
213 command: spec.executable,
214 args: spec.authArgs,
215 cwd: input.cwd,
216 allowNonZeroExit: true,
217 timeoutMs: 5_000,
218 maxOutputBytes: 8_000,
219 appendTruncationMarker: true,
220 })
221 .pipe(
222 Effect.map(
223 (result) =>
224 ({
225 ...item,
226 auth: spec.parseAuth(result),
227 }) satisfies SourceControlProviderDiscoveryItem,
228 ),
229 Effect.catch((cause) =>
230 Effect.succeed({
231 ...item,
232 auth: unknownAuth(Option.getOrUndefined(detailFromCause(cause))),
233 } satisfies SourceControlProviderDiscoveryItem),
234 ),
235 );
236 }),
237 );
238}