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 { processOutput } from "@t3tools/source-control-testing/TestSourceControlHost";
10import { assert, it } from "@effect/vitest";
11import * as Effect from "effect/Effect";
12import * as Option from "effect/Option";
13import { ChildProcessSpawner } from "effect/process";
14
15import * as GitLabSourceControlProvider from "./GitLabSourceControlProvider.ts";
16
17it("reads the account from glab output without status marks", () => {
18 const auth = GitLabSourceControlProvider.discovery.parseAuth(
19 processOutput(`gitlab.com
20Logged in to gitlab.com as gitlab-user
21`),
22 );
23 assert.strictEqual(auth.status, "authenticated");
24 assert.deepStrictEqual(auth.account, Option.some("gitlab-user"));
25});
26
27// Without `--all`, glab 1.79+ checks only the current host unless that is gitlab.com.
28it.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
56it.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
80it.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
110it.effect.each([
111 {
112 name: "without relying on host naming",
113 remoteUrl: "https://self-hosted.example.test/group/project.git",
114 host: "self-hosted.example.test",
115 stdout: `gitlab.com
116 x gitlab.com: API call failed: 401 Unauthorized
117 ! No token found
118self-hosted.example.test
119 ✓ Logged in to self-hosted.example.test as gitlab-user
120 ✓ Token found: ******
121`,
122 exitCode: 1,
123 },
124 {
125 name: "on a non-standard port",
126 remoteUrl: "https://self-hosted.example.test:8443/group/project.git",
127 host: "self-hosted.example.test:8443",
128 stdout: `self-hosted.example.test:8443
129 ✓ Logged in to self-hosted.example.test:8443 as gitlab-user
130 ✓ Token found: ******
131`,
132 exitCode: 0,
133 },
134] as const)("claims a self-hosted remote glab is signed in to $name", (scenario) =>
135 Effect.gen(function* () {
136 const remote = parseGitRemoteHost(scenario.remoteUrl);
137 assert.ok(remote);
138 const asked: Array<ReadonlyArray<string>> = [];
139 const verdict = yield* refineUnknownRemoteProvider({
140 specs: [GitLabSourceControlProvider.discovery],
141 cwd: "/repo",
142 context: {
143 provider: unknownRemoteProvider(remote),
144 remoteName: "origin",
145 remoteUrl: scenario.remoteUrl,
146 },
147 process: {
148 run: ({ command, args }) => {
149 asked.push([command, ...args]);
150 return Effect.succeed(
151 processOutput(scenario.stdout, {
152 exitCode: ChildProcessSpawner.ExitCode(scenario.exitCode),
153 }),
154 );
155 },
156 },
157 });
158 assert.deepStrictEqual(asked, [["glab", "auth", "status", "--hostname", scenario.host]]);
159 assert.strictEqual(verdict.provider?.kind, "gitlab");
160 assert.strictEqual(verdict.settled, true);
161 }),
162);