Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

GitLabCli.test.ts

326 lines9.1 KB

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