Skip to content

Commit 757ebc4

Browse files
committed
fix(server): resolve SSH host aliases when detecting the source control provider
Provider detection matched only the literal remote host, so a remote using an SSH-config alias (e.g. git@github-personal:owner/repo) resolved to the unknown provider and listChangeRequests failed. Resolve unrecognized SSH hosts through ssh -G and classify the canonical hostname; canonical and non-SSH remotes are unaffected.
1 parent bf79434 commit 757ebc4

4 files changed

Lines changed: 136 additions & 17 deletions

File tree

‎apps/server/src/sourceControl/SourceControlProviderRegistry.test.ts‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,3 +293,38 @@ it.effect("falls back to a non-origin remote when origin is not configured", ()
293293
assert.strictEqual(provider.kind, "azure-devops");
294294
}),
295295
);
296+
297+
it.effect("resolves an SSH host alias before classifying the remote", () =>
298+
Effect.gen(function* () {
299+
const registry = yield* makeRegistry({
300+
remotes: [{ name: "origin", url: "git@github-personal:noueii/t3code.git" }],
301+
process: {
302+
run: (input) =>
303+
Effect.succeed(
304+
input.command === "ssh" && input.args.includes("github-personal")
305+
? processOutput("hostname github.com\n")
306+
: processOutput(""),
307+
),
308+
},
309+
});
310+
311+
const provider = yield* registry.resolve({ cwd: "/repo" });
312+
313+
assert.strictEqual(provider.kind, "github");
314+
}),
315+
);
316+
317+
it.effect("keeps a remote unknown when its SSH host alias cannot be resolved", () =>
318+
Effect.gen(function* () {
319+
const registry = yield* makeRegistry({
320+
remotes: [{ name: "origin", url: "git@github-personal:noueii/t3code.git" }],
321+
process: {
322+
run: () => Effect.succeed(processOutput("")),
323+
},
324+
});
325+
326+
const provider = yield* registry.resolve({ cwd: "/repo" });
327+
328+
assert.strictEqual(provider.kind, "unknown");
329+
}),
330+
);

‎apps/server/src/sourceControl/SourceControlProviderRegistry.ts‎

Lines changed: 76 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,13 @@ import {
88
SourceControlProviderError,
99
type SourceControlProviderDiscoveryItem,
1010
} from "@t3tools/contracts";
11-
import type { SourceControlProviderKind } from "@t3tools/contracts";
12-
import { detectSourceControlProviderFromRemoteUrl } from "@t3tools/shared/sourceControl";
11+
import type { SourceControlProviderInfo, SourceControlProviderKind } from "@t3tools/contracts";
12+
import {
13+
detectSourceControlProviderFromHost,
14+
detectSourceControlProviderFromRemoteUrl,
15+
isSshRemoteUrl,
16+
parseRemoteHost,
17+
} from "@t3tools/shared/sourceControl";
1318

1419
import * as AzureDevOpsSourceControlProvider from "./AzureDevOpsSourceControlProvider.ts";
1520
import * as BitbucketSourceControlProvider from "./BitbucketSourceControlProvider.ts";
@@ -124,18 +129,20 @@ function unsupportedProvider(
124129
});
125130
}
126131

132+
interface SourceControlRemoteCandidate {
133+
readonly name: string;
134+
readonly url: string;
135+
readonly provider: SourceControlProviderInfo | null;
136+
}
137+
127138
function selectProviderContext(
128-
remotes: ReadonlyArray<{
129-
readonly name: string;
130-
readonly url: string;
131-
}>,
139+
remotes: ReadonlyArray<SourceControlRemoteCandidate>,
132140
): SourceControlProvider.SourceControlProviderContext | null {
133141
const candidates: Array<SourceControlProvider.SourceControlProviderContext> = [];
134142
for (const remote of remotes) {
135-
const provider = detectSourceControlProviderFromRemoteUrl(remote.url);
136-
if (provider) {
143+
if (remote.provider) {
137144
candidates.push({
138-
provider,
145+
provider: remote.provider,
139146
remoteName: remote.name,
140147
remoteUrl: remote.url,
141148
});
@@ -150,6 +157,46 @@ function selectProviderContext(
150157
);
151158
}
152159

160+
// `ssh -G` prints the effective configuration for a host, which is how a
161+
// ~/.ssh/config `Host` alias (e.g. `github-personal`) maps back to its canonical
162+
// hostname. Only a hostname-shaped host is passed through, so an alias can never
163+
// smuggle an option into the command.
164+
const SSH_HOST_PATTERN = /^[a-z0-9][a-z0-9._-]*$/;
165+
const SSH_CONFIG_HOSTNAME_PATTERN = /^hostname\s+(\S+)\s*$/im;
166+
const SSH_HOST_RESOLUTION_TIMEOUT_MS = 5_000;
167+
168+
const resolveSshHostAlias = Effect.fn("SourceControlProviderRegistry.resolveSshHostAlias")(
169+
function* (input: {
170+
readonly process: VcsProcess.VcsProcess["Service"];
171+
readonly cwd: string;
172+
readonly host: string;
173+
}): Effect.fn.Return<string | null> {
174+
if (!SSH_HOST_PATTERN.test(input.host)) {
175+
return null;
176+
}
177+
178+
const output = yield* input.process
179+
.run({
180+
operation: "source-control.detect.resolve-ssh-host",
181+
command: "ssh",
182+
args: ["-G", input.host],
183+
cwd: input.cwd,
184+
allowNonZeroExit: true,
185+
timeoutMs: SSH_HOST_RESOLUTION_TIMEOUT_MS,
186+
maxOutputBytes: 16_000,
187+
appendTruncationMarker: true,
188+
})
189+
.pipe(Effect.orElseSucceed(() => null));
190+
191+
if (output === null || output.exitCode !== 0) {
192+
return null;
193+
}
194+
195+
const hostname = SSH_CONFIG_HOSTNAME_PATTERN.exec(output.stdout)?.[1]?.toLowerCase();
196+
return hostname && hostname.length > 0 ? hostname : null;
197+
},
198+
);
199+
153200
function bindProviderContext(
154201
provider: SourceControlProvider.SourceControlProvider["Service"],
155202
context: SourceControlProvider.SourceControlProviderContext | null,
@@ -235,7 +282,26 @@ export const makeWithProviders = Effect.fn("makeSourceControlProviderRegistryWit
235282
}),
236283
),
237284
);
238-
const context = selectProviderContext(remotes.remotes);
285+
const candidates = yield* Effect.forEach(remotes.remotes, (remote) =>
286+
Effect.gen(function* () {
287+
let provider = detectSourceControlProviderFromRemoteUrl(remote.url);
288+
if ((provider === null || provider.kind === "unknown") && isSshRemoteUrl(remote.url)) {
289+
const host = parseRemoteHost(remote.url);
290+
if (host !== null) {
291+
const canonicalHost = yield* resolveSshHostAlias({ process, cwd, host });
292+
if (canonicalHost !== null && canonicalHost !== host) {
293+
provider = detectSourceControlProviderFromHost(canonicalHost) ?? provider;
294+
}
295+
}
296+
}
297+
return {
298+
name: remote.name,
299+
url: remote.url,
300+
provider,
301+
} satisfies SourceControlRemoteCandidate;
302+
}),
303+
);
304+
const context = selectProviderContext(candidates);
239305

240306
return yield* refineUnknownRemoteProvider({
241307
specs: discoverySpecs,

‎packages/shared/src/sourceControl.test.ts‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { describe, expect, it } from "vite-plus/test";
22

33
import {
44
sourceControlRepositorySelector,
5+
detectSourceControlProviderFromHost,
56
detectSourceControlProviderFromRemoteUrl,
67
getChangeRequestTerminologyForKind,
78
isSshRemoteUrl,
@@ -140,6 +141,17 @@ describe("detectSourceControlProviderFromRemoteUrl", () => {
140141
});
141142
});
142143

144+
describe("detectSourceControlProviderFromHost", () => {
145+
it("classifies canonical and self-hosted hosts", () => {
146+
expect(detectSourceControlProviderFromHost("github.com")?.kind).toBe("github");
147+
expect(detectSourceControlProviderFromHost("gitlab.example.com")?.kind).toBe("gitlab");
148+
});
149+
150+
it("leaves SSH aliases unknown, because upstream resolves them first", () => {
151+
expect(detectSourceControlProviderFromHost("github-personal")?.kind).toBe("unknown");
152+
});
153+
});
154+
143155
describe("isSshRemoteUrl", () => {
144156
it("recognises SCP-like SSH URLs with any SSH user prefix", () => {
145157
expect(isSshRemoteUrl("[email protected]:owner/repo.git")).toBe(true);

‎packages/shared/src/sourceControl.ts‎

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ export function isSshRemoteUrl(remoteUrl: string): boolean {
133133
return SCP_SSH_REMOTE_PATTERN.test(trimmed) || trimmed.toLowerCase().startsWith("ssh://");
134134
}
135135

136-
function parseRemoteHost(remoteUrl: string): string | null {
136+
export function parseRemoteHost(remoteUrl: string): string | null {
137137
const trimmed = remoteUrl.trim();
138138
if (trimmed.length === 0) {
139139
return null;
@@ -191,13 +191,9 @@ function isBitbucketHost(host: string): boolean {
191191
return host === "bitbucket.org" || hasDnsLabel(host, "bitbucket");
192192
}
193193

194-
export function detectSourceControlProviderFromRemoteUrl(
195-
remoteUrl: string,
194+
export function detectSourceControlProviderFromHost(
195+
host: string,
196196
): SourceControlProviderInfo | null {
197-
const host = parseRemoteHost(remoteUrl);
198-
if (!host) {
199-
return null;
200-
}
201197
const hostname = parseHostName(host);
202198

203199
if (isGitHubHost(hostname)) {
@@ -239,6 +235,16 @@ export function detectSourceControlProviderFromRemoteUrl(
239235
};
240236
}
241237

238+
export function detectSourceControlProviderFromRemoteUrl(
239+
remoteUrl: string,
240+
): SourceControlProviderInfo | null {
241+
const host = parseRemoteHost(remoteUrl);
242+
if (!host) {
243+
return null;
244+
}
245+
return detectSourceControlProviderFromHost(host);
246+
}
247+
242248
/**
243249
* The provider-native repository selector. `displayName` is the full path below the host, which
244250
* is what nested GitLab groups need; owner/name is the two-segment fallback for identities

0 commit comments

Comments
 (0)