Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions apps/server/src/project/RepositoryIdentityResolver.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
);

Expand Down Expand Up @@ -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)),
);

Expand Down
53 changes: 37 additions & 16 deletions apps/server/src/project/RepositoryIdentityResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ export interface RepositoryIdentityResolverOptions {
readonly refine?: (
identity: RepositoryIdentity,
) => Effect.Effect<RepositoryIdentity, SourceControlProviderError>;
/** `SourceControlHost.canonicalRemoteUrl`; absent, remotes are read as written. */
readonly canonicalRemoteUrl?: (remoteUrl: string) => Effect.Effect<string>;
}

export class RepositoryIdentityResolver extends Context.Service<
Expand Down Expand Up @@ -77,6 +79,7 @@ function parseRemoteFetchUrls(stdout: string): Map<string, string> {
function pickMarkedRemote(
remotes: ReadonlyMap<string, string>,
resolved: string,
canonicalUrls: ReadonlyMap<string, string>,
): { 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());
Expand All @@ -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;
Expand All @@ -104,8 +109,9 @@ function pickMarkedRemote(
function pickPrimaryRemote(
remotes: ReadonlyMap<string, string>,
resolved: string,
canonicalUrls: ReadonlyMap<string, string>,
): { 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) {
Expand All @@ -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,
Expand Down Expand Up @@ -264,6 +276,7 @@ const resolveRepositoryIdentityFromCacheKey = Effect.fn(
"RepositoryIdentityResolver.resolveFromCacheKey",
)(function* (
cacheKey: RepositoryIdentityCacheKey,
canonicalRemoteUrl: (remoteUrl: string) => Effect.Effect<string>,
): Effect.fn.Return<RepositoryIdentity | null, never, ProcessRunner.ProcessRunner> {
const processRunner = yield* ProcessRunner.ProcessRunner;
const env = gitEnv(yield* HostProcess.Platform);
Expand All @@ -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* (
Expand All @@ -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<unknown>) =>
Exit.match(exit, {
onSuccess: (value) =>
Expand All @@ -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,
Expand Down
3 changes: 3 additions & 0 deletions apps/server/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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);
Expand Down
7 changes: 7 additions & 0 deletions apps/server/src/sourceControl/ServerSourceControlHost.ts
Original file line number Diff line number Diff line change
@@ -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";

Expand All @@ -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(
Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,10 @@ function makeRegistry(input: {
readonly discovery?: ReadonlyArray<SourceControlProviderDiscoverySpec>;
readonly run?: SourceControlHost.SourceControlHost["Service"]["process"]["run"];
readonly settings?: Partial<ServerSettings.ServerSettingsService["Service"]>;
/** `HostName` by `Host`, as `ssh -G` reports them; `run` is not asked. */
readonly sshConfig?: Readonly<Record<string, string>>;
}) {
const run = input.run ?? (() => Effect.succeed(TestSourceControlHost.processOutput("")));
const driver = {
listRemotes: () =>
Effect.succeed({
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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<string> = [];
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({
Expand Down
41 changes: 37 additions & 4 deletions apps/server/src/sourceControl/SourceControlProviderRegistry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<SourceControlProvider.SourceControlProviderContext> = [];
for (const remote of remotes) {
const provider = sourceControlDefinitions.recognizeRemote(remote.url);
const provider = sourceControlDefinitions.recognizeRemote(remote.canonicalUrl);
if (provider) {
candidates.push({
provider,
Expand Down Expand Up @@ -243,7 +245,7 @@ export const makeWithProviders = Effect.fn("makeSourceControlProviderRegistryWit
function* (registrations: ReadonlyArray<SourceControlProviderRegistration>) {
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,
Expand Down Expand Up @@ -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));
},
);

Expand All @@ -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");
Expand Down
4 changes: 4 additions & 0 deletions packages/source-control-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
5 changes: 5 additions & 0 deletions packages/source-control-core/src/server/SourceControlHost.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ export class SourceControlHost extends Context.Service<
input: SourceControlProcessInput,
) => Effect.Effect<SourceControlProcessOutput, VcsError>;
};
/**
* 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<string>;
readonly git: {
readonly execute: (input: {
readonly operation: string;
Expand Down
Loading
Loading