Skip to content

Commit 9def583

Browse files
fix(gitlab): a signed-out default glab host no longer hides other signed-in hosts (#27840)
Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent 58cf69c commit 9def583

3 files changed

Lines changed: 125 additions & 18 deletions

File tree

‎packages/source-control-core/src/server/discovery.ts‎

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ export type SourceControlCliDiscoverySpec = SourceControlDiscoverySpecBase & {
4141
readonly executable: string;
4242
readonly versionArgs: ReadonlyArray<string>;
4343
readonly authArgs: ReadonlyArray<string>;
44+
/** Asked once when `authArgs` reports signed out; its answer counts only when it finds a sign-in. */
45+
readonly authRetryArgs?: ReadonlyArray<string>;
4446
readonly remoteRefinementArgs?: (
4547
context: SourceControlProvider.SourceControlProviderContext,
4648
) => ReadonlyArray<string>;
@@ -269,6 +271,37 @@ export function probeSourceControlProvider(input: {
269271
}
270272

271273
const spec = input.spec;
274+
const runAuth = (args: ReadonlyArray<string>) =>
275+
input.process
276+
.run({
277+
operation: "source-control.discovery.auth",
278+
command: spec.executable,
279+
args,
280+
cwd: input.cwd,
281+
allowNonZeroExit: true,
282+
timeoutMs: probeTimeoutMs(spec),
283+
maxOutputBytes: 8_000,
284+
appendTruncationMarker: true,
285+
})
286+
.pipe(
287+
Effect.map(spec.parseAuth),
288+
Effect.catch((cause) =>
289+
Effect.succeed(unknownAuth(Option.getOrUndefined(detailFromCause(cause)))),
290+
),
291+
);
292+
const retryArgs = spec.authRetryArgs;
293+
const auth =
294+
retryArgs === undefined
295+
? runAuth(spec.authArgs)
296+
: runAuth(spec.authArgs).pipe(
297+
Effect.filterOrElse(
298+
(result) => result.status !== "unauthenticated",
299+
(signedOut) =>
300+
runAuth(retryArgs).pipe(
301+
Effect.map((retry) => (retry.status === "authenticated" ? retry : signedOut)),
302+
),
303+
),
304+
);
272305

273306
// The auth check does not depend on the version output, so both spawn at once.
274307
return Effect.all(
@@ -278,23 +311,7 @@ export function probeSourceControlProvider(input: {
278311
process: input.process,
279312
cwd: input.cwd,
280313
}),
281-
input.process
282-
.run({
283-
operation: "source-control.discovery.auth",
284-
command: spec.executable,
285-
args: spec.authArgs,
286-
cwd: input.cwd,
287-
allowNonZeroExit: true,
288-
timeoutMs: probeTimeoutMs(spec),
289-
maxOutputBytes: 8_000,
290-
appendTruncationMarker: true,
291-
})
292-
.pipe(
293-
Effect.map(spec.parseAuth),
294-
Effect.catch((cause) =>
295-
Effect.succeed(unknownAuth(Option.getOrUndefined(detailFromCause(cause)))),
296-
),
297-
),
314+
auth,
298315
],
299316
{ concurrency: "unbounded" },
300317
).pipe(

‎packages/source-control-gitlab/src/server/GitLabSourceControlProvider.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,11 @@ export const discovery = {
146146
label: "GitLab",
147147
executable: "glab",
148148
versionArgs: ["--version"],
149+
// Without --all, glab 1.79+ checks only the current host unless that is gitlab.com; older
150+
// glab lists every host and rejects --all. --all waits on every host, so ask it only after
151+
// the current host reports signed out.
149152
authArgs: ["auth", "status"],
153+
authRetryArgs: ["auth", "status", "--all"],
150154
// glab host matching requires the remote's exact spelling.
151155
remoteRefinementArgs: ({ provider, remoteUrl }) => {
152156
const authority = /^[a-z][a-z0-9+.-]*:\/\/([^/]+)/iu.exec(remoteUrl.trim())?.[1];

‎packages/source-control-gitlab/src/server/gitLabDiscovery.test.ts‎

Lines changed: 87 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ import {
22
parseGitRemoteHost,
33
unknownRemoteProvider,
44
} from "@t3tools/source-control-core/shared/definition";
5-
import { refineUnknownRemoteProvider } from "@t3tools/source-control-core/server/discovery";
5+
import {
6+
probeSourceControlProvider,
7+
refineUnknownRemoteProvider,
8+
} from "@t3tools/source-control-core/server/discovery";
69
import { processOutput } from "@t3tools/source-control-testing/TestSourceControlHost";
710
import { assert, it } from "@effect/vitest";
811
import * as Effect from "effect/Effect";
@@ -21,6 +24,89 @@ Logged in to gitlab.com as gitlab-user
2124
assert.deepStrictEqual(auth.account, Option.some("gitlab-user"));
2225
});
2326

27+
// Without `--all`, glab 1.79+ checks only the current host unless that is gitlab.com.
28+
it.effect("reports a signed-in host when glab's default host is signed out", () =>
29+
Effect.gen(function* () {
30+
const defaultHostOnly = `gitlab.gnome.org
31+
x gitlab.gnome.org: API call failed: GET https://gitlab.gnome.org/api/v4/user: 401 {message: 401 Unauthorized}
32+
`;
33+
const allHosts = `gitlab.com
34+
✓ Logged in to gitlab.com as gitlab-user (/home/user/.config/glab-cli/config.yml)
35+
${defaultHostOnly}`;
36+
const item = yield* probeSourceControlProvider({
37+
spec: GitLabSourceControlProvider.discovery,
38+
cwd: "/repo",
39+
process: {
40+
run: ({ args }) =>
41+
Effect.succeed(
42+
args[0] === "--version"
43+
? processOutput("glab 1.124.0\n")
44+
: processOutput(args.includes("--all") ? allHosts : defaultHostOnly, {
45+
exitCode: ChildProcessSpawner.ExitCode(1),
46+
}),
47+
),
48+
},
49+
});
50+
assert.strictEqual(item.auth.status, "authenticated");
51+
assert.deepStrictEqual(item.auth.account, Option.some("gitlab-user"));
52+
assert.deepStrictEqual(item.auth.host, Option.some("gitlab.com"));
53+
}),
54+
);
55+
56+
it.effect("skips --all when the current glab host is signed in", () =>
57+
Effect.gen(function* () {
58+
const asked: Array<ReadonlyArray<string>> = [];
59+
const item = yield* probeSourceControlProvider({
60+
spec: GitLabSourceControlProvider.discovery,
61+
cwd: "/repo",
62+
process: {
63+
run: ({ args }) => {
64+
asked.push(args);
65+
return Effect.succeed(
66+
args[0] === "--version"
67+
? processOutput("glab 1.124.0\n")
68+
: processOutput(`gitlab.example.test
69+
✓ Logged in to gitlab.example.test as gitlab-user
70+
`),
71+
);
72+
},
73+
},
74+
});
75+
assert.strictEqual(item.auth.status, "authenticated");
76+
assert.isFalse(asked.some((args) => args.includes("--all")));
77+
}),
78+
);
79+
80+
it.effect("keeps glab's own sign-out detail when glab predates --all", () =>
81+
Effect.gen(function* () {
82+
const item = yield* probeSourceControlProvider({
83+
spec: GitLabSourceControlProvider.discovery,
84+
cwd: "/repo",
85+
process: {
86+
run: ({ args }) =>
87+
Effect.succeed(
88+
args[0] === "--version"
89+
? processOutput("glab 1.53.0\n")
90+
: args.includes("--all")
91+
? processOutput("", {
92+
stderr: "unknown flag: --all\n",
93+
exitCode: ChildProcessSpawner.ExitCode(1),
94+
})
95+
: processOutput("", {
96+
stderr: "No GitLab instances have been authenticated with glab.\n",
97+
exitCode: ChildProcessSpawner.ExitCode(1),
98+
}),
99+
),
100+
},
101+
});
102+
assert.strictEqual(item.auth.status, "unauthenticated");
103+
assert.deepStrictEqual(
104+
item.auth.detail,
105+
Option.some("No GitLab instances have been authenticated with glab."),
106+
);
107+
}),
108+
);
109+
24110
it.effect.each([
25111
{
26112
name: "without relying on host naming",

0 commit comments

Comments
 (0)