Open History to see the last commit
1import { assert, it, afterEach, expect, vi } from "@effect/vitest";
2import * as Effect from "effect/Effect";
3import * as Layer from "effect/Layer";
4import { ChildProcessSpawner } from "effect/unstable/process";
5
6import { VcsProcessExitError } from "@v12/contracts";
7
8import * as VcsProcess from "../vcs/VcsProcess.ts";
9import * as GitLabCli from "./GitLabCli.ts";
10
11const mockedRun = vi.fn<VcsProcess.VcsProcess["Service"]["run"]>();
12const layer = it.layer(
13 GitLabCli.layer.pipe(
14 Layer.provide(
15 Layer.mock(VcsProcess.VcsProcess)({
16 run: mockedRun,
17 }),
18 ),
19 ),
20);
21
22function processOutput(stdout: string): VcsProcess.VcsProcessOutput {
23 return {
24 exitCode: ChildProcessSpawner.ExitCode(0),
25 stdout,
26 stderr: "",
27 stdoutTruncated: false,
28 stderrTruncated: false,
29 };
30}
31
32afterEach(() => {
33 mockedRun.mockReset();
34});
35
36layer("GitLabCli.layer", (it) => {
37 it.effect("parses merge request view output", () =>
38 Effect.gen(function* () {
39 mockedRun.mockReturnValueOnce(
40 Effect.succeed(
41 processOutput(
42 // @effect-diagnostics-next-line preferSchemaOverJson:off
43 JSON.stringify({
44 iid: 42,
45 title: "Add MR thread creation",
46 web_url: "https://gitlab.com/12ya/V12/-/merge_requests/42",
47 target_branch: "main",
48 source_branch: "feature/mr-threads",
49 state: "opened",
50 source_project_id: 101,
51 target_project_id: 100,
52 source_project: {
53 path_with_namespace: "octocat/v12",
54 },
55 }),
56 ),
57 ),
58 );
59
60 const result = yield* Effect.gen(function* () {
61 const glab = yield* GitLabCli.GitLabCli;
62 return yield* glab.getMergeRequest({
63 cwd: "/repo",
64 reference: "42",
65 });
66 });
67
68 assert.deepStrictEqual(result, {
69 number: 42,
70 title: "Add MR thread creation",
71 url: "https://gitlab.com/12ya/V12/-/merge_requests/42",
72 baseRefName: "main",
73 headRefName: "feature/mr-threads",
74 state: "open",
75 isCrossRepository: true,
76 headRepositoryNameWithOwner: "octocat/v12",
77 headRepositoryOwnerLogin: "octocat",
78 });
79 expect(mockedRun).toHaveBeenCalledWith(
80 expect.objectContaining({
81 command: "glab",
82 cwd: "/repo",
83 args: ["mr", "view", "42", "--output", "json"],
84 }),
85 );
86 }),
87 );
88
89 it.effect("skips invalid entries when parsing MR lists", () =>
90 Effect.gen(function* () {
91 mockedRun.mockReturnValueOnce(
92 Effect.succeed(
93 processOutput(
94 // @effect-diagnostics-next-line preferSchemaOverJson:off
95 JSON.stringify([
96 {
97 iid: 0,
98 title: "invalid",
99 web_url: "https://gitlab.com/12ya/V12/-/merge_requests/0",
100 target_branch: "main",
101 source_branch: "feature/invalid",
102 },
103 {
104 iid: 43,
105 title: " Valid MR ",
106 web_url: " https://gitlab.com/12ya/V12/-/merge_requests/43 ",
107 target_branch: " main ",
108 source_branch: " feature/mr-list ",
109 state: "merged",
110 },
111 ]),
112 ),
113 ),
114 );
115
116 const result = yield* Effect.gen(function* () {
117 const glab = yield* GitLabCli.GitLabCli;
118 return yield* glab.listMergeRequests({
119 cwd: "/repo",
120 headSelector: "feature/mr-list",
121 state: "all",
122 });
123 });
124
125 assert.deepStrictEqual(result, [
126 {
127 number: 43,
128 title: "Valid MR",
129 url: "https://gitlab.com/12ya/V12/-/merge_requests/43",
130 baseRefName: "main",
131 headRefName: "feature/mr-list",
132 state: "merged",
133 },
134 ]);
135 expect(mockedRun).toHaveBeenCalledWith(
136 expect.objectContaining({
137 command: "glab",
138 cwd: "/repo",
139 args: [
140 "mr",
141 "list",
142 "--source-branch",
143 "feature/mr-list",
144 "--all",
145 "--per-page",
146 "20",
147 "--output",
148 "json",
149 ],
150 }),
151 );
152 }),
153 );
154
155 it.effect("reads repository clone URLs", () =>
156 Effect.gen(function* () {
157 mockedRun.mockReturnValueOnce(
158 Effect.succeed(
159 processOutput(
160 // @effect-diagnostics-next-line preferSchemaOverJson:off
161 JSON.stringify({
162 path_with_namespace: "octocat/v12",
163 web_url: "https://gitlab.com/octocat/v12",
164 http_url_to_repo: "https://gitlab.com/octocat/v12.git",
165 ssh_url_to_repo: "git@gitlab.com:octocat/v12.git",
166 }),
167 ),
168 ),
169 );
170
171 const result = yield* Effect.gen(function* () {
172 const glab = yield* GitLabCli.GitLabCli;
173 return yield* glab.getRepositoryCloneUrls({
174 cwd: "/repo",
175 repository: "octocat/v12",
176 });
177 });
178
179 assert.deepStrictEqual(result, {
180 nameWithOwner: "octocat/v12",
181 url: "https://gitlab.com/octocat/v12",
182 sshUrl: "git@gitlab.com:octocat/v12.git",
183 });
184 }),
185 );
186
187 it.effect("creates merge requests through the GitLab API without placing the body in argv", () =>
188 Effect.gen(function* () {
189 mockedRun.mockReturnValueOnce(Effect.succeed(processOutput("{}")));
190
191 const glab = yield* GitLabCli.GitLabCli;
192 yield* glab.createMergeRequest({
193 cwd: "/repo",
194 baseBranch: "main",
195 headSelector: "owner:feature/provider",
196 title: "Provider MR",
197 bodyFile: "/tmp/v12-mr-body.md",
198 });
199
200 expect(mockedRun).toHaveBeenCalledWith(
201 expect.objectContaining({
202 command: "glab",
203 cwd: "/repo",
204 args: [
205 "api",
206 "--method",
207 "POST",
208 "projects/:fullpath/merge_requests",
209 "--raw-field",
210 "source_branch=feature/provider",
211 "--raw-field",
212 "target_branch=main",
213 "--raw-field",
214 "title=Provider MR",
215 "--field",
216 "description=@/tmp/v12-mr-body.md",
217 ],
218 }),
219 );
220 }),
221 );
222
223 it.effect("creates repositories under an explicit namespace", () =>
224 Effect.gen(function* () {
225 mockedRun
226
227 .mockReturnValueOnce(
228 Effect.succeed(
229 processOutput(
230 // @effect-diagnostics-next-line preferSchemaOverJson:off
231 JSON.stringify({ id: 1234 }),
232 ),
233 ),
234 )
235 .mockReturnValueOnce(
236 Effect.succeed(
237 processOutput(
238 // @effect-diagnostics-next-line preferSchemaOverJson:off
239 JSON.stringify({
240 path_with_namespace: "octocat/v12",
241 web_url: "https://gitlab.com/octocat/v12",
242 http_url_to_repo: "https://gitlab.com/octocat/v12.git",
243 ssh_url_to_repo: "git@gitlab.com:octocat/v12.git",
244 }),
245 ),
246 ),
247 );
248
249 const glab = yield* GitLabCli.GitLabCli;
250 const result = yield* glab.createRepository({
251 cwd: "/repo",
252 repository: "octocat/v12",
253 visibility: "public",
254 });
255
256 assert.deepStrictEqual(result, {
257 nameWithOwner: "octocat/v12",
258 url: "https://gitlab.com/octocat/v12",
259 sshUrl: "git@gitlab.com:octocat/v12.git",
260 });
261 expect(mockedRun).toHaveBeenNthCalledWith(
262 1,
263 expect.objectContaining({
264 command: "glab",
265 cwd: "/repo",
266 args: ["api", "namespaces/octocat"],
267 }),
268 );
269 expect(mockedRun).toHaveBeenNthCalledWith(
270 2,
271 expect.objectContaining({
272 command: "glab",
273 cwd: "/repo",
274 args: [
275 "api",
276 "--method",
277 "POST",
278 "projects",
279 "--raw-field",
280 "path=v12",
281 "--raw-field",
282 "name=v12",
283 "--raw-field",
284 "visibility=public",
285 "--raw-field",
286 "namespace_id=1234",
287 ],
288 }),
289 );
290 }),
291 );
292
293 it.effect("does not pass unsupported force flags when checking out merge requests", () =>
294 Effect.gen(function* () {
295 mockedRun.mockReturnValueOnce(Effect.succeed(processOutput("")));
296
297 const glab = yield* GitLabCli.GitLabCli;
298 yield* glab.checkoutMergeRequest({
299 cwd: "/repo",
300 reference: "42",
301 force: true,
302 });
303
304 expect(mockedRun).toHaveBeenCalledWith(
305 expect.objectContaining({
306 command: "glab",
307 cwd: "/repo",
308 args: ["mr", "checkout", "42"],
309 }),
310 );
311 }),
312 );
313
314 it.effect("surfaces a friendly error when the merge request is not found", () =>
315 Effect.gen(function* () {
316 const cause = new VcsProcessExitError({
317 operation: "GitLabCli.execute",
318 command: "glab",
319 cwd: "/repo",
320 exitCode: 1,
321 detail: "GET 404 merge request not found",
322 failureKind: "not-found",
323 });
324 mockedRun.mockReturnValueOnce(Effect.fail(cause));
325
326 const error = yield* Effect.gen(function* () {
327 const glab = yield* GitLabCli.GitLabCli;
328 return yield* glab.getMergeRequest({
329 cwd: "/repo",
330 reference: "4888",
331 });
332 }).pipe(Effect.flip);
333
334 assert.equal(error.message.includes("Merge request 4888 was not found"), true);
335 assert.strictEqual(error._tag, "GitLabMergeRequestNotFoundError");
336 assert.strictEqual(error.command, "glab");
337 assert.strictEqual(error.cwd, "/repo");
338 assert.strictEqual(error.cause, cause);
339 assert.equal(error.message.includes(cause.detail), false);
340 }),
341 );
342
343 it.effect("keeps non-merge-request not-found failures generic", () =>
344 Effect.gen(function* () {
345 const cause = new VcsProcessExitError({
346 operation: "GitLabCli.execute",
347 command: "glab",
348 cwd: "/repo",
349 exitCode: 1,
350 detail: "GET 404 project not found",
351 failureKind: "not-found",
352 });
353 mockedRun.mockReturnValueOnce(Effect.fail(cause));
354
355 const error = yield* Effect.gen(function* () {
356 const glab = yield* GitLabCli.GitLabCli;
357 return yield* glab.getRepositoryCloneUrls({
358 cwd: "/repo",
359 repository: "missing/project",
360 });
361 }).pipe(Effect.flip);
362
363 assert.strictEqual(error._tag, "GitLabCliCommandError");
364 assert.strictEqual(error.cause, cause);
365 }),
366 );
367});