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:
1 parent
e8d67ffb91
commit
506e0fc049
8 files changed
+214
-52
No files matched your search
@@ -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),
|
||||
|
||||
@@ -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());
|
||||
@@ -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,
|
||||
},
|
||||
|
||||
@@ -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),
|
||||
},
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -274,34 +274,10 @@ export interface EnvironmentApi {
|
||||
) => () => void;
|
||||
};
|
||||
git: {
|
||||
/** @deprecated Use `EnvironmentApi.vcs.listBranches` for local VCS branch/ref listing. */
|
||||
listBranches: (input: GitListBranchesInput) => Promise<GitListBranchesResult>;
|
||||
/** @deprecated Use `EnvironmentApi.vcs.createWorktree` for local VCS workspace creation. */
|
||||
createWorktree: (input: GitCreateWorktreeInput) => Promise<GitCreateWorktreeResult>;
|
||||
/** @deprecated Use `EnvironmentApi.vcs.removeWorktree` for local VCS workspace removal. */
|
||||
removeWorktree: (input: GitRemoveWorktreeInput) => Promise<void>;
|
||||
/** @deprecated Use `EnvironmentApi.vcs.createBranch` for local VCS branch/ref creation. */
|
||||
createBranch: (input: GitCreateBranchInput) => Promise<GitCreateBranchResult>;
|
||||
/** @deprecated Use `EnvironmentApi.vcs.checkout` for local VCS checkout/switch operations. */
|
||||
checkout: (input: GitCheckoutInput) => Promise<GitCheckoutResult>;
|
||||
/** @deprecated Use `EnvironmentApi.vcs.init` for local VCS repository initialization. */
|
||||
init: (input: GitInitInput) => Promise<void>;
|
||||
resolvePullRequest: (input: GitPullRequestRefInput) => Promise<GitResolvePullRequestResult>;
|
||||
preparePullRequestThread: (
|
||||
input: GitPreparePullRequestThreadInput,
|
||||
) => Promise<GitPreparePullRequestThreadResult>;
|
||||
/** @deprecated Use `EnvironmentApi.vcs.pull` for local VCS pull/sync operations. */
|
||||
pull: (input: GitPullInput) => Promise<GitPullResult>;
|
||||
/** @deprecated Use `EnvironmentApi.vcs.refreshStatus` for local VCS status refreshes. */
|
||||
refreshStatus: (input: GitStatusInput) => Promise<GitStatusResult>;
|
||||
/** @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 }>;
|
||||
|
||||
@@ -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,
|
||||
|
||||
Reference in new issue
Block a user