@leitwerk-dev/gitlab 0.2.0 → 0.3.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 (43) hide show
  1. package/README.md +45 -5
  2. package/dist/capability.d.ts +5 -1
  3. package/dist/capability.d.ts.map +1 -1
  4. package/dist/chunk-H4JCQNDY.js +1162 -0
  5. package/dist/chunk-H4JCQNDY.js.map +1 -0
  6. package/dist/client.d.ts +230 -3
  7. package/dist/client.d.ts.map +1 -1
  8. package/dist/delivery-tools.d.ts +4 -0
  9. package/dist/delivery-tools.d.ts.map +1 -0
  10. package/dist/external.d.ts +80 -5
  11. package/dist/external.d.ts.map +1 -1
  12. package/dist/index.d.ts +13 -14
  13. package/dist/index.d.ts.map +1 -1
  14. package/dist/index.js +40 -20
  15. package/dist/index.js.map +1 -1
  16. package/dist/issue-watcher.d.ts +42 -0
  17. package/dist/issue-watcher.d.ts.map +1 -0
  18. package/dist/preflight.d.ts +10 -0
  19. package/dist/preflight.d.ts.map +1 -0
  20. package/dist/selection.d.ts +11 -1
  21. package/dist/selection.d.ts.map +1 -1
  22. package/dist/setup.d.ts +11 -0
  23. package/dist/setup.d.ts.map +1 -0
  24. package/dist/testing.d.ts +43 -4
  25. package/dist/testing.d.ts.map +1 -1
  26. package/dist/testing.js +139 -1
  27. package/dist/testing.js.map +1 -1
  28. package/dist/tools.d.ts +35 -4
  29. package/dist/tools.d.ts.map +1 -1
  30. package/package.json +7 -6
  31. package/src/capability.ts +5 -1
  32. package/src/client.ts +392 -20
  33. package/src/delivery-tools.ts +226 -0
  34. package/src/external.ts +200 -8
  35. package/src/index.ts +42 -26
  36. package/src/issue-watcher.ts +152 -0
  37. package/src/preflight.ts +70 -0
  38. package/src/selection.ts +19 -3
  39. package/src/setup.ts +22 -0
  40. package/src/testing.ts +161 -7
  41. package/src/tools.ts +106 -54
  42. package/dist/chunk-5GBBPAG5.js +0 -656
  43. package/dist/chunk-5GBBPAG5.js.map +0 -1
package/src/tools.ts CHANGED
@@ -1,9 +1,6 @@
1
1
  import { createHash } from "node:crypto";
2
- import {
3
- createWriteIdentity,
4
- type ExternalWriteLogRepoLike,
5
- ensureWrite,
6
- } from "@leitwerk-dev/external-writes";
2
+ import type { ExternalWrites } from "@leitwerk-dev/external-writes";
3
+
7
4
  import {
8
5
  type IntegrationToolExecutionContext,
9
6
  numberArg,
@@ -12,46 +9,88 @@ import {
12
9
  stringArg,
13
10
  } from "@leitwerk-dev/process-sdk";
14
11
  import type { GitLabIntegration } from "./capability.js";
15
- import { type GitLabClientLike, observeMergeRequest } from "./client.js";
16
- export function resolveGitLabBinding(
12
+ import { type GitLabClientLike, GitLabError, observeMergeRequest } from "./client.js";
13
+
14
+ async function existingRemote<T>(read: () => Promise<T>): Promise<T | null> {
15
+ try {
16
+ return await read();
17
+ } catch (error) {
18
+ if (error instanceof GitLabError && error.status === 404) return null;
19
+ throw error;
20
+ }
21
+ }
22
+ /** @public */
23
+ export function resolveGitLabRepositoryBinding(
17
24
  ctx: Pick<IntegrationToolExecutionContext, "project" | "process">,
18
- ): { profile: string; projectId: number; iid: number } {
25
+ /** @public */
26
+ /** @public */
27
+ ): {
28
+ /** @public */
29
+ profile: string;
30
+ /** @public */
31
+ projectId: number;
32
+ } {
19
33
  if (!ctx.project || ctx.project.instanceId !== ctx.process.id)
20
34
  throw new Error("An authorized GitLab process project is required");
21
35
  const binding = ctx.project.metadata?.gitlab as
22
- | { profile?: unknown; projectId?: unknown; iid?: unknown }
36
+ | { profile?: unknown; projectId?: unknown }
23
37
  | undefined;
24
38
  if (
25
39
  !binding ||
26
40
  typeof binding.profile !== "string" ||
41
+ !binding.profile.trim() ||
27
42
  typeof binding.projectId !== "number" ||
28
- typeof binding.iid !== "number"
43
+ !Number.isSafeInteger(binding.projectId) ||
44
+ binding.projectId <= 0
29
45
  )
30
46
  throw new Error("Invalid GitLab project binding");
31
- return { profile: binding.profile, projectId: binding.projectId, iid: binding.iid };
47
+ return { profile: binding.profile, projectId: binding.projectId };
32
48
  }
33
- async function findOrCreate<T>(find: () => Promise<T | undefined>, create: () => Promise<T>) {
34
- const existing = await find();
35
- if (existing) return existing;
36
- try {
37
- return await create();
38
- } catch (error) {
39
- const recovered = await find();
40
- if (!recovered) throw error;
41
- return recovered;
42
- }
49
+ /** @public */
50
+ export function resolveGitLabBinding(
51
+ ctx: Pick<IntegrationToolExecutionContext, "project" | "process">,
52
+ /** @public */
53
+ /** @public */
54
+ ): {
55
+ /** @public */
56
+ profile: string;
57
+ /** @public */
58
+ projectId: number;
59
+ /** @public */
60
+ iid: number;
61
+ } {
62
+ const repository = resolveGitLabRepositoryBinding(ctx);
63
+ const iid = (ctx.project?.metadata?.gitlab as { iid?: unknown })?.iid;
64
+ if (typeof iid !== "number" || !Number.isSafeInteger(iid) || iid <= 0)
65
+ throw new Error("Invalid GitLab project binding: a merge request is required");
66
+ return { ...repository, iid };
43
67
  }
68
+ /** @internal */
44
69
  export async function ensureGitLabComment(input: {
70
+ /** @internal */
45
71
  client: GitLabClientLike;
46
- writes: ExternalWriteLogRepoLike;
72
+ /** @internal */
73
+ writes: ExternalWrites;
74
+ /** @internal */
47
75
  instanceId: string;
76
+ /** @internal */
48
77
  projectId: number;
78
+ /** @internal */
49
79
  iid: number;
80
+ /** @internal */
50
81
  writeKey: string;
82
+ /** @internal */
51
83
  body: string;
84
+ /** @internal */
52
85
  discussionId?: string;
86
+ /** @internal */
53
87
  signal?: AbortSignal;
54
- }): Promise<{ marker: string }> {
88
+ /** @public */
89
+ /** @public */
90
+ }): Promise<{
91
+ /** @internal */
92
+ marker: string;
93
+ }> {
55
94
  const { client, writes, instanceId, projectId, iid, writeKey, signal } = input;
56
95
  const identity = [client.baseUrl, projectId, iid, instanceId, writeKey];
57
96
  if (input.discussionId) identity.push(input.discussionId);
@@ -63,52 +102,62 @@ export async function ensureGitLabComment(input: {
63
102
  : await client.listNotes(projectId, iid, signal);
64
103
  return notes.find((note) => note.body.includes(marker));
65
104
  };
66
- await ensureWrite(writes, instanceId, createWriteIdentity("gitlab.comment", digest), async () => {
67
- const note = await findOrCreate(find, () => {
68
- const body = `${input.body}\n\n${marker}`;
69
- return input.discussionId
70
- ? client.replyToDiscussion(projectId, iid, input.discussionId, body, signal)
71
- : client.addNote(projectId, iid, body, signal);
72
- });
73
- return { projectId, iid, noteId: note.id, marker };
74
- });
105
+ await writes.ensure(
106
+ { writeType: "gitlab.comment", dedupKey: digest },
107
+ {
108
+ reconcile: () => existingRemote(async () => (await find()) ?? null),
109
+ execute: () => {
110
+ const body = `${input.body}\n\n${marker}`;
111
+ return input.discussionId
112
+ ? client.replyToDiscussion(projectId, iid, input.discussionId, body, signal)
113
+ : client.addNote(projectId, iid, body, signal);
114
+ },
115
+ toMetadata: (note) => ({ projectId, iid, noteId: note.id, marker }),
116
+ },
117
+ );
75
118
  return { marker };
76
119
  }
120
+ /** @public */
77
121
  export async function ensureGitLabSeenReaction(input: {
122
+ /** @public */
78
123
  client: GitLabClientLike;
79
- writes: ExternalWriteLogRepoLike;
124
+ /** @public */
125
+ writes: ExternalWrites;
126
+ /** @public */
80
127
  instanceId: string;
128
+ /** @internal */
81
129
  projectId: number;
130
+ /** @internal */
82
131
  iid: number;
132
+ /** @public */
83
133
  noteId: number;
134
+ /** @public */
84
135
  signal?: AbortSignal;
85
136
  }): Promise<void> {
86
137
  const { client, writes, instanceId, projectId, iid, noteId, signal } = input;
87
138
  const digest = createHash("sha256")
88
139
  .update(JSON.stringify([client.baseUrl, projectId, iid, instanceId, "eyes", noteId]))
89
140
  .digest("hex");
90
- await ensureWrite(
91
- writes,
92
- instanceId,
93
- createWriteIdentity("gitlab.reaction", digest),
94
- async () => {
95
- const identity = await client.resolveGitIdentity(signal);
96
- const find = async () =>
97
- (await client.listNoteReactions(projectId, iid, noteId, signal)).find(
98
- (reaction) => reaction.name === "eyes" && reaction.user.username === identity.username,
99
- );
100
- const reaction = await findOrCreate(find, () =>
101
- client.addNoteReaction(projectId, iid, noteId, "eyes", signal),
102
- );
103
- return { projectId, iid, noteId, reactionId: reaction.id, name: "eyes" };
141
+ await writes.ensure(
142
+ { writeType: "gitlab.reaction", dedupKey: digest },
143
+ {
144
+ reconcile: () =>
145
+ existingRemote(async () => {
146
+ const identity = await client.resolveGitIdentity(signal);
147
+ return (
148
+ (await client.listNoteReactions(projectId, iid, noteId, signal)).find(
149
+ (reaction) =>
150
+ reaction.name === "eyes" && reaction.user.username === identity.username,
151
+ ) ?? null
152
+ );
153
+ }),
154
+ execute: () => client.addNoteReaction(projectId, iid, noteId, "eyes", signal),
155
+ toMetadata: (reaction) => ({ projectId, iid, noteId, reactionId: reaction.id, name: "eyes" }),
104
156
  },
105
157
  );
106
158
  }
107
- export function registerGitLabTools(
108
- api: ServerExtensionAPI,
109
- integration: GitLabIntegration,
110
- writes: ExternalWriteLogRepoLike,
111
- ) {
159
+ /** @internal */
160
+ export function registerGitLabTools(api: ServerExtensionAPI, integration: GitLabIntegration) {
112
161
  for (const [name, description, required] of [
113
162
  ["gitlab_observe_merge_request", "Read the current MR source revision and its latest CI", []],
114
163
  ["gitlab_get_changes", "Read the MR changes", []],
@@ -141,16 +190,19 @@ export function registerGitLabTools(
141
190
  [...required],
142
191
  ),
143
192
  async execute(ctx, args) {
193
+ if (name === "gitlab_get_identity")
194
+ return integration
195
+ .client(resolveGitLabRepositoryBinding(ctx).profile)
196
+ .resolveGitIdentity(ctx.signal);
144
197
  const b = resolveGitLabBinding(ctx);
145
198
  const client = integration.client(b.profile);
146
199
  if (name === "gitlab_observe_merge_request")
147
200
  return observeMergeRequest(client, b.projectId, b.iid, ctx.signal);
148
201
  if (name === "gitlab_get_changes") return client.getChanges(b.projectId, b.iid, ctx.signal);
149
- if (name === "gitlab_get_identity") return client.resolveGitIdentity(ctx.signal);
150
202
  if (name === "gitlab_comment" || name === "gitlab_reply")
151
203
  return ensureGitLabComment({
152
204
  client,
153
- writes,
205
+ writes: ctx.externalWrites,
154
206
  instanceId: ctx.process.id,
155
207
  projectId: b.projectId,
156
208
  iid: b.iid,