diff --git a/apps/server/src/project/RepositoryIdentityResolver.test.ts b/apps/server/src/project/RepositoryIdentityResolver.test.ts index 4377caf55c524..a731fd65d31fc 100644 --- a/apps/server/src/project/RepositoryIdentityResolver.test.ts +++ b/apps/server/src/project/RepositoryIdentityResolver.test.ts @@ -68,6 +68,16 @@ it.layer(NodeServices.layer)("RepositoryIdentityResolverLive", (it) => { expect(identity?.canonicalKey).toBe("github.com/t3tools/t3code"); expect(identity?.provider).toBe("github"); expect(identity?.displayName).toBe("T3Tools/t3code"); + + // An alias is keyed by the host ssh config sends it to; git keeps the written remote. + yield* git(cwd, ["remote", "set-url", "origin", "git@gh-work:T3Tools/t3code.git"]); + const aliased = yield* RepositoryIdentityResolver.make({ + canonicalRemoteUrl: (url) => Effect.succeed(url.replace("@gh-work:", "@github.com:")), + }).pipe(Effect.provide(ProcessRunner.layer)); + const aliasIdentity = yield* aliased.resolve(cwd); + expect(aliasIdentity?.canonicalKey).toBe("github.com/t3tools/t3code"); + expect(aliasIdentity?.provider).toBe("github"); + expect(aliasIdentity?.locator.remoteUrl).toBe("git@gh-work:T3Tools/t3code.git"); }).pipe(Effect.provide(RepositoryIdentityResolver.layer)), ); @@ -381,6 +391,16 @@ it.layer(NodeServices.layer)("RepositoryIdentityResolverLive", (it) => { yield* git(cwd, ["remote", "add", "ghe-parent", "https://ghe.example.com:8443/acme/web.git"]); yield* git(cwd, ["config", "remote.ghe-fork.gh-resolved", "acme/web"]); expect((yield* resolveFresh())?.locator.remoteName).toBe("ghe-parent"); + + // An SSH alias and the server it leads to are one host. + yield* git(cwd, ["config", "--unset", "remote.ghe-fork.gh-resolved"]); + yield* git(cwd, ["remote", "add", "alias-fork", "git@work:me/tool.git"]); + yield* git(cwd, ["remote", "add", "alias-parent", "git@code.example.test:acme/tool.git"]); + yield* git(cwd, ["config", "remote.alias-fork.gh-resolved", "acme/tool"]); + const aliased = yield* RepositoryIdentityResolver.make({ + canonicalRemoteUrl: (url) => Effect.succeed(url.replace("@work:", "@code.example.test:")), + }).pipe(Effect.provide(ProcessRunner.layer)); + expect((yield* aliased.resolve(cwd))?.locator.remoteName).toBe("alias-parent"); }).pipe(Effect.provide(RepositoryIdentityResolver.layer)), ); diff --git a/apps/server/src/project/RepositoryIdentityResolver.ts b/apps/server/src/project/RepositoryIdentityResolver.ts index ebd63a957d5b5..dc31371e259df 100644 --- a/apps/server/src/project/RepositoryIdentityResolver.ts +++ b/apps/server/src/project/RepositoryIdentityResolver.ts @@ -27,6 +27,8 @@ export interface RepositoryIdentityResolverOptions { readonly refine?: ( identity: RepositoryIdentity, ) => Effect.Effect; + /** `SourceControlHost.canonicalRemoteUrl`; absent, remotes are read as written. */ + readonly canonicalRemoteUrl?: (remoteUrl: string) => Effect.Effect; } export class RepositoryIdentityResolver extends Context.Service< @@ -77,6 +79,7 @@ function parseRemoteFetchUrls(stdout: string): Map { function pickMarkedRemote( remotes: ReadonlyMap, resolved: string, + canonicalUrls: ReadonlyMap, ): { readonly remoteName: string; readonly remoteUrl: string } | null { const marks = resolved.split("\n").flatMap((line) => { const match = /^remote\.(.+)\.gh-resolved\s+(\S+)$/u.exec(line.trim()); @@ -90,12 +93,14 @@ function pickMarkedRemote( if (!mark || others.length > 0) return null; if (mark.value === "base") return { remoteName: mark.remoteName, remoteUrl: mark.remoteUrl }; // Compare hosts with HTTP ports kept: one hostname can serve separate forges on two ports. - const host = parseGitRemoteHost(mark.remoteUrl)?.host; + // Two SSH aliases for one server are the same host. + const hostOf = (url: string) => parseGitRemoteHost(canonicalUrls.get(url) ?? url)?.host; + const host = hostOf(mark.remoteUrl); const markedPath = mark.value.toLowerCase(); const [remoteName, remoteUrl] = [...remotes.entries()].find( ([, url]) => - parseGitRemoteHost(url)?.host === host && + hostOf(url) === host && normalizeGitRemoteUrl(url).split("/").slice(1).join("/") === markedPath, ) ?? []; return remoteName && remoteUrl ? { remoteName, remoteUrl } : null; @@ -104,8 +109,9 @@ function pickMarkedRemote( function pickPrimaryRemote( remotes: ReadonlyMap, resolved: string, + canonicalUrls: ReadonlyMap, ): { readonly remoteName: string; readonly remoteUrl: string } | null { - const marked = pickMarkedRemote(remotes, resolved); + const marked = pickMarkedRemote(remotes, resolved, canonicalUrls); if (marked) return marked; for (const preferredRemoteName of ["upstream", "origin"] as const) { @@ -128,28 +134,34 @@ function repositoryPathOf(canonicalKey: string, remoteUrl: string): string { function buildRepositoryOrigin( originUrl: string | undefined, + canonicalOriginUrl: string | undefined, canonicalKey: string, ): RepositoryIdentity["origin"] { if (!originUrl) return undefined; - const originKey = sourceControlDefinitions.repositoryKeyFromRemote(originUrl); + const originKey = sourceControlDefinitions.repositoryKeyFromRemote( + canonicalOriginUrl ?? originUrl, + ); if (originKey === canonicalKey) return undefined; const displayName = repositoryPathOf(originKey, originUrl); return { canonicalKey: originKey, remoteUrl: originUrl, ...(displayName ? { displayName } : {}) }; } +// The key and host follow where ssh config sends an alias; the locator keeps git's own URL. function buildRepositoryIdentity(input: { readonly remoteName: string; readonly remoteUrl: string; + readonly canonicalRemoteUrl: string; readonly originUrl: string | undefined; + readonly canonicalOriginUrl: string | undefined; readonly rootPath: string; }): RepositoryIdentity { - const canonicalKey = sourceControlDefinitions.repositoryKeyFromRemote(input.remoteUrl); - const sourceControlProvider = sourceControlDefinitions.recognizeRemote(input.remoteUrl); + const canonicalKey = sourceControlDefinitions.repositoryKeyFromRemote(input.canonicalRemoteUrl); + const sourceControlProvider = sourceControlDefinitions.recognizeRemote(input.canonicalRemoteUrl); const repositoryPath = repositoryPathOf(canonicalKey, input.remoteUrl); const repositoryPathSegments = repositoryPath.split("/").filter((segment) => segment.length > 0); const [owner] = repositoryPathSegments; const repositoryName = repositoryPathSegments.at(-1); - const origin = buildRepositoryOrigin(input.originUrl, canonicalKey); + const origin = buildRepositoryOrigin(input.originUrl, input.canonicalOriginUrl, canonicalKey); return { canonicalKey, @@ -264,6 +276,7 @@ const resolveRepositoryIdentityFromCacheKey = Effect.fn( "RepositoryIdentityResolver.resolveFromCacheKey", )(function* ( cacheKey: RepositoryIdentityCacheKey, + canonicalRemoteUrl: (remoteUrl: string) => Effect.Effect, ): Effect.fn.Return { const processRunner = yield* ProcessRunner.ProcessRunner; const env = gitEnv(yield* HostProcess.Platform); @@ -290,14 +303,21 @@ const resolveRepositoryIdentityFromCacheKey = Effect.fn( resolvedResult._tag === "Some" && resolvedResult.value.code === 0 ? resolvedResult.value.stdout : ""; - const remote = pickPrimaryRemote(remotes, resolved); - return remote - ? buildRepositoryIdentity({ - ...remote, - originUrl: remotes.get("origin"), - rootPath: cacheKey.rootPath, - }) - : null; + const canonicalUrls = new Map( + yield* Effect.forEach(new Set(remotes.values()), (url) => + canonicalRemoteUrl(url).pipe(Effect.map((canonical) => [url, canonical] as const)), + ), + ); + const remote = pickPrimaryRemote(remotes, resolved, canonicalUrls); + if (!remote) return null; + const originUrl = remotes.get("origin"); + return buildRepositoryIdentity({ + ...remote, + canonicalRemoteUrl: canonicalUrls.get(remote.remoteUrl) ?? remote.remoteUrl, + originUrl, + canonicalOriginUrl: originUrl === undefined ? undefined : canonicalUrls.get(originUrl), + rootPath: cacheKey.rootPath, + }); }); export const make = Effect.fn("RepositoryIdentityResolver.make")(function* ( @@ -308,6 +328,7 @@ export const make = Effect.fn("RepositoryIdentityResolver.make")(function* ( const path = yield* Path.Path; const cacheCapacity = options.cacheCapacity ?? DEFAULT_REPOSITORY_IDENTITY_CACHE_CAPACITY; const refine = options.refine ?? Effect.succeed; + const canonicalRemoteUrl = options.canonicalRemoteUrl ?? Effect.succeed; const timeToLive = (exit: Exit.Exit) => Exit.match(exit, { onSuccess: (value) => @@ -330,7 +351,7 @@ export const make = Effect.fn("RepositoryIdentityResolver.make")(function* ( RepositoryIdentity | null >( (cacheKey) => - resolveRepositoryIdentityFromCacheKey(cacheKey).pipe( + resolveRepositoryIdentityFromCacheKey(cacheKey, canonicalRemoteUrl).pipe( Effect.provideService(ProcessRunner.ProcessRunner, processRunner), Effect.filterOrElse( (identity): identity is null => identity === null, diff --git a/apps/server/src/server.ts b/apps/server/src/server.ts index 9353fa2f508a8..5f0a03e582913 100644 --- a/apps/server/src/server.ts +++ b/apps/server/src/server.ts @@ -74,6 +74,7 @@ import * as CheckpointDiffQuery from "./checkpointing/CheckpointDiffQuery.ts"; import * as NativeChildWorkQuery from "./orchestration-v2/NativeChildWorkQuery.ts"; import * as CheckpointStore from "./checkpointing/CheckpointStore.ts"; import * as SourceControlBuiltInDrivers from "./sourceControl/builtInDrivers.ts"; +import * as SourceControlHost from "@t3tools/source-control-core/server/SourceControlHost"; import * as SourceControlSignIn from "./sourceControl/SourceControlSignIn.ts"; import { BUILT_IN_SOURCE_CONTROL_DRIVER_LIST } from "./sourceControl/sourceControlDefinitions.ts"; import * as TextGeneration from "./textGeneration/TextGeneration.ts"; @@ -402,7 +403,9 @@ const layerRepositoryIdentityResolver = Layer.effect( RepositoryIdentityResolver.RepositoryIdentityResolver, Effect.gen(function* () { const registry = yield* SourceControlProviderRegistry.SourceControlProviderRegistry; + const { canonicalRemoteUrl } = yield* SourceControlHost.SourceControlHost; return yield* RepositoryIdentityResolver.make({ + canonicalRemoteUrl, refine: Effect.fn(function* (identity: RepositoryIdentity) { for (const { kind } of BUILT_IN_SOURCE_CONTROL_DRIVER_LIST) { const provider = yield* registry.get(kind); diff --git a/apps/server/src/sourceControl/ServerSourceControlHost.ts b/apps/server/src/sourceControl/ServerSourceControlHost.ts index 00b648d0f0364..8e39f5a7bb0c9 100644 --- a/apps/server/src/sourceControl/ServerSourceControlHost.ts +++ b/apps/server/src/sourceControl/ServerSourceControlHost.ts @@ -1,5 +1,6 @@ import * as SecretResolver from "@t3tools/secret-provider-core/server/SecretResolver"; import * as SourceControlHost from "@t3tools/source-control-core/server/SourceControlHost"; +import * as SshRemoteHost from "@t3tools/source-control-core/server/sshRemoteHost"; import * as Effect from "effect/Effect"; import * as Layer from "effect/Layer"; @@ -8,6 +9,7 @@ import * as ServerSettings from "../serverSettings.ts"; import * as GitVcsDriver from "../vcs/GitVcsDriver.ts"; import * as VcsDriverRegistry from "../vcs/VcsDriverRegistry.ts"; import * as VcsProcess from "../vcs/VcsProcess.ts"; +import { sourceControlDefinitions } from "./sourceControlDefinitions.ts"; import { resolveSourceControlHostSecrets } from "./sourceControlHostSecrets.ts"; export const layer = Layer.effect( @@ -36,6 +38,11 @@ export const layer = Layer.effect( update: serverSettings.updateSettings, }, process: { run: process.run }, + canonicalRemoteUrl: yield* SshRemoteHost.make({ + run: process.run, + isPublicHost: (hostName) => + sourceControlDefinitions.findByPublicHost(hostName) !== undefined, + }), git: { execute: git.execute, resolveCommit: git.resolveCommit, diff --git a/apps/server/src/sourceControl/SourceControlProviderRegistry.test.ts b/apps/server/src/sourceControl/SourceControlProviderRegistry.test.ts index 9d472fe6e77c9..3a32f33cff890 100644 --- a/apps/server/src/sourceControl/SourceControlProviderRegistry.test.ts +++ b/apps/server/src/sourceControl/SourceControlProviderRegistry.test.ts @@ -91,7 +91,10 @@ function makeRegistry(input: { readonly discovery?: ReadonlyArray; readonly run?: SourceControlHost.SourceControlHost["Service"]["process"]["run"]; readonly settings?: Partial; + /** `HostName` by `Host`, as `ssh -G` reports them; `run` is not asked. */ + readonly sshConfig?: Readonly>; }) { + const run = input.run ?? (() => Effect.succeed(TestSourceControlHost.processOutput(""))); const driver = { listRemotes: () => Effect.succeed({ @@ -148,7 +151,14 @@ function makeRegistry(input: { }), TestSourceControlHost.layer({ process: { - run: input.run ?? (() => Effect.succeed(TestSourceControlHost.processOutput(""))), + run: (args) => + args.command === "ssh" + ? Effect.succeed( + TestSourceControlHost.processOutput( + `hostname ${input.sshConfig?.[args.args[1]!.replace(/^.*@/u, "")] ?? args.args[1]!.replace(/^.*@/u, "")}\n`, + ), + ) + : run(args), }, }), input.settings @@ -188,6 +198,44 @@ it.effect("routes an unknown remote to the integration that claims its server", }), ); +it.effect("places an SSH host alias by the server ssh config sends it to", () => + Effect.gen(function* () { + const asked: Array = []; + const sshConfig = { "gitlab-work": "code.example.com", "gh-work": "github.com" }; + const selfHosted = yield* makeRegistry({ + remotes: [{ name: "origin", url: "git@gitlab-work:group/project.git" }], + sshConfig, + run: (input) => { + asked.push([input.command, ...input.args].join(" ")); + return signedIn("code.example.com")(); + }, + }); + const handle = yield* selfHosted.resolveHandle({ cwd: "/repo" }); + assert.deepStrictEqual(handle.context, { + provider: { + kind: kind("gitlab"), + name: "gitlab Self-Hosted", + baseUrl: "https://code.example.com", + }, + remoteName: "origin", + remoteUrl: "git@gitlab-work:group/project.git", + }); + assert.deepStrictEqual(asked, ["gitlab-cli auth status --hostname code.example.com"]); + + // A caller's context, made from the written remote, is placed the same way. + const written = { + provider: { kind: kind("unknown"), name: "gh-work", baseUrl: "https://gh-work" }, + remoteName: "origin", + remoteUrl: "git@gh-work:owner/repo.git", + }; + const publicHost = yield* makeRegistry({ remotes: [], sshConfig }); + const placed = yield* publicHost.resolveHandle({ cwd: "/repo", context: written }); + assert.strictEqual(placed.context?.provider.kind, kind("github")); + assert.strictEqual(placed.context?.provider.baseUrl, "https://github.com"); + assert.strictEqual(placed.context?.remoteUrl, written.remoteUrl); + }), +); + it.effect("leaves a remote no integration claims unknown, with its server's address", () => Effect.gen(function* () { const registry = yield* makeRegistry({ diff --git a/apps/server/src/sourceControl/SourceControlProviderRegistry.ts b/apps/server/src/sourceControl/SourceControlProviderRegistry.ts index 36f5e7642ae60..c061d24837961 100644 --- a/apps/server/src/sourceControl/SourceControlProviderRegistry.ts +++ b/apps/server/src/sourceControl/SourceControlProviderRegistry.ts @@ -138,11 +138,13 @@ function selectProviderContext( remotes: ReadonlyArray<{ readonly name: string; readonly url: string; + /** Where ssh config sends the remote; the host is classified by it. */ + readonly canonicalUrl: string; }>, ): SourceControlProvider.SourceControlProviderContext | null { const candidates: Array = []; for (const remote of remotes) { - const provider = sourceControlDefinitions.recognizeRemote(remote.url); + const provider = sourceControlDefinitions.recognizeRemote(remote.canonicalUrl); if (provider) { candidates.push({ provider, @@ -243,7 +245,7 @@ export const makeWithProviders = Effect.fn("makeSourceControlProviderRegistryWit function* (registrations: ReadonlyArray) { const config = yield* ServerConfig.ServerConfig; const serverSettings = yield* ServerSettings.ServerSettingsService; - const { process } = yield* SourceControlHost.SourceControlHost; + const { process, canonicalRemoteUrl } = yield* SourceControlHost.SourceControlHost; const vcsRegistry = yield* VcsDriverRegistry.VcsDriverRegistry; const providers = new Map< SourceControlProviderKind, @@ -307,7 +309,12 @@ export const makeWithProviders = Effect.fn("makeSourceControlProviderRegistryWit }), ), ); - return yield* refineContext(cwd, selectProviderContext(remotes.remotes)); + const classified = yield* Effect.forEach(remotes.remotes, (remote) => + canonicalRemoteUrl(remote.url).pipe( + Effect.map((canonicalUrl) => ({ ...remote, canonicalUrl })), + ), + ); + return yield* refineContext(cwd, selectProviderContext(classified)); }, ); @@ -320,10 +327,36 @@ export const makeWithProviders = Effect.fn("makeSourceControlProviderRegistryWit timeToLive: (exit) => (Exit.isSuccess(exit) ? PROVIDER_DETECTION_CACHE_TTL : Duration.zero), }); + // A caller's context names the host its remote is written with; an SSH alias is placed by + // the host it leads to, keeping the kind the caller already settled. + const canonicalContext = Effect.fnUntraced(function* ( + context: SourceControlProvider.SourceControlProviderContext, + ) { + // An empty base URL asks one host's own login to place the remote. + const canonicalUrl = + context.provider.baseUrl === "" + ? context.remoteUrl + : yield* canonicalRemoteUrl(context.remoteUrl); + const provider = + canonicalUrl === context.remoteUrl + ? null + : sourceControlDefinitions.recognizeRemote(canonicalUrl); + if (provider === null) return context; + return { + ...context, + provider: + context.provider.kind === "unknown" + ? provider + : { ...provider, kind: context.provider.kind }, + }; + }); + const resolveHandle: SourceControlProviderRegistry["Service"]["resolveHandle"] = (input) => (input.context === undefined ? Cache.get(providerContextCache, input.cwd) - : refineContext(input.cwd, input.context) + : canonicalContext(input.context).pipe( + Effect.flatMap((context) => refineContext(input.cwd, context)), + ) ).pipe( Effect.map((context) => { const kind = context?.provider.kind ?? SourceControlProviderKind.make("unknown"); diff --git a/packages/source-control-core/package.json b/packages/source-control-core/package.json index b16f16d10f4d4..9b30a735a7814 100644 --- a/packages/source-control-core/package.json +++ b/packages/source-control-core/package.json @@ -50,6 +50,10 @@ "./server/SourceControlRateLimit": { "types": "./src/server/SourceControlRateLimit.ts", "import": "./src/server/SourceControlRateLimit.ts" + }, + "./server/sshRemoteHost": { + "types": "./src/server/sshRemoteHost.ts", + "import": "./src/server/sshRemoteHost.ts" } }, "scripts": { diff --git a/packages/source-control-core/src/server/SourceControlHost.ts b/packages/source-control-core/src/server/SourceControlHost.ts index e3d6f604cfddb..e23d3a27a74c7 100644 --- a/packages/source-control-core/src/server/SourceControlHost.ts +++ b/packages/source-control-core/src/server/SourceControlHost.ts @@ -85,6 +85,11 @@ export class SourceControlHost extends Context.Service< input: SourceControlProcessInput, ) => Effect.Effect; }; + /** + * An SSH remote with its host replaced by the `HostName` ssh config gives it, for telling + * which server an alias leads to. Git is still given the remote as written. + */ + readonly canonicalRemoteUrl: (remoteUrl: string) => Effect.Effect; readonly git: { readonly execute: (input: { readonly operation: string; diff --git a/packages/source-control-core/src/server/sshRemoteHost.test.ts b/packages/source-control-core/src/server/sshRemoteHost.test.ts new file mode 100644 index 0000000000000..636833acb84dd --- /dev/null +++ b/packages/source-control-core/src/server/sshRemoteHost.test.ts @@ -0,0 +1,71 @@ +import { assert, it } from "@effect/vitest"; +import * as Effect from "effect/Effect"; +import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner"; + +import * as SshRemoteHost from "./sshRemoteHost.ts"; + +const HOST_NAMES: Readonly> = { + "gh-work": "github.com", + "GitLab-Work": "gitlab.example.com", + "github.work": "ssh.github.com", + "code.example.com": "ssh.code.example.com", + lan: "10.0.0.5", + // A `Match user` block applies to the remote's user only. + "deploy@forge": "github.com", + named: "constructor", +}; + +it.effect("replaces an SSH alias with the host ssh config sends it to", () => + Effect.gen(function* () { + const asked: Array = []; + const canonicalRemoteUrl = yield* SshRemoteHost.make({ + isPublicHost: (hostName) => hostName === "github.com", + run: ({ args }) => { + const target = args[1]!; + const host = target.slice(target.lastIndexOf("@") + 1); + asked.push(target); + return Effect.succeed({ + exitCode: ChildProcessSpawner.ExitCode(0), + stdout: `host ${host}\nuser git\nhostname ${HOST_NAMES[target] ?? HOST_NAMES[host] ?? host}\nport 2222\n`, + stderr: "", + stdoutTruncated: false, + stderrTruncated: false, + }); + }, + }); + for (const [written, canonical] of [ + ["git@gh-work:owner/repo.git", "git@github.com:owner/repo.git"], + ["gh-work:owner/repo.git", "github.com:owner/repo.git"], + [ + "ssh://git@GitLab-Work:2222/group/project.git", + "ssh://git@gitlab.example.com:2222/group/project.git", + ], + ["git@github.work:owner/repo.git", "git@github.com:owner/repo.git"], + // A dotted host may be the web host itself, with its SSH endpoint elsewhere. + ["git@code.example.com:owner/repo.git", "git@code.example.com:owner/repo.git"], + ["git@lan:owner/repo.git", "git@lan:owner/repo.git"], + ["git@unconfigured:owner/repo.git", "git@unconfigured:owner/repo.git"], + ["https://gh-work/owner/repo.git", "https://gh-work/owner/repo.git"], + ["git@-oProxyCommand=x:owner/repo.git", "git@-oProxyCommand=x:owner/repo.git"], + ["-oProxyCommand=x@gh-work:owner/repo.git", "-oProxyCommand=x@gh-work:owner/repo.git"], + ["deploy@forge:owner/repo.git", "deploy@github.com:owner/repo.git"], + ["git@forge:owner/repo.git", "git@forge:owner/repo.git"], + ["git@named:owner/repo.git", "git@constructor:owner/repo.git"], + ] as const) { + assert.strictEqual(yield* canonicalRemoteUrl(written), canonical, written); + } + // One read per host, and none for a remote that is not SSH or not a host name. + assert.deepStrictEqual(asked, [ + "git@gh-work", + "gh-work", + "git@GitLab-Work", + "git@github.work", + "git@code.example.com", + "git@lan", + "git@unconfigured", + "deploy@forge", + "git@forge", + "git@named", + ]); + }), +); diff --git a/packages/source-control-core/src/server/sshRemoteHost.ts b/packages/source-control-core/src/server/sshRemoteHost.ts new file mode 100644 index 0000000000000..6baa9f7533403 --- /dev/null +++ b/packages/source-control-core/src/server/sshRemoteHost.ts @@ -0,0 +1,76 @@ +import * as HostProcess from "@t3tools/shared/HostProcess"; +import { isSshRemoteUrl } from "@t3tools/shared/sourceControl"; +import * as Cache from "effect/Cache"; +import * as Duration from "effect/Duration"; +import * as Effect from "effect/Effect"; + +import type * as SourceControlHost from "./SourceControlHost.ts"; + +// The host keeps its spelling: ssh matches `Host` patterns case-sensitively. A leading `-` on the +// user or host would be read by ssh as an option. +const SSH_REMOTE = + /^((?:ssh:\/\/)?)((?:[^-@/:][^@/:]*@)?)([A-Za-z0-9][A-Za-z0-9._-]*)([:/].*)?$/isu; +const HOST_NAME = /^[a-z0-9][a-z0-9.-]*$/u; +const IPV4 = /^[\d.]+$/u; + +// The public forges' SSH endpoints on port 443. +const SSH_ENDPOINT_HOSTS: ReadonlyMap = new Map([ + ["ssh.github.com", "github.com"], + ["altssh.gitlab.com", "gitlab.com"], +]); + +function hostNameOf(sshConfig: string): string | null { + const hostName = sshConfig + .split("\n") + .find((line) => line.startsWith("hostname ")) + ?.slice("hostname ".length) + .trim() + .toLowerCase(); + if (hostName === undefined || !HOST_NAME.test(hostName) || IPV4.test(hostName)) return null; + return SSH_ENDPOINT_HOSTS.get(hostName) ?? hostName; +} + +/** + * Builds `SourceControlHost.canonicalRemoteUrl`. A dotted host may itself be the web host with a + * separate SSH endpoint behind it, so it is replaced only by a public forge. + */ +export const make = Effect.fnUntraced(function* (input: { + readonly run: SourceControlHost.SourceControlHost["Service"]["process"]["run"]; + readonly isPublicHost: (hostName: string) => boolean; +}) { + const cwd = yield* HostProcess.WorkingDirectory; + // `ssh -G` prints the configuration it would use and opens no connection. + const hostNames = yield* Cache.make({ + capacity: 256, + timeToLive: Duration.minutes(5), + // Keyed by `user@host`: a `Match user` block can send one user of a host elsewhere. + lookup: (target: string) => + input + .run({ + operation: "SourceControlHost.canonicalRemoteUrl", + command: "ssh", + args: ["-G", target], + cwd, + allowNonZeroExit: true, + timeoutMs: 5_000, + maxOutputBytes: 64_000, + outputMode: "truncate", + }) + .pipe( + Effect.map((result) => (result.exitCode === 0 ? hostNameOf(result.stdout) : null)), + Effect.orElseSucceed(() => null), + ), + }); + return Effect.fn("SourceControlHost.canonicalRemoteUrl")(function* (remoteUrl: string) { + const [, scheme, user, host, rest = ""] = isSshRemoteUrl(remoteUrl) + ? (SSH_REMOTE.exec(remoteUrl.trim()) ?? []) + : []; + if (scheme === undefined || user === undefined || host === undefined) return remoteUrl; + const hostName = yield* Cache.get(hostNames, `${user}${host}`); + return hostName === null || + hostName === host.toLowerCase() || + (host.includes(".") && !input.isPublicHost(hostName)) + ? remoteUrl + : `${scheme}${user}${hostName}${rest}`; + }); +}); diff --git a/packages/source-control-github/src/server/GitHubSourceControlProvider.test.ts b/packages/source-control-github/src/server/GitHubSourceControlProvider.test.ts index 134966231ceb1..9f8636d486e4a 100644 --- a/packages/source-control-github/src/server/GitHubSourceControlProvider.test.ts +++ b/packages/source-control-github/src/server/GitHubSourceControlProvider.test.ts @@ -164,6 +164,8 @@ function harness(input: { readonly localBranches?: ReadonlyArray; readonly resolved?: string; readonly gitFailure?: unknown; + /** `HostName` by `Host`, as `ssh -G` reports them. */ + readonly sshConfig?: Readonly>; }) { const git: Array = []; const graphqlCalls: Array = []; @@ -203,9 +205,13 @@ function harness(input: { process: { run: (args) => Effect.succeed( - args.args[0] === "remote" - ? processOutput(input.remotes) - : processOutput(input.resolved ?? "", input.resolved ? 0 : 1), + args.command === "ssh" + ? processOutput( + `user git\nhostname ${input.sshConfig?.[args.args[1]!.replace(/^.*@/u, "")] ?? args.args[1]!.replace(/^.*@/u, "")}\n`, + ) + : args.args[0] === "remote" + ? processOutput(input.remotes) + : processOutput(input.resolved ?? "", input.resolved ? 0 : 1), ), }, }); @@ -325,10 +331,14 @@ describe("GitHubSourceControlProvider repository resolution", () => { ["github", "git@github:acme/web.git"], ["github.com-scale", "git@github.com-scale:acme/web.git"], ["github.com-scale", "ssh://git@github.com-scale:22/acme/web.git"], + // Only ssh config says where these two lead. + ["gh-work", "git@gh-work:acme/web.git"], + ["github.work", "ssh://git@github.work/acme/web.git"], ] as const)("reads an SSH alias on %s through github.com (%s)", ([host, remoteUrl]) => { const hosts: string[] = []; const { layer } = harness({ remotes: remotesOutput(["origin", remoteUrl]), + sshConfig: { "gh-work": "github.com", "github.work": "ssh.github.com" }, api: { rest: (input) => Effect.sync(() => { @@ -359,7 +369,16 @@ describe("GitHubSourceControlProvider repository resolution", () => { cwd: "/repo", context: { ...githubContext(host), remoteUrl }, }); - assert.deepStrictEqual(hosts, ["github.com repos/acme/web", "github.com repos/acme/web"]); + // A pasted alias remote is looked up on the host it leads to and cloned as written. + assert.deepStrictEqual( + yield* gh.getRepositoryCloneUrls({ cwd: "/repo", repository: ` ${remoteUrl} ` }), + { nameWithOwner: "acme/web", url: remoteUrl, sshUrl: remoteUrl }, + ); + assert.deepStrictEqual(hosts, [ + "github.com repos/acme/web", + "github.com repos/acme/web", + "github.com repos/acme/web", + ]); }).pipe(Effect.provide(layer)); }); diff --git a/packages/source-control-github/src/server/GitHubSourceControlProvider.ts b/packages/source-control-github/src/server/GitHubSourceControlProvider.ts index e510473aa756d..fc6e75f6a8cc2 100644 --- a/packages/source-control-github/src/server/GitHubSourceControlProvider.ts +++ b/packages/source-control-github/src/server/GitHubSourceControlProvider.ts @@ -305,29 +305,29 @@ export const makeDiscovery = Effect.gen(function* () { } satisfies SourceControlProviderDiscoveryItem); }), // Claim disabled hosts so errors identify the setting. - refineUnknownRemote: ({ context }) => { + refineUnknownRemote: Effect.fnUntraced(function* ({ context }) { const url = URL.parse(context.provider.baseUrl); - if (!url?.host) return Effect.succeed(null); - const host = gitHubApiHostForRemote(context.remoteUrl) ?? url.host; + if (!url?.host) return null; + const host = + gitHubApiHostForRemote(yield* sourceControlHost.canonicalRemoteUrl(context.remoteUrl)) ?? + url.host; // A timeout or keyring failure propagates: the registry does not keep that answer. - return api.credential(host).pipe( + const known = yield* api.credential(host).pipe( Effect.as(true), Effect.catchTags({ GitHubCliMissingError: () => Effect.succeed(false), GitHubHostDisabledError: () => Effect.succeed(true), GitHubNotSignedInError: () => Effect.succeed(false), }), - Effect.map((known) => - known - ? ({ - kind: SourceControlProviderKind.make("github"), - name: host === "github.com" ? "GitHub" : "GitHub Self-Hosted", - baseUrl: host === url.host ? context.provider.baseUrl : `https://${host}`, - } as const) - : null, - ), ); - }, + return known + ? ({ + kind: SourceControlProviderKind.make("github"), + name: host === "github.com" ? "GitHub" : "GitHub Self-Hosted", + baseUrl: host === url.host ? context.provider.baseUrl : `https://${host}`, + } as const) + : null; + }), } satisfies SourceControlManagedCliDiscoverySpec; }); @@ -518,6 +518,14 @@ export const make = Effect.gen(function* () { const git = sourceControlHost.git; const fileSystem = yield* FileSystem.FileSystem; + // An SSH alias is read as the host ssh config sends it to; git keeps the remote's name. + const canonicalFetchRemotes = (remotes: string) => + Effect.forEach(parseFetchRemotes(remotes), (remote) => + sourceControlHost + .canonicalRemoteUrl(remote.url) + .pipe(Effect.map((url) => ({ name: remote.name, url }))), + ); + const gitRead = (cwd: string, args: ReadonlyArray) => process.run({ operation: "GitHubSourceControlProvider.resolveRepository", @@ -538,6 +546,14 @@ export const make = Effect.gen(function* () { remotes: remotes?.exitCode === 0 ? remotes.stdout : "", resolved: resolved?.exitCode === 0 ? resolved.stdout : "", })), + Effect.flatMap((read) => + canonicalFetchRemotes(read.remotes).pipe( + Effect.map((remotes) => ({ + ...read, + remotes: remotes.map(({ name, url }) => `${name}\t${url} (fetch)`).join("\n"), + })), + ), + ), ); const resolveRepository = Effect.fn("GitHubSourceControlProvider.resolveRepository")( @@ -547,7 +563,18 @@ export const make = Effect.gen(function* () { readonly context?: SourceControlProvider.SourceControlProviderContext | undefined; }) { const envRepository = environment.GH_REPO?.trim(); - const hostHint = input.host ?? contextHost(input.context); + // A context made from the written remote names the alias, not the host it leads to. + const contextUrl = input.context?.remoteUrl; + const canonicalUrl = + contextUrl === undefined + ? undefined + : yield* sourceControlHost.canonicalRemoteUrl(contextUrl); + const hostHint = + (canonicalUrl === undefined || canonicalUrl === contextUrl + ? undefined + : (gitHubApiHostForRemote(canonicalUrl) ?? undefined)) ?? + input.host ?? + contextHost(input.context); const defaultHost = (hostHint ?? environment.GH_HOST ?? "github.com").toLowerCase(); if (envRepository) { const locator = parseGitHubRepositorySelector(envRepository, defaultHost); @@ -790,8 +817,9 @@ export const make = Effect.gen(function* () { Effect.map((result) => (result.exitCode === 0 ? result.stdout : "")), Effect.mapError(gitFailure), ); + const fetchRemotes = yield* canonicalFetchRemotes(remotes); const remoteFor = (nameWithOwner: string) => - parseFetchRemotes(remotes).find( + fetchRemotes.find( (remote) => (gitHubApiHostForRemote(remote.url) ?? normalizeGitRemoteUrl(remote.url).split("/")[0]) === base.host && @@ -959,7 +987,10 @@ export const make = Effect.gen(function* () { return SourceControlProvider.SourceControlProvider.of({ kind: SourceControlProviderKind.make("github"), resolveRemoteWebHost: Effect.fn("GitHubSourceControlProvider.resolveRemoteWebHost")( - ({ context }) => Effect.succeed(gitHubApiHostForRemote(context.remoteUrl)), + ({ context }) => + sourceControlHost + .canonicalRemoteUrl(context.remoteUrl) + .pipe(Effect.map(gitHubApiHostForRemote)), ), // GitHub matches bare heads; read a full page to disambiguate forks by owner. A default branch // collides with most forks, so its owner-qualified lookup has GitHub filter by owner instead. @@ -1095,6 +1126,17 @@ export const make = Effect.gen(function* () { ), getRepositoryCloneUrls: Effect.fn("GitHubSourceControlProvider.getRepositoryCloneUrls")( function* (input) { + // A pasted SSH remote is cloned as written, so an alias keeps choosing its key. + const sshRemote = input.repository.trim(); + if (isSshRemoteUrl(sshRemote)) { + const canonical = yield* sourceControlHost.canonicalRemoteUrl(sshRemote); + const [remoteHost, owner, name, ...rest] = normalizeGitRemoteUrl(canonical).split("/"); + const host = gitHubApiHostForRemote(canonical) ?? remoteHost; + if (!host || !owner || !name || rest.length > 0) + return yield* failure("Repositories are named owner/name."); + const repository = yield* readRepository({ host, owner, name }); + return { nameWithOwner: repository.full_name, url: sshRemote, sshUrl: sshRemote }; + } const fallbackHost = ( input.host ?? (yield* resolveRepository(input).pipe( diff --git a/packages/source-control-github/src/server/gitHubDiscovery.test.ts b/packages/source-control-github/src/server/gitHubDiscovery.test.ts index 3c34ddd05bb84..3c6e3513f39dc 100644 --- a/packages/source-control-github/src/server/gitHubDiscovery.test.ts +++ b/packages/source-control-github/src/server/gitHubDiscovery.test.ts @@ -21,14 +21,23 @@ const unknownContext = (baseUrl: string) => ({ remoteUrl: "git@code.example.test:team/project.git", }); -// The default host dies on any command, so refinement never reaches `gh`. +// The host dies on any command but the ssh config read, so refinement never reaches `gh`. const makeDiscovery = (credential: GitHubRest.GitHubRest["Service"]["credential"]) => GitHubSourceControlProvider.makeDiscovery.pipe( Effect.provide( Layer.mergeAll( Layer.mock(GitHubRest.GitHubRest)({ credential }), Layer.mock(GitHubAppSignIn.GitHubAppSignIn)({ enabled: false }), - TestSourceControlHost.layer(), + TestSourceControlHost.layer({ + process: { + run: (input) => + input.command === "ssh" + ? Effect.succeed( + TestSourceControlHost.processOutput(`hostname ${input.args.at(-1)}\n`), + ) + : Effect.die(`Unexpected ${input.command} run in ${input.operation}.`), + }, + }), ), ), Effect.provideService(HostProcess.Environment, {}), @@ -109,6 +118,49 @@ it.effect.each([ }), ); +it.effect("recognizes an SSH alias whose ssh config HostName is github.com", () => + Effect.gen(function* () { + const discovery = yield* GitHubSourceControlProvider.makeDiscovery.pipe( + Effect.provide( + Layer.mergeAll( + Layer.mock(GitHubRest.GitHubRest)({ + credential: (host) => + host === "github.com" + ? Effect.succeed({ token: Redacted.make("token"), fingerprint: `${host}:fp` }) + : Effect.fail(new GitHubCredentials.GitHubNotSignedInError({ host })), + }), + Layer.mock(GitHubAppSignIn.GitHubAppSignIn)({ enabled: false }), + TestSourceControlHost.layer({ + process: { + run: (input) => + Effect.succeed( + input.command === "ssh" && input.args.includes("-G") + ? TestSourceControlHost.processOutput( + "user git\nhostname github.com\nport 22\n", + ) + : TestSourceControlHost.processOutput("", { + exitCode: ChildProcessSpawner.ExitCode(1), + }), + ), + }, + }), + ), + ), + Effect.provideService(HostProcess.Environment, {}), + ); + if (discovery.type !== "managed-cli") return assert.fail("expected a managed CLI"); + const provider = yield* discovery.refineUnknownRemote({ + cwd: "/repo", + context: { ...unknownContext("https://gh-work"), remoteUrl: "git@gh-work:team/project.git" }, + }); + assert.deepStrictEqual(provider, { + kind: SourceControlProviderKind.make("github"), + name: "GitHub", + baseUrl: "https://github.com", + }); + }), +); + const decodeSettings = Schema.decodeSync(ServerSettings); const referencedTokenSettings = decodeSettings({ sourceControlHosts: { diff --git a/packages/source-control-gitlab/src/server/gitLabDiscovery.test.ts b/packages/source-control-gitlab/src/server/gitLabDiscovery.test.ts index f0fdcba5aa065..82c49024a55be 100644 --- a/packages/source-control-gitlab/src/server/gitLabDiscovery.test.ts +++ b/packages/source-control-gitlab/src/server/gitLabDiscovery.test.ts @@ -128,6 +128,18 @@ self-hosted.example.test stdout: `self-hosted.example.test:8443 ✓ Logged in to self-hosted.example.test:8443 as gitlab-user ✓ Token found: ****** +`, + exitCode: 0, + }, + { + // The registry places an SSH alias by its ssh config HostName and keeps the written remote. + name: "behind an SSH alias", + remoteUrl: "git@gitlab.example.com:group/project.git", + writtenRemoteUrl: "git@gitlab-work:group/project.git", + host: "gitlab.example.com", + stdout: `gitlab.example.com + ✓ Logged in to gitlab.example.com as gitlab-user + ✓ Token found: ****** `, exitCode: 0, }, @@ -142,7 +154,7 @@ self-hosted.example.test context: { provider: unknownRemoteProvider(remote), remoteName: "origin", - remoteUrl: scenario.remoteUrl, + remoteUrl: "writtenRemoteUrl" in scenario ? scenario.writtenRemoteUrl : scenario.remoteUrl, }, process: { run: ({ command, args }) => { diff --git a/packages/source-control-testing/src/TestSourceControlHost.ts b/packages/source-control-testing/src/TestSourceControlHost.ts index 37d491d8cc52d..726d07311da5d 100644 --- a/packages/source-control-testing/src/TestSourceControlHost.ts +++ b/packages/source-control-testing/src/TestSourceControlHost.ts @@ -9,6 +9,7 @@ import { } from "@t3tools/contracts"; import { applyServerSettingsPatch } from "@t3tools/shared/serverSettings"; import * as SourceControlHost from "@t3tools/source-control-core/server/SourceControlHost"; +import * as SshRemoteHost from "@t3tools/source-control-core/server/sshRemoteHost"; import * as Context from "effect/Context"; import * as Effect from "effect/Effect"; import * as Layer from "effect/Layer"; @@ -57,13 +58,16 @@ export const layer = ( const settings = yield* Ref.make(options.settings ?? DEFAULT_SERVER_SETTINGS); const update = (patch: ServerSettingsPatch) => Ref.updateAndGet(settings, (current) => applyServerSettingsPatch(current, patch)); + const run = + options.process?.run ?? + ((input) => Effect.die(`Unexpected ${input.command} run in ${input.operation}.`)); const host = SourceControlHost.SourceControlHost.of({ settings: { get: Ref.get(settings), getWithSecrets: () => Ref.get(settings), update }, - process: { - run: - options.process?.run ?? - ((input) => Effect.die(`Unexpected ${input.command} run in ${input.operation}.`)), - }, + process: { run }, + canonicalRemoteUrl: yield* SshRemoteHost.make({ + run, + isPublicHost: (hostName) => hostName === "github.com" || hostName === "gitlab.com", + }), git: { ...failingGit, ...options.git }, }); return Context.make(SourceControlHost.SourceControlHost, host).pipe(