Add pluggable source control provider detection

- Detect GitHub, GitLab, and Azure DevOps remotes in the shared parser
- Cache provider resolution and return unsupported stubs for unregistered kinds
- Remove deprecated git RPCs from the web IPC surface
This commit is contained in:
Julius Marminge authored and Julius Marminge committed 2026-05-02 14:22:14 -07:00
1 parent 0d4e8f75a9
commit eb711cc7af
8 files changed
+214 -52

No files matched your search

@@ -0,0 +1,82 @@
import { assert, it } from "@effect/vitest";
import { Effect, Layer } from "effect";
import { GitHubCli } from "../git/Services/GitHubCli.ts";
import * as GitVcsDriver from "../vcs/GitVcsDriver.ts";
import * as SourceControlProviderRegistry from "./SourceControlProviderRegistry.ts";
const processResult = (stdout: string) => ({
stdout,
stderr: "",
code: 0,
signal: null,
timedOut: false,
stdoutTruncated: false,
stderrTruncated: false,
});
function makeRegistry(input: {
readonly originUrl?: string | null;
readonly remoteVerboseOutput?: string;
}) {
const gitLayer = Layer.mock(GitVcsDriver.GitVcsDriver)({
readConfigValue: (_cwd, key) =>
key === "remote.origin.url" ? Effect.succeed(input.originUrl ?? null) : Effect.succeed(null),
execute: () => Effect.succeed(processResult(input.remoteVerboseOutput ?? "")),
});
return SourceControlProviderRegistry.make().pipe(
Effect.provide(Layer.mergeAll(gitLayer, Layer.mock(GitHubCli)({}))),
);
}
it.effect("routes GitHub remotes to the GitHub provider", () =>
Effect.gen(function* () {
const registry = yield* makeRegistry({
originUrl: "[email protected]:pingdotgg/t3code.git",
});
const provider = yield* registry.resolve({ cwd: "/repo" });
assert.strictEqual(provider.kind, "github");
}),
);
it.effect(
"detects GitLab remotes and returns an unsupported provider until one is registered",
() =>
Effect.gen(function* () {
const registry = yield* makeRegistry({
originUrl: "[email protected]:group/project.git",
});
const provider = yield* registry.resolve({ cwd: "/repo" });
assert.strictEqual(provider.kind, "gitlab");
const error = yield* Effect.flip(
provider.listChangeRequests({
cwd: "/repo",
headSelector: "feature/source-control",
state: "open",
}),
);
assert.strictEqual(error.provider, "gitlab");
}),
);
it.effect("falls back to remote verbose output when origin is not configured", () =>
Effect.gen(function* () {
const registry = yield* makeRegistry({
originUrl: null,
remoteVerboseOutput: [
"upstream\thttps://dev.azure.com/acme/project/_git/repo (fetch)",
"upstream\thttps://dev.azure.com/acme/project/_git/repo (push)",
].join("\n"),
});
const provider = yield* registry.resolve({ cwd: "/repo" });
assert.strictEqual(provider.kind, "azure-devops");
}),
);
@@ -1,8 +1,14 @@
import { Context, Effect, Layer } from "effect";
import type { SourceControlProviderError } from "@t3tools/contracts";
import { Cache, Context, Duration, Effect, Exit, Layer } from "effect";
import { SourceControlProviderError } from "@t3tools/contracts";
import type { SourceControlProviderKind } from "@t3tools/contracts";
import { detectGitHostingProviderFromRemoteUrl } from "@t3tools/shared/git";
import { SourceControlProvider, type SourceControlProviderShape } from "./SourceControlProvider.ts";
import * as GitHubSourceControlProvider from "./GitHubSourceControlProvider.ts";
import * as GitVcsDriver from "../vcs/GitVcsDriver.ts";
const PROVIDER_DETECTION_CACHE_CAPACITY = 2_048;
const PROVIDER_DETECTION_CACHE_TTL = Duration.seconds(5);
export interface SourceControlProviderRegistryShape {
readonly resolve: (input: {
@@ -15,14 +21,102 @@ export class SourceControlProviderRegistry extends Context.Service<
SourceControlProviderRegistryShape
>()("t3/source-control/SourceControlProviderRegistry") {}
function unsupportedProvider(kind: SourceControlProviderKind): SourceControlProviderShape {
const unsupported = (operation: string) =>
Effect.fail(
new SourceControlProviderError({
provider: kind,
operation,
detail: `No ${kind} source control provider is registered.`,
}),
);
return SourceControlProvider.of({
kind,
listChangeRequests: () => unsupported("listChangeRequests"),
getChangeRequest: () => unsupported("getChangeRequest"),
createChangeRequest: () => unsupported("createChangeRequest"),
getRepositoryCloneUrls: () => unsupported("getRepositoryCloneUrls"),
getDefaultBranch: () => unsupported("getDefaultBranch"),
checkoutChangeRequest: () => unsupported("checkoutChangeRequest"),
});
}
function providerDetectionError(operation: string, cwd: string, cause: unknown) {
return new SourceControlProviderError({
provider: "unknown",
operation,
detail: `Failed to detect source control provider for ${cwd}.`,
cause,
});
}
function firstRemoteUrlFromVerboseOutput(output: string): string | null {
for (const line of output.split("\n")) {
const trimmed = line.trim();
if (trimmed.length === 0) {
continue;
}
const match = /^\S+\s+(\S+)\s+\((?:fetch|push)\)$/.exec(trimmed);
const remoteUrl = match?.[1]?.trim() ?? "";
if (remoteUrl.length > 0) {
return remoteUrl;
}
}
return null;
}
export const make = Effect.fn("makeSourceControlProviderRegistry")(function* () {
const github = yield* SourceControlProvider;
const github = yield* GitHubSourceControlProvider.make();
const git = yield* GitVcsDriver.GitVcsDriver;
const providers: Partial<Record<SourceControlProviderKind, SourceControlProviderShape>> = {
github,
};
const detectProviderKind = Effect.fn("SourceControlProviderRegistry.detectProviderKind")(
function* (cwd: string) {
const originUrl = yield* git
.readConfigValue(cwd, "remote.origin.url")
.pipe(Effect.catch(() => Effect.succeed(null)));
const remoteUrl =
originUrl ??
(yield* git
.execute({
operation: "SourceControlProviderRegistry.detectProvider.remoteVerbose",
cwd,
args: ["remote", "-v"],
allowNonZeroExit: true,
})
.pipe(
Effect.map((result) =>
result.code === 0 ? firstRemoteUrlFromVerboseOutput(result.stdout) : null,
),
Effect.mapError((error) => providerDetectionError("detectProvider", cwd, error)),
));
if (!remoteUrl) {
return "unknown" as const;
}
return detectGitHostingProviderFromRemoteUrl(remoteUrl)?.kind ?? "unknown";
},
);
const providerKindCache = yield* Cache.makeWith<
string,
SourceControlProviderKind,
SourceControlProviderError
>(detectProviderKind, {
capacity: PROVIDER_DETECTION_CACHE_CAPACITY,
timeToLive: (exit) => (Exit.isSuccess(exit) ? PROVIDER_DETECTION_CACHE_TTL : Duration.zero),
});
return SourceControlProviderRegistry.of({
resolve: () => Effect.succeed(github),
resolve: (input) =>
Cache.get(providerKindCache, input.cwd).pipe(
Effect.map((kind) => providers[kind] ?? unsupportedProvider(kind)),
),
});
});
export const layer = Layer.effect(SourceControlProviderRegistry, make()).pipe(
Layer.provide(GitHubSourceControlProvider.layer),
);
export const layer = Layer.effect(SourceControlProviderRegistry, make());