Skip to content

Commit 623e4c9

Browse files
fix(gitlab): public gitlab.com lookups still resolve for self-hosted-only users
With one self-hosted host signed in, repository lookup sent every `glab api projects/...` call to that host, so a public gitlab.com project 404'd where main finds it through glab's gitlab.com fallback. When the signed-in host reports not-found, retry once without GITLAB_HOST. AI-assisted; verified with the tests and tools listed above.
1 parent 643c852 commit 623e4c9

2 files changed

Lines changed: 49 additions & 5 deletions

File tree

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,37 @@ layer("GitLabCli.layer", (it) => {
310310
}),
311311
);
312312

313+
it.effect("falls back to glab's own host when the signed-in host lacks the project", () =>
314+
Effect.gen(function* () {
315+
mockGlab(["gitlab.selfhosted.test"]);
316+
const answer = mockedRun.getMockImplementation()!;
317+
mockedRun.mockImplementation((input) =>
318+
input.args[0] === "api" && input.env !== undefined
319+
? Effect.fail(
320+
new VcsProcessExitError({
321+
operation: "GitLabCli.execute",
322+
command: "glab",
323+
cwd: "/server",
324+
exitCode: 1,
325+
detail: "GET 404 project not found",
326+
failureKind: "not-found",
327+
}),
328+
)
329+
: answer(input),
330+
);
331+
332+
const glab = yield* GitLabCli.GitLabCli;
333+
const urls = yield* glab.getRepositoryCloneUrls({ cwd: "/server", repository: "team/app" });
334+
335+
assert.strictEqual(urls.nameWithOwner, "team/app");
336+
expect(
337+
mockedRun.mock.calls
338+
.filter(([input]) => input.args[0] === "api")
339+
.map(([input]) => input.env),
340+
).toEqual([{ GITLAB_HOST: "gitlab.selfhosted.test" }, undefined]);
341+
}),
342+
);
343+
313344
it.effect("leaves an explicit GITLAB_HOST to glab without probing sign-ins", () =>
314345
Effect.gen(function* () {
315346
mockGlab(["gitlab.selfhosted.test"]);

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

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
TrimmedNonEmptyString,
1313
type SourceControlRepositoryVisibility,
1414
type VcsError,
15+
VcsProcessExitError,
1516
type VcsProcessExitFailureKind,
1617
} from "@t3tools/contracts";
1718

@@ -27,6 +28,7 @@ import type * as SourceControlProvider from "@t3tools/source-control-core/server
2728

2829
const DEFAULT_TIMEOUT_MS = 30_000;
2930
const AUTH_STATUS_TIMEOUT_MS = 5_000;
31+
const isVcsProcessExitError = Schema.is(VcsProcessExitError);
3032

3133
// glab reads its fallback host from these, so a set one means the user chose the host.
3234
const glabHostIsExplicit = Config.all(
@@ -590,13 +592,24 @@ export const make = Effect.gen(function* () {
590592
);
591593
}),
592594
getRepositoryCloneUrls: Effect.fn("GitLabCli.getRepositoryCloneUrls")(function (input) {
595+
const lookup = (env?: NodeJS.ProcessEnv) =>
596+
execute({
597+
cwd: input.cwd,
598+
args: ["api", `projects/${encodeURIComponent(input.repository)}`],
599+
env,
600+
});
593601
return signedInHostEnv(input.cwd).pipe(
594602
Effect.flatMap((env) =>
595-
execute({
596-
cwd: input.cwd,
597-
args: ["api", `projects/${encodeURIComponent(input.repository)}`],
598-
env,
599-
}),
603+
env === undefined
604+
? lookup()
605+
: // A public gitlab.com project still resolves through glab's own fallback host.
606+
lookup(env).pipe(
607+
Effect.catchIf(
608+
(error) =>
609+
isVcsProcessExitError(error.cause) && error.cause.failureKind === "not-found",
610+
() => lookup(),
611+
),
612+
),
600613
),
601614
Effect.map((result) => result.stdout.trim()),
602615
Effect.flatMap((raw) =>

0 commit comments

Comments
 (0)