Skip to content

Commit aa92be3

Browse files
mwolsononly21milJorrinKievit
committed
fix(server): Stop re-probing hosting CLIs for unrecognized hosts
Remember the unknown-remote refinement verdict for five minutes, shared by checkouts whose remotes have the same server and path prefix. Callers that arrive while a probe runs wait on it. A missing CLI, a missing GitHub login, or glab reporting no login for the host counts as an answer. A timeout, an API or keyring failure, or a claim reached after an earlier check failed is not cached. glab is asked about the remote's own host, spelled as the remote spells it, and a Settings change clears the cache. Fixes #12060 Co-authored-by: Victor Vogel <[email protected]> Co-authored-by: Jorrin <[email protected]>
1 parent 40a7908 commit aa92be3

7 files changed

Lines changed: 482 additions & 52 deletions

File tree

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

Lines changed: 310 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,23 @@ import { SourceControlProviderKind } from "@t3tools/contracts";
22
import { assert, it } from "@effect/vitest";
33
import * as NodeServices from "@effect/platform-node/NodeServices";
44
import * as DateTime from "effect/DateTime";
5+
import * as Deferred from "effect/Deferred";
56
import * as Effect from "effect/Effect";
7+
import * as Fiber from "effect/Fiber";
68
import * as Layer from "effect/Layer";
79
import * as Option from "effect/Option";
10+
import * as PlatformError from "effect/PlatformError";
11+
import * as PubSub from "effect/PubSub";
812
import * as Redacted from "effect/Redacted";
13+
import * as Stream from "effect/Stream";
14+
import * as TestClock from "effect/testing/TestClock";
915
import { ChildProcessSpawner } from "effect/process";
10-
import { VcsRepositoryDetectionError } from "@t3tools/contracts";
16+
import {
17+
DEFAULT_SERVER_SETTINGS,
18+
VcsProcessSpawnError,
19+
VcsProcessTimeoutError,
20+
VcsRepositoryDetectionError,
21+
} from "@t3tools/contracts";
1122

1223
import * as ServerSettings from "../serverSettings.ts";
1324
import * as ServerConfig from "../config.ts";
@@ -54,6 +65,9 @@ function makeRegistry(input: {
5465
readonly process?: Partial<VcsProcess.VcsProcess["Service"]>;
5566
readonly githubApi?: Partial<GitHubApi.GitHubApi["Service"]>;
5667
readonly gitlab?: Partial<GitLabCli.GitLabCli["Service"]>;
68+
readonly forgejo?: Partial<ForgejoCli.ForgejoCli["Service"]>;
69+
readonly settings?: Partial<ServerSettings.ServerSettingsService["Service"]>;
70+
readonly started?: Effect.Effect<unknown>;
5771
readonly resolve?: VcsDriverRegistry.VcsDriverRegistry["Service"]["resolve"];
5872
}) {
5973
const driver = {
@@ -99,6 +113,7 @@ function makeRegistry(input: {
99113
});
100114

101115
return SourceControlProviderRegistry.make.pipe(
116+
Effect.tap(input.started ?? Effect.void),
102117
Effect.provide(
103118
Layer.mergeAll(
104119
NodeServices.layer,
@@ -118,7 +133,9 @@ function makeRegistry(input: {
118133
Layer.mock(AzureDevOpsPullRequestCli.AzureDevOpsPullRequestCli)({}),
119134
Layer.mock(BitbucketApi.BitbucketApi)({}),
120135
Layer.mock(BitbucketPullRequestApi.BitbucketPullRequestApi)({}),
121-
ServerSettings.ServerSettingsService.layerTest(),
136+
input.settings
137+
? Layer.mock(ServerSettings.ServerSettingsService)(input.settings)
138+
: ServerSettings.ServerSettingsService.layerTest(),
122139
Layer.mock(GitHubPullRequestApi.GitHubPullRequestApi)({}),
123140
Layer.mock(GitHubApi.GitHubApi)({
124141
// No GitHub credential unless a test supplies one, so custom hosts stay unclaimed.
@@ -128,7 +145,10 @@ function makeRegistry(input: {
128145
Layer.mock(GitVcsDriver.GitVcsDriver)({}),
129146
Layer.mock(GitLabCli.GitLabCli)(input.gitlab ?? {}),
130147
Layer.mock(GitLabPullRequestCli.GitLabPullRequestCli)({}),
131-
Layer.mock(ForgejoCli.ForgejoCli)({ listLogins: () => Effect.succeed([]) }),
148+
Layer.mock(ForgejoCli.ForgejoCli)({
149+
listLogins: () => Effect.succeed([]),
150+
...input.forgejo,
151+
}),
132152
Layer.mock(GitCafeApi.GitCafeApi)({}),
133153
Layer.mock(GitCafeCredentials.GitCafeCredentials)({
134154
cliEnv: () => Effect.succeed({}),
@@ -459,3 +479,290 @@ it.effect("skips GitHub discovery for the identity resolver's empty base URL", (
459479
assert.deepStrictEqual(hosts, ["githubenterprise.dev.example.com"]);
460480
}),
461481
);
482+
483+
const notFound = (command: string, cwd: string, module = "ChildProcess", method = "spawn") =>
484+
new VcsProcessSpawnError({
485+
operation: "test",
486+
command,
487+
cwd,
488+
cause: PlatformError.systemError({ _tag: "NotFound", module, method }),
489+
});
490+
491+
const resolveUnknown = (
492+
registry: SourceControlProviderRegistry.SourceControlProviderRegistry["Service"],
493+
repository: string,
494+
host = "git.example.internal",
495+
) =>
496+
registry.resolveHandle({
497+
cwd: `/${repository}`,
498+
context: {
499+
provider: {
500+
kind: SourceControlProviderKind.make("unknown"),
501+
name: host.toLowerCase(),
502+
baseUrl: `https://${host.toLowerCase()}`,
503+
},
504+
remoteName: "origin",
505+
remoteUrl: `git@${host}:team/${repository}.git`,
506+
},
507+
});
508+
509+
it.effect("asks once for every checkout of a server no integration claims", () =>
510+
Effect.gen(function* () {
511+
const asked: Array<string> = [];
512+
const release = yield* Deferred.make<void>();
513+
const registry = yield* makeRegistry({
514+
remotes: [],
515+
process: {
516+
run: ({ command, cwd }) => {
517+
asked.push(command);
518+
return Deferred.await(release).pipe(Effect.andThen(Effect.fail(notFound(command, cwd))));
519+
},
520+
},
521+
forgejo: {
522+
listLogins: ({ command = "tea", cwd }) => {
523+
asked.push(command);
524+
const cause = notFound(command, cwd);
525+
return Effect.fail(new ForgejoCli.ForgejoCliError({ command, cwd, detail: "", cause }));
526+
},
527+
},
528+
});
529+
530+
// Each caller runs until it waits, so once all have started the first probe is still open.
531+
const started = yield* Effect.forEach(["a", "b", "c"], () => Deferred.make<void>());
532+
const callers = yield* Effect.forEach(
533+
["a", "b", "c"],
534+
(repository, index) =>
535+
Deferred.succeed(started[index]!, undefined).pipe(
536+
Effect.andThen(resolveUnknown(registry, repository)),
537+
),
538+
{ concurrency: "unbounded" },
539+
).pipe(Effect.forkChild);
540+
yield* Effect.forEach(started, Deferred.await);
541+
assert.deepStrictEqual(asked, ["glab"]);
542+
yield* Deferred.succeed(release, undefined);
543+
const handles = yield* Fiber.join(callers);
544+
yield* resolveUnknown(registry, "d");
545+
assert.deepStrictEqual(
546+
handles.map((handle) => [handle.provider.kind, handle.context?.remoteUrl]),
547+
["a", "b", "c"].map((name) => ["unknown", `[email protected]:team/${name}.git`]),
548+
);
549+
assert.deepStrictEqual(asked, ["glab", "fj", "tea"]);
550+
551+
// A login made in a terminal sends no signal, so the server is asked again later.
552+
yield* TestClock.adjust("5 minutes");
553+
yield* resolveUnknown(registry, "a");
554+
assert.strictEqual(asked.length, 6);
555+
}),
556+
);
557+
558+
it.effect.each(["tea", "gh"] as const)("asks again after %s could not run", (failing) =>
559+
Effect.gen(function* () {
560+
let attempts = 0;
561+
const fail = <E>(error: E) => {
562+
attempts += 1;
563+
return Effect.fail(error);
564+
};
565+
const registry = yield* makeRegistry({
566+
remotes: [],
567+
process: { run: ({ command, cwd }) => Effect.fail(notFound(command, cwd)) },
568+
githubApi: {
569+
credential: (host) =>
570+
failing === "gh"
571+
? fail(new GitHubCredentials.GitHubCliFailedError({ host, cause: "timeout" }))
572+
: Effect.fail(new GitHubCredentials.GitHubNotSignedInError({ host })),
573+
},
574+
forgejo: {
575+
listLogins: ({ command, cwd }) =>
576+
failing === "tea" && command !== "fj"
577+
? fail(new ForgejoCli.ForgejoCliError({ command: "tea", cwd, detail: "Failed." }))
578+
: Effect.succeed([]),
579+
},
580+
});
581+
for (const repository of ["a", "b"]) {
582+
const handle = yield* resolveUnknown(registry, repository);
583+
assert.strictEqual(handle.provider.kind, "unknown");
584+
}
585+
assert.strictEqual(attempts, 2);
586+
}),
587+
);
588+
589+
const glabReply = (stderr: string, exitCode = 1) =>
590+
Effect.succeed(processOutput("", { stderr, exitCode: ChildProcessSpawner.ExitCode(exitCode) }));
591+
const glabLogin = (host = "git.example.internal") =>
592+
glabReply(`${host}\n ✓ Logged in to ${host} as maria (keyring)\n`, 0);
593+
594+
it.effect.each([
595+
{ name: "a login", replies: [glabLogin()], found: ["gitlab", "gitlab"], asks: 1 },
596+
{
597+
name: "a login under the remote's spelling",
598+
host: "Git.Example.Internal",
599+
replies: [glabLogin("Git.Example.Internal")],
600+
found: ["gitlab", "gitlab"],
601+
asks: 1,
602+
},
603+
...[
604+
["no login", "x git.example.internal has not been authenticated with glab"],
605+
["no login on glab 1.22", "x git.example.internal not authenticated with glab"],
606+
["no instances", "no GitLab instances have been authenticated with glab"],
607+
["no instance on glab 1.22", "No GitLab instance has been authenticated with glab"],
608+
].map(([name, message]) => ({
609+
name,
610+
host: undefined,
611+
replies: [glabReply(message!)],
612+
found: ["unknown", "unknown"],
613+
asks: 1,
614+
})),
615+
{
616+
name: "a failed check",
617+
replies: [glabReply("git.example.internal\n x git.example.internal: API call failed\n")],
618+
found: ["unknown", "unknown"],
619+
asks: 2,
620+
},
621+
{
622+
name: "a timeout",
623+
replies: [
624+
Effect.fail(
625+
new VcsProcessTimeoutError({ operation: "test", command: "glab", cwd: "/a", timeoutMs: 1 }),
626+
),
627+
],
628+
found: ["unknown", "unknown"],
629+
asks: 2,
630+
},
631+
{
632+
name: "a check failed in a missing checkout",
633+
replies: [Effect.fail(notFound("glab", "/a", "FileSystem", "access")), glabLogin()],
634+
found: ["unknown", "gitlab"],
635+
asks: 2,
636+
},
637+
])("asks glab about the remote's own host and keeps $name", (scenario) =>
638+
Effect.gen(function* () {
639+
const host = scenario.host ?? "git.example.internal";
640+
const asked: Array<ReadonlyArray<string>> = [];
641+
const registry = yield* makeRegistry({
642+
remotes: [],
643+
process: {
644+
// glab holds a login only under the host's exact spelling.
645+
run: ({ args, cwd }) => {
646+
asked.push([cwd, ...args]);
647+
return args[3] === host
648+
? (scenario.replies[asked.length - 1] ?? scenario.replies[0]!)
649+
: glabReply("x git.example.internal has not been authenticated with glab");
650+
},
651+
},
652+
});
653+
const found = yield* Effect.forEach(["a", "b"], (repository) =>
654+
resolveUnknown(registry, repository, host).pipe(Effect.map(({ provider }) => provider.kind)),
655+
);
656+
assert.deepStrictEqual(found, scenario.found);
657+
assert.deepStrictEqual(
658+
asked,
659+
["/a", "/b"]
660+
.slice(0, scenario.asks)
661+
.map((cwd) => [cwd, "auth", "status", "--hostname", host]),
662+
);
663+
}),
664+
);
665+
666+
it.effect("does not keep a tea login found while fj could not be read", () =>
667+
Effect.gen(function* () {
668+
let fjReadable = false;
669+
const loginAt = (port: number) => ({
670+
name: "login",
671+
url: `https://git.example.internal:${port}`,
672+
ssh_host: "git.example.internal",
673+
user: "maria",
674+
default: "true",
675+
});
676+
const registry = yield* makeRegistry({
677+
remotes: [],
678+
process: { run: ({ command, cwd }) => Effect.fail(notFound(command, cwd)) },
679+
forgejo: {
680+
listLogins: ({ command, cwd }) =>
681+
command === "fj" && !fjReadable
682+
? Effect.fail(new ForgejoCli.ForgejoCliError({ command, cwd, detail: "Unreadable." }))
683+
: Effect.succeed([loginAt(command === "fj" ? 4000 : 3000)]),
684+
},
685+
});
686+
const fallback = yield* resolveUnknown(registry, "a");
687+
assert.strictEqual(fallback.context?.provider.baseUrl, "https://git.example.internal:3000");
688+
fjReadable = true;
689+
const recovered = yield* resolveUnknown(registry, "b");
690+
assert.strictEqual(recovered.context?.provider.baseUrl, "https://git.example.internal:4000");
691+
}),
692+
);
693+
694+
it.effect.each([
695+
{ first: "https://host-a.example/repo.git", second: "https://host-b.example/repo.git" },
696+
{ first: "ssh://[email protected]/repo.git", second: "ssh://[email protected]/repo.git" },
697+
{ first: "https://host.example:8443/repo.git", second: "https://host.example:9443/repo.git" },
698+
])("keeps the lookups for $first and $second apart", ({ first, second }) =>
699+
Effect.gen(function* () {
700+
const asked: Array<string | undefined> = [];
701+
const registry = yield* makeRegistry({
702+
remotes: [],
703+
process: { run: ({ command, cwd }) => Effect.fail(notFound(command, cwd)) },
704+
forgejo: {
705+
listLogins: ({ command, remoteUrl }) => {
706+
if (command === "tea") asked.push(remoteUrl);
707+
return Effect.succeed([]);
708+
},
709+
},
710+
});
711+
for (const remoteUrl of [first, second]) {
712+
// The identity resolver's context, where the remote alone names the server.
713+
const provider = {
714+
kind: SourceControlProviderKind.make("unknown"),
715+
name: "Unknown",
716+
baseUrl: "",
717+
};
718+
yield* registry.resolveHandle({
719+
cwd: "/repo",
720+
context: { provider, remoteName: "origin", remoteUrl },
721+
});
722+
}
723+
assert.deepStrictEqual(asked, [first, second]);
724+
}),
725+
);
726+
727+
it.effect("asks again once Settings change", () =>
728+
Effect.gen(function* () {
729+
const changes = yield* PubSub.unbounded<typeof DEFAULT_SERVER_SETTINGS>();
730+
const handled = yield* Effect.forEach([0, 1], () => Deferred.make<void>());
731+
let changeCount = 0;
732+
const markHandled = Effect.suspend(() => Deferred.succeed(handled[changeCount++]!, undefined));
733+
let signedIn = false;
734+
const registry = yield* makeRegistry({
735+
remotes: [],
736+
settings: {
737+
subscribeChanges: PubSub.subscribe(changes).pipe(
738+
Effect.map((subscription) =>
739+
// A change is marked handled once the registry asks for the next one.
740+
Stream.fromSubscription(subscription).pipe(
741+
Stream.flatMap((settings) =>
742+
Stream.concat(Stream.make(settings), Stream.fromEffectDrain(markHandled)),
743+
),
744+
),
745+
),
746+
),
747+
},
748+
// Saved as the registry finishes starting, before its consumer has run.
749+
started: PubSub.publish(changes, DEFAULT_SERVER_SETTINGS),
750+
process: { run: ({ command, cwd }) => Effect.fail(notFound(command, cwd)) },
751+
githubApi: {
752+
credential: (host) =>
753+
signedIn
754+
? Effect.succeed({ token: Redacted.make("token"), fingerprint: `${host}:fingerprint` })
755+
: Effect.fail(new GitHubCredentials.GitHubNotSignedInError({ host })),
756+
},
757+
});
758+
yield* Deferred.await(handled[0]!);
759+
assert.strictEqual((yield* resolveUnknown(registry, "a")).provider.kind, "unknown");
760+
signedIn = true;
761+
assert.strictEqual((yield* resolveUnknown(registry, "b")).provider.kind, "unknown");
762+
763+
// A token saved for the host in Settings.
764+
yield* PubSub.publish(changes, DEFAULT_SERVER_SETTINGS);
765+
yield* Deferred.await(handled[1]!);
766+
assert.strictEqual((yield* resolveUnknown(registry, "c")).provider.kind, "github");
767+
}),
768+
);

0 commit comments

Comments
 (0)