Skip to content

Commit 9c2eaa9

Browse files
committed
fix(vcs): keep self-hosted ports and bound SSH alias probes
1 parent 11cbd8c commit 9c2eaa9

5 files changed

Lines changed: 170 additions & 6 deletions

File tree

‎apps/server/src/pullRequest/PullRequestService.test.ts‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,56 @@ function makeService(input: {
176176
);
177177
}
178178

179+
it("keeps a nonstandard port when deriving a host from a provider base URL", () => {
180+
assert.strictEqual(
181+
PullRequestService.hostnameFromProviderBaseUrl("https://github.company:8443"),
182+
"github.company:8443",
183+
);
184+
assert.strictEqual(
185+
PullRequestService.hostnameFromProviderBaseUrl("https://github.company:8443/api/v3"),
186+
"github.company:8443",
187+
);
188+
assert.strictEqual(
189+
PullRequestService.hostnameFromProviderBaseUrl("https://github.com"),
190+
"github.com",
191+
);
192+
});
193+
194+
it.effect("keeps a nonstandard port on a detected self-hosted GitHub host", () =>
195+
Effect.gen(function* () {
196+
const hosts: string[] = [];
197+
const selfHosted = project({
198+
id: "p1",
199+
title: "ghe",
200+
workspaceRoot: "/ghe",
201+
repository: "owner/repo",
202+
provider: "unknown",
203+
host: "github.company:8443",
204+
});
205+
const service = yield* makeService({
206+
projects: [selfHosted],
207+
providers: [
208+
fakeProvider("github", {
209+
listChangeRequests: (input) => {
210+
hosts.push(input.host);
211+
return Effect.succeed({ items: [], truncated: false, continues: true });
212+
},
213+
}),
214+
],
215+
resolveHandle: ({ context }) =>
216+
Effect.succeed({
217+
context: { ...context!, provider: { ...context!.provider, kind: "github" } },
218+
provider: undefined as never,
219+
}),
220+
});
221+
222+
const result = yield* service.list({ state: "open" });
223+
224+
assert.strictEqual(result.providers[0]?.host, "github.company:8443");
225+
assert.deepStrictEqual(hosts, ["github.company:8443"]);
226+
}),
227+
);
228+
179229
it.effect("refines unknown self-hosted GitLab projects before listing merge requests", () =>
180230
Effect.gen(function* () {
181231
let refinementCalls = 0;

‎apps/server/src/pullRequest/PullRequestService.ts‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -394,10 +394,14 @@ export function repositoryIdentityOf(project: OrchestrationProjectShell): string
394394
return identity.owner && identity.name ? `${identity.owner}/${identity.name}` : null;
395395
}
396396

397-
function hostnameFromProviderBaseUrl(baseUrl: string): string | null {
397+
/**
398+
* Host a detected provider base URL should be stored as. `URL.hostname` drops a
399+
* nonstandard port, so `https://github.company:8443` must use `URL.host`.
400+
*/
401+
export function hostnameFromProviderBaseUrl(baseUrl: string): string | null {
398402
try {
399-
const hostname = new URL(baseUrl).hostname.toLowerCase();
400-
return hostname.length > 0 ? hostname : null;
403+
const host = new URL(baseUrl).host.toLowerCase();
404+
return host.length > 0 ? host : null;
401405
} catch {
402406
return null;
403407
}

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

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,66 @@
11
import { assert, describe, it } from "@effect/vitest";
2+
import * as NodeServices from "@effect/platform-node/NodeServices";
23
import { detectSourceControlProviderFromRemoteUrl } from "@t3tools/shared/sourceControl";
4+
import * as Duration from "effect/Duration";
35
import * as Effect from "effect/Effect";
6+
import * as Fiber from "effect/Fiber";
7+
import * as Layer from "effect/Layer";
8+
import * as Sink from "effect/Sink";
9+
import * as Stream from "effect/Stream";
10+
import * as TestClock from "effect/testing/TestClock";
11+
import { ChildProcessSpawner } from "effect/unstable/process";
412

513
import {
614
gitRemoteSshAliasToResolve,
715
resolveGitRemoteForSourceControl,
16+
SSH_CONFIG_RESOLVE_TIMEOUT_MS,
817
} from "./resolveGitRemoteForSourceControl.ts";
918

19+
const encoder = new TextEncoder();
20+
21+
const makeFailedProcess = (input: { readonly stdout: string; readonly stderr?: string }) => {
22+
const stdoutStream = Stream.make(encoder.encode(input.stdout));
23+
const stderrStream = input.stderr ? Stream.make(encoder.encode(input.stderr)) : Stream.empty;
24+
return ChildProcessSpawner.makeHandle({
25+
pid: ChildProcessSpawner.ProcessId(123),
26+
stdout: stdoutStream,
27+
stderr: stderrStream,
28+
all: Stream.empty,
29+
exitCode: Effect.succeed(ChildProcessSpawner.ExitCode(1)),
30+
isRunning: Effect.succeed(false),
31+
kill: () => Effect.void,
32+
stdin: Sink.drain,
33+
getInputFd: () => Sink.drain,
34+
getOutputFd: () => Stream.empty,
35+
unref: Effect.succeed(Effect.void),
36+
});
37+
};
38+
39+
const makeNeverFinishingProcess = () => {
40+
let finish: ((exitCode: ChildProcessSpawner.ExitCode) => void) | null = null;
41+
return ChildProcessSpawner.makeHandle({
42+
pid: ChildProcessSpawner.ProcessId(123),
43+
stdout: Stream.empty,
44+
stderr: Stream.empty,
45+
all: Stream.empty,
46+
exitCode: Effect.callback<ChildProcessSpawner.ExitCode>((resume) => {
47+
finish = (exitCode) => resume(Effect.succeed(exitCode));
48+
return Effect.sync(() => {
49+
finish = null;
50+
});
51+
}),
52+
isRunning: Effect.succeed(true),
53+
kill: () =>
54+
Effect.sync(() => {
55+
finish?.(ChildProcessSpawner.ExitCode(143));
56+
}),
57+
stdin: Sink.drain,
58+
getInputFd: () => Sink.drain,
59+
getOutputFd: () => Stream.empty,
60+
unref: Effect.succeed(Effect.void),
61+
});
62+
};
63+
1064
describe("gitRemoteSshAliasToResolve", () => {
1165
it("selects undotted SSH aliases and skips real hostnames", () => {
1266
assert.strictEqual(
@@ -66,4 +120,43 @@ describe("resolveGitRemoteForSourceControl", () => {
66120
assert.strictEqual(resolveCalls, 0);
67121
}),
68122
);
123+
124+
it.effect("leaves the original remote when ssh -G fails", () => {
125+
const spawner = ChildProcessSpawner.make(() =>
126+
Effect.succeed(makeFailedProcess({ stdout: "", stderr: "ssh: Could not resolve hostname" })),
127+
);
128+
const processLayer = Layer.mergeAll(
129+
NodeServices.layer,
130+
Layer.succeed(ChildProcessSpawner.ChildProcessSpawner, spawner),
131+
);
132+
133+
return Effect.gen(function* () {
134+
const original = "git@github-personal:owner/repo.git";
135+
const rewritten = yield* resolveGitRemoteForSourceControl(original);
136+
137+
assert.strictEqual(rewritten, original);
138+
assert.strictEqual(detectSourceControlProviderFromRemoteUrl(rewritten)?.kind, "unknown");
139+
}).pipe(Effect.provide(processLayer));
140+
});
141+
142+
it.effect("leaves the original remote when ssh -G times out", () => {
143+
const spawner = ChildProcessSpawner.make(() => Effect.succeed(makeNeverFinishingProcess()));
144+
const processLayer = Layer.mergeAll(
145+
NodeServices.layer,
146+
Layer.succeed(ChildProcessSpawner.ChildProcessSpawner, spawner),
147+
TestClock.layer(),
148+
);
149+
150+
return Effect.gen(function* () {
151+
const original = "git@github-personal:owner/repo.git";
152+
const fiber = yield* Effect.forkChild(resolveGitRemoteForSourceControl(original));
153+
yield* Effect.yieldNow;
154+
yield* TestClock.adjust(Duration.millis(SSH_CONFIG_RESOLVE_TIMEOUT_MS));
155+
156+
const rewritten = yield* Fiber.join(fiber);
157+
158+
assert.strictEqual(rewritten, original);
159+
assert.strictEqual(detectSourceControlProviderFromRemoteUrl(rewritten)?.kind, "unknown");
160+
}).pipe(Effect.provide(processLayer));
161+
});
69162
});

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,14 @@ export function gitRemoteSshAliasToResolve(remoteUrl: string): string | null {
5858
return hostname;
5959
}
6060

61+
/** Same budget as `RepositoryIdentityResolver`'s `ssh -G` probe. */
62+
export const SSH_CONFIG_RESOLVE_TIMEOUT_MS = 5_000;
63+
6164
export const resolveSshHostnameFromConfig: ResolveSshHostname<ResolveGitRemoteServices> = (alias) =>
62-
resolveSshTarget(alias).pipe(
65+
resolveSshTarget(alias, {
66+
timeoutMs: SSH_CONFIG_RESOLVE_TIMEOUT_MS,
67+
fallbackOnError: false,
68+
}).pipe(
6369
Effect.map((target) => {
6470
const hostname = target.hostname.trim();
6571
return hostname.length > 0 ? hostname : null;

‎packages/ssh/src/command.ts‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,7 @@ export const runSshCommand = Effect.fn("ssh/command.runSshCommand")(function* (
327327

328328
export const resolveSshTarget = Effect.fn("ssh/command.resolveSshTarget")(function* (
329329
alias: string,
330+
options?: { readonly timeoutMs?: number; readonly fallbackOnError?: boolean },
330331
): Effect.fn.Return<
331332
DesktopSshEnvironmentTarget,
332333
SshCommandError | SshInvalidTargetError,
@@ -338,19 +339,29 @@ export const resolveSshTarget = Effect.fn("ssh/command.resolveSshTarget")(functi
338339
}
339340

340341
yield* Effect.logDebug("ssh.target.resolve.start", { alias: trimmedAlias });
341-
return yield* runSshCommand(
342+
const resolved = runSshCommand(
342343
{
343344
alias: trimmedAlias,
344345
hostname: trimmedAlias,
345346
username: null,
346347
port: null,
347348
},
348-
{ preHostArgs: ["-G"] },
349+
{
350+
preHostArgs: ["-G"],
351+
...(options?.timeoutMs === undefined ? {} : { timeoutMs: options.timeoutMs }),
352+
},
349353
).pipe(
350354
Effect.map((result) => parseSshResolveOutput(trimmedAlias, result.stdout)),
351355
Effect.tap((target) =>
352356
Effect.logDebug("ssh.target.resolve.succeeded", sshTargetLogFields(target)),
353357
),
358+
);
359+
360+
if (options?.fallbackOnError === false) {
361+
return yield* resolved;
362+
}
363+
364+
return yield* resolved.pipe(
354365
Effect.catch((cause) =>
355366
Effect.logDebug("ssh.target.resolve.fallback", { alias: trimmedAlias, cause }).pipe(
356367
Effect.as({

0 commit comments

Comments
 (0)