@leitwerk-dev/gitlab 0.3.1 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (56) hide show
  1. package/README.md +154 -1
  2. package/dist/capability.d.ts +3 -0
  3. package/dist/capability.d.ts.map +1 -1
  4. package/dist/chunk-YPCOVBQS.js +2343 -0
  5. package/dist/chunk-YPCOVBQS.js.map +1 -0
  6. package/dist/client.d.ts +99 -2
  7. package/dist/client.d.ts.map +1 -1
  8. package/dist/external.d.ts +23 -1
  9. package/dist/external.d.ts.map +1 -1
  10. package/dist/index.d.ts +8 -3
  11. package/dist/index.d.ts.map +1 -1
  12. package/dist/index.js +512 -10
  13. package/dist/index.js.map +1 -1
  14. package/dist/issue-watcher.d.ts +2 -0
  15. package/dist/issue-watcher.d.ts.map +1 -1
  16. package/dist/launch.d.ts +40 -0
  17. package/dist/launch.d.ts.map +1 -0
  18. package/dist/maintenance-labels.d.ts +30 -0
  19. package/dist/maintenance-labels.d.ts.map +1 -0
  20. package/dist/maintenance.d.ts +132 -0
  21. package/dist/maintenance.d.ts.map +1 -0
  22. package/dist/publication-maintenance.d.ts +7 -0
  23. package/dist/publication-maintenance.d.ts.map +1 -0
  24. package/dist/publication.d.ts +37 -0
  25. package/dist/publication.d.ts.map +1 -0
  26. package/dist/repository-catalog.d.ts +16 -0
  27. package/dist/repository-catalog.d.ts.map +1 -0
  28. package/dist/setup.d.ts +8 -2
  29. package/dist/setup.d.ts.map +1 -1
  30. package/dist/testing.d.ts +10 -1
  31. package/dist/testing.d.ts.map +1 -1
  32. package/dist/testing.js +125 -5
  33. package/dist/testing.js.map +1 -1
  34. package/dist/ticket-creation.d.ts +5 -0
  35. package/dist/ticket-creation.d.ts.map +1 -0
  36. package/dist/tools.d.ts +100 -3
  37. package/dist/tools.d.ts.map +1 -1
  38. package/package.json +7 -7
  39. package/src/capability.ts +3 -0
  40. package/src/client.ts +275 -6
  41. package/src/delivery-tools.ts +4 -4
  42. package/src/external.ts +90 -19
  43. package/src/index.ts +58 -10
  44. package/src/issue-watcher.ts +10 -1
  45. package/src/launch.ts +63 -0
  46. package/src/maintenance-labels.ts +129 -0
  47. package/src/maintenance.ts +759 -0
  48. package/src/publication-maintenance.ts +288 -0
  49. package/src/publication.ts +234 -0
  50. package/src/repository-catalog.ts +94 -0
  51. package/src/setup.ts +115 -6
  52. package/src/testing.ts +143 -4
  53. package/src/ticket-creation.ts +145 -0
  54. package/src/tools.ts +382 -23
  55. package/dist/chunk-TQIZKDAM.js +0 -1160
  56. package/dist/chunk-TQIZKDAM.js.map +0 -1
package/src/client.ts CHANGED
@@ -9,6 +9,8 @@ export interface GitLabProfile {
9
9
  /** @internal */
10
10
  token: string;
11
11
  /** @internal */
12
+ ignoredCommentUsers?: readonly string[];
13
+ /** @internal */
12
14
  gitIdentity?: {
13
15
  /** @internal */
14
16
  name: string;
@@ -24,6 +26,8 @@ export interface GitLabProject {
24
26
  path_with_namespace: string;
25
27
  /** @public */
26
28
  http_url_to_repo: string;
29
+ /** SSH clone URL supplied by GitLab. @public */
30
+ ssh_url_to_repo?: string;
27
31
  /** @public */
28
32
  web_url: string;
29
33
  /** @internal */
@@ -31,6 +35,10 @@ export interface GitLabProject {
31
35
  /** @public */
32
36
  archived?: boolean;
33
37
  /** @internal */
38
+ issues_enabled?: boolean;
39
+ /** @internal */
40
+ issues_access_level?: string;
41
+ /** @internal */
34
42
  permissions?: {
35
43
  /** @internal */
36
44
  project_access?: {
@@ -249,6 +257,25 @@ export interface GitLabDiscussion {
249
257
  };
250
258
  })[];
251
259
  }
260
+ /** @internal */
261
+ export interface GitLabDiscussionPosition {
262
+ /** @internal */
263
+ position_type: "text";
264
+ /** @internal */
265
+ base_sha: string;
266
+ /** @internal */
267
+ start_sha: string;
268
+ /** @internal */
269
+ head_sha: string;
270
+ /** @internal */
271
+ old_path: string;
272
+ /** @internal */
273
+ new_path: string;
274
+ /** @internal */
275
+ old_line?: number;
276
+ /** @internal */
277
+ new_line?: number;
278
+ }
252
279
  /** @public */
253
280
  export interface GitLabIdentity {
254
281
  /** @public */
@@ -324,6 +351,7 @@ export function parseGitLabProfiles(raw: unknown): Map<string, GitLabProfile> {
324
351
  profiles.set(name, {
325
352
  baseUrl,
326
353
  token,
354
+ ignoredCommentUsers: parseIgnoredCommentUsers(item.ignored_comment_users),
327
355
  ...(identity
328
356
  ? { gitIdentity: { name: String(identity.name), email: String(identity.email) } }
329
357
  : {}),
@@ -331,15 +359,32 @@ export function parseGitLabProfiles(raw: unknown): Map<string, GitLabProfile> {
331
359
  }
332
360
  return profiles;
333
361
  }
362
+
363
+ function parseIgnoredCommentUsers(raw: unknown): string[] {
364
+ if (raw === undefined) return [];
365
+ if (!Array.isArray(raw) || raw.some((user) => typeof user !== "string"))
366
+ throw new Error("GitLab ignored_comment_users must be a list of usernames");
367
+ const users = raw.map((user: string) => user.trim().replace(/^@/, "").toLowerCase());
368
+ if (users.some((user) => !/^[a-z0-9_.-]+$/.test(user)))
369
+ throw new Error("GitLab ignored_comment_users requires nonempty GitLab usernames");
370
+ return [...new Set(users)];
371
+ }
334
372
  const projectPath = (id: number | string) => `/projects/${encodeURIComponent(id)}`;
335
373
  const mrPath = (id: number, iid: number) => `${projectPath(id)}/merge_requests/${iid}`;
336
374
 
375
+ function nextGitLabPage(response: Response, page: number, count: number, perPage = 100) {
376
+ const next = response.headers.get("x-next-page");
377
+ if (next === "" || (next === null && count < perPage)) return null;
378
+ return next === null ? page + 1 : Number(next);
379
+ }
380
+
337
381
  /** GitLab v4 API. Errors deliberately omit response bodies, headers and tokens. */
338
382
  /** @public */
339
383
  export class GitLabClient {
340
384
  /** @public */
341
385
  readonly baseUrl: string;
342
386
  readonly #profile: GitLabProfile;
387
+ readonly #ignoredCommentUsers: ReadonlySet<string>;
343
388
  readonly #fetch: typeof fetch;
344
389
  readonly #sleep: (ms: number) => Promise<unknown>;
345
390
  /** @internal */
@@ -358,6 +403,7 @@ export class GitLabClient {
358
403
  if (!profile.token || /[\r\n\0]/.test(profile.token))
359
404
  throw new Error("GitLab requires a valid token");
360
405
  this.#profile = profile;
406
+ this.#ignoredCommentUsers = new Set(parseIgnoredCommentUsers(profile.ignoredCommentUsers));
361
407
  this.baseUrl = profile.baseUrl;
362
408
  this.#fetch = options.fetch ?? fetch;
363
409
  this.#sleep = options.sleep ?? ((ms) => new Promise((resolve) => setTimeout(resolve, ms)));
@@ -426,9 +472,8 @@ export class GitLabClient {
426
472
  const batch = await this.json<T[]>(response);
427
473
  if (!Array.isArray(batch)) throw new Error("Invalid GitLab list response");
428
474
  rows.push(...batch);
429
- const next = response.headers.get("x-next-page");
430
- if (next === "" || (next === null && batch.length < 100)) return rows;
431
- const nextPage = next === null ? page + 1 : Number(next);
475
+ const nextPage = nextGitLabPage(response, page, batch.length);
476
+ if (nextPage === null) return rows;
432
477
  if (!Number.isInteger(nextPage) || nextPage <= page)
433
478
  throw new Error("Invalid GitLab pagination");
434
479
  page = nextPage;
@@ -436,8 +481,24 @@ export class GitLabClient {
436
481
  throw new Error("GitLab pagination limit exceeded; discovery incomplete");
437
482
  }
438
483
  /** @public */
439
- listIssues(id: number, signal?: AbortSignal): Promise<GitLabIssue[]> {
440
- return this.pages(`${projectPath(id)}/issues?state=opened&scope=all`, signal);
484
+ listIssues(
485
+ id: number,
486
+ signal?: AbortSignal,
487
+ state: "opened" | "all" = "opened",
488
+ ): Promise<GitLabIssue[]> {
489
+ return this.pages(`${projectPath(id)}/issues?state=${state}&scope=all`, signal);
490
+ }
491
+ /** @internal */
492
+ createIssue(
493
+ id: number,
494
+ input: {
495
+ /** @internal */ title: string;
496
+ /** @internal */ description: string;
497
+ /** @internal */ labels: string;
498
+ },
499
+ signal?: AbortSignal,
500
+ ): Promise<GitLabIssue> {
501
+ return this.request(`${projectPath(id)}/issues`, signal, input);
441
502
  }
442
503
  /** @public */
443
504
  getIssue(id: number, iid: number, signal?: AbortSignal): Promise<GitLabIssue> {
@@ -546,6 +607,120 @@ export class GitLabClient {
546
607
  listProjects(signal?: AbortSignal): Promise<GitLabProject[]> {
547
608
  return this.pages("/projects?archived=false", signal);
548
609
  }
610
+ /** Read at most one page of matching repositories. @internal */
611
+ searchProjects(search: string, signal?: AbortSignal): Promise<GitLabProject[]> {
612
+ const query = new URLSearchParams({
613
+ archived: "false",
614
+ search,
615
+ search_namespaces: "true",
616
+ per_page: "100",
617
+ page: "1",
618
+ order_by: "path",
619
+ sort: "asc",
620
+ });
621
+ return this.request(`/projects?${query}`, signal);
622
+ }
623
+ /** @internal */
624
+ listRepositoryTree(
625
+ id: number,
626
+ ref: string,
627
+ path = "",
628
+ signal?: AbortSignal,
629
+ ): Promise<
630
+ {
631
+ /** @internal */
632
+ path: string;
633
+ /** @internal */
634
+ type: string;
635
+ /** @internal */
636
+ id: string;
637
+ }[]
638
+ > {
639
+ return this.pages(
640
+ `${projectPath(id)}/repository/tree?ref=${encodeURIComponent(ref)}&path=${encodeURIComponent(path)}`,
641
+ signal,
642
+ );
643
+ }
644
+ /** @internal */
645
+ getRepositoryFile(
646
+ id: number,
647
+ ref: string,
648
+ path: string,
649
+ signal?: AbortSignal,
650
+ ): Promise<{
651
+ /** @internal */
652
+ file_path: string;
653
+ /** @internal */
654
+ content: string;
655
+ /** @internal */
656
+ encoding: string;
657
+ /** @internal */
658
+ last_commit_id: string;
659
+ }> {
660
+ return this.request(
661
+ `${projectPath(id)}/repository/files/${encodeURIComponent(path)}?ref=${encodeURIComponent(ref)}`,
662
+ signal,
663
+ );
664
+ }
665
+ /** Project-scoped basic blob search at a full commit, one page per request. @internal */
666
+ async searchRepositoryCode(
667
+ id: number,
668
+ ref: string,
669
+ search: string,
670
+ page = 1,
671
+ perPage = 20,
672
+ signal?: AbortSignal,
673
+ ): Promise<{
674
+ /** @internal */ revision: string;
675
+ /** @internal */ results: {
676
+ /** @internal */ path: string;
677
+ /** @internal */ snippet: string;
678
+ /** @internal */ startLine: number;
679
+ }[];
680
+ /** @internal */ page: number;
681
+ /** @internal */ nextPage: number | null;
682
+ }> {
683
+ if (!/^[a-f0-9]{40,64}$/i.test(ref)) throw new Error("Code search requires a full commit ID");
684
+ if (!search.trim()) throw new Error("Code search requires a search term");
685
+ if (
686
+ !Number.isSafeInteger(page) ||
687
+ page < 1 ||
688
+ !Number.isSafeInteger(perPage) ||
689
+ perPage < 1 ||
690
+ perPage > 100
691
+ )
692
+ throw new Error("Invalid GitLab search pagination");
693
+ const query = new URLSearchParams({
694
+ scope: "blobs",
695
+ search_type: "basic",
696
+ ref,
697
+ search,
698
+ page: String(page),
699
+ per_page: String(perPage),
700
+ });
701
+ const response = await this.response(`${projectPath(id)}/search?${query}`, signal);
702
+ const rows = await this.json<{ path: string; data: string; startline: number }[]>(response);
703
+ if (
704
+ !Array.isArray(rows) ||
705
+ rows.some(
706
+ (row) =>
707
+ !row ||
708
+ typeof row.path !== "string" ||
709
+ typeof row.data !== "string" ||
710
+ !Number.isSafeInteger(row.startline),
711
+ )
712
+ )
713
+ throw new Error("Invalid GitLab code search response");
714
+ const nextPage = nextGitLabPage(response, page, rows.length, perPage);
715
+ if (nextPage !== null && (!Number.isSafeInteger(nextPage) || nextPage <= page))
716
+ throw new Error("Invalid GitLab pagination");
717
+ return {
718
+ revision: ref,
719
+ results: rows.map((row) => ({ path: row.path, snippet: row.data, startLine: row.startline })),
720
+ page,
721
+ nextPage,
722
+ };
723
+ }
549
724
  /** @internal */
550
725
  getGroup(id: number | string, signal?: AbortSignal): Promise<GitLabGroup> {
551
726
  return this.request(`/groups/${encodeURIComponent(id)}`, signal);
@@ -572,6 +747,26 @@ export class GitLabClient {
572
747
  getMergeRequest(id: number, iid: number, signal?: AbortSignal): Promise<GitLabMergeRequest> {
573
748
  return this.request(mrPath(id, iid), signal);
574
749
  }
750
+ /** Read every label transition, including changes between watcher polls. @public */
751
+ listMergeRequestLabelEvents(
752
+ id: number,
753
+ iid: number,
754
+ signal?: AbortSignal,
755
+ ): Promise<GitLabLabelEvent[]> {
756
+ return this.pages(`${mrPath(id, iid)}/resource_label_events`, signal);
757
+ }
758
+ /** Apply label deltas without replacing unrelated labels. @public */
759
+ updateMergeRequestLabels(
760
+ id: number,
761
+ iid: number,
762
+ patch: {
763
+ /** @public */ add_labels?: string;
764
+ /** @public */ remove_labels?: string;
765
+ },
766
+ signal?: AbortSignal,
767
+ ): Promise<GitLabMergeRequest> {
768
+ return this.request(mrPath(id, iid), signal, patch, "PUT");
769
+ }
575
770
  /** @public */
576
771
  getChanges(id: number, iid: number, signal?: AbortSignal): Promise<GitLabDiff[]> {
577
772
  return this.pages(`${mrPath(id, iid)}/diffs`, signal);
@@ -587,6 +782,12 @@ export class GitLabClient {
587
782
  getCommit(id: number, sha: string, signal?: AbortSignal): Promise<GitLabCommit> {
588
783
  return this.request(`${projectPath(id)}/repository/commits/${encodeURIComponent(sha)}`, signal);
589
784
  }
785
+ /** Find shared ancestry without cloning a repository. @public */
786
+ getMergeBase(id: number, refs: readonly string[], signal?: AbortSignal): Promise<GitLabCommit> {
787
+ const query = new URLSearchParams();
788
+ for (const ref of refs) query.append("refs[]", ref);
789
+ return this.request(`${projectPath(id)}/repository/merge_base?${query}`, signal);
790
+ }
590
791
  /** @internal */
591
792
  listMergeRequestPipelines(
592
793
  id: number,
@@ -635,6 +836,20 @@ export class GitLabClient {
635
836
  );
636
837
  }
637
838
  /** @internal */
839
+ listDiscussions(id: number, iid: number, signal?: AbortSignal): Promise<GitLabDiscussion[]> {
840
+ return this.pages(`${mrPath(id, iid)}/discussions`, signal);
841
+ }
842
+ /** @internal */
843
+ addDiscussion(
844
+ id: number,
845
+ iid: number,
846
+ body: string,
847
+ position: GitLabDiscussionPosition,
848
+ signal?: AbortSignal,
849
+ ): Promise<GitLabDiscussion> {
850
+ return this.request(`${mrPath(id, iid)}/discussions`, signal, { body, position });
851
+ }
852
+ /** @internal */
638
853
  replyToDiscussion(
639
854
  id: number,
640
855
  iid: number,
@@ -649,6 +864,21 @@ export class GitLabClient {
649
864
  );
650
865
  }
651
866
  /** @internal */
867
+ resolveDiscussion(
868
+ id: number,
869
+ iid: number,
870
+ discussionId: string,
871
+ resolved: boolean,
872
+ signal?: AbortSignal,
873
+ ): Promise<GitLabDiscussion> {
874
+ return this.request(
875
+ `${mrPath(id, iid)}/discussions/${encodeURIComponent(discussionId)}`,
876
+ signal,
877
+ { resolved },
878
+ "PUT",
879
+ );
880
+ }
881
+ /** @internal */
652
882
  listNoteReactions(
653
883
  id: number,
654
884
  iid: number,
@@ -684,6 +914,7 @@ export class GitLabClient {
684
914
  note.system ||
685
915
  note.resolved ||
686
916
  note.author.bot ||
917
+ this.#ignoredCommentUsers.has(note.author.username.toLowerCase()) ||
687
918
  /<!-- leitwerk:gitlab:[a-f0-9]{64} -->/.test(note.body) ||
688
919
  !note.body.trim() ||
689
920
  !Number.isFinite(Date.parse(note.created_at))
@@ -711,6 +942,16 @@ export class GitLabClient {
711
942
  addNote(id: number, iid: number, body: string, signal?: AbortSignal): Promise<GitLabNote> {
712
943
  return this.request(`${mrPath(id, iid)}/notes`, signal, { body });
713
944
  }
945
+ /** @internal */
946
+ updateNote(
947
+ id: number,
948
+ iid: number,
949
+ noteId: number,
950
+ body: string,
951
+ signal?: AbortSignal,
952
+ ): Promise<GitLabNote> {
953
+ return this.request(`${mrPath(id, iid)}/notes/${noteId}`, signal, { body }, "PUT");
954
+ }
714
955
  /** @public */
715
956
  async resolveGitIdentity(signal?: AbortSignal): Promise<GitLabIdentity> {
716
957
  const user = await this.request<{
@@ -766,9 +1007,24 @@ export class GitLabClient {
766
1007
  }
767
1008
  }
768
1009
  /** @public */
1010
+ export interface GitLabLabelEvent {
1011
+ /** Monotonically increasing GitLab resource-label event identity. @public */
1012
+ id: number;
1013
+ /** @public */
1014
+ action: "add" | "remove";
1015
+ /** @public */
1016
+ created_at: string;
1017
+ /** Deleted labels may have no name. @public */
1018
+ label: {
1019
+ /** @public */
1020
+ name: string;
1021
+ } | null;
1022
+ }
1023
+ /** @public */
769
1024
  export type GitLabClientLike = Pick<
770
1025
  GitLabClient,
771
1026
  | "listIssues"
1027
+ | "createIssue"
772
1028
  | "getIssue"
773
1029
  | "updateIssue"
774
1030
  | "listIssueNotes"
@@ -780,6 +1036,8 @@ export type GitLabClientLike = Pick<
780
1036
  | "preflightRepository"
781
1037
  | "addNote"
782
1038
  | "addNoteReaction"
1039
+ | "addDiscussion"
1040
+ | "updateNote"
783
1041
  | "baseUrl"
784
1042
  | "getBranch"
785
1043
  | "getChanges"
@@ -788,20 +1046,31 @@ export type GitLabClientLike = Pick<
788
1046
  | "getGroup"
789
1047
  | "getJobTrace"
790
1048
  | "getMergeRequest"
1049
+ | "listMergeRequestLabelEvents"
1050
+ | "updateMergeRequestLabels"
791
1051
  | "getPipeline"
792
1052
  | "getProject"
793
1053
  | "listBranchPipelines"
794
1054
  | "listFailedJobs"
795
1055
  | "listGroupProjects"
796
1056
  | "listMergeRequestFeedback"
1057
+ | "listDiscussions"
797
1058
  | "listMergeRequestPipelines"
798
1059
  | "listMergeRequests"
799
1060
  | "listNoteReactions"
800
1061
  | "listNotes"
801
1062
  | "listProjects"
1063
+ | "searchProjects"
802
1064
  | "replyToDiscussion"
1065
+ | "resolveDiscussion"
803
1066
  | "resolveGitIdentity"
804
- >;
1067
+ > &
1068
+ Partial<
1069
+ Pick<
1070
+ GitLabClient,
1071
+ "listRepositoryTree" | "getRepositoryFile" | "getMergeBase" | "searchRepositoryCode"
1072
+ >
1073
+ >;
805
1074
 
806
1075
  /** A pending current pipeline supersedes every older result. Synthetic merges must contain this source head. */
807
1076
  /** @public */
@@ -26,7 +26,7 @@ export function registerGitLabDeliveryTools(
26
26
  "body",
27
27
  ]),
28
28
  async execute(ctx, args) {
29
- const binding = resolveGitLabRepositoryBinding(ctx);
29
+ const binding = resolveGitLabRepositoryBinding(ctx, integration);
30
30
  const client = integration.client(binding.profile);
31
31
  const project = projects.getByInstanceAndKey(ctx.process.id, ctx.project?.key ?? "");
32
32
  if (!project?.workBranch || project.workBranch === project.baseBranch)
@@ -92,7 +92,7 @@ export function registerGitLabDeliveryTools(
92
92
  description: "Read human discussion feedback on the bound merge request",
93
93
  parameters: projectParameters(),
94
94
  async execute(ctx) {
95
- const b = resolveGitLabBinding(ctx);
95
+ const b = resolveGitLabBinding(ctx, integration);
96
96
  return integration.client(b.profile).listMergeRequestFeedback(b.projectId, b.iid, ctx.signal);
97
97
  },
98
98
  });
@@ -101,7 +101,7 @@ export function registerGitLabDeliveryTools(
101
101
  description: "Reconcile the bot's seen reaction on bound merge request feedback",
102
102
  parameters: projectParameters({ noteId: { type: "integer" } }, ["noteId"]),
103
103
  async execute(ctx, args) {
104
- const b = resolveGitLabBinding(ctx);
104
+ const b = resolveGitLabBinding(ctx, integration);
105
105
  const client = integration.client(b.profile);
106
106
  const noteId = numberArg(args, "noteId");
107
107
  if (
@@ -142,7 +142,7 @@ export function registerGitLabDeliveryTools(
142
142
  ["issueNumber"],
143
143
  ),
144
144
  async execute(ctx, args) {
145
- const b = resolveGitLabRepositoryBinding(ctx);
145
+ const b = resolveGitLabRepositoryBinding(ctx, integration);
146
146
  const client = integration.client(b.profile);
147
147
  const iid = numberArg(args, "issueNumber");
148
148
  const bound = (ctx.project?.metadata?.gitlab as { issueIid?: number })?.issueIid;