From 506e0fc049af02ab9d856ea968846e0eca80cfdf Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Fri, 1 May 2026 15:51:10 -0700 Subject: [PATCH] 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 --- apps/server/src/git/Layers/GitManager.test.ts | 15 ++- .../SourceControlProviderRegistry.test.ts | 82 +++++++++++++ .../SourceControlProviderRegistry.ts | 108 ++++++++++++++++-- apps/web/src/environmentApi.ts | 9 -- .../environments/runtime/connection.test.ts | 9 -- packages/contracts/src/git.ts | 7 +- packages/contracts/src/ipc.ts | 24 ---- packages/shared/src/git.ts | 12 ++ 8 files changed, 214 insertions(+), 52 deletions(-) create mode 100644 apps/server/src/sourceControl/SourceControlProviderRegistry.test.ts diff --git a/apps/server/src/git/Layers/GitManager.test.ts b/apps/server/src/git/Layers/GitManager.test.ts index 1b5db1c1bd..feabb8c2ea 100644 --- a/apps/server/src/git/Layers/GitManager.test.ts +++ b/apps/server/src/git/Layers/GitManager.test.ts @@ -23,6 +23,7 @@ import { import { type TextGenerationShape, TextGeneration } from "../Services/TextGeneration.ts"; import * as GitVcsDriver from "../../vcs/GitVcsDriver.ts"; import * as VcsProcess from "../../vcs/VcsProcess.ts"; +import * as GitHubSourceControlProvider from "../../sourceControl/GitHubSourceControlProvider.ts"; import * as SourceControlProviderRegistry from "../../sourceControl/SourceControlProviderRegistry.ts"; import { makeGitManager } from "./GitManager.ts"; import { ServerConfig } from "../../config.ts"; @@ -650,9 +651,19 @@ function makeManager(input?: { Layer.provideMerge(NodeServices.layer), Layer.provideMerge(ServerConfigLayer), ); + const sourceControlRegistryLayer = Layer.effect( + SourceControlProviderRegistry.SourceControlProviderRegistry, + GitHubSourceControlProvider.make().pipe( + Effect.map((provider) => + SourceControlProviderRegistry.SourceControlProviderRegistry.of({ + resolve: () => Effect.succeed(provider), + }), + ), + Effect.provide(Layer.succeed(GitHubCli, gitHubCli)), + ), + ); const managerLayer = Layer.mergeAll( - SourceControlProviderRegistry.layer.pipe(Layer.provide(Layer.succeed(GitHubCli, gitHubCli))), Layer.succeed(TextGeneration, textGeneration), Layer.succeed( ProjectSetupScriptRunner, @@ -662,7 +673,7 @@ function makeManager(input?: { ), vcsDriverLayer, serverSettingsLayer, - ).pipe(Layer.provideMerge(NodeServices.layer)); + ).pipe(Layer.provideMerge(sourceControlRegistryLayer), Layer.provideMerge(NodeServices.layer)); return makeGitManager().pipe( Effect.provide(managerLayer), diff --git a/apps/server/src/sourceControl/SourceControlProviderRegistry.test.ts b/apps/server/src/sourceControl/SourceControlProviderRegistry.test.ts new file mode 100644 index 0000000000..222c202286 --- /dev/null +++ b/apps/server/src/sourceControl/SourceControlProviderRegistry.test.ts @@ -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: "git@github.com: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: "git@gitlab.com: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"); + }), +); diff --git a/apps/server/src/sourceControl/SourceControlProviderRegistry.ts b/apps/server/src/sourceControl/SourceControlProviderRegistry.ts index 73488a3b62..d54d887814 100644 --- a/apps/server/src/sourceControl/SourceControlProviderRegistry.ts +++ b/apps/server/src/sourceControl/SourceControlProviderRegistry.ts @@ -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> = { + 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()); diff --git a/apps/web/src/environmentApi.ts b/apps/web/src/environmentApi.ts index 2bb951ad7a..9f38cfadf6 100644 --- a/apps/web/src/environmentApi.ts +++ b/apps/web/src/environmentApi.ts @@ -35,15 +35,6 @@ export function createEnvironmentApi(rpcClient: WsRpcClient): EnvironmentApi { init: rpcClient.vcs.init, }, git: { - pull: rpcClient.vcs.pull, - refreshStatus: rpcClient.vcs.refreshStatus, - onStatus: (input, callback, options) => rpcClient.vcs.onStatus(input, callback, options), - listBranches: rpcClient.vcs.listBranches, - createWorktree: rpcClient.vcs.createWorktree, - removeWorktree: rpcClient.vcs.removeWorktree, - createBranch: rpcClient.vcs.createBranch, - checkout: rpcClient.vcs.checkout, - init: rpcClient.vcs.init, resolvePullRequest: rpcClient.git.resolvePullRequest, preparePullRequestThread: rpcClient.git.preparePullRequestThread, }, diff --git a/apps/web/src/environments/runtime/connection.test.ts b/apps/web/src/environments/runtime/connection.test.ts index 57f2324145..64ff7a1c3c 100644 --- a/apps/web/src/environments/runtime/connection.test.ts +++ b/apps/web/src/environments/runtime/connection.test.ts @@ -85,16 +85,7 @@ function createTestClient() { openInEditor: vi.fn(async () => undefined), }, git: { - pull: vi.fn(async () => undefined), - refreshStatus: vi.fn(async () => undefined), - onStatus: vi.fn(() => () => undefined), runStackedAction: vi.fn(async () => ({}) as any), - listBranches: vi.fn(async () => []), - createWorktree: vi.fn(async () => undefined), - removeWorktree: vi.fn(async () => undefined), - createBranch: vi.fn(async () => undefined), - checkout: vi.fn(async () => undefined), - init: vi.fn(async () => undefined), resolvePullRequest: vi.fn(async () => undefined), preparePullRequestThread: vi.fn(async () => undefined), }, diff --git a/packages/contracts/src/git.ts b/packages/contracts/src/git.ts index 6a6e7cbe64..837549a235 100644 --- a/packages/contracts/src/git.ts +++ b/packages/contracts/src/git.ts @@ -45,7 +45,12 @@ const GitStatusPrState = Schema.Literals(["open", "closed", "merged"]); const GitPullRequestReference = TrimmedNonEmptyStringSchema; const GitPullRequestState = Schema.Literals(["open", "closed", "merged"]); const GitPreparePullRequestThreadMode = Schema.Literals(["local", "worktree"]); -export const GitHostingProviderKind = Schema.Literals(["github", "gitlab", "unknown"]); +export const GitHostingProviderKind = Schema.Literals([ + "github", + "gitlab", + "azure-devops", + "unknown", +]); export type GitHostingProviderKind = typeof GitHostingProviderKind.Type; export const GitHostingProvider = Schema.Struct({ kind: GitHostingProviderKind, diff --git a/packages/contracts/src/ipc.ts b/packages/contracts/src/ipc.ts index 4950bc5f74..48181c7c46 100644 --- a/packages/contracts/src/ipc.ts +++ b/packages/contracts/src/ipc.ts @@ -274,34 +274,10 @@ export interface EnvironmentApi { ) => () => void; }; git: { - /** @deprecated Use `EnvironmentApi.vcs.listBranches` for local VCS branch/ref listing. */ - listBranches: (input: GitListBranchesInput) => Promise; - /** @deprecated Use `EnvironmentApi.vcs.createWorktree` for local VCS workspace creation. */ - createWorktree: (input: GitCreateWorktreeInput) => Promise; - /** @deprecated Use `EnvironmentApi.vcs.removeWorktree` for local VCS workspace removal. */ - removeWorktree: (input: GitRemoveWorktreeInput) => Promise; - /** @deprecated Use `EnvironmentApi.vcs.createBranch` for local VCS branch/ref creation. */ - createBranch: (input: GitCreateBranchInput) => Promise; - /** @deprecated Use `EnvironmentApi.vcs.checkout` for local VCS checkout/switch operations. */ - checkout: (input: GitCheckoutInput) => Promise; - /** @deprecated Use `EnvironmentApi.vcs.init` for local VCS repository initialization. */ - init: (input: GitInitInput) => Promise; resolvePullRequest: (input: GitPullRequestRefInput) => Promise; preparePullRequestThread: ( input: GitPreparePullRequestThreadInput, ) => Promise; - /** @deprecated Use `EnvironmentApi.vcs.pull` for local VCS pull/sync operations. */ - pull: (input: GitPullInput) => Promise; - /** @deprecated Use `EnvironmentApi.vcs.refreshStatus` for local VCS status refreshes. */ - refreshStatus: (input: GitStatusInput) => Promise; - /** @deprecated Use `EnvironmentApi.vcs.onStatus` for local VCS status subscriptions. */ - onStatus: ( - input: GitStatusInput, - callback: (status: GitStatusResult) => void, - options?: { - onResubscribe?: () => void; - }, - ) => () => void; }; orchestration: { dispatchCommand: (command: ClientOrchestrationCommand) => Promise<{ sequence: number }>; diff --git a/packages/shared/src/git.ts b/packages/shared/src/git.ts index 4570ba9ecb..c60b49e43f 100644 --- a/packages/shared/src/git.ts +++ b/packages/shared/src/git.ts @@ -225,6 +225,10 @@ function isGitLabHost(host: string): boolean { return host === "gitlab.com" || host.includes("gitlab"); } +function isAzureDevOpsHost(host: string): boolean { + return host === "dev.azure.com" || host.endsWith(".visualstudio.com"); +} + export function detectGitHostingProviderFromRemoteUrl( remoteUrl: string, ): GitHostingProvider | null { @@ -249,6 +253,14 @@ export function detectGitHostingProviderFromRemoteUrl( }; } + if (isAzureDevOpsHost(host)) { + return { + kind: "azure-devops", + name: "Azure DevOps", + baseUrl: toBaseUrl(host), + }; + } + return { kind: "unknown", name: host,