Open History to see the last commit
1import {
2 parseGitRemoteHost,
3 unknownRemoteProvider,
4} from "@t3tools/source-control-core/shared/definition";
5import {
6 probeSourceControlProvider,
7 refineUnknownRemoteProvider,
8} from "@t3tools/source-control-core/server/discovery";
9import type * as SourceControlHost from "@t3tools/source-control-core/server/SourceControlHost";
10import { layer, processOutput } from "@t3tools/source-control-testing/TestSourceControlHost";
11import { assert, it } from "@effect/vitest";
12import * as Effect from "effect/Effect";
13import * as Option from "effect/Option";
14import { ChildProcessSpawner } from "effect/process";
15
16import * as GitLabSourceControlProvider from "./GitLabSourceControlProvider.ts";
17
18it("reads the account from glab output without status marks", () => {
19 const auth = GitLabSourceControlProvider.discovery.parseAuth(
20 processOutput(`gitlab.com
21Logged in to gitlab.com as gitlab-user
22`),
23 );
24 assert.strictEqual(auth.status, "authenticated");
25 assert.deepStrictEqual(auth.account, Option.some("gitlab-user"));
26});
27
28// Without `--all`, glab 1.79+ checks only the current host unless that is gitlab.com.
29it.effect("reports a signed-in host when glab's default host is signed out", () =>
30 Effect.gen(function* () {
31 const defaultHostOnly = `gitlab.gnome.org
32 x gitlab.gnome.org: API call failed: GET https://gitlab.gnome.org/api/v4/user: 401 {message: 401 Unauthorized}
33`;
34 const allHosts = `gitlab.com
35 ✓ Logged in to gitlab.com as gitlab-user (/home/user/.config/glab-cli/config.yml)
36${defaultHostOnly}`;
37 const item = yield* probeSourceControlProvider({
38 spec: GitLabSourceControlProvider.discovery,
39 cwd: "/repo",
40 process: {
41 run: ({ args }) =>
42 Effect.succeed(
43 args[0] === "--version"
44 ? processOutput("glab 1.124.0\n")
45 : processOutput(args.includes("--all") ? allHosts : defaultHostOnly, {
46 exitCode: ChildProcessSpawner.ExitCode(1),
47 }),
48 ),
49 },
50 });
51 assert.strictEqual(item.auth.status, "authenticated");
52 assert.deepStrictEqual(item.auth.account, Option.some("gitlab-user"));
53 assert.deepStrictEqual(item.auth.host, Option.some("gitlab.com"));
54 }),
55);
56
57it.effect("skips --all when the current glab host is signed in", () =>
58 Effect.gen(function* () {
59 const asked: Array<ReadonlyArray<string>> = [];
60 const item = yield* probeSourceControlProvider({
61 spec: GitLabSourceControlProvider.discovery,
62 cwd: "/repo",
63 process: {
64 run: ({ args }) => {
65 asked.push(args);
66 return Effect.succeed(
67 args[0] === "--version"
68 ? processOutput("glab 1.124.0\n")
69 : processOutput(`gitlab.example.test
70 ✓ Logged in to gitlab.example.test as gitlab-user
71`),
72 );
73 },
74 },
75 });
76 assert.strictEqual(item.auth.status, "authenticated");
77 assert.isFalse(asked.some((args) => args.includes("--all")));
78 }),
79);
80
81it.effect("keeps glab's own sign-out detail when glab predates --all", () =>
82 Effect.gen(function* () {
83 const item = yield* probeSourceControlProvider({
84 spec: GitLabSourceControlProvider.discovery,
85 cwd: "/repo",
86 process: {
87 run: ({ args }) =>
88 Effect.succeed(
89 args[0] === "--version"
90 ? processOutput("glab 1.53.0\n")
91 : args.includes("--all")
92 ? processOutput("", {
93 stderr: "unknown flag: --all\n",
94 exitCode: ChildProcessSpawner.ExitCode(1),
95 })
96 : processOutput("", {
97 stderr: "No GitLab instances have been authenticated with glab.\n",
98 exitCode: ChildProcessSpawner.ExitCode(1),
99 }),
100 ),
101 },
102 });
103 assert.strictEqual(item.auth.status, "unauthenticated");
104 assert.deepStrictEqual(
105 item.auth.detail,
106 Option.some("No GitLab instances have been authenticated with glab."),
107 );
108 }),
109);
110
111it.effect.each([
112 {
113 name: "without relying on host naming",
114 remoteUrl: "https://self-hosted.example.test/group/project.git",
115 host: "self-hosted.example.test",
116 stdout: `gitlab.com
117 x gitlab.com: API call failed: 401 Unauthorized
118 ! No token found
119self-hosted.example.test
120 ✓ Logged in to self-hosted.example.test as gitlab-user
121 ✓ Token found: ******
122`,
123 exitCode: 1,
124 },
125 {
126 name: "on a non-standard port",
127 remoteUrl: "https://self-hosted.example.test:8443/group/project.git",
128 host: "self-hosted.example.test:8443",
129 stdout: `self-hosted.example.test:8443
130 ✓ Logged in to self-hosted.example.test:8443 as gitlab-user
131 ✓ Token found: ******
132`,
133 exitCode: 0,
134 },
135] as const)("claims a self-hosted remote glab is signed in to $name", (scenario) =>
136 Effect.gen(function* () {
137 const remote = parseGitRemoteHost(scenario.remoteUrl);
138 assert.ok(remote);
139 const asked: Array<ReadonlyArray<string>> = [];
140 const verdict = yield* refineUnknownRemoteProvider({
141 specs: [GitLabSourceControlProvider.discovery],
142 cwd: "/repo",
143 context: {
144 provider: unknownRemoteProvider(remote),
145 remoteName: "origin",
146 remoteUrl: scenario.remoteUrl,
147 },
148 process: {
149 run: ({ command, args }) => {
150 asked.push([command, ...args]);
151 return Effect.succeed(
152 processOutput(scenario.stdout, {
153 exitCode: ChildProcessSpawner.ExitCode(scenario.exitCode),
154 }),
155 );
156 },
157 },
158 });
159 assert.deepStrictEqual(asked, [["glab", "auth", "status", "--hostname", scenario.host]]);
160 assert.strictEqual(verdict.provider?.kind, "gitlab");
161 assert.strictEqual(verdict.settled, true);
162 }),
163);
164
165function refineWithLocalConfig(input: {
166 readonly remoteUrl: string;
167 readonly run: SourceControlHost.SourceControlHost["Service"]["process"]["run"];
168}) {
169 return Effect.gen(function* () {
170 const spec = yield* GitLabSourceControlProvider.makeDiscovery;
171 const remote = parseGitRemoteHost(input.remoteUrl);
172 assert.ok(remote);
173 return yield* refineUnknownRemoteProvider({
174 specs: [spec],
175 process: { run: input.run },
176 cwd: "/repo",
177 context: {
178 provider: unknownRemoteProvider(remote),
179 remoteName: "origin",
180 remoteUrl: input.remoteUrl,
181 },
182 });
183 }).pipe(Effect.provide(layer({ process: { run: input.run } })));
184}
185
186// `glab auth status` calls the host's API, so an offline host is found only through local config.
187it.effect("claims a remote from glab's local config without asking its API", () =>
188 Effect.gen(function* () {
189 const asked: Array<ReadonlyArray<string>> = [];
190 const verdict = yield* refineWithLocalConfig({
191 remoteUrl: "git@Self-Hosted.Example.Test:group/project.git",
192 run: ({ command, args }) => {
193 asked.push([command, ...args]);
194 return Effect.succeed(processOutput("https://self-hosted.example.test/\n"));
195 },
196 });
197 assert.strictEqual(verdict.provider?.kind, "gitlab");
198 assert.strictEqual(verdict.provider?.baseUrl, "https://self-hosted.example.test");
199 assert.deepStrictEqual(asked, [
200 ["glab", "config", "get", "api_host", "--host", "Self-Hosted.Example.Test"],
201 ]);
202 }),
203);
204
205it.effect.each([
206 { label: "empty", stdout: "", signedIn: true },
207 { label: "a separate API host", stdout: "api.example.test:8443\n", signedIn: true },
208 { label: "another port", stdout: "self-hosted.example.test:9443\n", signedIn: false },
209 { label: "unreadable", stdout: "", exitCode: 1, signedIn: false },
210])("falls back to glab auth status when local config is $label", (scenario) =>
211 Effect.gen(function* () {
212 const asked: Array<ReadonlyArray<string>> = [];
213 const verdict = yield* refineWithLocalConfig({
214 remoteUrl: "https://self-hosted.example.test:8443/group/project.git",
215 run: ({ command, args }) => {
216 asked.push([command, ...args]);
217 if (args[0] === "config") {
218 return Effect.succeed(
219 processOutput(scenario.stdout, {
220 exitCode: ChildProcessSpawner.ExitCode(scenario.exitCode ?? 0),
221 }),
222 );
223 }
224 return Effect.succeed(
225 scenario.signedIn
226 ? processOutput(
227 "self-hosted.example.test:8443\n ✓ Logged in to self-hosted.example.test:8443 as token-user\n",
228 )
229 : processOutput("", {
230 stderr:
231 "You are not authenticated with glab. Run glab auth login to authenticate.\n",
232 exitCode: ChildProcessSpawner.ExitCode(1),
233 }),
234 );
235 },
236 });
237 assert.strictEqual(verdict.provider?.kind ?? null, scenario.signedIn ? "gitlab" : null);
238 assert.strictEqual(verdict.settled, true);
239 assert.deepStrictEqual(asked, [
240 ["glab", "config", "get", "api_host", "--host", "self-hosted.example.test:8443"],
241 ["glab", "auth", "status", "--hostname", "self-hosted.example.test:8443"],
242 ]);
243 }),
244);