- 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
Unified · Split
| @@ -23,6 +23,7 @@ import { · 22 unmodified lines | ||
| 23 | 23 | import { type TextGenerationShape, TextGeneration } from "../Services/TextGeneration.ts"; |
| 24 | 24 | import * as GitVcsDriver from "../../vcs/GitVcsDriver.ts"; |
| 25 | 25 | import * as VcsProcess from "../../vcs/VcsProcess.ts"; |
| 26 | +import * as GitHubSourceControlProvider from "../../sourceControl/GitHubSourceControlProvider.ts"; | |
| 26 | 27 | import * as SourceControlProviderRegistry from "../../sourceControl/SourceControlProviderRegistry.ts"; |
| 27 | 28 | import { makeGitManager } from "./GitManager.ts"; |
| 28 | 29 | import { ServerConfig } from "../../config.ts"; |
| @@ -650,9 +651,19 @@ function makeManager(input?: { · 621 unmodified lines | ||
| 650 | 651 | Layer.provideMerge(NodeServices.layer), |
| 651 | 652 | Layer.provideMerge(ServerConfigLayer), |
| 652 | 653 | ); |
| 654 | + const sourceControlRegistryLayer = Layer.effect( | |
| 655 | + SourceControlProviderRegistry.SourceControlProviderRegistry, | |
| 656 | + GitHubSourceControlProvider.make().pipe( | |
| 657 | + Effect.map((provider) => | |
| 658 | + SourceControlProviderRegistry.SourceControlProviderRegistry.of({ | |
| 659 | + resolve: () => Effect.succeed(provider), | |
| 660 | + }), | |
| 661 | + ), | |
| 662 | + Effect.provide(Layer.succeed(GitHubCli, gitHubCli)), | |
| 663 | + ), | |
| 664 | + ); | |
| 653 | 665 | |
| 654 | 666 | const managerLayer = Layer.mergeAll( |
| 655 | − SourceControlProviderRegistry.layer.pipe(Layer.provide(Layer.succeed(GitHubCli, gitHubCli))), | |
| 656 | 667 | Layer.succeed(TextGeneration, textGeneration), |
| 657 | 668 | Layer.succeed( |
| 658 | 669 | ProjectSetupScriptRunner, |
| @@ -662,7 +673,7 @@ function makeManager(input?: { · 3 unmodified lines | ||
| 662 | 673 | ), |
| 663 | 674 | vcsDriverLayer, |
| 664 | 675 | serverSettingsLayer, |
| 665 | − ).pipe(Layer.provideMerge(NodeServices.layer)); | |
| 676 | + ).pipe(Layer.provideMerge(sourceControlRegistryLayer), Layer.provideMerge(NodeServices.layer)); | |
| 666 | 677 | |
| 667 | 678 | return makeGitManager().pipe( |
| 668 | 679 | Effect.provide(managerLayer), |
| @@ -0,0 +1,82 @@ | ||
| 1 | +import { assert, it } from "@effect/vitest"; | |
| 2 | +import { Effect, Layer } from "effect"; | |
| 3 | + | |
| 4 | +import { GitHubCli } from "../git/Services/GitHubCli.ts"; | |
| 5 | +import * as GitVcsDriver from "../vcs/GitVcsDriver.ts"; | |
| 6 | +import * as SourceControlProviderRegistry from "./SourceControlProviderRegistry.ts"; | |
| 7 | + | |
| 8 | +const processResult = (stdout: string) => ({ | |
| 9 | + stdout, | |
| 10 | + stderr: "", | |
| 11 | + code: 0, | |
| 12 | + signal: null, | |
| 13 | + timedOut: false, | |
| 14 | + stdoutTruncated: false, | |
| 15 | + stderrTruncated: false, | |
| 16 | +}); | |
| 17 | + | |
| 18 | +function makeRegistry(input: { | |
| 19 | + readonly originUrl?: string | null; | |
| 20 | + readonly remoteVerboseOutput?: string; | |
| 21 | +}) { | |
| 22 | + const gitLayer = Layer.mock(GitVcsDriver.GitVcsDriver)({ | |
| 23 | + readConfigValue: (_cwd, key) => | |
| 24 | + key === "remote.origin.url" ? Effect.succeed(input.originUrl ?? null) : Effect.succeed(null), | |
| 25 | + execute: () => Effect.succeed(processResult(input.remoteVerboseOutput ?? "")), | |
| 26 | + }); | |
| 27 | + | |
| 28 | + return SourceControlProviderRegistry.make().pipe( | |
| 29 | + Effect.provide(Layer.mergeAll(gitLayer, Layer.mock(GitHubCli)({}))), | |
| 30 | + ); | |
| 31 | +} | |
| 32 | + | |
| 33 | +it.effect("routes GitHub remotes to the GitHub provider", () => | |
| 34 | + Effect.gen(function* () { | |
| 35 | + const registry = yield* makeRegistry({ | |
| 36 | + originUrl: "git@github.com:pingdotgg/t3code.git", | |
| 37 | + }); | |
| 38 | + | |
| 39 | + const provider = yield* registry.resolve({ cwd: "/repo" }); | |
| 40 | + | |
| 41 | + assert.strictEqual(provider.kind, "github"); | |
| 42 | + }), | |
| 43 | +); | |
| 44 | + | |
| 45 | +it.effect( | |
| 46 | + "detects GitLab remotes and returns an unsupported provider until one is registered", | |
| 47 | + () => | |
| 48 | + Effect.gen(function* () { | |
| 49 | + const registry = yield* makeRegistry({ | |
| 50 | + originUrl: "git@gitlab.com:group/project.git", | |
| 51 | + }); | |
| 52 | + | |
| 53 | + const provider = yield* registry.resolve({ cwd: "/repo" }); | |
| 54 | + | |
| 55 | + assert.strictEqual(provider.kind, "gitlab"); | |
| 56 | + const error = yield* Effect.flip( | |
| 57 | + provider.listChangeRequests({ | |
| 58 | + cwd: "/repo", | |
| 59 | + headSelector: "feature/source-control", | |
| 60 | + state: "open", | |
| 61 | + }), | |
| 62 | + ); | |
| 63 | + | |
| 64 | + assert.strictEqual(error.provider, "gitlab"); | |
| 65 | + }), | |
| 66 | +); | |
| 67 | + | |
| 68 | +it.effect("falls back to remote verbose output when origin is not configured", () => | |
| 69 | + Effect.gen(function* () { | |
| 70 | + const registry = yield* makeRegistry({ | |
| 71 | + originUrl: null, | |
| 72 | + remoteVerboseOutput: [ | |
| 73 | + "upstream\thttps://dev.azure.com/acme/project/_git/repo (fetch)", | |
| 74 | + "upstream\thttps://dev.azure.com/acme/project/_git/repo (push)", | |
| 75 | + ].join("\n"), | |
| 76 | + }); | |
| 77 | + | |
| 78 | + const provider = yield* registry.resolve({ cwd: "/repo" }); | |
| 79 | + | |
| 80 | + assert.strictEqual(provider.kind, "azure-devops"); | |
| 81 | + }), | |
| 82 | +); | |
| @@ -1,8 +1,14 @@ | ||
| 1 | −import { Context, Effect, Layer } from "effect"; | |
| 2 | −import type { SourceControlProviderError } from "@t3tools/contracts"; | |
| 1 | +import { Cache, Context, Duration, Effect, Exit, Layer } from "effect"; | |
| 2 | +import { SourceControlProviderError } from "@t3tools/contracts"; | |
| 3 | +import type { SourceControlProviderKind } from "@t3tools/contracts"; | |
| 4 | +import { detectGitHostingProviderFromRemoteUrl } from "@t3tools/shared/git"; | |
| 3 | 5 | |
| 4 | 6 | import { SourceControlProvider, type SourceControlProviderShape } from "./SourceControlProvider.ts"; |
| 5 | 7 | import * as GitHubSourceControlProvider from "./GitHubSourceControlProvider.ts"; |
| 8 | +import * as GitVcsDriver from "../vcs/GitVcsDriver.ts"; | |
| 9 | + | |
| 10 | +const PROVIDER_DETECTION_CACHE_CAPACITY = 2_048; | |
| 11 | +const PROVIDER_DETECTION_CACHE_TTL = Duration.seconds(5); | |
| 6 | 12 | |
| 7 | 13 | export interface SourceControlProviderRegistryShape { |
| 8 | 14 | readonly resolve: (input: { |
| @@ -15,14 +21,102 @@ export class SourceControlProviderRegistry extends Context.Service< · 6 unmodified lines | ||
| 15 | 21 | SourceControlProviderRegistryShape |
| 16 | 22 | >()("t3/source-control/SourceControlProviderRegistry") {} |
| 17 | 23 | |
| 24 | +function unsupportedProvider(kind: SourceControlProviderKind): SourceControlProviderShape { | |
| 25 | + const unsupported = (operation: string) => | |
| 26 | + Effect.fail( | |
| 27 | + new SourceControlProviderError({ | |
| 28 | + provider: kind, | |
| 29 | + operation, | |
| 30 | + detail: `No ${kind} source control provider is registered.`, | |
| 31 | + }), | |
| 32 | + ); | |
| 33 | + | |
| 34 | + return SourceControlProvider.of({ | |
| 35 | + kind, | |
| 36 | + listChangeRequests: () => unsupported("listChangeRequests"), | |
| 37 | + getChangeRequest: () => unsupported("getChangeRequest"), | |
| 38 | + createChangeRequest: () => unsupported("createChangeRequest"), | |
| 39 | + getRepositoryCloneUrls: () => unsupported("getRepositoryCloneUrls"), | |
| 40 | + getDefaultBranch: () => unsupported("getDefaultBranch"), | |
| 41 | + checkoutChangeRequest: () => unsupported("checkoutChangeRequest"), | |
| 42 | + }); | |
| 43 | +} | |
| 44 | + | |
| 45 | +function providerDetectionError(operation: string, cwd: string, cause: unknown) { | |
| 46 | + return new SourceControlProviderError({ | |
| 47 | + provider: "unknown", | |
| 48 | + operation, | |
| 49 | + detail: `Failed to detect source control provider for ${cwd}.`, | |
| 50 | + cause, | |
| 51 | + }); | |
| 52 | +} | |
| 53 | + | |
| 54 | +function firstRemoteUrlFromVerboseOutput(output: string): string | null { | |
| 55 | + for (const line of output.split("\n")) { | |
| 56 | + const trimmed = line.trim(); | |
| 57 | + if (trimmed.length === 0) { | |
| 58 | + continue; | |
| 59 | + } | |
| 60 | + const match = /^\S+\s+(\S+)\s+\((?:fetch|push)\)$/.exec(trimmed); | |
| 61 | + const remoteUrl = match?.[1]?.trim() ?? ""; | |
| 62 | + if (remoteUrl.length > 0) { | |
| 63 | + return remoteUrl; | |
| 64 | + } | |
| 65 | + } | |
| 66 | + return null; | |
| 67 | +} | |
| 68 | + | |
| 18 | 69 | export const make = Effect.fn("makeSourceControlProviderRegistry")(function* () { |
| 19 | − const github = yield* SourceControlProvider; | |
| 70 | + const github = yield* GitHubSourceControlProvider.make(); | |
| 71 | + const git = yield* GitVcsDriver.GitVcsDriver; | |
| 72 | + const providers: Partial<Record<SourceControlProviderKind, SourceControlProviderShape>> = { | |
| 73 | + github, | |
| 74 | + }; | |
| 75 | + | |
| 76 | + const detectProviderKind = Effect.fn("SourceControlProviderRegistry.detectProviderKind")( | |
| 77 | + function* (cwd: string) { | |
| 78 | + const originUrl = yield* git | |
| 79 | + .readConfigValue(cwd, "remote.origin.url") | |
| 80 | + .pipe(Effect.catch(() => Effect.succeed(null))); | |
| 81 | + const remoteUrl = | |
| 82 | + originUrl ?? | |
| 83 | + (yield* git | |
| 84 | + .execute({ | |
| 85 | + operation: "SourceControlProviderRegistry.detectProvider.remoteVerbose", | |
| 86 | + cwd, | |
| 87 | + args: ["remote", "-v"], | |
| 88 | + allowNonZeroExit: true, | |
| 89 | + }) | |
| 90 | + .pipe( | |
| 91 | + Effect.map((result) => | |
| 92 | + result.code === 0 ? firstRemoteUrlFromVerboseOutput(result.stdout) : null, | |
| 93 | + ), | |
| 94 | + Effect.mapError((error) => providerDetectionError("detectProvider", cwd, error)), | |
| 95 | + )); | |
| 96 | + | |
| 97 | + if (!remoteUrl) { | |
| 98 | + return "unknown" as const; | |
| 99 | + } | |
| 100 | + | |
| 101 | + return detectGitHostingProviderFromRemoteUrl(remoteUrl)?.kind ?? "unknown"; | |
| 102 | + }, | |
| 103 | + ); | |
| 104 | + | |
| 105 | + const providerKindCache = yield* Cache.makeWith< | |
| 106 | + string, | |
| 107 | + SourceControlProviderKind, | |
| 108 | + SourceControlProviderError | |
| 109 | + >(detectProviderKind, { | |
| 110 | + capacity: PROVIDER_DETECTION_CACHE_CAPACITY, | |
| 111 | + timeToLive: (exit) => (Exit.isSuccess(exit) ? PROVIDER_DETECTION_CACHE_TTL : Duration.zero), | |
| 112 | + }); | |
| 20 | 113 | |
| 21 | 114 | return SourceControlProviderRegistry.of({ |
| 22 | − resolve: () => Effect.succeed(github), | |
| 115 | + resolve: (input) => | |
| 116 | + Cache.get(providerKindCache, input.cwd).pipe( | |
| 117 | + Effect.map((kind) => providers[kind] ?? unsupportedProvider(kind)), | |
| 118 | + ), | |
| 23 | 119 | }); |
| 24 | 120 | }); |
| 25 | 121 | |
| 26 | −export const layer = Layer.effect(SourceControlProviderRegistry, make()).pipe( | |
| 27 | − Layer.provide(GitHubSourceControlProvider.layer), | |
| 28 | −); | |
| 122 | +export const layer = Layer.effect(SourceControlProviderRegistry, make()); | |
| @@ -35,15 +35,6 @@ export function createEnvironmentApi(rpcClient: WsRpcClient): EnvironmentApi { · 34 unmodified lines | ||
| 35 | 35 | init: rpcClient.vcs.init, |
| 36 | 36 | }, |
| 37 | 37 | git: { |
| 38 | − pull: rpcClient.vcs.pull, | |
| 39 | − refreshStatus: rpcClient.vcs.refreshStatus, | |
| 40 | − onStatus: (input, callback, options) => rpcClient.vcs.onStatus(input, callback, options), | |
| 41 | − listBranches: rpcClient.vcs.listBranches, | |
| 42 | − createWorktree: rpcClient.vcs.createWorktree, | |
| 43 | − removeWorktree: rpcClient.vcs.removeWorktree, | |
| 44 | − createBranch: rpcClient.vcs.createBranch, | |
| 45 | − checkout: rpcClient.vcs.checkout, | |
| 46 | − init: rpcClient.vcs.init, | |
| 47 | 38 | resolvePullRequest: rpcClient.git.resolvePullRequest, |
| 48 | 39 | preparePullRequestThread: rpcClient.git.preparePullRequestThread, |
| 49 | 40 | }, |
| @@ -85,16 +85,7 @@ function createTestClient() { · 84 unmodified lines | ||
| 85 | 85 | openInEditor: vi.fn(async () => undefined), |
| 86 | 86 | }, |
| 87 | 87 | git: { |
| 88 | − pull: vi.fn(async () => undefined), | |
| 89 | − refreshStatus: vi.fn(async () => undefined), | |
| 90 | − onStatus: vi.fn(() => () => undefined), | |
| 91 | 88 | runStackedAction: vi.fn(async () => ({}) as any), |
| 92 | − listBranches: vi.fn(async () => []), | |
| 93 | − createWorktree: vi.fn(async () => undefined), | |
| 94 | − removeWorktree: vi.fn(async () => undefined), | |
| 95 | − createBranch: vi.fn(async () => undefined), | |
| 96 | − checkout: vi.fn(async () => undefined), | |
| 97 | − init: vi.fn(async () => undefined), | |
| 98 | 89 | resolvePullRequest: vi.fn(async () => undefined), |
| 99 | 90 | preparePullRequestThread: vi.fn(async () => undefined), |
| 100 | 91 | }, |
| @@ -45,7 +45,12 @@ const GitStatusPrState = Schema.Literals(["open", "closed", "merged"]); · 44 unmodified lines | ||
| 45 | 45 | const GitPullRequestReference = TrimmedNonEmptyStringSchema; |
| 46 | 46 | const GitPullRequestState = Schema.Literals(["open", "closed", "merged"]); |
| 47 | 47 | const GitPreparePullRequestThreadMode = Schema.Literals(["local", "worktree"]); |
| 48 | −export const GitHostingProviderKind = Schema.Literals(["github", "gitlab", "unknown"]); | |
| 48 | +export const GitHostingProviderKind = Schema.Literals([ | |
| 49 | + "github", | |
| 50 | + "gitlab", | |
| 51 | + "azure-devops", | |
| 52 | + "unknown", | |
| 53 | +]); | |
| 49 | 54 | export type GitHostingProviderKind = typeof GitHostingProviderKind.Type; |
| 50 | 55 | export const GitHostingProvider = Schema.Struct({ |
| 51 | 56 | kind: GitHostingProviderKind, |
| @@ -274,34 +274,10 @@ export interface EnvironmentApi { · 273 unmodified lines | ||
| 274 | 274 | ) => () => void; |
| 275 | 275 | }; |
| 276 | 276 | git: { |
| 277 | − /** @deprecated Use `EnvironmentApi.vcs.listBranches` for local VCS branch/ref listing. */ | |
| 278 | − listBranches: (input: GitListBranchesInput) => Promise<GitListBranchesResult>; | |
| 279 | − /** @deprecated Use `EnvironmentApi.vcs.createWorktree` for local VCS workspace creation. */ | |
| 280 | − createWorktree: (input: GitCreateWorktreeInput) => Promise<GitCreateWorktreeResult>; | |
| 281 | − /** @deprecated Use `EnvironmentApi.vcs.removeWorktree` for local VCS workspace removal. */ | |
| 282 | − removeWorktree: (input: GitRemoveWorktreeInput) => Promise<void>; | |
| 283 | − /** @deprecated Use `EnvironmentApi.vcs.createBranch` for local VCS branch/ref creation. */ | |
| 284 | − createBranch: (input: GitCreateBranchInput) => Promise<GitCreateBranchResult>; | |
| 285 | − /** @deprecated Use `EnvironmentApi.vcs.checkout` for local VCS checkout/switch operations. */ | |
| 286 | − checkout: (input: GitCheckoutInput) => Promise<GitCheckoutResult>; | |
| 287 | − /** @deprecated Use `EnvironmentApi.vcs.init` for local VCS repository initialization. */ | |
| 288 | − init: (input: GitInitInput) => Promise<void>; | |
| 289 | 277 | resolvePullRequest: (input: GitPullRequestRefInput) => Promise<GitResolvePullRequestResult>; |
| 290 | 278 | preparePullRequestThread: ( |
| 291 | 279 | input: GitPreparePullRequestThreadInput, |
| 292 | 280 | ) => Promise<GitPreparePullRequestThreadResult>; |
| 293 | − /** @deprecated Use `EnvironmentApi.vcs.pull` for local VCS pull/sync operations. */ | |
| 294 | − pull: (input: GitPullInput) => Promise<GitPullResult>; | |
| 295 | − /** @deprecated Use `EnvironmentApi.vcs.refreshStatus` for local VCS status refreshes. */ | |
| 296 | − refreshStatus: (input: GitStatusInput) => Promise<GitStatusResult>; | |
| 297 | − /** @deprecated Use `EnvironmentApi.vcs.onStatus` for local VCS status subscriptions. */ | |
| 298 | − onStatus: ( | |
| 299 | − input: GitStatusInput, | |
| 300 | − callback: (status: GitStatusResult) => void, | |
| 301 | − options?: { | |
| 302 | − onResubscribe?: () => void; | |
| 303 | − }, | |
| 304 | − ) => () => void; | |
| 305 | 281 | }; |
| 306 | 282 | orchestration: { |
| 307 | 283 | dispatchCommand: (command: ClientOrchestrationCommand) => Promise<{ sequence: number }>; |
| @@ -225,6 +225,10 @@ function isGitLabHost(host: string): boolean { · 224 unmodified lines | ||
| 225 | 225 | return host === "gitlab.com" || host.includes("gitlab"); |
| 226 | 226 | } |
| 227 | 227 | |
| 228 | +function isAzureDevOpsHost(host: string): boolean { | |
| 229 | + return host === "dev.azure.com" || host.endsWith(".visualstudio.com"); | |
| 230 | +} | |
| 231 | + | |
| 228 | 232 | export function detectGitHostingProviderFromRemoteUrl( |
| 229 | 233 | remoteUrl: string, |
| 230 | 234 | ): GitHostingProvider | null { |
| @@ -249,6 +253,14 @@ export function detectGitHostingProviderFromRemoteUrl( · 18 unmodified lines | ||
| 249 | 253 | }; |
| 250 | 254 | } |
| 251 | 255 | |
| 256 | + if (isAzureDevOpsHost(host)) { | |
| 257 | + return { | |
| 258 | + kind: "azure-devops", | |
| 259 | + name: "Azure DevOps", | |
| 260 | + baseUrl: toBaseUrl(host), | |
| 261 | + }; | |
| 262 | + } | |
| 263 | + | |
| 252 | 264 | return { |
| 253 | 265 | kind: "unknown", |
| 254 | 266 | name: host, |