The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import { afterEach, assert, describe, expect, it, vi } from "@effect/vitest"; |
| 2 | import { VcsProcessExitError } from "@t3tools/contracts"; |
| 3 | import * as Effect from "effect/Effect"; |
| 4 | import * as Layer from "effect/Layer"; |
| 5 | import { ChildProcessSpawner } from "effect/process"; |
| 6 | |
| 7 | import * as GitLabCli from "./GitLabCli.ts"; |
| 8 | import * as GitLabPullRequestCli from "./GitLabPullRequestCli.ts"; |
| 9 | |
| 10 | const mockedExecute = vi.fn<GitLabCli.GitLabCli["Service"]["execute"]>(); |
| 11 | |
| 12 | const layer = it.layer( |
| 13 | GitLabPullRequestCli.layer.pipe( |
| 14 | Layer.provide( |
| 15 | Layer.mock(GitLabCli.GitLabCli)({ |
| 16 | execute: mockedExecute, |
| 17 | }), |
| 18 | ), |
| 19 | ), |
| 20 | ); |
| 21 | |
| 22 | function output(stdout: string, stdoutTruncated = false, stdoutInvalidUtf8 = false) { |
| 23 | return { |
| 24 | exitCode: ChildProcessSpawner.ExitCode(0), |
| 25 | stdout, |
| 26 | stderr: "", |
| 27 | stdoutTruncated, |
| 28 | stderrTruncated: false, |
| 29 | stdoutInvalidUtf8, |
| 30 | }; |
| 31 | } |
| 32 | |
| 33 | function mergeRequests(count: number, firstNumber: number): string { |
| 34 | return JSON.stringify( |
| 35 | Array.from({ length: count }, (_, index) => ({ |
| 36 | iid: firstNumber + index, |
| 37 | title: `Merge request ${firstNumber + index}`, |
| 38 | web_url: `https://gitlab.com/acme/web/-/merge_requests/${firstNumber + index}`, |
| 39 | source_branch: "feat/page", |
| 40 | target_branch: "main", |
| 41 | created_at: "2026-07-01T00:00:00Z", |
| 42 | updated_at: "2026-07-02T00:00:00Z", |
| 43 | })), |
| 44 | ); |
| 45 | } |
| 46 | |
| 47 | function diffPage(firstIndex: number, count = 100): string { |
| 48 | return JSON.stringify( |
| 49 | Array.from({ length: count }, (_, index) => ({ |
| 50 | old_path: `src/${firstIndex + index}.ts`, |
| 51 | new_path: `src/${firstIndex + index}.ts`, |
| 52 | diff: "@@ -1 +1 @@\n-a\n+b\n", |
| 53 | })), |
| 54 | ); |
| 55 | } |
| 56 | |
| 57 | function notes(count: number, firstId: number): string { |
| 58 | return JSON.stringify( |
| 59 | Array.from({ length: count }, (_, index) => ({ |
| 60 | id: firstId + index, |
| 61 | body: `note ${firstId + index}`, |
| 62 | author: { username: "bilal" }, |
| 63 | created_at: "2026-07-01T00:00:00Z", |
| 64 | })), |
| 65 | ); |
| 66 | } |
| 67 | |
| 68 | const author = { id: 1, username: "bilal" }; |
| 69 | const labelPermission = output( |
| 70 | JSON.stringify({ |
| 71 | data: { project: { mergeRequest: { userPermissions: { adminMergeRequest: false } } } }, |
| 72 | }), |
| 73 | ); |
| 74 | const reviewer = { id: 5, username: "octocat" }; |
| 75 | |
| 76 | function mergeRequestJson(overrides: Record<string, unknown>): string { |
| 77 | return JSON.stringify({ |
| 78 | iid: 7, |
| 79 | title: "Merge request 7", |
| 80 | web_url: "https://gitlab.com/acme/web/-/merge_requests/7", |
| 81 | source_branch: "feat/page", |
| 82 | target_branch: "main", |
| 83 | created_at: "2026-07-01T00:00:00Z", |
| 84 | updated_at: "2026-07-02T00:00:00Z", |
| 85 | author, |
| 86 | ...overrides, |
| 87 | }); |
| 88 | } |
| 89 | |
| 90 | function commitJson(id: string) { |
| 91 | return { id, title: "Commit", committed_date: "2026-07-01T00:00:00Z" }; |
| 92 | } |
| 93 | |
| 94 | function sourceBranchJson(overrides: Record<string, unknown>): string { |
| 95 | return JSON.stringify({ |
| 96 | data: { |
| 97 | project: { |
| 98 | mergeRequest: { |
| 99 | state: "merged", |
| 100 | sourceBranch: "feat/page", |
| 101 | diffHeadSha: "head", |
| 102 | sourceProject: { fullPath: "acme/web" }, |
| 103 | userPermissions: { removeSourceBranch: true }, |
| 104 | ...overrides, |
| 105 | }, |
| 106 | }, |
| 107 | }, |
| 108 | }); |
| 109 | } |
| 110 | |
| 111 | function argsOfCall(index: number): ReadonlyArray<string> { |
| 112 | return callAt(index).args; |
| 113 | } |
| 114 | |
| 115 | function callAt(index: number) { |
| 116 | const call = mockedExecute.mock.calls[index]; |
| 117 | assert.isDefined(call); |
| 118 | return call[0]; |
| 119 | } |
| 120 | |
| 121 | function graphqlListPage( |
| 122 | numbers: ReadonlyArray<number>, |
| 123 | pageInfo: { readonly hasNextPage: boolean; readonly endCursor: string | null }, |
| 124 | ): string { |
| 125 | return JSON.stringify({ |
| 126 | data: { |
| 127 | metadata: { enterprise: true }, |
| 128 | project: { |
| 129 | mergeRequests: { |
| 130 | pageInfo, |
| 131 | nodes: numbers.map((number) => ({ |
| 132 | iid: String(number), |
| 133 | title: `Merge request ${number}`, |
| 134 | webUrl: `https://gitlab.com/acme/web/-/merge_requests/${number}`, |
| 135 | createdAt: "2026-07-01T00:00:00Z", |
| 136 | updatedAt: "2026-07-02T00:00:00Z", |
| 137 | sourceBranch: "feat/page", |
| 138 | targetBranch: "main", |
| 139 | approved: true, |
| 140 | approvedBy: { nodes: [{ username: "julius" }] }, |
| 141 | })), |
| 142 | }, |
| 143 | }, |
| 144 | }, |
| 145 | }); |
| 146 | } |
| 147 | |
| 148 | function graphqlRequestOf(index: number) { |
| 149 | return JSON.parse(callAt(index).stdin ?? "{}") as { |
| 150 | readonly query: string; |
| 151 | readonly variables: Record<string, unknown>; |
| 152 | }; |
| 153 | } |
| 154 | |
| 155 | afterEach(() => { |
| 156 | mockedExecute.mockReset(); |
| 157 | }); |
| 158 | |
| 159 | layer("GitLabPullRequestCli.layer", (it) => { |
| 160 | it.effect("batches approval reads at GitLab's page limit and skips an empty list", () => |
| 161 | Effect.gen(function* () { |
| 162 | mockedExecute.mockImplementation(({ stdin }) => { |
| 163 | const { variables } = JSON.parse(stdin ?? "{}"); |
| 164 | return Effect.succeed( |
| 165 | output( |
| 166 | JSON.stringify({ |
| 167 | data: { |
| 168 | metadata: { enterprise: true }, |
| 169 | project: { |
| 170 | mergeRequests: { |
| 171 | nodes: variables.iids.map((iid: string) => ({ |
| 172 | iid, |
| 173 | approved: true, |
| 174 | approvedBy: { nodes: [{ username: "reviewer" }] }, |
| 175 | })), |
| 176 | }, |
| 177 | }, |
| 178 | }, |
| 179 | }), |
| 180 | ), |
| 181 | ); |
| 182 | }); |
| 183 | const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli; |
| 184 | const reference = { cwd: "/w", repository: "acme/web", host: "gitlab.internal" }; |
| 185 | expect(yield* cli.getListStates({ ...reference, numbers: [] })).toEqual(new Map()); |
| 186 | expect(mockedExecute).not.toHaveBeenCalled(); |
| 187 | const decisions = yield* cli.getListStates({ |
| 188 | ...reference, |
| 189 | numbers: Array.from({ length: 101 }, (_, index) => index + 1), |
| 190 | }); |
| 191 | expect(callAt(0).host).toBe(reference.host); |
| 192 | expect(callAt(1).host).toBe(reference.host); |
| 193 | expect(decisions.size).toBe(101); |
| 194 | expect(decisions.get(101)).toEqual({ reviewDecision: "approved", checksState: null }); |
| 195 | expect(mockedExecute).toHaveBeenCalledTimes(2); |
| 196 | expect( |
| 197 | [0, 1].map((index) => JSON.parse(callAt(index).stdin ?? "{}").variables.iids.length), |
| 198 | ).toEqual([100, 1]); |
| 199 | expect(argsOfCall(0)).toEqual([ |
| 200 | "api", |
| 201 | "graphql", |
| 202 | "--method", |
| 203 | "POST", |
| 204 | "--input", |
| 205 | "-", |
| 206 | "--header", |
| 207 | "Content-Type: application/json", |
| 208 | ]); |
| 209 | }), |
| 210 | ); |
| 211 | |
| 212 | it.effect("asks GitLab for one row more than the page, to probe for a next page", () => |
| 213 | Effect.gen(function* () { |
| 214 | mockedExecute.mockReturnValueOnce(Effect.succeed(output(mergeRequests(3, 1)))); |
| 215 | const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli; |
| 216 | |
| 217 | const batch = yield* cli.listMergeRequests({ |
| 218 | cwd: "/w", |
| 219 | repository: "acme/web", |
| 220 | state: "open", |
| 221 | involvement: "all", |
| 222 | viewer: "bilal", |
| 223 | limit: 10, |
| 224 | }); |
| 225 | |
| 226 | assert.strictEqual(batch.items.length, 3); |
| 227 | assert.isFalse(batch.truncated); |
| 228 | assert.strictEqual(batch.cursorAdvance, 3); |
| 229 | const path = argsOfCall(0)[1] ?? ""; |
| 230 | expect(path).toContain("projects/acme%2Fweb/merge_requests"); |
| 231 | expect(path).toContain("per_page=11"); |
| 232 | expect(path).toContain("state=opened"); |
| 233 | }), |
| 234 | ); |
| 235 | |
| 236 | it.effect("walks pages at a fixed size, because GitLab pages by offset", () => |
| 237 | Effect.gen(function* () { |
| 238 | mockedExecute |
| 239 | .mockReturnValueOnce(Effect.succeed(output(mergeRequests(100, 1)))) |
| 240 | .mockReturnValueOnce(Effect.succeed(output(mergeRequests(100, 101)))); |
| 241 | const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli; |
| 242 | |
| 243 | const batch = yield* cli.listMergeRequests({ |
| 244 | cwd: "/w", |
| 245 | repository: "acme/web", |
| 246 | state: "open", |
| 247 | involvement: "all", |
| 248 | viewer: "bilal", |
| 249 | limit: 150, |
| 250 | }); |
| 251 | |
| 252 | assert.strictEqual(batch.items.length, 150); |
| 253 | assert.isTrue(batch.truncated); |
| 254 | for (const index of [0, 1]) { |
| 255 | expect(argsOfCall(index)[1]).toContain("per_page=100"); |
| 256 | } |
| 257 | expect(argsOfCall(0)[1]).toContain("page=1"); |
| 258 | expect(argsOfCall(1)[1]).toContain("page=2"); |
| 259 | }), |
| 260 | ); |
| 261 | |
| 262 | it.effect("asks GitLab for the labels, author and draft state it can filter on", () => |
| 263 | Effect.gen(function* () { |
| 264 | mockedExecute.mockReturnValueOnce(Effect.succeed(output(mergeRequests(1, 1)))); |
| 265 | const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli; |
| 266 | |
| 267 | yield* cli.listMergeRequests({ |
| 268 | cwd: "/w", |
| 269 | repository: "acme/web", |
| 270 | state: "open", |
| 271 | involvement: "all", |
| 272 | viewer: "bilal", |
| 273 | limit: 10, |
| 274 | filters: { |
| 275 | labels: [["bug"], ["size:S", "size:XS"], ["a,b"], ["None"]], |
| 276 | excludedLabels: ["wontfix", "stale", "any"], |
| 277 | author: "@me", |
| 278 | draft: "only", |
| 279 | review: "approved", |
| 280 | }, |
| 281 | }); |
| 282 | |
| 283 | const params = new URLSearchParams((argsOfCall(0)[1] ?? "").split("?")[1]); |
| 284 | expect(params.get("labels")).toBe("bug"); |
| 285 | expect(params.get("not[labels]")).toBe("wontfix,stale"); |
| 286 | expect(params.get("author_username")).toBe("bilal"); |
| 287 | expect(params.get("wip")).toBe("yes"); |
| 288 | expect(params.get("approved_by_usernames[]")).toBe("Any"); |
| 289 | }), |
| 290 | ); |
| 291 | |
| 292 | it.effect("narrows by approval only where GitLab returns every row the decision keeps", () => |
| 293 | Effect.gen(function* () { |
| 294 | const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli; |
| 295 | const reviews = ["none", "review-required", "changes-requested"] as const; |
| 296 | for (const review of reviews) { |
| 297 | mockedExecute.mockReturnValueOnce(Effect.succeed(output(mergeRequests(1, 1)))); |
| 298 | yield* cli.listMergeRequests({ |
| 299 | cwd: "/w", |
| 300 | repository: "acme/web", |
| 301 | state: "open", |
| 302 | involvement: "all", |
| 303 | viewer: "bilal", |
| 304 | limit: 10, |
| 305 | filters: { review }, |
| 306 | }); |
| 307 | } |
| 308 | |
| 309 | const sent = reviews.map((_, call) => { |
| 310 | const params = new URLSearchParams((argsOfCall(call)[1] ?? "").split("?")[1]); |
| 311 | return [params.get("approved_by_ids[]"), params.get("approved_by_usernames[]")]; |
| 312 | }); |
| 313 | expect(sent).toEqual([ |
| 314 | ["None", null], |
| 315 | [null, null], |
| 316 | [null, null], |
| 317 | ]); |
| 318 | }), |
| 319 | ); |
| 320 | |
| 321 | it.effect( |
| 322 | "lists through GraphQL with the filters GitLab narrows there, declaring only those", |
| 323 | () => |
| 324 | Effect.gen(function* () { |
| 325 | mockedExecute.mockReturnValue( |
| 326 | Effect.succeed(output(graphqlListPage([1], { hasNextPage: false, endCursor: null }))), |
| 327 | ); |
| 328 | const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli; |
| 329 | |
| 330 | yield* cli.listMergeRequests({ |
| 331 | cwd: "/w", |
| 332 | repository: "acme/web", |
| 333 | state: "open", |
| 334 | involvement: "all", |
| 335 | viewer: "bilal", |
| 336 | limit: 10, |
| 337 | query: " page ", |
| 338 | graphql: true, |
| 339 | cursor: { |
| 340 | updatedBefore: "2026-07-03T00:00:00Z", |
| 341 | delivered: 10, |
| 342 | resumeAfter: "eyJpZCI6IjEifQ", |
| 343 | }, |
| 344 | filters: { |
| 345 | labels: [["bug"], ["size:S", "size:XS"]], |
| 346 | excludedLabels: ["wontfix"], |
| 347 | author: "octocat", |
| 348 | draft: "hide", |
| 349 | review: "changes-requested", |
| 350 | }, |
| 351 | }); |
| 352 | |
| 353 | const { query, variables } = graphqlRequestOf(0); |
| 354 | expect(variables).toEqual({ |
| 355 | fullPath: "acme/web", |
| 356 | first: 10, |
| 357 | after: "eyJpZCI6IjEifQ", |
| 358 | state: "opened", |
| 359 | authorUsername: "octocat", |
| 360 | search: "page", |
| 361 | labels: ["bug"], |
| 362 | draft: false, |
| 363 | reviewStates: ["REQUESTED_CHANGES"], |
| 364 | notLabels: ["wontfix"], |
| 365 | }); |
| 366 | expect(query).toContain("reviewStates: $reviewStates"); |
| 367 | expect(query).toContain("not: { labels: $notLabels }"); |
| 368 | expect(query).not.toContain("$approvedBy"); |
| 369 | |
| 370 | // Beside a reviewer, GitLab would match only that reviewer's own state. |
| 371 | yield* cli.listMergeRequests({ |
| 372 | cwd: "/w", |
| 373 | repository: "acme/web", |
| 374 | state: "open", |
| 375 | involvement: "reviewing", |
| 376 | viewer: "bilal", |
| 377 | limit: 10, |
| 378 | graphql: true, |
| 379 | filters: { review: "changes-requested" }, |
| 380 | }); |
| 381 | expect(graphqlRequestOf(1).variables).toMatchObject({ reviewerUsername: "bilal" }); |
| 382 | expect(graphqlRequestOf(1).variables).not.toHaveProperty("reviewStates"); |
| 383 | }), |
| 384 | ); |
| 385 | |
| 386 | it.effect("walks GraphQL pages by GitLab's cursor and hands the last one back", () => |
| 387 | Effect.gen(function* () { |
| 388 | mockedExecute |
| 389 | .mockReturnValueOnce( |
| 390 | Effect.succeed(output(graphqlListPage([1, 2], { hasNextPage: true, endCursor: "c1" }))), |
| 391 | ) |
| 392 | .mockReturnValueOnce( |
| 393 | Effect.succeed(output(graphqlListPage([3], { hasNextPage: true, endCursor: "c2" }))), |
| 394 | ); |
| 395 | const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli; |
| 396 | |
| 397 | const batch = yield* cli.listMergeRequests({ |
| 398 | cwd: "/w", |
| 399 | repository: "acme/web", |
| 400 | state: "open", |
| 401 | involvement: "all", |
| 402 | viewer: "bilal", |
| 403 | limit: 3, |
| 404 | graphql: true, |
| 405 | filters: { review: "approved" }, |
| 406 | }); |
| 407 | |
| 408 | expect(batch).toMatchObject({ truncated: true, cursorAdvance: 3, resumeAfter: "c2" }); |
| 409 | expect(batch.items.map((item) => item.number)).toEqual([1, 2, 3]); |
| 410 | expect(batch.listStates?.get(3)).toEqual({ reviewDecision: "approved", checksState: null }); |
| 411 | expect([0, 1].map((call) => graphqlRequestOf(call).variables)).toMatchObject([ |
| 412 | { first: 3, after: null, approvedBy: ["Any"] }, |
| 413 | { first: 1, after: "c1" }, |
| 414 | ]); |
| 415 | }), |
| 416 | ); |
| 417 | |
| 418 | it.effect("reads REST for a REST cursor, and when a first GraphQL read or command fails", () => |
| 419 | Effect.gen(function* () { |
| 420 | mockedExecute |
| 421 | .mockReturnValueOnce(Effect.succeed(output(mergeRequests(1, 1)))) |
| 422 | .mockReturnValueOnce( |
| 423 | Effect.succeed( |
| 424 | output(JSON.stringify({ errors: [{ message: "Field 'search' doesn't exist" }] })), |
| 425 | ), |
| 426 | ) |
| 427 | .mockReturnValueOnce(Effect.succeed(output(mergeRequests(1, 1)))) |
| 428 | .mockReturnValueOnce(Effect.succeed(output(JSON.stringify({ errors: [] })))) |
| 429 | .mockReturnValueOnce( |
| 430 | Effect.fail( |
| 431 | new GitLabCli.GitLabCliCommandError({ |
| 432 | operation: "execute", |
| 433 | command: "glab", |
| 434 | cwd: "/w", |
| 435 | cause: new Error("502 Bad Gateway"), |
| 436 | }), |
| 437 | ), |
| 438 | ) |
| 439 | .mockReturnValueOnce(Effect.succeed(output(mergeRequests(1, 2)))); |
| 440 | const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli; |
| 441 | const list = (cursor?: { updatedBefore: string; delivered: number; resumeAfter?: string }) => |
| 442 | cli.listMergeRequests({ |
| 443 | cwd: "/w", |
| 444 | repository: "acme/web", |
| 445 | state: "open", |
| 446 | involvement: "all", |
| 447 | viewer: "bilal", |
| 448 | limit: 10, |
| 449 | // A cursor, not the host's current answer, picks the API. |
| 450 | graphql: cursor?.resumeAfter === undefined, |
| 451 | ...(cursor === undefined ? {} : { cursor }), |
| 452 | }); |
| 453 | |
| 454 | yield* list({ updatedBefore: "2026-07-03T00:00:00Z", delivered: 10 }); |
| 455 | const fellBack = yield* list(); |
| 456 | // A resumed GraphQL read stays on GraphQL and fails, since REST would read `delivered` as an offset. |
| 457 | const resumed = yield* Effect.result( |
| 458 | list({ updatedBefore: "2026-07-03T00:00:00Z", delivered: 10, resumeAfter: "c1" }), |
| 459 | ); |
| 460 | const commandFailed = yield* list(); |
| 461 | |
| 462 | expect(fellBack.items.map((item) => item.number)).toEqual([1]); |
| 463 | expect(fellBack).not.toHaveProperty("resumeAfter"); |
| 464 | expect(mockedExecute.mock.calls.map(([request]) => request.args[1]?.split("?")[0])).toEqual([ |
| 465 | "projects/acme%2Fweb/merge_requests", |
| 466 | "graphql", |
| 467 | "projects/acme%2Fweb/merge_requests", |
| 468 | "graphql", |
| 469 | "graphql", |
| 470 | "projects/acme%2Fweb/merge_requests", |
| 471 | ]); |
| 472 | expect(commandFailed.items.map((item) => item.number)).toEqual([2]); |
| 473 | expect(resumed).toMatchObject({ |
| 474 | _tag: "Failure", |
| 475 | failure: { _tag: "GitLabMergeRequestReadError" }, |
| 476 | }); |
| 477 | }), |
| 478 | ); |
| 479 | |
| 480 | it.effect("carries on from the number of rows already delivered", () => |
| 481 | Effect.gen(function* () { |
| 482 | mockedExecute.mockReturnValueOnce(Effect.succeed(output(mergeRequests(3, 1)))); |
| 483 | const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli; |
| 484 | |
| 485 | yield* cli.listMergeRequests({ |
| 486 | cwd: "/w", |
| 487 | repository: "acme/web", |
| 488 | state: "open", |
| 489 | involvement: "all", |
| 490 | viewer: "bilal", |
| 491 | limit: 10, |
| 492 | cursor: { updatedBefore: "2026-07-02T00:00:00Z", delivered: 10 }, |
| 493 | }); |
| 494 | |
| 495 | const path = argsOfCall(0)[1] ?? ""; |
| 496 | expect(path).not.toContain("updated_before="); |
| 497 | expect(path).toContain("order_by=updated_at"); |
| 498 | expect(path).toContain("with_labels_details=true"); |
| 499 | expect(path).toContain("per_page=11"); |
| 500 | expect(path).toContain("page=1"); |
| 501 | }), |
| 502 | ); |
| 503 | |
| 504 | it.effect("advances beyond several pages sharing the cursor timestamp", () => |
| 505 | Effect.gen(function* () { |
| 506 | mockedExecute |
| 507 | .mockReturnValueOnce(Effect.succeed(output(mergeRequests(11, 144)))) |
| 508 | .mockReturnValueOnce(Effect.succeed(output(mergeRequests(11, 155)))); |
| 509 | const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli; |
| 510 | |
| 511 | const batch = yield* cli.listMergeRequests({ |
| 512 | cwd: "/w", |
| 513 | repository: "acme/web", |
| 514 | state: "open", |
| 515 | involvement: "all", |
| 516 | viewer: "bilal", |
| 517 | limit: 10, |
| 518 | cursor: { updatedBefore: "2026-07-02T00:00:00Z", delivered: 150 }, |
| 519 | }); |
| 520 | |
| 521 | expect(argsOfCall(0)[1]).toContain("per_page=11"); |
| 522 | expect(argsOfCall(0)[1]).toContain("page=14"); |
| 523 | expect(argsOfCall(1)[1]).toContain("page=15"); |
| 524 | expect(batch.items.map((item) => item.number)).toEqual([ |
| 525 | 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, |
| 526 | ]); |
| 527 | assert.isTrue(batch.truncated); |
| 528 | }), |
| 529 | ); |
| 530 | |
| 531 | it.effect("advances the cursor through malformed raw rows", () => |
| 532 | Effect.gen(function* () { |
| 533 | const rows = JSON.parse(mergeRequests(2, 1)) as ReadonlyArray<unknown>; |
| 534 | mockedExecute.mockReturnValueOnce( |
| 535 | Effect.succeed(output(JSON.stringify([{ iid: "malformed" }, ...rows]))), |
| 536 | ); |
| 537 | const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli; |
| 538 | |
| 539 | const batch = yield* cli.listMergeRequests({ |
| 540 | cwd: "/w", |
| 541 | repository: "acme/web", |
| 542 | state: "open", |
| 543 | involvement: "all", |
| 544 | viewer: "bilal", |
| 545 | limit: 2, |
| 546 | }); |
| 547 | |
| 548 | expect(batch.items.map((item) => item.number)).toEqual([1, 2]); |
| 549 | assert.strictEqual(batch.cursorAdvance, 3); |
| 550 | assert.isTrue(batch.truncated); |
| 551 | }), |
| 552 | ); |
| 553 | |
| 554 | it.effect("URL-encodes a search, so it cannot add a parameter of its own", () => |
| 555 | Effect.gen(function* () { |
| 556 | mockedExecute.mockReturnValueOnce(Effect.succeed(output("[]"))); |
| 557 | const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli; |
| 558 | |
| 559 | yield* cli.listMergeRequests({ |
| 560 | cwd: "/w", |
| 561 | repository: "acme/web", |
| 562 | state: "open", |
| 563 | involvement: "all", |
| 564 | viewer: "bilal", |
| 565 | limit: 10, |
| 566 | query: '-a&per_page=1 "b"', |
| 567 | }); |
| 568 | |
| 569 | const path = argsOfCall(0)[1] ?? ""; |
| 570 | expect(path).toContain("search=-a%26per_page%3D1%20%22b%22"); |
| 571 | assert.strictEqual(path.match(/per_page=/g)?.length, 1); |
| 572 | }), |
| 573 | ); |
| 574 | |
| 575 | it.effect("stops walking on a short page", () => |
| 576 | Effect.gen(function* () { |
| 577 | mockedExecute.mockReturnValueOnce(Effect.succeed(output(mergeRequests(40, 1)))); |
| 578 | const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli; |
| 579 | |
| 580 | const batch = yield* cli.listMergeRequests({ |
| 581 | cwd: "/w", |
| 582 | repository: "acme/web", |
| 583 | state: "open", |
| 584 | involvement: "all", |
| 585 | viewer: "bilal", |
| 586 | limit: 150, |
| 587 | }); |
| 588 | |
| 589 | assert.strictEqual(batch.items.length, 40); |
| 590 | assert.isFalse(batch.truncated); |
| 591 | assert.strictEqual(mockedExecute.mock.calls.length, 1); |
| 592 | }), |
| 593 | ); |
| 594 | |
| 595 | it.effect("reads a page of merge requests larger than the VCS default output limit", () => |
| 596 | Effect.gen(function* () { |
| 597 | const vcsDefaultMaxOutputBytes = 1_000_000; |
| 598 | const rows = (JSON.parse(mergeRequests(100, 1)) as ReadonlyArray<object>).map((row) => ({ |
| 599 | ...row, |
| 600 | description: "x".repeat(20_000), |
| 601 | })); |
| 602 | const response = JSON.stringify(rows); |
| 603 | expect(Buffer.byteLength(response)).toBeGreaterThan(vcsDefaultMaxOutputBytes); |
| 604 | mockedExecute.mockImplementationOnce(({ maxOutputBytes = vcsDefaultMaxOutputBytes }) => |
| 605 | Effect.succeed( |
| 606 | maxOutputBytes >= Buffer.byteLength(response) |
| 607 | ? output(response) |
| 608 | : output(response.slice(0, maxOutputBytes), true), |
| 609 | ), |
| 610 | ); |
| 611 | const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli; |
| 612 | |
| 613 | const batch = yield* cli.listMergeRequests({ |
| 614 | cwd: "/w", |
| 615 | repository: "acme/web", |
| 616 | state: "open", |
| 617 | involvement: "all", |
| 618 | viewer: "bilal", |
| 619 | limit: 99, |
| 620 | }); |
| 621 | |
| 622 | assert.strictEqual(batch.items.length, 99); |
| 623 | assert.isTrue(batch.truncated); |
| 624 | }), |
| 625 | ); |
| 626 | |
| 627 | it.effect("stops walking when every row on a page fails to decode", () => |
| 628 | Effect.gen(function* () { |
| 629 | const unusable = JSON.stringify(Array.from({ length: 100 }, () => ({ iid: "nope" }))); |
| 630 | mockedExecute.mockReturnValue(Effect.succeed(output(unusable))); |
| 631 | const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli; |
| 632 | |
| 633 | const batch = yield* cli.listMergeRequests({ |
| 634 | cwd: "/w", |
| 635 | repository: "acme/web", |
| 636 | state: "open", |
| 637 | involvement: "all", |
| 638 | viewer: "bilal", |
| 639 | limit: 150, |
| 640 | }); |
| 641 | |
| 642 | assert.strictEqual(batch.items.length, 0); |
| 643 | assert.strictEqual(mockedExecute.mock.calls.length, 2); |
| 644 | }), |
| 645 | ); |
| 646 | |
| 647 | it.effect.each([ |
| 648 | { action: "merge", autoMerge: "false", mergeMethod: "squash" }, |
| 649 | { action: "enable-auto-merge", autoMerge: "true", mergeMethod: "squash" }, |
| 650 | { action: "merge", autoMerge: "false", mergeMethod: "rebase" }, |
| 651 | ] as const)( |
| 652 | "$action with $mergeMethod succeeds on a project requiring the current source SHA", |
| 653 | ({ action, autoMerge, mergeMethod }) => |
| 654 | Effect.gen(function* () { |
| 655 | const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli; |
| 656 | |
| 657 | for (const headSha of ["first-head", "updated-head"]) { |
| 658 | let currentHeadSha = headSha; |
| 659 | mockedExecute.mockImplementation(({ cwd, args }) => { |
| 660 | if (args[0] === "mr" && args[1] === "rebase") { |
| 661 | return Effect.sync(() => { |
| 662 | currentHeadSha = `${headSha}-rebased`; |
| 663 | return output(""); |
| 664 | }); |
| 665 | } |
| 666 | if (args[0] === "api") { |
| 667 | return Effect.succeed( |
| 668 | output( |
| 669 | mergeRequestJson({ |
| 670 | diff_refs: { base_sha: "base", head_sha: currentHeadSha, start_sha: "start" }, |
| 671 | }), |
| 672 | ), |
| 673 | ); |
| 674 | } |
| 675 | if (args.includes("--rebase") || args[args.indexOf("--sha") + 1] !== currentHeadSha) { |
| 676 | return Effect.fail( |
| 677 | new GitLabCli.GitLabCliCommandError({ |
| 678 | operation: "execute", |
| 679 | command: "glab", |
| 680 | cwd, |
| 681 | cause: new Error( |
| 682 | args.includes("--rebase") |
| 683 | ? "Merge must not combine --rebase with --sha: rebase changes the source HEAD" |
| 684 | : "SHA must match the current source branch HEAD", |
| 685 | ), |
| 686 | }), |
| 687 | ); |
| 688 | } |
| 689 | return Effect.succeed(output("")); |
| 690 | }); |
| 691 | |
| 692 | yield* cli.runMergeRequestAction({ |
| 693 | cwd: "/w", |
| 694 | repository: "acme/web", |
| 695 | number: 7, |
| 696 | action, |
| 697 | mergeMethod, |
| 698 | }); |
| 699 | |
| 700 | const callsPerAction = mergeMethod === "rebase" ? 3 : 2; |
| 701 | expect( |
| 702 | mockedExecute.mock.calls.slice(-callsPerAction).map(([input]) => input.args), |
| 703 | ).toEqual([ |
| 704 | ...(mergeMethod === "rebase" ? [["mr", "rebase", "7", "--repo", "acme/web"]] : []), |
| 705 | ["api", "projects/acme%2Fweb/merge_requests/7"], |
| 706 | [ |
| 707 | "mr", |
| 708 | "merge", |
| 709 | "7", |
| 710 | "--repo", |
| 711 | "acme/web", |
| 712 | `--auto-merge=${autoMerge}`, |
| 713 | "--yes", |
| 714 | ...(mergeMethod === "squash" ? ["--squash"] : []), |
| 715 | "--sha", |
| 716 | mergeMethod === "rebase" ? `${headSha}-rebased` : headSha, |
| 717 | ], |
| 718 | ]); |
| 719 | } |
| 720 | expect(mockedExecute).toHaveBeenCalledTimes(mergeMethod === "rebase" ? 6 : 4); |
| 721 | }), |
| 722 | ); |
| 723 | |
| 724 | it.effect("merges pinned to the head the caller reviewed, not the one it reads", () => |
| 725 | Effect.gen(function* () { |
| 726 | const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli; |
| 727 | mockedExecute.mockImplementation(({ args }) => |
| 728 | Effect.succeed( |
| 729 | output( |
| 730 | args[0] === "api" |
| 731 | ? mergeRequestJson({ |
| 732 | diff_refs: { base_sha: "base", head_sha: "pushed-head", start_sha: "start" }, |
| 733 | }) |
| 734 | : "", |
| 735 | ), |
| 736 | ), |
| 737 | ); |
| 738 | yield* cli.runMergeRequestAction({ |
| 739 | cwd: "/w", |
| 740 | repository: "acme/web", |
| 741 | number: 7, |
| 742 | action: "merge", |
| 743 | mergeMethod: "squash", |
| 744 | expectedHeadSha: "reviewed-head", |
| 745 | }); |
| 746 | const merge = mockedExecute.mock.calls.at(-1)?.[0].args ?? []; |
| 747 | expect(merge[merge.indexOf("--sha") + 1]).toBe("reviewed-head"); |
| 748 | |
| 749 | // A rebase cannot be pinned, so a head that moved refuses it before rebasing. |
| 750 | mockedExecute.mockClear(); |
| 751 | const refused = yield* Effect.flip( |
| 752 | cli.runMergeRequestAction({ |
| 753 | cwd: "/w", |
| 754 | repository: "acme/web", |
| 755 | number: 7, |
| 756 | action: "merge", |
| 757 | mergeMethod: "rebase", |
| 758 | expectedHeadSha: "reviewed-head", |
| 759 | }), |
| 760 | ); |
| 761 | expect(refused._tag).toBe("GitLabMutationRefusedError"); |
| 762 | expect(mockedExecute.mock.calls.map(([call]) => call.args[1])).not.toContain("rebase"); |
| 763 | }), |
| 764 | ); |
| 765 | |
| 766 | it.effect.each([ |
| 767 | { |
| 768 | mergeMethod: "squash", |
| 769 | squashOnMerge: false, |
| 770 | squash: "Add thing\n\nCo-authored-by: Codex <noreply@openai.com>", |
| 771 | flags: [ |
| 772 | "--squash", |
| 773 | "--sha", |
| 774 | "head", |
| 775 | "--message", |
| 776 | "Merge branch 'feature' into 'main'\n\nAdd thing\n\nCo-authored-by: Ada <ada@example.com>", |
| 777 | "--squash-message", |
| 778 | "Add thing", |
| 779 | ], |
| 780 | }, |
| 781 | { |
| 782 | mergeMethod: "merge", |
| 783 | squashOnMerge: false, |
| 784 | squash: "Add thing\n\nCo-authored-by: Codex <noreply@openai.com>", |
| 785 | flags: [ |
| 786 | "--sha", |
| 787 | "head", |
| 788 | "--message", |
| 789 | "Merge branch 'feature' into 'main'\n\nAdd thing\n\nCo-authored-by: Ada <ada@example.com>", |
| 790 | ], |
| 791 | }, |
| 792 | { |
| 793 | mergeMethod: "merge", |
| 794 | squashOnMerge: true, |
| 795 | squash: "Add thing\n\nCo-authored-by: Codex <noreply@openai.com>", |
| 796 | flags: [ |
| 797 | "--sha", |
| 798 | "head", |
| 799 | "--message", |
| 800 | "Merge branch 'feature' into 'main'\n\nAdd thing\n\nCo-authored-by: Ada <ada@example.com>", |
| 801 | "--squash", |
| 802 | "--squash-message", |
| 803 | "Add thing", |
| 804 | ], |
| 805 | }, |
| 806 | { |
| 807 | mergeMethod: "squash", |
| 808 | squashOnMerge: false, |
| 809 | squash: "Co-authored-by: Codex <noreply@openai.com>", |
| 810 | flags: [ |
| 811 | "--squash", |
| 812 | "--sha", |
| 813 | "head", |
| 814 | "--message", |
| 815 | "Merge branch 'feature' into 'main'\n\nAdd thing\n\nCo-authored-by: Ada <ada@example.com>", |
| 816 | "--squash-message", |
| 817 | "Add thing", |
| 818 | ], |
| 819 | }, |
| 820 | ] as const)( |
| 821 | "removes agent credits from GitLab's default messages for a $mergeMethod (squash on merge: $squashOnMerge)", |
| 822 | ({ mergeMethod, squashOnMerge, squash, flags }) => |
| 823 | Effect.gen(function* () { |
| 824 | const credits = |
| 825 | "Co-authored-by: Ada <ada@example.com>\nCo-authored-by: Claude <noreply@anthropic.com>"; |
| 826 | mockedExecute.mockImplementation(({ args }) => |
| 827 | Effect.succeed( |
| 828 | output( |
| 829 | args[1] === "graphql" |
| 830 | ? JSON.stringify({ |
| 831 | data: { |
| 832 | project: { |
| 833 | mergeRequest: { |
| 834 | title: "Add thing", |
| 835 | squashOnMerge, |
| 836 | defaultMergeCommitMessage: `Merge branch 'feature' into 'main'\n\nAdd thing\n\n${credits}`, |
| 837 | defaultSquashCommitMessage: squash, |
| 838 | }, |
| 839 | }, |
| 840 | }, |
| 841 | }) |
| 842 | : args[0] === "api" |
| 843 | ? mergeRequestJson({ |
| 844 | diff_refs: { base_sha: "base", head_sha: "head", start_sha: "start" }, |
| 845 | }) |
| 846 | : "", |
| 847 | ), |
| 848 | ), |
| 849 | ); |
| 850 | const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli; |
| 851 | |
| 852 | yield* cli.runMergeRequestAction({ |
| 853 | cwd: "/w", |
| 854 | repository: "acme/web", |
| 855 | number: 7, |
| 856 | action: "merge", |
| 857 | mergeMethod, |
| 858 | removeAgentCreditsOnMerge: true, |
| 859 | }); |
| 860 | |
| 861 | expect(mockedExecute.mock.calls.at(-1)?.[0].args).toEqual([ |
| 862 | "mr", |
| 863 | "merge", |
| 864 | "7", |
| 865 | "--repo", |
| 866 | "acme/web", |
| 867 | "--auto-merge=false", |
| 868 | "--yes", |
| 869 | ...flags, |
| 870 | ]); |
| 871 | }), |
| 872 | ); |
| 873 | |
| 874 | it.effect("keeps GitLab's merge messages when they carry no agent credits", () => |
| 875 | Effect.gen(function* () { |
| 876 | mockedExecute.mockImplementation(({ args }) => |
| 877 | Effect.succeed( |
| 878 | output( |
| 879 | args[1] === "graphql" |
| 880 | ? JSON.stringify({ |
| 881 | data: { |
| 882 | project: { |
| 883 | mergeRequest: { |
| 884 | defaultMergeCommitMessage: "Merge branch 'feature' into 'main'", |
| 885 | defaultSquashCommitMessage: "Add thing", |
| 886 | }, |
| 887 | }, |
| 888 | }, |
| 889 | }) |
| 890 | : args[0] === "api" |
| 891 | ? mergeRequestJson({ |
| 892 | diff_refs: { base_sha: "base", head_sha: "head", start_sha: "start" }, |
| 893 | }) |
| 894 | : "", |
| 895 | ), |
| 896 | ), |
| 897 | ); |
| 898 | const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli; |
| 899 | |
| 900 | yield* cli.runMergeRequestAction({ |
| 901 | cwd: "/w", |
| 902 | repository: "acme/web", |
| 903 | number: 7, |
| 904 | action: "enable-auto-merge", |
| 905 | mergeMethod: "squash", |
| 906 | removeAgentCreditsOnMerge: true, |
| 907 | }); |
| 908 | |
| 909 | expect(mockedExecute.mock.calls.at(-1)?.[0].args).toEqual([ |
| 910 | "mr", |
| 911 | "merge", |
| 912 | "7", |
| 913 | "--repo", |
| 914 | "acme/web", |
| 915 | "--auto-merge=true", |
| 916 | "--yes", |
| 917 | "--squash", |
| 918 | "--sha", |
| 919 | "head", |
| 920 | ]); |
| 921 | }), |
| 922 | ); |
| 923 | |
| 924 | it.effect.each(["merge", "enable-auto-merge"] as const)( |
| 925 | "does not run %s when GitLab has no diff revisions", |
| 926 | (action) => |
| 927 | Effect.gen(function* () { |
| 928 | mockedExecute.mockReturnValue( |
| 929 | Effect.succeed(output(mergeRequestJson({ diff_refs: null }))), |
| 930 | ); |
| 931 | const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli; |
| 932 | |
| 933 | const result = yield* Effect.result( |
| 934 | cli.runMergeRequestAction({ |
| 935 | cwd: "/w", |
| 936 | repository: "acme/web", |
| 937 | number: 7, |
| 938 | action, |
| 939 | mergeMethod: "squash", |
| 940 | }), |
| 941 | ); |
| 942 | |
| 943 | expect(mockedExecute.mock.calls.map(([input]) => input.args)).toEqual([ |
| 944 | ["api", "projects/acme%2Fweb/merge_requests/7"], |
| 945 | ]); |
| 946 | expect(result).toMatchObject({ |
| 947 | _tag: "Failure", |
| 948 | failure: { _tag: "GitLabDiffRefsUnavailableError" }, |
| 949 | }); |
| 950 | }), |
| 951 | ); |
| 952 | |
| 953 | it.effect("reverts a merged merge request onto a new branch and opens a merge request", () => |
| 954 | Effect.gen(function* () { |
| 955 | const mergeCommit = "dac72fa101758a44b0068a23666559920a74478f"; |
| 956 | mockedExecute |
| 957 | .mockReturnValueOnce( |
| 958 | Effect.succeed( |
| 959 | output( |
| 960 | mergeRequestJson({ |
| 961 | state: "merged", |
| 962 | merge_commit_sha: mergeCommit, |
| 963 | squash_commit_sha: "cb2d9f660ebe946e5056ae98993db5ea72b9bad1", |
| 964 | }), |
| 965 | ), |
| 966 | ), |
| 967 | ) |
| 968 | .mockReturnValue(Effect.succeed(output("{}"))); |
| 969 | const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli; |
| 970 | |
| 971 | yield* cli.runMergeRequestAction({ |
| 972 | cwd: "/w", |
| 973 | repository: "acme/web", |
| 974 | number: 7, |
| 975 | action: "revert", |
| 976 | }); |
| 977 | |
| 978 | expect( |
| 979 | mockedExecute.mock.calls.map(([input]) => [ |
| 980 | input.args[1], |
| 981 | input.stdin && JSON.parse(input.stdin), |
| 982 | ]), |
| 983 | ).toEqual([ |
| 984 | ["projects/acme%2Fweb/merge_requests/7", undefined], |
| 985 | ["projects/acme%2Fweb/repository/branches", { branch: "revert-dac72fa1", ref: "main" }], |
| 986 | [ |
| 987 | `projects/acme%2Fweb/repository/commits/${mergeCommit}/revert`, |
| 988 | { branch: "revert-dac72fa1" }, |
| 989 | ], |
| 990 | [ |
| 991 | "projects/acme%2Fweb/merge_requests", |
| 992 | { |
| 993 | source_branch: "revert-dac72fa1", |
| 994 | target_branch: "main", |
| 995 | title: 'Revert "Merge request 7"', |
| 996 | description: "This reverts merge request !7", |
| 997 | remove_source_branch: true, |
| 998 | }, |
| 999 | ], |
| 1000 | ]); |
| 1001 | }), |
| 1002 | ); |
| 1003 | |
| 1004 | it.effect.each([ |
| 1005 | ["the revert conflicts", "/revert", "Failure"], |
| 1006 | ["opening the merge request fails", "/merge_requests", "Failure"], |
| 1007 | ["the request is interrupted", "/revert", "Interrupt"], |
| 1008 | ] as const)("deletes the revert branch when %s", ([, failingPath, outcome]) => |
| 1009 | Effect.gen(function* () { |
| 1010 | mockedExecute.mockImplementation(({ args, cwd }) => |
| 1011 | args[1]?.endsWith(failingPath) && args.includes("POST") |
| 1012 | ? outcome === "Interrupt" |
| 1013 | ? Effect.interrupt |
| 1014 | : Effect.fail( |
| 1015 | new GitLabCli.GitLabCliCommandError({ |
| 1016 | operation: "execute", |
| 1017 | command: "glab", |
| 1018 | cwd, |
| 1019 | cause: new Error("GitLab rejected the write"), |
| 1020 | }), |
| 1021 | ) |
| 1022 | : Effect.succeed( |
| 1023 | output( |
| 1024 | mergeRequestJson({ |
| 1025 | state: "merged", |
| 1026 | merge_commit_sha: null, |
| 1027 | squash_commit_sha: "abc12345ff", |
| 1028 | }), |
| 1029 | ), |
| 1030 | ), |
| 1031 | ); |
| 1032 | const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli; |
| 1033 | |
| 1034 | const exit = yield* Effect.exit( |
| 1035 | cli.runMergeRequestAction({ |
| 1036 | cwd: "/w", |
| 1037 | repository: "acme/web", |
| 1038 | number: 7, |
| 1039 | action: "revert", |
| 1040 | }), |
| 1041 | ); |
| 1042 | |
| 1043 | expect(exit._tag).toBe("Failure"); |
| 1044 | expect(mockedExecute.mock.calls.at(-1)?.[0].args.slice(1, 4)).toEqual([ |
| 1045 | "projects/acme%2Fweb/repository/branches/revert-abc12345", |
| 1046 | "--method", |
| 1047 | "DELETE", |
| 1048 | ]); |
| 1049 | }), |
| 1050 | ); |
| 1051 | |
| 1052 | it.effect.each([ |
| 1053 | ["reverts the sole commit of", [commitJson("1111111122")], "revert-11111111"], |
| 1054 | [ |
| 1055 | "refuses to revert several commits of", |
| 1056 | [commitJson("1111111122"), commitJson("2222222233")], |
| 1057 | "GitLabRevertUnavailableError", |
| 1058 | ], |
| 1059 | ] as const)("%s a fast-forward merge without squash", ([, commits, expected]) => |
| 1060 | Effect.gen(function* () { |
| 1061 | mockedExecute |
| 1062 | .mockReturnValueOnce( |
| 1063 | Effect.succeed( |
| 1064 | output( |
| 1065 | mergeRequestJson({ |
| 1066 | state: "merged", |
| 1067 | merge_commit_sha: null, |
| 1068 | squash_commit_sha: null, |
| 1069 | }), |
| 1070 | ), |
| 1071 | ), |
| 1072 | ) |
| 1073 | .mockReturnValueOnce(Effect.succeed(output(JSON.stringify(commits)))) |
| 1074 | .mockReturnValue(Effect.succeed(output("{}"))); |
| 1075 | const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli; |
| 1076 | |
| 1077 | const result = yield* Effect.result( |
| 1078 | cli.runMergeRequestAction({ |
| 1079 | cwd: "/w", |
| 1080 | repository: "acme/web", |
| 1081 | number: 7, |
| 1082 | action: "revert", |
| 1083 | }), |
| 1084 | ); |
| 1085 | |
| 1086 | expect(argsOfCall(1)[1]).toBe("projects/acme%2Fweb/merge_requests/7/commits?per_page=2"); |
| 1087 | if (expected === "GitLabRevertUnavailableError") { |
| 1088 | expect(result).toMatchObject({ _tag: "Failure", failure: { _tag: expected } }); |
| 1089 | } else { |
| 1090 | expect(JSON.parse(callAt(2).stdin ?? "")).toEqual({ branch: expected, ref: "main" }); |
| 1091 | } |
| 1092 | }), |
| 1093 | ); |
| 1094 | |
| 1095 | it.effect("refuses to revert a merge request that is not merged before any write", () => |
| 1096 | Effect.gen(function* () { |
| 1097 | mockedExecute |
| 1098 | .mockReturnValueOnce( |
| 1099 | Effect.succeed( |
| 1100 | output( |
| 1101 | mergeRequestJson({ |
| 1102 | state: "opened", |
| 1103 | merge_commit_sha: null, |
| 1104 | squash_commit_sha: null, |
| 1105 | }), |
| 1106 | ), |
| 1107 | ), |
| 1108 | ) |
| 1109 | .mockReturnValue(Effect.succeed(output(JSON.stringify([commitJson("1111111122")])))); |
| 1110 | const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli; |
| 1111 | |
| 1112 | const result = yield* Effect.result( |
| 1113 | cli.runMergeRequestAction({ |
| 1114 | cwd: "/w", |
| 1115 | repository: "acme/web", |
| 1116 | number: 7, |
| 1117 | action: "revert", |
| 1118 | }), |
| 1119 | ); |
| 1120 | |
| 1121 | expect(result).toMatchObject({ |
| 1122 | _tag: "Failure", |
| 1123 | failure: { _tag: "GitLabRevertUnavailableError", reason: "not-merged" }, |
| 1124 | }); |
| 1125 | expect(mockedExecute).toHaveBeenCalledTimes(1); |
| 1126 | }), |
| 1127 | ); |
| 1128 | |
| 1129 | it.effect("sends a comment body over stdin, never in argv", () => |
| 1130 | Effect.gen(function* () { |
| 1131 | mockedExecute.mockReturnValueOnce(Effect.succeed(output(""))); |
| 1132 | const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli; |
| 1133 | |
| 1134 | yield* cli.commentOnMergeRequest({ |
| 1135 | cwd: "/w", |
| 1136 | repository: "acme/web", |
| 1137 | number: 7, |
| 1138 | body: "true", |
| 1139 | }); |
| 1140 | |
| 1141 | const call = mockedExecute.mock.calls[0]; |
| 1142 | assert.isDefined(call); |
| 1143 | expect(call[0].args).toEqual([ |
| 1144 | "api", |
| 1145 | "projects/acme%2Fweb/merge_requests/7/notes", |
| 1146 | "--method", |
| 1147 | "POST", |
| 1148 | "--input", |
| 1149 | "-", |
| 1150 | "--header", |
| 1151 | "Content-Type: application/json", |
| 1152 | ]); |
| 1153 | expect(call[0].stdin).toBe('{"body":"true"}'); |
| 1154 | }), |
| 1155 | ); |
| 1156 | |
| 1157 | it.effect("reads one diff page and hands back the cursor for the next", () => |
| 1158 | Effect.gen(function* () { |
| 1159 | mockedExecute.mockReturnValueOnce(Effect.succeed(output(diffPage(0)))); |
| 1160 | const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli; |
| 1161 | |
| 1162 | const diff = yield* cli.getMergeRequestDiff({ |
| 1163 | cwd: "/w", |
| 1164 | repository: "acme/web", |
| 1165 | number: 7, |
| 1166 | }); |
| 1167 | |
| 1168 | assert.strictEqual(mockedExecute.mock.calls.length, 1); |
| 1169 | assert.isNotNull(diff.nextCursor); |
| 1170 | assert.isFalse(diff.truncated); |
| 1171 | expect(argsOfCall(0)[1]).toContain("merge_requests/7/diffs?per_page=30&page=1"); |
| 1172 | }), |
| 1173 | ); |
| 1174 | |
| 1175 | it.effect("counts a file GitLab withheld as too large and offers to read it alone", () => |
| 1176 | Effect.gen(function* () { |
| 1177 | mockedExecute |
| 1178 | .mockReturnValueOnce( |
| 1179 | Effect.succeed( |
| 1180 | output( |
| 1181 | JSON.stringify([ |
| 1182 | { old_path: "src/a.ts", new_path: "src/a.ts", diff: "@@ -1 +1 @@\n-a\n+b\n" }, |
| 1183 | { old_path: "src/huge.txt", new_path: "src/huge.txt", diff: "", too_large: true }, |
| 1184 | ]), |
| 1185 | ), |
| 1186 | ), |
| 1187 | ) |
| 1188 | .mockReturnValueOnce( |
| 1189 | Effect.succeed( |
| 1190 | output( |
| 1191 | JSON.stringify({ |
| 1192 | data: { |
| 1193 | project: { |
| 1194 | mergeRequest: { |
| 1195 | diffStatsSummary: { fileCount: 2 }, |
| 1196 | diffStats: [ |
| 1197 | { path: "src/a.ts", additions: 1, deletions: 1 }, |
| 1198 | { path: "src/huge.txt", additions: 20000, deletions: 0 }, |
| 1199 | ], |
| 1200 | }, |
| 1201 | }, |
| 1202 | }, |
| 1203 | }), |
| 1204 | ), |
| 1205 | ), |
| 1206 | ); |
| 1207 | const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli; |
| 1208 | |
| 1209 | const diff = yield* cli.getMergeRequestDiff({ cwd: "/w", repository: "acme/web", number: 7 }); |
| 1210 | |
| 1211 | expect(diff.omittedFileStats).toEqual([ |
| 1212 | { path: "src/huge.txt", additions: 20000, deletions: 0 }, |
| 1213 | ]); |
| 1214 | assert.isTrue(diff.withheldFilesReadable); |
| 1215 | }), |
| 1216 | ); |
| 1217 | |
| 1218 | it.effect("reads one withheld file's section from the merge request's raw diff", () => |
| 1219 | Effect.gen(function* () { |
| 1220 | mockedExecute.mockReturnValueOnce( |
| 1221 | Effect.succeed( |
| 1222 | output( |
| 1223 | [ |
| 1224 | "diff --git a/src/a.ts b/src/a.ts", |
| 1225 | "--- a/src/a.ts", |
| 1226 | "+++ b/src/a.ts", |
| 1227 | "@@ -1 +1 @@", |
| 1228 | "-a", |
| 1229 | "+b", |
| 1230 | "diff --git a/src/huge.txt b/src/huge.txt", |
| 1231 | "new file mode 100644", |
| 1232 | "--- /dev/null", |
| 1233 | "+++ b/src/huge.txt", |
| 1234 | "@@ -0,0 +1,2 @@", |
| 1235 | "+one", |
| 1236 | "+two", |
| 1237 | "", |
| 1238 | ].join("\n"), |
| 1239 | ), |
| 1240 | ), |
| 1241 | ); |
| 1242 | const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli; |
| 1243 | |
| 1244 | const diff = yield* cli.getMergeRequestDiff({ |
| 1245 | cwd: "/w", |
| 1246 | repository: "acme/web", |
| 1247 | number: 7, |
| 1248 | path: "src/huge.txt", |
| 1249 | }); |
| 1250 | |
| 1251 | expect(argsOfCall(0)[1]).toBe("projects/acme%2Fweb/merge_requests/7/raw_diffs"); |
| 1252 | assert.strictEqual( |
| 1253 | diff.patch, |
| 1254 | "diff --git a/src/huge.txt b/src/huge.txt\nnew file mode 100644\n--- /dev/null\n+++ b/src/huge.txt\n@@ -0,0 +1,2 @@\n+one\n+two\n", |
| 1255 | ); |
| 1256 | }), |
| 1257 | ); |
| 1258 | |
| 1259 | it.effect( |
| 1260 | "pages a merge request diff 30 files at a time, which GitLab 15.7 to 17.10 accept", |
| 1261 | () => |
| 1262 | Effect.gen(function* () { |
| 1263 | mockedExecute |
| 1264 | .mockReturnValueOnce(Effect.succeed(output(diffPage(0, 30)))) |
| 1265 | .mockReturnValueOnce(Effect.succeed(output(diffPage(30, 30)))) |
| 1266 | .mockReturnValueOnce(Effect.succeed(output(diffPage(60, 5)))); |
| 1267 | const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli; |
| 1268 | const target = { cwd: "/w", repository: "acme/web", number: 7 }; |
| 1269 | |
| 1270 | let patch = ""; |
| 1271 | let cursor: string | undefined; |
| 1272 | do { |
| 1273 | const slice = yield* cli.getMergeRequestDiff({ ...target, cursor }); |
| 1274 | patch += slice.patch; |
| 1275 | cursor = slice.nextCursor ?? undefined; |
| 1276 | } while (cursor !== undefined); |
| 1277 | |
| 1278 | expect(mockedExecute.mock.calls.map(([call]) => call.args[1])).toEqual([ |
| 1279 | "projects/acme%2Fweb/merge_requests/7/diffs?per_page=30&page=1", |
| 1280 | "projects/acme%2Fweb/merge_requests/7/diffs?per_page=30&page=2", |
| 1281 | "projects/acme%2Fweb/merge_requests/7/diffs?per_page=30&page=3", |
| 1282 | ]); |
| 1283 | assert.strictEqual(patch.match(/^diff --git /gm)?.length, 65); |
| 1284 | expect(patch).toContain("diff --git a/src/64.ts b/src/64.ts"); |
| 1285 | }), |
| 1286 | ); |
| 1287 | |
| 1288 | it.effect("refuses a cursor it never handed out rather than reading it into a query", () => |
| 1289 | Effect.gen(function* () { |
| 1290 | const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli; |
| 1291 | |
| 1292 | const error = yield* Effect.flip( |
| 1293 | cli.getMergeRequestDiff({ |
| 1294 | cwd: "/w", |
| 1295 | repository: "acme/web", |
| 1296 | number: 7, |
| 1297 | cursor: "1&per_page=1", |
| 1298 | }), |
| 1299 | ); |
| 1300 | |
| 1301 | assert.strictEqual(error._tag, "GitLabDiffCursorError"); |
| 1302 | assert.strictEqual(mockedExecute.mock.calls.length, 0); |
| 1303 | }), |
| 1304 | ); |
| 1305 | |
| 1306 | it.effect("reads a named commit from its own diff, and pages inside it", () => |
| 1307 | Effect.gen(function* () { |
| 1308 | mockedExecute |
| 1309 | .mockReturnValueOnce(Effect.succeed(output(diffPage(0)))) |
| 1310 | .mockReturnValueOnce(Effect.succeed(output(diffPage(100, 3)))); |
| 1311 | const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli; |
| 1312 | const target = { |
| 1313 | cwd: "/w", |
| 1314 | repository: "acme/web", |
| 1315 | number: 7, |
| 1316 | commit: "a1b2c3d4e5f6a7b8c9d0e1f2a3b4c5d6e7f8a9b0", |
| 1317 | }; |
| 1318 | |
| 1319 | const first = yield* cli.getMergeRequestDiff(target); |
| 1320 | assert.isNotNull(first.nextCursor); |
| 1321 | const second = yield* cli.getMergeRequestDiff({ ...target, cursor: first.nextCursor }); |
| 1322 | |
| 1323 | const commitPath = |
| 1324 | "projects/acme%2Fweb/repository/commits/a1b2c3d4e5f6a7b8c9d0e1f2a3b4c5d6e7f8a9b0/diff"; |
| 1325 | expect(argsOfCall(0)[1]).toBe(`${commitPath}?per_page=100&page=1`); |
| 1326 | expect(argsOfCall(1)[1]).toBe(`${commitPath}?per_page=100&page=2`); |
| 1327 | assert.isNull(second.nextCursor); |
| 1328 | }), |
| 1329 | ); |
| 1330 | |
| 1331 | it.effect("refuses a commit that is not a sha rather than reading it into a path", () => |
| 1332 | Effect.gen(function* () { |
| 1333 | const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli; |
| 1334 | |
| 1335 | const error = yield* Effect.flip( |
| 1336 | cli.getMergeRequestDiff({ |
| 1337 | cwd: "/w", |
| 1338 | repository: "acme/web", |
| 1339 | number: 7, |
| 1340 | commit: "../../merge_requests/8/diffs", |
| 1341 | }), |
| 1342 | ); |
| 1343 | |
| 1344 | assert.strictEqual(error._tag, "GitLabDiffCommitError"); |
| 1345 | assert.strictEqual(mockedExecute.mock.calls.length, 0); |
| 1346 | }), |
| 1347 | ); |
| 1348 | |
| 1349 | it.effect("reports a commit with no parent as a structured error", () => |
| 1350 | Effect.gen(function* () { |
| 1351 | mockedExecute.mockReturnValueOnce( |
| 1352 | Effect.succeed(output(JSON.stringify({ id: "a1b2c3d", parent_ids: [] }))), |
| 1353 | ); |
| 1354 | const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli; |
| 1355 | |
| 1356 | const error = yield* Effect.flip( |
| 1357 | cli.getMergeRequestDiffFileContents({ |
| 1358 | cwd: "/w", |
| 1359 | repository: "acme/web", |
| 1360 | number: 7, |
| 1361 | commit: "a1b2c3d", |
| 1362 | changeType: "change", |
| 1363 | oldPath: "src/a.ts", |
| 1364 | newPath: "src/a.ts", |
| 1365 | }), |
| 1366 | ); |
| 1367 | |
| 1368 | assert.strictEqual(error._tag, "GitLabDiffCommitParentUnavailableError"); |
| 1369 | if (error._tag === "GitLabDiffCommitParentUnavailableError") { |
| 1370 | assert.strictEqual(error.commit, "a1b2c3d"); |
| 1371 | } |
| 1372 | }), |
| 1373 | ); |
| 1374 | |
| 1375 | it.effect("expands a new file from a root commit without requiring a parent", () => |
| 1376 | Effect.gen(function* () { |
| 1377 | mockedExecute.mockReturnValueOnce( |
| 1378 | Effect.succeed(output(JSON.stringify({ id: "a1b2c3d", parent_ids: [] }))), |
| 1379 | ); |
| 1380 | mockedExecute.mockReturnValueOnce(Effect.succeed(output("first contents\n"))); |
| 1381 | const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli; |
| 1382 | |
| 1383 | const contents = yield* cli.getMergeRequestDiffFileContents({ |
| 1384 | cwd: "/w", |
| 1385 | repository: "acme/web", |
| 1386 | number: 7, |
| 1387 | commit: "a1b2c3d", |
| 1388 | changeType: "new", |
| 1389 | oldPath: "src/first.ts", |
| 1390 | newPath: "src/first.ts", |
| 1391 | }); |
| 1392 | |
| 1393 | expect(contents).toEqual({ oldContents: "", newContents: "first contents\n" }); |
| 1394 | expect(argsOfCall(1)[1]).toContain("raw?ref=a1b2c3d"); |
| 1395 | }), |
| 1396 | ); |
| 1397 | |
| 1398 | it.effect("reports undecodable diff file contents as binary", () => |
| 1399 | Effect.gen(function* () { |
| 1400 | mockedExecute.mockReturnValueOnce( |
| 1401 | Effect.succeed( |
| 1402 | output( |
| 1403 | JSON.stringify({ |
| 1404 | diff_refs: { |
| 1405 | base_sha: "a1b2c3d", |
| 1406 | head_sha: "b1c2d3e", |
| 1407 | start_sha: "a1b2c3d", |
| 1408 | }, |
| 1409 | }), |
| 1410 | ), |
| 1411 | ), |
| 1412 | ); |
| 1413 | mockedExecute.mockReturnValueOnce( |
| 1414 | Effect.succeed(output("binary\uFFFDcontents", false, true)), |
| 1415 | ); |
| 1416 | const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli; |
| 1417 | |
| 1418 | const error = yield* Effect.flip( |
| 1419 | cli.getMergeRequestDiffFileContents({ |
| 1420 | cwd: "/w", |
| 1421 | repository: "acme/web", |
| 1422 | number: 7, |
| 1423 | changeType: "deleted", |
| 1424 | oldPath: "assets/logo.png", |
| 1425 | newPath: "assets/logo.png", |
| 1426 | }), |
| 1427 | ); |
| 1428 | |
| 1429 | assert.strictEqual(error._tag, "GitLabDiffFileContentsUnavailableError"); |
| 1430 | if (error._tag === "GitLabDiffFileContentsUnavailableError") { |
| 1431 | assert.strictEqual(error.path, "assets/logo.png"); |
| 1432 | assert.strictEqual(error.reason, "binary"); |
| 1433 | } |
| 1434 | }), |
| 1435 | ); |
| 1436 | |
| 1437 | it.effect("returns valid text containing a literal replacement character", () => |
| 1438 | Effect.gen(function* () { |
| 1439 | mockedExecute.mockReturnValueOnce( |
| 1440 | Effect.succeed( |
| 1441 | output( |
| 1442 | JSON.stringify({ |
| 1443 | diff_refs: { |
| 1444 | base_sha: "a1b2c3d", |
| 1445 | head_sha: "b1c2d3e", |
| 1446 | start_sha: "a1b2c3d", |
| 1447 | }, |
| 1448 | }), |
| 1449 | ), |
| 1450 | ), |
| 1451 | ); |
| 1452 | mockedExecute.mockReturnValueOnce(Effect.succeed(output("before\uFFFDafter"))); |
| 1453 | const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli; |
| 1454 | |
| 1455 | const contents = yield* cli.getMergeRequestDiffFileContents({ |
| 1456 | cwd: "/w", |
| 1457 | repository: "acme/web", |
| 1458 | number: 7, |
| 1459 | changeType: "deleted", |
| 1460 | oldPath: "docs/encoding.md", |
| 1461 | newPath: "docs/encoding.md", |
| 1462 | }); |
| 1463 | |
| 1464 | assert.strictEqual(contents.oldContents, "before\uFFFDafter"); |
| 1465 | }), |
| 1466 | ); |
| 1467 | |
| 1468 | it.effect("ends the diff on a page with no files rather than asking for it again", () => |
| 1469 | Effect.gen(function* () { |
| 1470 | mockedExecute.mockReturnValueOnce(Effect.succeed(output("[]"))); |
| 1471 | const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli; |
| 1472 | |
| 1473 | const diff = yield* cli.getMergeRequestDiff({ |
| 1474 | cwd: "/w", |
| 1475 | repository: "acme/web", |
| 1476 | number: 7, |
| 1477 | cursor: "4", |
| 1478 | }); |
| 1479 | |
| 1480 | assert.strictEqual(diff.patch, ""); |
| 1481 | assert.isNull(diff.nextCursor); |
| 1482 | }), |
| 1483 | ); |
| 1484 | |
| 1485 | it.effect("fails a diff page cut off mid-JSON rather than calling the diff whole", () => |
| 1486 | Effect.gen(function* () { |
| 1487 | mockedExecute.mockReturnValueOnce( |
| 1488 | Effect.succeed({ ...output('[{"old_path":"src/x.ts","new_p'), stdoutTruncated: true }), |
| 1489 | ); |
| 1490 | const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli; |
| 1491 | |
| 1492 | const error = yield* Effect.flip( |
| 1493 | cli.getMergeRequestDiff({ cwd: "/w", repository: "acme/web", number: 7 }), |
| 1494 | ); |
| 1495 | |
| 1496 | assert.strictEqual(error._tag, "GitLabMergeRequestReadError"); |
| 1497 | }), |
| 1498 | ); |
| 1499 | |
| 1500 | it.effect("offers no squash when the project does not say it allows one", () => |
| 1501 | Effect.gen(function* () { |
| 1502 | mockedExecute.mockReturnValueOnce( |
| 1503 | Effect.succeed(output(JSON.stringify({ merge_method: "merge" }))), |
| 1504 | ); |
| 1505 | const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli; |
| 1506 | |
| 1507 | const capabilities = yield* cli.getProjectMergeCapabilities({ |
| 1508 | cwd: "/w", |
| 1509 | repository: "acme/web", |
| 1510 | }); |
| 1511 | |
| 1512 | assert.deepStrictEqual(capabilities, { merge: true, squash: false, rebase: false }); |
| 1513 | }), |
| 1514 | ); |
| 1515 | |
| 1516 | it.effect("reads the project's merge settings as its merge capabilities", () => |
| 1517 | Effect.gen(function* () { |
| 1518 | mockedExecute.mockReturnValueOnce( |
| 1519 | Effect.succeed(output(JSON.stringify({ merge_method: "ff", squash_option: "never" }))), |
| 1520 | ); |
| 1521 | const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli; |
| 1522 | |
| 1523 | const capabilities = yield* cli.getProjectMergeCapabilities({ |
| 1524 | cwd: "/w", |
| 1525 | repository: "acme/web", |
| 1526 | }); |
| 1527 | |
| 1528 | assert.deepStrictEqual(capabilities, { merge: false, squash: false, rebase: true }); |
| 1529 | }), |
| 1530 | ); |
| 1531 | |
| 1532 | it.effect("fails when the authenticated account has no username", () => |
| 1533 | Effect.gen(function* () { |
| 1534 | mockedExecute.mockReturnValueOnce(Effect.succeed(output(JSON.stringify({ username: "" })))); |
| 1535 | const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli; |
| 1536 | |
| 1537 | const error = yield* Effect.flip(cli.getViewerUsername({ cwd: "/w" })); |
| 1538 | |
| 1539 | assert.strictEqual(error._tag, "GitLabViewerUnavailableError"); |
| 1540 | }), |
| 1541 | ); |
| 1542 | |
| 1543 | it.effect("walks the notes until GitLab answers with a short page", () => |
| 1544 | Effect.gen(function* () { |
| 1545 | mockedExecute.mockReturnValueOnce(Effect.succeed(output(notes(100, 1)))); |
| 1546 | mockedExecute.mockReturnValueOnce(Effect.succeed(output(notes(2, 101)))); |
| 1547 | const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli; |
| 1548 | |
| 1549 | const { comments, truncated } = yield* cli.listNotes({ |
| 1550 | cwd: "/w", |
| 1551 | repository: "acme/web", |
| 1552 | number: 7, |
| 1553 | }); |
| 1554 | |
| 1555 | expect(argsOfCall(0).join(" ")).toContain("page=1"); |
| 1556 | expect(argsOfCall(1).join(" ")).toContain("page=2"); |
| 1557 | assert.strictEqual(comments.length, 102); |
| 1558 | assert.isFalse(truncated); |
| 1559 | }), |
| 1560 | ); |
| 1561 | |
| 1562 | it.effect("stops the note walk at its bound and says the conversation was cut short", () => |
| 1563 | Effect.gen(function* () { |
| 1564 | mockedExecute.mockReturnValue(Effect.succeed(output(notes(100, 1)))); |
| 1565 | const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli; |
| 1566 | |
| 1567 | const { truncated } = yield* cli.listNotes({ |
| 1568 | cwd: "/w", |
| 1569 | repository: "acme/web", |
| 1570 | number: 7, |
| 1571 | }); |
| 1572 | |
| 1573 | assert.strictEqual(mockedExecute.mock.calls.length, 10); |
| 1574 | assert.isTrue(truncated); |
| 1575 | }), |
| 1576 | ); |
| 1577 | |
| 1578 | it.effect( |
| 1579 | "reads a positioned discussion at its line and an overview discussion as a thread and a finding", |
| 1580 | () => |
| 1581 | Effect.gen(function* () { |
| 1582 | mockedExecute.mockReturnValueOnce( |
| 1583 | Effect.succeed( |
| 1584 | output( |
| 1585 | JSON.stringify([ |
| 1586 | { |
| 1587 | id: "abc123", |
| 1588 | notes: [ |
| 1589 | { |
| 1590 | id: 1, |
| 1591 | body: "rename this", |
| 1592 | author: { username: "bilal", avatar_url: "https://avatars/b.png" }, |
| 1593 | created_at: "2026-07-01T00:00:00Z", |
| 1594 | resolvable: true, |
| 1595 | resolved: true, |
| 1596 | position: { |
| 1597 | position_type: "text", |
| 1598 | new_path: "src/a.ts", |
| 1599 | old_path: "src/a.ts", |
| 1600 | new_line: 12, |
| 1601 | old_line: null, |
| 1602 | base_sha: "base", |
| 1603 | start_sha: "start", |
| 1604 | head_sha: "head", |
| 1605 | }, |
| 1606 | }, |
| 1607 | { |
| 1608 | id: 2, |
| 1609 | body: "done", |
| 1610 | author: { username: "julius" }, |
| 1611 | created_at: "2026-07-01T01:00:00Z", |
| 1612 | }, |
| 1613 | ], |
| 1614 | }, |
| 1615 | { id: "def456", notes: [{ id: 3, body: "ship it", created_at: "2026-07-01Z" }] }, |
| 1616 | { |
| 1617 | id: "ghi789", |
| 1618 | notes: [ |
| 1619 | { |
| 1620 | id: 4, |
| 1621 | body: "mention the new flag in the README", |
| 1622 | created_at: "2026-07-01T02:00:00Z", |
| 1623 | resolvable: true, |
| 1624 | resolved: false, |
| 1625 | }, |
| 1626 | { |
| 1627 | id: 5, |
| 1628 | body: "will do", |
| 1629 | created_at: "2026-07-01T03:00:00Z", |
| 1630 | resolvable: true, |
| 1631 | resolved: false, |
| 1632 | }, |
| 1633 | ], |
| 1634 | }, |
| 1635 | { |
| 1636 | id: "jkl012", |
| 1637 | notes: [ |
| 1638 | { |
| 1639 | id: 6, |
| 1640 | body: "already addressed", |
| 1641 | created_at: "2026-07-01T04:00:00Z", |
| 1642 | resolvable: true, |
| 1643 | resolved: true, |
| 1644 | }, |
| 1645 | ], |
| 1646 | }, |
| 1647 | { |
| 1648 | id: "file789", |
| 1649 | notes: [ |
| 1650 | { |
| 1651 | id: 7, |
| 1652 | body: "split this file", |
| 1653 | created_at: "2026-07-01Z", |
| 1654 | position: { |
| 1655 | position_type: "file", |
| 1656 | new_path: "src/b.ts", |
| 1657 | old_path: "src/b.ts", |
| 1658 | new_line: null, |
| 1659 | old_line: null, |
| 1660 | }, |
| 1661 | }, |
| 1662 | ], |
| 1663 | }, |
| 1664 | { |
| 1665 | id: "img012", |
| 1666 | notes: [ |
| 1667 | { |
| 1668 | id: 8, |
| 1669 | body: "logo is blurry", |
| 1670 | created_at: "2026-07-01Z", |
| 1671 | position: { |
| 1672 | position_type: "image", |
| 1673 | new_path: "logo.png", |
| 1674 | old_path: "logo.png", |
| 1675 | new_line: null, |
| 1676 | old_line: null, |
| 1677 | }, |
| 1678 | }, |
| 1679 | ], |
| 1680 | }, |
| 1681 | ]), |
| 1682 | ), |
| 1683 | ), |
| 1684 | ); |
| 1685 | const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli; |
| 1686 | |
| 1687 | const { threads, diffRefsByThreadId, overviewFindingIds } = yield* cli.listDiscussions({ |
| 1688 | cwd: "/w", |
| 1689 | repository: "acme/web", |
| 1690 | number: 7, |
| 1691 | }); |
| 1692 | |
| 1693 | expect(overviewFindingIds).toEqual(["4"]); |
| 1694 | expect(threads.map(({ id, path, line, side }) => ({ id, path, line, side }))).toEqual([ |
| 1695 | { id: "abc123", path: "src/a.ts", line: 12, side: "right" }, |
| 1696 | { id: "ghi789", path: null, line: null, side: "right" }, |
| 1697 | { id: "jkl012", path: null, line: null, side: "right" }, |
| 1698 | { id: "file789", path: "src/b.ts", line: null, side: "right" }, |
| 1699 | { id: "img012", path: "logo.png", line: null, side: "right" }, |
| 1700 | ]); |
| 1701 | expect(threads[1]).toMatchObject({ isResolved: false }); |
| 1702 | expect(threads[1]?.comments.map(({ id }) => id)).toEqual(["4", "5"]); |
| 1703 | expect(threads[2]).toMatchObject({ isResolved: true }); |
| 1704 | expect([...diffRefsByThreadId]).toEqual([ |
| 1705 | ["abc123", { baseSha: "base", startSha: "start", headSha: "head" }], |
| 1706 | ]); |
| 1707 | expect(threads[0]).toMatchObject({ isResolved: true }); |
| 1708 | assert.strictEqual(threads[0]?.comments.length, 2); |
| 1709 | }), |
| 1710 | ); |
| 1711 | |
| 1712 | it.effect("sends a review as its comments, then its summary, then the verdict", () => |
| 1713 | Effect.gen(function* () { |
| 1714 | mockedExecute.mockReturnValueOnce( |
| 1715 | Effect.succeed( |
| 1716 | output( |
| 1717 | JSON.stringify({ |
| 1718 | iid: 7, |
| 1719 | title: "t", |
| 1720 | web_url: "https://gitlab.com/acme/web/-/merge_requests/7", |
| 1721 | source_branch: "feat", |
| 1722 | target_branch: "main", |
| 1723 | created_at: "2026-07-01T00:00:00Z", |
| 1724 | updated_at: "2026-07-01T00:00:00Z", |
| 1725 | diff_refs: { base_sha: "base", head_sha: "head", start_sha: "start" }, |
| 1726 | }), |
| 1727 | ), |
| 1728 | ), |
| 1729 | ); |
| 1730 | // Line 12 is added after a removed line, so GitLab's old counter there is 11, not 0. |
| 1731 | mockedExecute.mockReturnValueOnce( |
| 1732 | Effect.succeed( |
| 1733 | output( |
| 1734 | JSON.stringify([ |
| 1735 | { |
| 1736 | old_path: "src/a.ts", |
| 1737 | new_path: "src/b.ts", |
| 1738 | renamed_file: true, |
| 1739 | diff: "@@ -8,4 +9,4 @@\n a\n b\n-c\n+d\n+e\n f\n-g\n", |
| 1740 | }, |
| 1741 | ]), |
| 1742 | ), |
| 1743 | ), |
| 1744 | ); |
| 1745 | mockedExecute.mockReturnValue(Effect.succeed(output("{}"))); |
| 1746 | const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli; |
| 1747 | |
| 1748 | yield* cli.submitReview({ |
| 1749 | cwd: "/w", |
| 1750 | repository: "acme/web", |
| 1751 | number: 7, |
| 1752 | verdict: "approve", |
| 1753 | body: "Looks right.", |
| 1754 | comments: [ |
| 1755 | { |
| 1756 | path: "src/b.ts", |
| 1757 | oldPath: "src/a.ts", |
| 1758 | position: { kind: "deleted", oldLine: 4 }, |
| 1759 | body: "why remove?", |
| 1760 | }, |
| 1761 | { |
| 1762 | path: "src/b.ts", |
| 1763 | oldPath: "src/a.ts", |
| 1764 | startPosition: { kind: "context", oldLine: 9, newLine: 10, side: "right" }, |
| 1765 | position: { kind: "added", newLine: 12 }, |
| 1766 | body: "this block", |
| 1767 | }, |
| 1768 | { |
| 1769 | path: "src/b.ts", |
| 1770 | startPosition: { kind: "added", newLine: 12 }, |
| 1771 | position: { kind: "added", newLine: 12 }, |
| 1772 | body: "one line", |
| 1773 | }, |
| 1774 | ], |
| 1775 | }); |
| 1776 | |
| 1777 | expect(argsOfCall(0)[1]).toContain("merge_requests/7"); |
| 1778 | expect(argsOfCall(1)[1]).toContain("merge_requests/7/diffs"); |
| 1779 | expect(argsOfCall(2)[1]).toContain("/discussions"); |
| 1780 | expect(JSON.parse(callAt(2).stdin ?? "")).toEqual({ |
| 1781 | body: "why remove?", |
| 1782 | position: { |
| 1783 | base_sha: "base", |
| 1784 | head_sha: "head", |
| 1785 | start_sha: "start", |
| 1786 | position_type: "text", |
| 1787 | old_path: "src/a.ts", |
| 1788 | new_path: "src/b.ts", |
| 1789 | old_line: 4, |
| 1790 | }, |
| 1791 | }); |
| 1792 | // sha1("src/b.ts") |
| 1793 | const fileHash = "e55a8d6c5dd26e72538fd71fb7695dd829c8dab5"; |
| 1794 | expect(JSON.parse(callAt(3).stdin ?? "").position).toMatchObject({ |
| 1795 | new_line: 12, |
| 1796 | line_range: { |
| 1797 | start: { line_code: `${fileHash}_9_10`, type: null, old_line: 9, new_line: 10 }, |
| 1798 | end: { line_code: `${fileHash}_11_12`, type: "new", new_line: 12 }, |
| 1799 | }, |
| 1800 | }); |
| 1801 | expect(JSON.parse(callAt(4).stdin ?? "").position).not.toHaveProperty("line_range"); |
| 1802 | expect(argsOfCall(5)[1]).toContain("/notes"); |
| 1803 | expect(argsOfCall(6)[1]).toContain("/approve"); |
| 1804 | }), |
| 1805 | ); |
| 1806 | |
| 1807 | it.effect("requests changes as a reviewer and reports what GitLab refused", () => |
| 1808 | Effect.gen(function* () { |
| 1809 | mockedExecute |
| 1810 | .mockReturnValueOnce(Effect.succeed(output(JSON.stringify({ username: "bilal" })))) |
| 1811 | .mockReturnValueOnce( |
| 1812 | Effect.succeed( |
| 1813 | output(JSON.stringify({ data: { mergeRequestSetReviewers: { errors: [] } } })), |
| 1814 | ), |
| 1815 | ) |
| 1816 | .mockReturnValueOnce( |
| 1817 | Effect.succeed( |
| 1818 | output( |
| 1819 | JSON.stringify({ |
| 1820 | data: { mergeRequestRequestChanges: { errors: ["Merge request is locked"] } }, |
| 1821 | }), |
| 1822 | ), |
| 1823 | ), |
| 1824 | ); |
| 1825 | const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli; |
| 1826 | |
| 1827 | const error = yield* Effect.flip( |
| 1828 | cli.submitReview({ |
| 1829 | cwd: "/w", |
| 1830 | repository: "acme/web", |
| 1831 | number: 7, |
| 1832 | verdict: "request-changes", |
| 1833 | body: "Needs work", |
| 1834 | comments: [{ path: "src/a.ts", position: { kind: "added", newLine: 4 }, body: "nit" }], |
| 1835 | }), |
| 1836 | ); |
| 1837 | |
| 1838 | expect(mockedExecute).toHaveBeenCalledTimes(3); |
| 1839 | const target = { projectPath: "acme/web", iid: "7" }; |
| 1840 | const setReviewers = JSON.parse(callAt(1).stdin ?? ""); |
| 1841 | expect(setReviewers.query).toContain("operationMode: APPEND"); |
| 1842 | expect(setReviewers.variables).toEqual({ ...target, reviewers: ["bilal"] }); |
| 1843 | const requestChanges = JSON.parse(callAt(2).stdin ?? ""); |
| 1844 | expect(requestChanges.query).toContain("mergeRequestRequestChanges"); |
| 1845 | expect(requestChanges.variables).toEqual(target); |
| 1846 | assert.strictEqual(error._tag, "GitLabMutationRefusedError"); |
| 1847 | expect(error.detail).toBe("GitLab refused it: Merge request is locked"); |
| 1848 | }), |
| 1849 | ); |
| 1850 | |
| 1851 | it.effect("removes the reader's own award from a later page, and stops at an empty page", () => |
| 1852 | Effect.gen(function* () { |
| 1853 | const others = Array.from({ length: 100 }, (_, index) => |
| 1854 | index === 0 ? null : { id: index, name: "thumbsup", user: { username: "julius" } }, |
| 1855 | ); |
| 1856 | const page = (awards: ReadonlyArray<unknown>) => |
| 1857 | Effect.succeed(output(JSON.stringify(awards))); |
| 1858 | mockedExecute.mockReturnValueOnce( |
| 1859 | Effect.succeed(output(JSON.stringify({ username: "bilal" }))), |
| 1860 | ); |
| 1861 | mockedExecute.mockReturnValueOnce(page(others)); |
| 1862 | mockedExecute.mockReturnValueOnce( |
| 1863 | page([{ id: 500, name: "thumbsup", user: { username: "bilal" } }]), |
| 1864 | ); |
| 1865 | mockedExecute.mockReturnValueOnce(Effect.succeed(output("{}"))); |
| 1866 | mockedExecute.mockReturnValueOnce( |
| 1867 | Effect.succeed(output(JSON.stringify({ username: "bilal" }))), |
| 1868 | ); |
| 1869 | mockedExecute.mockReturnValueOnce(page(others)); |
| 1870 | mockedExecute.mockReturnValueOnce(page([])); |
| 1871 | const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli; |
| 1872 | const remove = { |
| 1873 | cwd: "/w", |
| 1874 | repository: "acme/web", |
| 1875 | number: 7, |
| 1876 | content: "thumbs-up", |
| 1877 | reacted: false, |
| 1878 | } as const; |
| 1879 | |
| 1880 | yield* cli.setReaction(remove); |
| 1881 | yield* cli.setReaction(remove); |
| 1882 | |
| 1883 | const awards = "projects/acme%2Fweb/merge_requests/7/award_emoji"; |
| 1884 | expect(mockedExecute.mock.calls.map((_, index) => argsOfCall(index)[1])).toEqual([ |
| 1885 | "user", |
| 1886 | `${awards}?per_page=100&page=1`, |
| 1887 | `${awards}?per_page=100&page=2`, |
| 1888 | `${awards}/500`, |
| 1889 | "user", |
| 1890 | `${awards}?per_page=100&page=1`, |
| 1891 | `${awards}?per_page=100&page=2`, |
| 1892 | ]); |
| 1893 | expect(argsOfCall(3)).toContain("DELETE"); |
| 1894 | }), |
| 1895 | ); |
| 1896 | |
| 1897 | it.effect("names a merge request with no diff revisions rather than calling it unreadable", () => |
| 1898 | Effect.gen(function* () { |
| 1899 | mockedExecute.mockReturnValueOnce( |
| 1900 | Effect.succeed( |
| 1901 | output( |
| 1902 | JSON.stringify({ |
| 1903 | iid: 7, |
| 1904 | title: "t", |
| 1905 | web_url: "https://gitlab.com/acme/web/-/merge_requests/7", |
| 1906 | source_branch: "feat", |
| 1907 | target_branch: "main", |
| 1908 | created_at: "2026-07-01T00:00:00Z", |
| 1909 | updated_at: "2026-07-01T00:00:00Z", |
| 1910 | diff_refs: null, |
| 1911 | }), |
| 1912 | ), |
| 1913 | ), |
| 1914 | ); |
| 1915 | const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli; |
| 1916 | |
| 1917 | const error = yield* Effect.flip( |
| 1918 | cli.submitReview({ |
| 1919 | cwd: "/w", |
| 1920 | repository: "acme/web", |
| 1921 | number: 7, |
| 1922 | verdict: "comment", |
| 1923 | body: "", |
| 1924 | comments: [{ path: "src/a.ts", position: { kind: "added", newLine: 4 }, body: "nit" }], |
| 1925 | }), |
| 1926 | ); |
| 1927 | |
| 1928 | assert.strictEqual(error._tag, "GitLabDiffRefsUnavailableError"); |
| 1929 | }), |
| 1930 | ); |
| 1931 | |
| 1932 | it.effect("shows a failed child pipeline that left the head pipeline green", () => |
| 1933 | Effect.gen(function* () { |
| 1934 | mockedExecute.mockImplementation((request) => { |
| 1935 | switch (request.args[1]) { |
| 1936 | case "graphql": |
| 1937 | return Effect.succeed(output("{}")); |
| 1938 | case "projects/12/pipelines/31/bridges?per_page=100": |
| 1939 | return Effect.succeed( |
| 1940 | output( |
| 1941 | JSON.stringify([ |
| 1942 | { |
| 1943 | name: "deploy", |
| 1944 | status: "success", |
| 1945 | downstream_pipeline: { |
| 1946 | status: "failed", |
| 1947 | web_url: "https://gitlab.com/bilal/web/-/pipelines/32", |
| 1948 | }, |
| 1949 | }, |
| 1950 | { name: "docs", status: "created", downstream_pipeline: null }, |
| 1951 | ]), |
| 1952 | ), |
| 1953 | ); |
| 1954 | default: |
| 1955 | return Effect.succeed( |
| 1956 | output( |
| 1957 | mergeRequestJson({ |
| 1958 | // A fork's pipeline runs in the source project, so its id is not the target's. |
| 1959 | head_pipeline: { |
| 1960 | id: 31, |
| 1961 | project_id: 12, |
| 1962 | status: "success", |
| 1963 | web_url: "https://gitlab.com/bilal/web/-/pipelines/31", |
| 1964 | }, |
| 1965 | }), |
| 1966 | ), |
| 1967 | ); |
| 1968 | } |
| 1969 | }); |
| 1970 | const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli; |
| 1971 | |
| 1972 | const detail = yield* cli.getMergeRequestDetail({ |
| 1973 | cwd: "/w", |
| 1974 | repository: "acme/web", |
| 1975 | number: 7, |
| 1976 | }); |
| 1977 | |
| 1978 | expect(detail.checks.map((check) => [check.name, check.status, check.url])).toEqual([ |
| 1979 | ["Pipeline", "success", "https://gitlab.com/bilal/web/-/pipelines/31"], |
| 1980 | ["deploy", "failure", "https://gitlab.com/bilal/web/-/pipelines/32"], |
| 1981 | ]); |
| 1982 | }), |
| 1983 | ); |
| 1984 | |
| 1985 | it.effect("keeps the one pipeline check when the trigger jobs cannot be read whole", () => |
| 1986 | Effect.gen(function* () { |
| 1987 | const detailOutput = output( |
| 1988 | mergeRequestJson({ head_pipeline: { id: 31, project_id: 12, status: "success" } }), |
| 1989 | ); |
| 1990 | const failedChild = { name: "deploy", downstream_pipeline: { status: "failed" } }; |
| 1991 | const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli; |
| 1992 | |
| 1993 | for (const [scenario, bridges] of [ |
| 1994 | [ |
| 1995 | "read fails", |
| 1996 | Effect.fail( |
| 1997 | new GitLabCli.GitLabCliCommandError({ |
| 1998 | operation: "execute", |
| 1999 | command: "glab", |
| 2000 | cwd: "/w", |
| 2001 | cause: new Error("403 Forbidden"), |
| 2002 | }), |
| 2003 | ), |
| 2004 | ], |
| 2005 | // A full page may have left out the trigger job whose pipeline failed. |
| 2006 | [ |
| 2007 | "page is full", |
| 2008 | Effect.succeed(output(JSON.stringify(Array.from({ length: 100 }, () => failedChild)))), |
| 2009 | ], |
| 2010 | ] as const) { |
| 2011 | mockedExecute.mockImplementation((request) => |
| 2012 | request.args[1] === "graphql" |
| 2013 | ? Effect.succeed(output("{}")) |
| 2014 | : request.args[1]?.includes("/bridges") |
| 2015 | ? bridges |
| 2016 | : Effect.succeed(detailOutput), |
| 2017 | ); |
| 2018 | const detail = yield* cli.getMergeRequestDetail({ |
| 2019 | cwd: "/w", |
| 2020 | repository: "acme/web", |
| 2021 | number: 7, |
| 2022 | }); |
| 2023 | expect( |
| 2024 | detail.checks.map((check) => [check.name, check.status]), |
| 2025 | scenario, |
| 2026 | ).toEqual([["Pipeline", "success"]]); |
| 2027 | } |
| 2028 | }), |
| 2029 | ); |
| 2030 | |
| 2031 | it.effect.each([ |
| 2032 | { |
| 2033 | answer: JSON.stringify({ |
| 2034 | data: { project: { mergeRequest: { userPermissions: { adminMergeRequest: true } } } }, |
| 2035 | }), |
| 2036 | expected: true, |
| 2037 | }, |
| 2038 | { answer: JSON.stringify({ errors: [{ message: "Field unknown" }] }), expected: false }, |
| 2039 | ])( |
| 2040 | "reads merge and label permissions without reading the trigger jobs or fork settings", |
| 2041 | ({ answer, expected }) => |
| 2042 | Effect.gen(function* () { |
| 2043 | mockedExecute.mockImplementation(({ args }) => |
| 2044 | Effect.succeed( |
| 2045 | output( |
| 2046 | args[1] === "graphql" |
| 2047 | ? answer |
| 2048 | : mergeRequestJson({ |
| 2049 | head_pipeline: { id: 31, project_id: 12, status: "success" }, |
| 2050 | user: { can_merge: false }, |
| 2051 | source_project_id: 12, |
| 2052 | target_project_id: 12, |
| 2053 | allow_collaboration: null, |
| 2054 | }), |
| 2055 | ), |
| 2056 | ), |
| 2057 | ); |
| 2058 | const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli; |
| 2059 | |
| 2060 | const detail = yield* cli.getMergeRequestDetail({ |
| 2061 | cwd: "/w", |
| 2062 | repository: "acme/web", |
| 2063 | number: 7, |
| 2064 | downstreamChecks: false, |
| 2065 | }); |
| 2066 | |
| 2067 | expect(detail.viewerCanMerge).toBe(false); |
| 2068 | expect(detail.maintainerCanModify).toBeUndefined(); |
| 2069 | expect(detail.viewerCanEditLabels).toBe(expected); |
| 2070 | expect(mockedExecute).toHaveBeenCalledTimes(2); |
| 2071 | }), |
| 2072 | ); |
| 2073 | |
| 2074 | it.effect( |
| 2075 | "offers the project's and its groups' labels, marking those already applied, and keeps an applied label past the page", |
| 2076 | () => |
| 2077 | Effect.gen(function* () { |
| 2078 | mockedExecute.mockImplementation(({ args }) => |
| 2079 | Effect.succeed( |
| 2080 | output( |
| 2081 | args[1]?.includes("/labels?") |
| 2082 | ? JSON.stringify([ |
| 2083 | { name: "bug", color: "#d9534f", description: "Something is broken" }, |
| 2084 | { name: "docs", color: null, description: null }, |
| 2085 | { name: "legacy", color: null, description: null, archived: true }, |
| 2086 | { name: "wontfix", color: null, description: null, archived: true }, |
| 2087 | ]) |
| 2088 | : mergeRequestJson({ labels: ["bug", "wontfix", "stale"] }), |
| 2089 | ), |
| 2090 | ), |
| 2091 | ); |
| 2092 | const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli; |
| 2093 | |
| 2094 | const list = yield* cli.listLabelCandidates({ |
| 2095 | cwd: "/w", |
| 2096 | repository: "acme/web", |
| 2097 | number: 7, |
| 2098 | }); |
| 2099 | |
| 2100 | expect( |
| 2101 | mockedExecute.mock.calls |
| 2102 | .map(([call]) => call.args[1]) |
| 2103 | .find((path) => path?.includes("/labels?")), |
| 2104 | ).toBe("projects/acme%2Fweb/labels?per_page=100&include_ancestor_groups=true"); |
| 2105 | expect(list).toEqual({ |
| 2106 | candidates: [ |
| 2107 | { name: "stale", color: null, description: null, isApplied: true }, |
| 2108 | { name: "bug", color: "#d9534f", description: "Something is broken", isApplied: true }, |
| 2109 | { name: "docs", color: null, description: null, isApplied: false }, |
| 2110 | { name: "wontfix", color: null, description: null, isApplied: true }, |
| 2111 | ], |
| 2112 | truncated: false, |
| 2113 | }); |
| 2114 | }), |
| 2115 | ); |
| 2116 | |
| 2117 | it.effect("adds labels the project has in one write", () => |
| 2118 | Effect.gen(function* () { |
| 2119 | mockedExecute.mockImplementation(({ args }) => |
| 2120 | Effect.succeed( |
| 2121 | output( |
| 2122 | args.includes("PUT") |
| 2123 | ? "{}" |
| 2124 | : JSON.stringify({ |
| 2125 | name: decodeURIComponent(args[1]?.split("/labels/")[1]?.split("?")[0] ?? ""), |
| 2126 | color: null, |
| 2127 | }), |
| 2128 | ), |
| 2129 | ), |
| 2130 | ); |
| 2131 | const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli; |
| 2132 | |
| 2133 | yield* cli.setLabels({ |
| 2134 | cwd: "/w", |
| 2135 | repository: "acme/web", |
| 2136 | number: 7, |
| 2137 | labels: ["bug", "docs"], |
| 2138 | applied: true, |
| 2139 | }); |
| 2140 | |
| 2141 | const write = mockedExecute.mock.calls.find(([call]) => call.args.includes("PUT"))?.[0]; |
| 2142 | assert.isDefined(write); |
| 2143 | expect(write.args[1]).toBe("projects/acme%2Fweb/merge_requests/7"); |
| 2144 | expect(JSON.parse(write.stdin ?? "")).toEqual({ add_labels: "bug,docs" }); |
| 2145 | expect(argsOfCall(0)[1]).toBe("projects/acme%2Fweb/labels/bug?include_ancestor_groups=true"); |
| 2146 | }), |
| 2147 | ); |
| 2148 | |
| 2149 | it.effect("refuses to add a label the project lacks, since GitLab would create it", () => |
| 2150 | Effect.gen(function* () { |
| 2151 | mockedExecute.mockImplementation(() => |
| 2152 | Effect.fail( |
| 2153 | new GitLabCli.GitLabCliCommandError({ |
| 2154 | operation: "execute", |
| 2155 | command: "glab", |
| 2156 | cwd: "/w", |
| 2157 | cause: new VcsProcessExitError({ |
| 2158 | operation: "GitLabCli.execute", |
| 2159 | command: "glab", |
| 2160 | cwd: "/w", |
| 2161 | exitCode: 1, |
| 2162 | detail: "404 Label Not Found", |
| 2163 | failureKind: "not-found", |
| 2164 | }), |
| 2165 | }), |
| 2166 | ), |
| 2167 | ); |
| 2168 | const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli; |
| 2169 | |
| 2170 | const result = yield* cli |
| 2171 | .setLabels({ cwd: "/w", repository: "acme/web", number: 7, labels: ["bug"], applied: true }) |
| 2172 | .pipe(Effect.flip); |
| 2173 | |
| 2174 | expect(result).toMatchObject({ _tag: "GitLabUnknownLabelError", labels: ["bug"] }); |
| 2175 | expect(argsOfCall(0)[1]).toBe("projects/acme%2Fweb/labels/bug?include_ancestor_groups=true"); |
| 2176 | expect(mockedExecute.mock.calls.some(([call]) => call.args.includes("PUT"))).toBe(false); |
| 2177 | }), |
| 2178 | ); |
| 2179 | |
| 2180 | it.effect("removes labels without looking them up", () => |
| 2181 | Effect.gen(function* () { |
| 2182 | mockedExecute.mockReturnValueOnce(Effect.succeed(output("{}"))); |
| 2183 | const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli; |
| 2184 | |
| 2185 | yield* cli.setLabels({ |
| 2186 | cwd: "/w", |
| 2187 | repository: "acme/web", |
| 2188 | number: 7, |
| 2189 | labels: ["bug"], |
| 2190 | applied: false, |
| 2191 | }); |
| 2192 | |
| 2193 | expect(mockedExecute).toHaveBeenCalledTimes(1); |
| 2194 | expect(JSON.parse(callAt(0).stdin ?? "")).toEqual({ remove_labels: "bug" }); |
| 2195 | }), |
| 2196 | ); |
| 2197 | |
| 2198 | it.effect.each([ |
| 2199 | { answer: sourceBranchJson({}), expected: true }, |
| 2200 | { answer: "not json", expected: false }, |
| 2201 | ])("asks GitLab whether a merged merge request's branch can be deleted", ({ answer, expected }) => |
| 2202 | Effect.gen(function* () { |
| 2203 | mockedExecute |
| 2204 | .mockReturnValueOnce(Effect.succeed(output(mergeRequestJson({ state: "merged" })))) |
| 2205 | .mockReturnValueOnce(Effect.succeed(labelPermission)) |
| 2206 | .mockReturnValueOnce(Effect.succeed(output(answer))); |
| 2207 | const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli; |
| 2208 | |
| 2209 | const detail = yield* cli.getMergeRequestDetail({ |
| 2210 | cwd: "/w", |
| 2211 | repository: "acme/web", |
| 2212 | number: 7, |
| 2213 | }); |
| 2214 | |
| 2215 | expect(detail.viewerCanDeleteBranch).toBe(expected); |
| 2216 | expect(argsOfCall(2)).toContain("graphql"); |
| 2217 | }), |
| 2218 | ); |
| 2219 | |
| 2220 | it.effect("deletes the source branch in the source project once its head is the merged one", () => |
| 2221 | Effect.gen(function* () { |
| 2222 | mockedExecute |
| 2223 | .mockReturnValueOnce( |
| 2224 | Effect.succeed(output(sourceBranchJson({ sourceProject: { fullPath: "fork/web" } }))), |
| 2225 | ) |
| 2226 | .mockReturnValueOnce( |
| 2227 | Effect.succeed( |
| 2228 | output( |
| 2229 | JSON.stringify({ |
| 2230 | default: false, |
| 2231 | commit: { id: "head", authored_date: "2026-11-03T20:29:40.000-07:00" }, |
| 2232 | }), |
| 2233 | ), |
| 2234 | ), |
| 2235 | ) |
| 2236 | .mockReturnValueOnce(Effect.succeed(output(""))); |
| 2237 | const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli; |
| 2238 | |
| 2239 | yield* cli.runMergeRequestAction({ |
| 2240 | cwd: "/w", |
| 2241 | repository: "acme/web", |
| 2242 | number: 7, |
| 2243 | action: "delete-branch", |
| 2244 | }); |
| 2245 | |
| 2246 | expect(argsOfCall(1)).toEqual(["api", "projects/fork%2Fweb/repository/branches/feat%2Fpage"]); |
| 2247 | expect(argsOfCall(2)).toEqual([ |
| 2248 | "api", |
| 2249 | "projects/fork%2Fweb/repository/branches/feat%2Fpage", |
| 2250 | "--method", |
| 2251 | "DELETE", |
| 2252 | "--header", |
| 2253 | "If-Unmodified-Since: Wed, 04 Nov 2026 03:29:40 GMT", |
| 2254 | ]); |
| 2255 | }), |
| 2256 | ); |
| 2257 | |
| 2258 | it.effect.each([ |
| 2259 | { reason: "open", source: { state: "opened" }, branch: undefined }, |
| 2260 | { reason: "open", source: { state: "locked" }, branch: undefined }, |
| 2261 | { |
| 2262 | reason: "not-allowed", |
| 2263 | source: { userPermissions: { removeSourceBranch: false } }, |
| 2264 | branch: undefined, |
| 2265 | }, |
| 2266 | { reason: "default-branch", source: {}, branch: { default: true, commit: { id: "head" } } }, |
| 2267 | { reason: "head-changed", source: {}, branch: { default: false, commit: { id: "newer" } } }, |
| 2268 | ])("refuses to delete the source branch when it is $reason", ({ reason, source, branch }) => |
| 2269 | Effect.gen(function* () { |
| 2270 | mockedExecute.mockReturnValueOnce(Effect.succeed(output(sourceBranchJson(source)))); |
| 2271 | if (branch !== undefined) { |
| 2272 | mockedExecute.mockReturnValueOnce(Effect.succeed(output(JSON.stringify(branch)))); |
| 2273 | } |
| 2274 | const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli; |
| 2275 | |
| 2276 | const error = yield* cli |
| 2277 | .runMergeRequestAction({ |
| 2278 | cwd: "/w", |
| 2279 | repository: "acme/web", |
| 2280 | number: 7, |
| 2281 | action: "delete-branch", |
| 2282 | }) |
| 2283 | .pipe(Effect.flip); |
| 2284 | |
| 2285 | expect(error).toMatchObject({ _tag: "GitLabBranchDeletionRefusedError", reason }); |
| 2286 | expect(mockedExecute).toHaveBeenCalledTimes(branch === undefined ? 1 : 2); |
| 2287 | }), |
| 2288 | ); |
| 2289 | |
| 2290 | it.effect.each([ |
| 2291 | { viewer: "the author who can push to the fork", username: "bilal", allowed: true }, |
| 2292 | { |
| 2293 | viewer: "the author who turned them off", |
| 2294 | username: "bilal", |
| 2295 | allowCollaboration: false, |
| 2296 | allowed: true, |
| 2297 | }, |
| 2298 | { viewer: "a target maintainer the fork lets push", username: "octocat", allowed: false }, |
| 2299 | { |
| 2300 | viewer: "the author without push access", |
| 2301 | username: "bilal", |
| 2302 | pushToSourceBranch: false, |
| 2303 | allowed: false, |
| 2304 | }, |
| 2305 | { |
| 2306 | viewer: "a viewer on a host that rejects the permission read", |
| 2307 | username: "bilal", |
| 2308 | rejected: true, |
| 2309 | allowed: false, |
| 2310 | }, |
| 2311 | { |
| 2312 | viewer: "the author of a protected source branch", |
| 2313 | username: "bilal", |
| 2314 | sourceBranchProtected: true, |
| 2315 | allowed: false, |
| 2316 | }, |
| 2317 | { |
| 2318 | viewer: "the author of a private fork", |
| 2319 | username: "bilal", |
| 2320 | sourceVisibility: "private", |
| 2321 | allowed: false, |
| 2322 | }, |
| 2323 | { |
| 2324 | viewer: "the author targeting a private project", |
| 2325 | username: "bilal", |
| 2326 | targetVisibility: "private", |
| 2327 | allowed: false, |
| 2328 | }, |
| 2329 | ])("reads a fork's maintainer edits and lets $viewer change them", (row) => |
| 2330 | Effect.gen(function* () { |
| 2331 | const allowCollaboration = row.allowCollaboration ?? true; |
| 2332 | mockedExecute.mockImplementation(({ args }) => |
| 2333 | Effect.succeed( |
| 2334 | output( |
| 2335 | args[1] === "graphql" |
| 2336 | ? JSON.stringify( |
| 2337 | row.rejected === true |
| 2338 | ? { errors: [{ message: "Field 'pushToSourceBranch' doesn't exist" }] } |
| 2339 | : { |
| 2340 | data: { |
| 2341 | currentUser: { username: row.username }, |
| 2342 | project: { |
| 2343 | visibility: row.targetVisibility ?? "public", |
| 2344 | mergeRequest: { |
| 2345 | author: { username: "bilal" }, |
| 2346 | userPermissions: { |
| 2347 | pushToSourceBranch: row.pushToSourceBranch ?? true, |
| 2348 | }, |
| 2349 | sourceBranchProtected: row.sourceBranchProtected ?? false, |
| 2350 | sourceProject: { visibility: row.sourceVisibility ?? "public" }, |
| 2351 | }, |
| 2352 | }, |
| 2353 | }, |
| 2354 | }, |
| 2355 | ) |
| 2356 | : mergeRequestJson({ |
| 2357 | author, |
| 2358 | source_project_id: 40, |
| 2359 | target_project_id: 12, |
| 2360 | allow_collaboration: allowCollaboration, |
| 2361 | }), |
| 2362 | ), |
| 2363 | ), |
| 2364 | ); |
| 2365 | const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli; |
| 2366 | |
| 2367 | const detail = yield* cli.getMergeRequestDetail({ |
| 2368 | cwd: "/w", |
| 2369 | repository: "acme/web", |
| 2370 | number: 7, |
| 2371 | downstreamChecks: false, |
| 2372 | }); |
| 2373 | |
| 2374 | expect(detail.maintainerCanModify).toBe(allowCollaboration); |
| 2375 | expect(detail.viewerCanEditMaintainerEdits).toBe(row.allowed); |
| 2376 | }), |
| 2377 | ); |
| 2378 | |
| 2379 | it.effect("asks GraphQL for merge request label colours the single REST GET cannot return", () => |
| 2380 | Effect.gen(function* () { |
| 2381 | const restLabels = ["bug", ...Array.from({ length: 100 }, (_, index) => `extra-${index}`)]; |
| 2382 | const graphqlNodes = [ |
| 2383 | { title: "bug", color: "#E24329", description: "Something is broken" }, |
| 2384 | ...Array.from({ length: 99 }, (_, index) => ({ |
| 2385 | title: `extra-${index}`, |
| 2386 | color: "#FFFFFF", |
| 2387 | })), |
| 2388 | ]; |
| 2389 | mockedExecute.mockImplementation(({ args }) => |
| 2390 | Effect.succeed( |
| 2391 | output( |
| 2392 | args[1] === "graphql" |
| 2393 | ? JSON.stringify({ |
| 2394 | data: { |
| 2395 | project: { |
| 2396 | mergeRequest: { |
| 2397 | userPermissions: { adminMergeRequest: false }, |
| 2398 | labels: { nodes: graphqlNodes }, |
| 2399 | }, |
| 2400 | }, |
| 2401 | }, |
| 2402 | }) |
| 2403 | : mergeRequestJson({ labels: restLabels }), |
| 2404 | ), |
| 2405 | ), |
| 2406 | ); |
| 2407 | const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli; |
| 2408 | |
| 2409 | const detail = yield* cli.getMergeRequestDetail({ |
| 2410 | cwd: "/w", |
| 2411 | repository: "acme/web", |
| 2412 | number: 7, |
| 2413 | downstreamChecks: false, |
| 2414 | maintainerEdits: false, |
| 2415 | }); |
| 2416 | |
| 2417 | const graphql = mockedExecute.mock.calls.find(([call]) => call.args[1] === "graphql"); |
| 2418 | assert.isDefined(graphql); |
| 2419 | expect(JSON.parse(graphql[0].stdin ?? "").query).toContain( |
| 2420 | "labels(first: 100) { nodes { title color description } }", |
| 2421 | ); |
| 2422 | expect( |
| 2423 | mockedExecute.mock.calls.some(([call]) => call.args[1]?.includes("with_labels_details")), |
| 2424 | ).toBe(false); |
| 2425 | expect(detail.labels).toHaveLength(101); |
| 2426 | expect(detail.labels[0]).toEqual({ |
| 2427 | name: "bug", |
| 2428 | color: "#E24329", |
| 2429 | description: "Something is broken", |
| 2430 | }); |
| 2431 | expect(detail.labels[1]).toEqual({ name: "extra-0", color: "#FFFFFF", description: null }); |
| 2432 | expect(detail.labels.at(-1)).toEqual({ name: "extra-99", color: null }); |
| 2433 | }), |
| 2434 | ); |
| 2435 | |
| 2436 | it.effect("skips a fork's maintainer edits permission read when the caller opts out", () => |
| 2437 | Effect.gen(function* () { |
| 2438 | mockedExecute |
| 2439 | .mockReturnValueOnce( |
| 2440 | Effect.succeed( |
| 2441 | output( |
| 2442 | mergeRequestJson({ |
| 2443 | source_project_id: 40, |
| 2444 | target_project_id: 12, |
| 2445 | allow_collaboration: true, |
| 2446 | }), |
| 2447 | ), |
| 2448 | ), |
| 2449 | ) |
| 2450 | .mockReturnValueOnce(Effect.succeed(labelPermission)); |
| 2451 | const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli; |
| 2452 | |
| 2453 | const detail = yield* cli.getMergeRequestDetail({ |
| 2454 | cwd: "/w", |
| 2455 | repository: "acme/web", |
| 2456 | number: 7, |
| 2457 | downstreamChecks: false, |
| 2458 | maintainerEdits: false, |
| 2459 | }); |
| 2460 | |
| 2461 | expect(detail.maintainerCanModify).toBe(true); |
| 2462 | expect(detail.viewerCanEditMaintainerEdits).toBeUndefined(); |
| 2463 | expect(mockedExecute).toHaveBeenCalledTimes(2); |
| 2464 | }), |
| 2465 | ); |
| 2466 | |
| 2467 | it.effect("sends maintainer edits as allow_collaboration", () => |
| 2468 | Effect.gen(function* () { |
| 2469 | mockedExecute.mockReturnValueOnce(Effect.succeed(output("{}"))); |
| 2470 | const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli; |
| 2471 | |
| 2472 | yield* cli.updateMergeRequest({ |
| 2473 | cwd: "/w", |
| 2474 | repository: "acme/web", |
| 2475 | number: 7, |
| 2476 | allowCollaboration: false, |
| 2477 | }); |
| 2478 | |
| 2479 | expect(argsOfCall(0)).toContain("PUT"); |
| 2480 | expect(JSON.parse(callAt(0).stdin ?? "")).toEqual({ allow_collaboration: false }); |
| 2481 | }), |
| 2482 | ); |
| 2483 | |
| 2484 | it.effect.each([ |
| 2485 | ["th", "projects/acme%2Fweb/users?per_page=100"], |
| 2486 | ["tho", "projects/acme%2Fweb/users?per_page=100&search=tho"], |
| 2487 | ] as const)("searches project users only from three characters (%s)", ([search, path]) => |
| 2488 | Effect.gen(function* () { |
| 2489 | mockedExecute.mockReturnValueOnce(Effect.succeed(output(JSON.stringify([author])))); |
| 2490 | const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli; |
| 2491 | |
| 2492 | yield* cli.listProjectUsers({ cwd: "/w", repository: "acme/web", search }); |
| 2493 | |
| 2494 | expect(argsOfCall(0)[1]).toBe(path); |
| 2495 | }), |
| 2496 | ); |
| 2497 | |
| 2498 | it.effect("reads who has access to the project and who is already on the merge request", () => |
| 2499 | Effect.gen(function* () { |
| 2500 | mockedExecute |
| 2501 | .mockReturnValueOnce(Effect.succeed(output(mergeRequestJson({ reviewers: [reviewer] })))) |
| 2502 | .mockReturnValueOnce( |
| 2503 | Effect.succeed(output(JSON.stringify([author, reviewer, { id: 9, username: "hubot" }]))), |
| 2504 | ); |
| 2505 | const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli; |
| 2506 | |
| 2507 | const list = yield* cli.listReviewerCandidates({ |
| 2508 | cwd: "/w", |
| 2509 | repository: "acme/web", |
| 2510 | number: 7, |
| 2511 | }); |
| 2512 | |
| 2513 | expect(argsOfCall(1)[1]).toBe("projects/acme%2Fweb/users?per_page=100"); |
| 2514 | expect(list.candidates.map((candidate) => [candidate.id, candidate.isRequested])).toEqual([ |
| 2515 | ["5", true], |
| 2516 | ["9", false], |
| 2517 | ]); |
| 2518 | assert.isFalse(list.truncated); |
| 2519 | }), |
| 2520 | ); |
| 2521 | |
| 2522 | it.effect( |
| 2523 | "lists the requested reviewers first, even when the users page does not reach them", |
| 2524 | () => |
| 2525 | Effect.gen(function* () { |
| 2526 | mockedExecute |
| 2527 | .mockReturnValueOnce( |
| 2528 | Effect.succeed(output(mergeRequestJson({ reviewers: [{ id: 42, username: "late" }] }))), |
| 2529 | ) |
| 2530 | .mockReturnValueOnce(Effect.succeed(output(JSON.stringify([author, reviewer])))); |
| 2531 | const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli; |
| 2532 | |
| 2533 | const list = yield* cli.listReviewerCandidates({ |
| 2534 | cwd: "/w", |
| 2535 | repository: "acme/web", |
| 2536 | number: 7, |
| 2537 | }); |
| 2538 | |
| 2539 | expect( |
| 2540 | list.candidates.map((candidate) => [ |
| 2541 | candidate.login, |
| 2542 | candidate.id, |
| 2543 | candidate.isRequested, |
| 2544 | ]), |
| 2545 | ).toEqual([ |
| 2546 | ["late", "42", true], |
| 2547 | [reviewer.username, "5", false], |
| 2548 | ]); |
| 2549 | }), |
| 2550 | ); |
| 2551 | |
| 2552 | it.effect("marks a reviewer GitLab sent without a username as requested on the users page", () => |
| 2553 | Effect.gen(function* () { |
| 2554 | mockedExecute |
| 2555 | .mockReturnValueOnce( |
| 2556 | Effect.succeed(output(mergeRequestJson({ reviewers: [{ id: 5, username: "" }] }))), |
| 2557 | ) |
| 2558 | .mockReturnValueOnce(Effect.succeed(output(JSON.stringify([author, reviewer])))); |
| 2559 | const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli; |
| 2560 | |
| 2561 | const list = yield* cli.listReviewerCandidates({ |
| 2562 | cwd: "/w", |
| 2563 | repository: "acme/web", |
| 2564 | number: 7, |
| 2565 | }); |
| 2566 | |
| 2567 | expect(list.candidates.map((candidate) => [candidate.id, candidate.isRequested])).toEqual([ |
| 2568 | ["5", true], |
| 2569 | ]); |
| 2570 | }), |
| 2571 | ); |
| 2572 | |
| 2573 | it.effect("asks GitLab to search the project's users when the menu has a query", () => |
| 2574 | Effect.gen(function* () { |
| 2575 | mockedExecute |
| 2576 | .mockReturnValueOnce( |
| 2577 | Effect.succeed( |
| 2578 | output(mergeRequestJson({ reviewers: [reviewer, { id: 42, username: "unrelated" }] })), |
| 2579 | ), |
| 2580 | ) |
| 2581 | .mockReturnValueOnce(Effect.succeed(output(JSON.stringify([reviewer])))); |
| 2582 | const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli; |
| 2583 | |
| 2584 | const list = yield* cli.listReviewerCandidates({ |
| 2585 | cwd: "/w", |
| 2586 | repository: "acme/web", |
| 2587 | number: 7, |
| 2588 | query: "octo cat", |
| 2589 | }); |
| 2590 | |
| 2591 | expect(list.candidates.map((candidate) => [candidate.login, candidate.isRequested])).toEqual([ |
| 2592 | [reviewer.username, true], |
| 2593 | ]); |
| 2594 | expect(argsOfCall(1)[1]).toBe("projects/acme%2Fweb/users?per_page=100&search=octo%20cat"); |
| 2595 | }), |
| 2596 | ); |
| 2597 | |
| 2598 | it.effect("writes the reviewer set back with the one being asked added to it", () => |
| 2599 | Effect.gen(function* () { |
| 2600 | mockedExecute |
| 2601 | .mockReturnValueOnce(Effect.succeed(output(mergeRequestJson({ reviewers: [reviewer] })))) |
| 2602 | .mockReturnValueOnce(Effect.succeed(output("{}"))); |
| 2603 | const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli; |
| 2604 | |
| 2605 | yield* cli.setReviewerRequest({ |
| 2606 | cwd: "/w", |
| 2607 | repository: "acme/web", |
| 2608 | number: 7, |
| 2609 | reviewers: [{ id: "9" }], |
| 2610 | requested: true, |
| 2611 | }); |
| 2612 | |
| 2613 | expect(argsOfCall(1)).toContain("PUT"); |
| 2614 | expect(JSON.parse(callAt(1).stdin ?? "")).toEqual({ reviewer_ids: [5, 9] }); |
| 2615 | }), |
| 2616 | ); |
| 2617 | |
| 2618 | it.effect("keeps a reviewer it cannot show when writing the set back", () => |
| 2619 | Effect.gen(function* () { |
| 2620 | mockedExecute |
| 2621 | .mockReturnValueOnce( |
| 2622 | Effect.succeed(output(mergeRequestJson({ reviewers: [{ id: 3, username: "" }] }))), |
| 2623 | ) |
| 2624 | .mockReturnValueOnce(Effect.succeed(output("{}"))); |
| 2625 | const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli; |
| 2626 | |
| 2627 | yield* cli.setReviewerRequest({ |
| 2628 | cwd: "/w", |
| 2629 | repository: "acme/web", |
| 2630 | number: 7, |
| 2631 | reviewers: [{ id: "9" }], |
| 2632 | requested: true, |
| 2633 | }); |
| 2634 | |
| 2635 | expect(JSON.parse(callAt(1).stdin ?? "")).toEqual({ reviewer_ids: [3, 9] }); |
| 2636 | }), |
| 2637 | ); |
| 2638 | |
| 2639 | it.effect("takes a reviewer out of the set rather than clearing it", () => |
| 2640 | Effect.gen(function* () { |
| 2641 | mockedExecute |
| 2642 | .mockReturnValueOnce( |
| 2643 | Effect.succeed( |
| 2644 | output(mergeRequestJson({ reviewers: [reviewer, { id: 9, username: "hubot" }] })), |
| 2645 | ), |
| 2646 | ) |
| 2647 | .mockReturnValueOnce(Effect.succeed(output("{}"))); |
| 2648 | const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli; |
| 2649 | |
| 2650 | yield* cli.setReviewerRequest({ |
| 2651 | cwd: "/w", |
| 2652 | repository: "acme/web", |
| 2653 | number: 7, |
| 2654 | reviewers: [{ id: "9" }], |
| 2655 | requested: false, |
| 2656 | }); |
| 2657 | |
| 2658 | expect(JSON.parse(callAt(1).stdin ?? "")).toEqual({ reviewer_ids: [5] }); |
| 2659 | }), |
| 2660 | ); |
| 2661 | |
| 2662 | it.effect("ignores an id GitLab could not have handed out, which names nobody", () => |
| 2663 | Effect.gen(function* () { |
| 2664 | mockedExecute |
| 2665 | .mockReturnValueOnce(Effect.succeed(output(mergeRequestJson({ reviewers: [reviewer] })))) |
| 2666 | .mockReturnValueOnce(Effect.succeed(output("{}"))); |
| 2667 | const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli; |
| 2668 | |
| 2669 | yield* cli.setReviewerRequest({ |
| 2670 | cwd: "/w", |
| 2671 | repository: "acme/web", |
| 2672 | number: 7, |
| 2673 | reviewers: [{ id: "octocat" }], |
| 2674 | requested: true, |
| 2675 | }); |
| 2676 | |
| 2677 | expect(JSON.parse(callAt(1).stdin ?? "")).toEqual({ reviewer_ids: [5] }); |
| 2678 | }), |
| 2679 | ); |
| 2680 | it.effect("reads blob ids for the marked paths at the merge request's head", () => |
| 2681 | Effect.gen(function* () { |
| 2682 | mockedExecute.mockReturnValueOnce( |
| 2683 | Effect.succeed( |
| 2684 | output( |
| 2685 | JSON.stringify({ |
| 2686 | iid: 7, |
| 2687 | title: "t", |
| 2688 | web_url: "https://gitlab.com/acme/web/-/merge_requests/7", |
| 2689 | source_branch: "feat", |
| 2690 | target_branch: "main", |
| 2691 | created_at: "2026-07-01T00:00:00Z", |
| 2692 | updated_at: "2026-07-01T00:00:00Z", |
| 2693 | diff_refs: { base_sha: "base", head_sha: "head", start_sha: "start" }, |
| 2694 | }), |
| 2695 | ), |
| 2696 | ), |
| 2697 | ); |
| 2698 | mockedExecute.mockReturnValueOnce( |
| 2699 | Effect.succeed( |
| 2700 | output( |
| 2701 | JSON.stringify({ |
| 2702 | data: { |
| 2703 | project: { repository: { blobs: { nodes: [{ path: "src/a.ts", oid: "aaa" }] } } }, |
| 2704 | }, |
| 2705 | }), |
| 2706 | ), |
| 2707 | ), |
| 2708 | ); |
| 2709 | const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli; |
| 2710 | |
| 2711 | const revisions = yield* cli.getFileRevisions({ |
| 2712 | cwd: "/w", |
| 2713 | repository: "acme/web", |
| 2714 | number: 7, |
| 2715 | paths: ["src/a.ts", "src/gone.ts"], |
| 2716 | }); |
| 2717 | |
| 2718 | expect([...revisions]).toEqual([ |
| 2719 | ["src/a.ts", "aaa"], |
| 2720 | ["src/gone.ts", ""], |
| 2721 | ]); |
| 2722 | const body: unknown = JSON.parse(callAt(1).stdin ?? "{}"); |
| 2723 | expect(body).toMatchObject({ |
| 2724 | variables: { fullPath: "acme/web", ref: "head", paths: ["src/a.ts", "src/gone.ts"] }, |
| 2725 | }); |
| 2726 | }), |
| 2727 | ); |
| 2728 | |
| 2729 | it.effect("leaves the paths out when GitLab did not answer the blobs query", () => |
| 2730 | Effect.gen(function* () { |
| 2731 | mockedExecute.mockReturnValueOnce( |
| 2732 | Effect.succeed( |
| 2733 | output( |
| 2734 | JSON.stringify({ |
| 2735 | iid: 7, |
| 2736 | title: "t", |
| 2737 | web_url: "https://gitlab.com/acme/web/-/merge_requests/7", |
| 2738 | source_branch: "feat", |
| 2739 | target_branch: "main", |
| 2740 | created_at: "2026-07-01T00:00:00Z", |
| 2741 | updated_at: "2026-07-01T00:00:00Z", |
| 2742 | diff_refs: { base_sha: "base", head_sha: "head", start_sha: "start" }, |
| 2743 | }), |
| 2744 | ), |
| 2745 | ), |
| 2746 | ); |
| 2747 | mockedExecute.mockReturnValueOnce( |
| 2748 | Effect.succeed(output(JSON.stringify({ data: { project: null } }))), |
| 2749 | ); |
| 2750 | const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli; |
| 2751 | |
| 2752 | const revisions = yield* cli.getFileRevisions({ |
| 2753 | cwd: "/w", |
| 2754 | repository: "acme/web", |
| 2755 | number: 7, |
| 2756 | paths: ["src/a.ts", "src/b.ts"], |
| 2757 | }); |
| 2758 | |
| 2759 | expect([...revisions]).toEqual([]); |
| 2760 | }), |
| 2761 | ); |
| 2762 | |
| 2763 | it.effect("splits the paths across requests, because GitLab charges the query by how many", () => |
| 2764 | Effect.gen(function* () { |
| 2765 | const paths = Array.from({ length: 150 }, (_, index) => `src/${index}.ts`); |
| 2766 | mockedExecute.mockReturnValueOnce( |
| 2767 | Effect.succeed( |
| 2768 | output( |
| 2769 | JSON.stringify({ |
| 2770 | iid: 7, |
| 2771 | title: "t", |
| 2772 | web_url: "https://gitlab.com/acme/web/-/merge_requests/7", |
| 2773 | source_branch: "feat", |
| 2774 | target_branch: "main", |
| 2775 | created_at: "2026-07-01T00:00:00Z", |
| 2776 | updated_at: "2026-07-01T00:00:00Z", |
| 2777 | diff_refs: { base_sha: "base", head_sha: "head", start_sha: "start" }, |
| 2778 | }), |
| 2779 | ), |
| 2780 | ), |
| 2781 | ); |
| 2782 | mockedExecute.mockImplementation((request) => { |
| 2783 | const body = JSON.parse(request.stdin ?? "{}") as { |
| 2784 | readonly variables: { readonly paths: ReadonlyArray<string> }; |
| 2785 | }; |
| 2786 | return Effect.succeed( |
| 2787 | output( |
| 2788 | JSON.stringify({ |
| 2789 | data: { |
| 2790 | project: { |
| 2791 | repository: { |
| 2792 | blobs: { |
| 2793 | nodes: body.variables.paths.map((path) => ({ path, oid: `oid-${path}` })), |
| 2794 | }, |
| 2795 | }, |
| 2796 | }, |
| 2797 | }, |
| 2798 | }), |
| 2799 | ), |
| 2800 | ); |
| 2801 | }); |
| 2802 | const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli; |
| 2803 | |
| 2804 | const revisions = yield* cli.getFileRevisions({ |
| 2805 | cwd: "/w", |
| 2806 | repository: "acme/web", |
| 2807 | number: 7, |
| 2808 | paths, |
| 2809 | }); |
| 2810 | |
| 2811 | assert.strictEqual(revisions.size, 150); |
| 2812 | assert.strictEqual(revisions.get("src/149.ts"), "oid-src/149.ts"); |
| 2813 | assert.strictEqual(mockedExecute.mock.calls.length, 3); |
| 2814 | }), |
| 2815 | ); |
| 2816 | |
| 2817 | it.effect( |
| 2818 | "reads line counts a hundred at a time per project, keeping the reads that answered", |
| 2819 | () => |
| 2820 | Effect.gen(function* () { |
| 2821 | const variablesOf = (stdin: string | undefined) => |
| 2822 | ( |
| 2823 | JSON.parse(stdin ?? "{}") as { |
| 2824 | readonly variables: { |
| 2825 | readonly fullPath: string; |
| 2826 | readonly iids: ReadonlyArray<string>; |
| 2827 | }; |
| 2828 | } |
| 2829 | ).variables; |
| 2830 | mockedExecute.mockImplementation((request) => { |
| 2831 | const { fullPath, iids } = variablesOf(request.stdin); |
| 2832 | // GitLab answers only what it has (iid 3 is missing), and one project's read fails. |
| 2833 | const nodes = iids |
| 2834 | .filter((iid) => iid !== "3") |
| 2835 | .map((iid) => ({ iid, diffStatsSummary: { additions: Number(iid), deletions: 1 } })); |
| 2836 | return Effect.succeed( |
| 2837 | output( |
| 2838 | JSON.stringify( |
| 2839 | fullPath === "acme/api" |
| 2840 | ? { errors: [{ message: "Query timed out" }] } |
| 2841 | : { data: { project: { mergeRequests: { nodes } } } }, |
| 2842 | ), |
| 2843 | ), |
| 2844 | ); |
| 2845 | }); |
| 2846 | const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli; |
| 2847 | |
| 2848 | const stats = yield* cli.listMergeRequestStats({ |
| 2849 | cwd: "/w", |
| 2850 | changeRequests: [ |
| 2851 | ...Array.from({ length: 150 }, (_, index) => ({ |
| 2852 | repository: "acme/web", |
| 2853 | number: index + 1, |
| 2854 | })), |
| 2855 | { repository: "acme/api", number: 9 }, |
| 2856 | ], |
| 2857 | }); |
| 2858 | |
| 2859 | expect(stats).toHaveLength(149); |
| 2860 | expect(stats).toContainEqual({ |
| 2861 | repository: "acme/web", |
| 2862 | number: 150, |
| 2863 | additions: 150, |
| 2864 | deletions: 1, |
| 2865 | }); |
| 2866 | expect(stats.some((stat) => stat.number === 3 || stat.repository === "acme/api")).toBe( |
| 2867 | false, |
| 2868 | ); |
| 2869 | expect( |
| 2870 | mockedExecute.mock.calls.map(([request]) => { |
| 2871 | const { fullPath, iids } = variablesOf(request.stdin); |
| 2872 | return [fullPath, iids.length]; |
| 2873 | }), |
| 2874 | ).toEqual([ |
| 2875 | ["acme/web", 100], |
| 2876 | ["acme/web", 50], |
| 2877 | ["acme/api", 1], |
| 2878 | ]); |
| 2879 | }), |
| 2880 | ); |
| 2881 | it.effect.each([ |
| 2882 | GitLabCli.GitLabCliUnavailableError, |
| 2883 | GitLabCli.GitLabCliAuthenticationError, |
| 2884 | GitLabCli.GitLabCliRateLimitError, |
| 2885 | ])("propagates unusable CLI and account failures from stats (%s)", (ErrorType) => |
| 2886 | Effect.gen(function* () { |
| 2887 | const error = new ErrorType({ |
| 2888 | operation: "execute", |
| 2889 | command: "glab", |
| 2890 | cwd: "/w", |
| 2891 | cause: null, |
| 2892 | }); |
| 2893 | mockedExecute.mockReturnValueOnce(Effect.fail(error)); |
| 2894 | const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli; |
| 2895 | const result = yield* Effect.result( |
| 2896 | cli.listMergeRequestStats({ |
| 2897 | cwd: "/w", |
| 2898 | changeRequests: [{ repository: "acme/web", number: 7 }], |
| 2899 | }), |
| 2900 | ); |
| 2901 | expect(result).toMatchObject({ _tag: "Failure", failure: error }); |
| 2902 | }), |
| 2903 | ); |
| 2904 | |
| 2905 | it.effect("keeps merge request references when the issue search fails", () => |
| 2906 | Effect.gen(function* () { |
| 2907 | mockedExecute.mockImplementation((input) => |
| 2908 | JSON.stringify(input).includes("/issues?") |
| 2909 | ? Effect.fail( |
| 2910 | new GitLabCli.GitLabCliCommandError({ |
| 2911 | operation: "execute", |
| 2912 | command: "glab", |
| 2913 | cwd: "/w", |
| 2914 | cause: null, |
| 2915 | }), |
| 2916 | ) |
| 2917 | : Effect.succeed(output(JSON.stringify([{ iid: 5, title: "Ship it", state: "opened" }]))), |
| 2918 | ); |
| 2919 | const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli; |
| 2920 | const page = yield* cli.searchProjectReferences({ cwd: "/w", repository: "acme/web" }); |
| 2921 | expect(page.items.map((item) => item.reference)).toEqual(["!5"]); |
| 2922 | }), |
| 2923 | ); |
| 2924 | |
| 2925 | describe("the head pipeline's stages", () => { |
| 2926 | const pipeline = { |
| 2927 | id: 9, |
| 2928 | project_id: 12, |
| 2929 | status: "failed", |
| 2930 | web_url: "https://gitlab.com/acme/web/-/pipelines/9", |
| 2931 | source: "merge_request_event", |
| 2932 | }; |
| 2933 | const stages = JSON.stringify({ |
| 2934 | data: { |
| 2935 | project: { |
| 2936 | mergeRequest: { |
| 2937 | headPipeline: { |
| 2938 | path: "/acme/web/-/pipelines/9", |
| 2939 | stages: { |
| 2940 | nodes: [ |
| 2941 | { name: "build", status: "success", detailedStatus: { label: "passed" } }, |
| 2942 | { |
| 2943 | name: "test", |
| 2944 | status: "failed", |
| 2945 | jobs: { |
| 2946 | nodes: [{ name: "unit", status: "FAILED", webPath: "/acme/web/-/jobs/3" }], |
| 2947 | }, |
| 2948 | }, |
| 2949 | { |
| 2950 | name: "deploy", |
| 2951 | status: "success", |
| 2952 | jobs: { |
| 2953 | nodes: [ |
| 2954 | // A trigger job without `strategy: depend` succeeds whatever its child does. |
| 2955 | { |
| 2956 | name: "child-pipeline", |
| 2957 | status: "SUCCESS", |
| 2958 | webPath: null, |
| 2959 | downstreamPipeline: { |
| 2960 | status: "FAILED", |
| 2961 | path: "/acme/child/-/pipelines/10", |
| 2962 | }, |
| 2963 | }, |
| 2964 | ], |
| 2965 | }, |
| 2966 | }, |
| 2967 | ], |
| 2968 | }, |
| 2969 | }, |
| 2970 | }, |
| 2971 | }, |
| 2972 | }, |
| 2973 | }); |
| 2974 | |
| 2975 | function answer(graphql: Effect.Effect<ReturnType<typeof output>, GitLabCli.GitLabCliError>) { |
| 2976 | mockedExecute.mockImplementation((request) => |
| 2977 | request.args[1] === "graphql" |
| 2978 | ? request.stdin?.includes("adminMergeRequest") |
| 2979 | ? Effect.succeed(labelPermission) |
| 2980 | : graphql |
| 2981 | : request.args[1]?.includes("/bridges") |
| 2982 | ? Effect.succeed( |
| 2983 | output( |
| 2984 | JSON.stringify([ |
| 2985 | { |
| 2986 | name: "child-pipeline", |
| 2987 | status: "success", |
| 2988 | downstream_pipeline: { |
| 2989 | status: "failed", |
| 2990 | web_url: "https://gitlab.com/acme/child/-/pipelines/10", |
| 2991 | }, |
| 2992 | }, |
| 2993 | ]), |
| 2994 | ), |
| 2995 | ) |
| 2996 | : Effect.succeed(output(mergeRequestJson({ head_pipeline: pipeline }))), |
| 2997 | ); |
| 2998 | } |
| 2999 | |
| 3000 | const read = (downstreamChecks: boolean) => |
| 3001 | GitLabPullRequestCli.GitLabPullRequestCli.pipe( |
| 3002 | Effect.flatMap((cli) => |
| 3003 | cli.getMergeRequestDetail({ |
| 3004 | cwd: "/w", |
| 3005 | repository: "group/acme/web", |
| 3006 | number: 7, |
| 3007 | downstreamChecks, |
| 3008 | }), |
| 3009 | ), |
| 3010 | ); |
| 3011 | |
| 3012 | it.effect("shows the stage jobs, and a stage GitLab listed no jobs for", () => |
| 3013 | Effect.gen(function* () { |
| 3014 | answer(Effect.succeed(output(stages))); |
| 3015 | |
| 3016 | const detail = yield* read(true); |
| 3017 | |
| 3018 | const graphql = mockedExecute.mock.calls.find(([call]) => call.stdin?.includes("stages")); |
| 3019 | assert.isDefined(graphql); |
| 3020 | expect(JSON.parse(graphql[0].stdin ?? "").variables).toEqual({ |
| 3021 | fullPath: "group/acme/web", |
| 3022 | iid: "7", |
| 3023 | }); |
| 3024 | expect(detail.checks).toEqual([ |
| 3025 | { |
| 3026 | name: "build", |
| 3027 | status: "success", |
| 3028 | description: "passed", |
| 3029 | url: "https://gitlab.com/acme/web/-/pipelines/9", |
| 3030 | }, |
| 3031 | { |
| 3032 | name: "test / unit", |
| 3033 | status: "failure", |
| 3034 | description: null, |
| 3035 | url: "https://gitlab.com/acme/web/-/jobs/3", |
| 3036 | }, |
| 3037 | { |
| 3038 | name: "deploy / child-pipeline", |
| 3039 | status: "failure", |
| 3040 | description: "Downstream pipeline", |
| 3041 | url: "https://gitlab.com/acme/child/-/pipelines/10", |
| 3042 | }, |
| 3043 | ]); |
| 3044 | }), |
| 3045 | ); |
| 3046 | |
| 3047 | it.effect("keeps the pipeline check where the stages cannot be read", () => |
| 3048 | Effect.gen(function* () { |
| 3049 | answer( |
| 3050 | Effect.fail( |
| 3051 | new GitLabCli.GitLabCliCommandError({ |
| 3052 | operation: "execute", |
| 3053 | command: "glab", |
| 3054 | cwd: "/w", |
| 3055 | cause: new Error("graphql unavailable"), |
| 3056 | }), |
| 3057 | ), |
| 3058 | ); |
| 3059 | |
| 3060 | const detail = yield* read(true); |
| 3061 | |
| 3062 | expect(detail.checks).toEqual([ |
| 3063 | { |
| 3064 | name: "Pipeline", |
| 3065 | status: "failure", |
| 3066 | description: "merge_request_event", |
| 3067 | url: "https://gitlab.com/acme/web/-/pipelines/9", |
| 3068 | }, |
| 3069 | { |
| 3070 | name: "child-pipeline", |
| 3071 | status: "failure", |
| 3072 | description: "Downstream pipeline", |
| 3073 | url: "https://gitlab.com/acme/child/-/pipelines/10", |
| 3074 | }, |
| 3075 | ]); |
| 3076 | }), |
| 3077 | ); |
| 3078 | |
| 3079 | it.effect("asks for no stages when the caller only needs the merge request", () => |
| 3080 | Effect.gen(function* () { |
| 3081 | answer(Effect.succeed(output(stages))); |
| 3082 | |
| 3083 | const detail = yield* read(false); |
| 3084 | |
| 3085 | expect( |
| 3086 | mockedExecute.mock.calls.filter(([call]) => call.stdin?.includes("stages")), |
| 3087 | ).toHaveLength(0); |
| 3088 | expect(detail.checks.map((check) => check.name)).toEqual(["Pipeline"]); |
| 3089 | }), |
| 3090 | ); |
| 3091 | }); |
| 3092 | }); |
| 3093 | |
| 3094 | it("asks for a numeric reference's iid besides the recent issues and merge requests", () => { |
| 3095 | const recent = [ |
| 3096 | ["order_by", "updated_at"], |
| 3097 | ["sort", "desc"], |
| 3098 | ]; |
| 3099 | expect(GitLabPullRequestCli.referenceSearches("12")).toEqual([ |
| 3100 | { kind: "issue", params: [["iids[]", "12"]] }, |
| 3101 | { kind: "change-request", params: [["iids[]", "12"]] }, |
| 3102 | { kind: "issue", params: recent }, |
| 3103 | { kind: "change-request", params: recent }, |
| 3104 | ]); |
| 3105 | expect(GitLabPullRequestCli.referenceSearches("crash")).toEqual([ |
| 3106 | { kind: "issue", params: [...recent, ["search", "crash"], ["in", "title"]] }, |
| 3107 | { kind: "change-request", params: [...recent, ["search", "crash"], ["in", "title"]] }, |
| 3108 | ]); |
| 3109 | expect(GitLabPullRequestCli.referenceSearches("cr")).toEqual([ |
| 3110 | { kind: "issue", params: recent }, |
| 3111 | { kind: "change-request", params: recent }, |
| 3112 | ]); |
| 3113 | expect(GitLabPullRequestCli.referenceSearches("12", "!")).toEqual([ |
| 3114 | { kind: "change-request", params: [["iids[]", "12"]] }, |
| 3115 | { kind: "change-request", params: recent }, |
| 3116 | ]); |
| 3117 | expect(GitLabPullRequestCli.referenceSearches(undefined, "#")).toEqual([ |
| 3118 | { kind: "issue", params: recent }, |
| 3119 | ]); |
| 3120 | }); |