Skip to content
TypeScript

fix(gitlab): a signed-out default glab host no longer hides other signed-in hosts (#27840)

Julius Marminge9def583f8e31
diff --git a/packages/source-control-core/src/server/discovery.ts b/packages/source-control-core/src/server/discovery.ts
index 9a890117175..d5e58c2282b 100644
--- a/packages/source-control-core/src/server/discovery.ts
+++ b/packages/source-control-core/src/server/discovery.ts
@@ -41,6 +41,8 @@ export type SourceControlCliDiscoverySpec = SourceControlDiscoverySpecBase & {
readonly executable: string;
readonly versionArgs: ReadonlyArray<string>;
readonly authArgs: ReadonlyArray<string>;
+ /** Asked once when `authArgs` reports signed out; its answer counts only when it finds a sign-in. */
+ readonly authRetryArgs?: ReadonlyArray<string>;
readonly remoteRefinementArgs?: (
context: SourceControlProvider.SourceControlProviderContext,
) => ReadonlyArray<string>;
@@ -269,6 +271,37 @@ export function probeSourceControlProvider(input: {
}
const spec = input.spec;
+ const runAuth = (args: ReadonlyArray<string>) =>
+ input.process
+ .run({
+ operation: "source-control.discovery.auth",
+ command: spec.executable,
+ args,
+ cwd: input.cwd,
+ allowNonZeroExit: true,
+ timeoutMs: probeTimeoutMs(spec),
+ maxOutputBytes: 8_000,
+ appendTruncationMarker: true,
+ })
+ .pipe(
+ Effect.map(spec.parseAuth),
+ Effect.catch((cause) =>
+ Effect.succeed(unknownAuth(Option.getOrUndefined(detailFromCause(cause)))),
+ ),
+ );
+ const retryArgs = spec.authRetryArgs;
+ const auth =
+ retryArgs === undefined
+ ? runAuth(spec.authArgs)
+ : runAuth(spec.authArgs).pipe(
+ Effect.filterOrElse(
+ (result) => result.status !== "unauthenticated",
+ (signedOut) =>
+ runAuth(retryArgs).pipe(
+ Effect.map((retry) => (retry.status === "authenticated" ? retry : signedOut)),
+ ),
+ ),
+ );
// The auth check does not depend on the version output, so both spawn at once.
return Effect.all(
@@ -278,23 +311,7 @@ export function probeSourceControlProvider(input: {
process: input.process,
cwd: input.cwd,
}),
- input.process
- .run({
- operation: "source-control.discovery.auth",
- command: spec.executable,
- args: spec.authArgs,
- cwd: input.cwd,
- allowNonZeroExit: true,
- timeoutMs: probeTimeoutMs(spec),
- maxOutputBytes: 8_000,
- appendTruncationMarker: true,
- })
- .pipe(
- Effect.map(spec.parseAuth),
- Effect.catch((cause) =>
- Effect.succeed(unknownAuth(Option.getOrUndefined(detailFromCause(cause)))),
- ),
- ),
+ auth,
],
{ concurrency: "unbounded" },
).pipe(
diff --git a/packages/source-control-gitlab/src/server/GitLabSourceControlProvider.ts b/packages/source-control-gitlab/src/server/GitLabSourceControlProvider.ts
index beeb6b38af1..21c499cae9e 100644
--- a/packages/source-control-gitlab/src/server/GitLabSourceControlProvider.ts
+++ b/packages/source-control-gitlab/src/server/GitLabSourceControlProvider.ts
@@ -146,7 +146,11 @@ export const discovery = {
label: "GitLab",
executable: "glab",
versionArgs: ["--version"],
+ // Without --all, glab 1.79+ checks only the current host unless that is gitlab.com; older
+ // glab lists every host and rejects --all. --all waits on every host, so ask it only after
+ // the current host reports signed out.
authArgs: ["auth", "status"],
+ authRetryArgs: ["auth", "status", "--all"],
// glab host matching requires the remote's exact spelling.
remoteRefinementArgs: ({ provider, remoteUrl }) => {
const authority = /^[a-z][a-z0-9+.-]*:\/\/([^/]+)/iu.exec(remoteUrl.trim())?.[1];
diff --git a/packages/source-control-gitlab/src/server/gitLabDiscovery.test.ts b/packages/source-control-gitlab/src/server/gitLabDiscovery.test.ts
index a30f676c9c9..f0fdcba5aa0 100644
--- a/packages/source-control-gitlab/src/server/gitLabDiscovery.test.ts
+++ b/packages/source-control-gitlab/src/server/gitLabDiscovery.test.ts
@@ -2,7 +2,10 @@ import {
parseGitRemoteHost,
unknownRemoteProvider,
} from "@t3tools/source-control-core/shared/definition";
-import { refineUnknownRemoteProvider } from "@t3tools/source-control-core/server/discovery";
+import {
+ probeSourceControlProvider,
+ refineUnknownRemoteProvider,
+} from "@t3tools/source-control-core/server/discovery";
import { processOutput } from "@t3tools/source-control-testing/TestSourceControlHost";
import { assert, it } from "@effect/vitest";
import * as Effect from "effect/Effect";
@@ -21,6 +24,89 @@ Logged in to gitlab.com as gitlab-user
assert.deepStrictEqual(auth.account, Option.some("gitlab-user"));
});
+// Without `--all`, glab 1.79+ checks only the current host unless that is gitlab.com.
+it.effect("reports a signed-in host when glab's default host is signed out", () =>
+ Effect.gen(function* () {
+ const defaultHostOnly = `gitlab.gnome.org
+ x gitlab.gnome.org: API call failed: GET https://gitlab.gnome.org/api/v4/user: 401 {message: 401 Unauthorized}
+`;
+ const allHosts = `gitlab.com
+ ✓ Logged in to gitlab.com as gitlab-user (/home/user/.config/glab-cli/config.yml)
+${defaultHostOnly}`;
+ const item = yield* probeSourceControlProvider({
+ spec: GitLabSourceControlProvider.discovery,
+ cwd: "/repo",
+ process: {
+ run: ({ args }) =>
+ Effect.succeed(
+ args[0] === "--version"
+ ? processOutput("glab 1.124.0\n")
+ : processOutput(args.includes("--all") ? allHosts : defaultHostOnly, {
+ exitCode: ChildProcessSpawner.ExitCode(1),
+ }),
+ ),
+ },
+ });
+ assert.strictEqual(item.auth.status, "authenticated");
+ assert.deepStrictEqual(item.auth.account, Option.some("gitlab-user"));
+ assert.deepStrictEqual(item.auth.host, Option.some("gitlab.com"));
+ }),
+);
+
+it.effect("skips --all when the current glab host is signed in", () =>
+ Effect.gen(function* () {
+ const asked: Array<ReadonlyArray<string>> = [];
+ const item = yield* probeSourceControlProvider({
+ spec: GitLabSourceControlProvider.discovery,
+ cwd: "/repo",
+ process: {
+ run: ({ args }) => {
+ asked.push(args);
+ return Effect.succeed(
+ args[0] === "--version"
+ ? processOutput("glab 1.124.0\n")
+ : processOutput(`gitlab.example.test
+ ✓ Logged in to gitlab.example.test as gitlab-user
+`),
+ );
+ },
+ },
+ });
+ assert.strictEqual(item.auth.status, "authenticated");
+ assert.isFalse(asked.some((args) => args.includes("--all")));
+ }),
+);
+
+it.effect("keeps glab's own sign-out detail when glab predates --all", () =>
+ Effect.gen(function* () {
+ const item = yield* probeSourceControlProvider({
+ spec: GitLabSourceControlProvider.discovery,
+ cwd: "/repo",
+ process: {
+ run: ({ args }) =>
+ Effect.succeed(
+ args[0] === "--version"
+ ? processOutput("glab 1.53.0\n")
+ : args.includes("--all")
+ ? processOutput("", {
+ stderr: "unknown flag: --all\n",
+ exitCode: ChildProcessSpawner.ExitCode(1),
+ })
+ : processOutput("", {
+ stderr: "No GitLab instances have been authenticated with glab.\n",
+ exitCode: ChildProcessSpawner.ExitCode(1),
+ }),
+ ),
+ },
+ });
+ assert.strictEqual(item.auth.status, "unauthenticated");
+ assert.deepStrictEqual(
+ item.auth.detail,
+ Option.some("No GitLab instances have been authenticated with glab."),
+ );
+ }),
+);
+
it.effect.each([
{
name: "without relying on host naming",