@leitwerk-dev/gitlab 0.3.1 → 0.4.1
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.
- package/README.md +174 -1
- package/dist/capability.d.ts +3 -0
- package/dist/capability.d.ts.map +1 -1
- package/dist/chunk-23NUGZ3Q.js +2429 -0
- package/dist/chunk-23NUGZ3Q.js.map +1 -0
- package/dist/client.d.ts +116 -2
- package/dist/client.d.ts.map +1 -1
- package/dist/external.d.ts +23 -1
- package/dist/external.d.ts.map +1 -1
- package/dist/index.d.ts +8 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +519 -10
- package/dist/index.js.map +1 -1
- package/dist/issue-watcher.d.ts +2 -0
- package/dist/issue-watcher.d.ts.map +1 -1
- package/dist/launch.d.ts +40 -0
- package/dist/launch.d.ts.map +1 -0
- package/dist/maintenance-labels.d.ts +30 -0
- package/dist/maintenance-labels.d.ts.map +1 -0
- package/dist/maintenance.d.ts +122 -0
- package/dist/maintenance.d.ts.map +1 -0
- package/dist/publication-maintenance.d.ts +7 -0
- package/dist/publication-maintenance.d.ts.map +1 -0
- package/dist/publication.d.ts +39 -0
- package/dist/publication.d.ts.map +1 -0
- package/dist/repository-catalog.d.ts +16 -0
- package/dist/repository-catalog.d.ts.map +1 -0
- package/dist/setup.d.ts +8 -2
- package/dist/setup.d.ts.map +1 -1
- package/dist/testing.d.ts +14 -1
- package/dist/testing.d.ts.map +1 -1
- package/dist/testing.js +158 -5
- package/dist/testing.js.map +1 -1
- package/dist/ticket-creation.d.ts +5 -0
- package/dist/ticket-creation.d.ts.map +1 -0
- package/dist/tools.d.ts +144 -3
- package/dist/tools.d.ts.map +1 -1
- package/package.json +7 -7
- package/src/capability.ts +3 -0
- package/src/client.ts +321 -7
- package/src/delivery-tools.ts +4 -4
- package/src/external.ts +90 -19
- package/src/index.ts +57 -10
- package/src/issue-watcher.ts +10 -1
- package/src/launch.ts +63 -0
- package/src/maintenance-labels.ts +129 -0
- package/src/maintenance.ts +721 -0
- package/src/publication-maintenance.ts +290 -0
- package/src/publication.ts +254 -0
- package/src/repository-catalog.ts +94 -0
- package/src/setup.ts +115 -6
- package/src/testing.ts +178 -4
- package/src/ticket-creation.ts +145 -0
- package/src/tools.ts +508 -23
- package/dist/chunk-TQIZKDAM.js +0 -1160
- package/dist/chunk-TQIZKDAM.js.map +0 -1
package/src/tools.ts
CHANGED
|
@@ -11,6 +11,9 @@ import {
|
|
|
11
11
|
import type { GitLabIntegration } from "./capability.js";
|
|
12
12
|
import { type GitLabClientLike, GitLabError, observeMergeRequest } from "./client.js";
|
|
13
13
|
|
|
14
|
+
const hashIdentity = (identity: readonly unknown[]) =>
|
|
15
|
+
createHash("sha256").update(JSON.stringify(identity)).digest("hex");
|
|
16
|
+
|
|
14
17
|
async function existingRemote<T>(read: () => Promise<T>): Promise<T | null> {
|
|
15
18
|
try {
|
|
16
19
|
return await read();
|
|
@@ -22,7 +25,7 @@ async function existingRemote<T>(read: () => Promise<T>): Promise<T | null> {
|
|
|
22
25
|
/** @public */
|
|
23
26
|
export function resolveGitLabRepositoryBinding(
|
|
24
27
|
ctx: Pick<IntegrationToolExecutionContext, "project" | "process">,
|
|
25
|
-
|
|
28
|
+
integration?: GitLabIntegration,
|
|
26
29
|
): {
|
|
27
30
|
/** @public */
|
|
28
31
|
profile: string;
|
|
@@ -32,7 +35,7 @@ export function resolveGitLabRepositoryBinding(
|
|
|
32
35
|
if (!ctx.project || ctx.project.instanceId !== ctx.process.id)
|
|
33
36
|
throw new Error("An authorized GitLab process project is required");
|
|
34
37
|
const binding = ctx.project.metadata?.gitlab as
|
|
35
|
-
| { profile?: unknown; projectId?: unknown }
|
|
38
|
+
| { profile?: unknown; projectId?: unknown; origin?: unknown }
|
|
36
39
|
| undefined;
|
|
37
40
|
if (
|
|
38
41
|
!binding ||
|
|
@@ -43,12 +46,18 @@ export function resolveGitLabRepositoryBinding(
|
|
|
43
46
|
binding.projectId <= 0
|
|
44
47
|
)
|
|
45
48
|
throw new Error("Invalid GitLab project binding");
|
|
49
|
+
if (
|
|
50
|
+
binding.origin !== undefined &&
|
|
51
|
+
integration &&
|
|
52
|
+
integration.client(binding.profile).baseUrl !== binding.origin
|
|
53
|
+
)
|
|
54
|
+
throw new Error("GitLab profile installation changed from the pinned repository");
|
|
46
55
|
return { profile: binding.profile, projectId: binding.projectId };
|
|
47
56
|
}
|
|
48
57
|
/** @public */
|
|
49
58
|
export function resolveGitLabBinding(
|
|
50
59
|
ctx: Pick<IntegrationToolExecutionContext, "project" | "process">,
|
|
51
|
-
|
|
60
|
+
integration?: GitLabIntegration,
|
|
52
61
|
): {
|
|
53
62
|
/** @public */
|
|
54
63
|
profile: string;
|
|
@@ -57,12 +66,37 @@ export function resolveGitLabBinding(
|
|
|
57
66
|
/** @public */
|
|
58
67
|
iid: number;
|
|
59
68
|
} {
|
|
60
|
-
const repository = resolveGitLabRepositoryBinding(ctx);
|
|
69
|
+
const repository = resolveGitLabRepositoryBinding(ctx, integration);
|
|
61
70
|
const iid = (ctx.project?.metadata?.gitlab as { iid?: unknown })?.iid;
|
|
62
71
|
if (typeof iid !== "number" || !Number.isSafeInteger(iid) || iid <= 0)
|
|
63
72
|
throw new Error("Invalid GitLab project binding: a merge request is required");
|
|
64
73
|
return { ...repository, iid };
|
|
65
74
|
}
|
|
75
|
+
/** Stable marker shared by retry-safe comment creation and later updates. @public */
|
|
76
|
+
export function gitLabCommentMarker(input: {
|
|
77
|
+
/** @public */
|
|
78
|
+
baseUrl: string;
|
|
79
|
+
/** @public */
|
|
80
|
+
projectId: number;
|
|
81
|
+
/** @public */
|
|
82
|
+
iid: number;
|
|
83
|
+
/** @public */
|
|
84
|
+
instanceId: string;
|
|
85
|
+
/** @public */
|
|
86
|
+
writeKey: string;
|
|
87
|
+
/** @public */
|
|
88
|
+
discussionId?: string;
|
|
89
|
+
}): string {
|
|
90
|
+
const identity: (string | number)[] = [
|
|
91
|
+
input.baseUrl,
|
|
92
|
+
input.projectId,
|
|
93
|
+
input.iid,
|
|
94
|
+
input.instanceId,
|
|
95
|
+
input.writeKey,
|
|
96
|
+
];
|
|
97
|
+
if (input.discussionId) identity.push(input.discussionId);
|
|
98
|
+
return `<!-- leitwerk:gitlab:${hashIdentity(identity)} -->`;
|
|
99
|
+
}
|
|
66
100
|
/** @internal */
|
|
67
101
|
export async function ensureGitLabComment(input: {
|
|
68
102
|
/** @internal */
|
|
@@ -83,16 +117,22 @@ export async function ensureGitLabComment(input: {
|
|
|
83
117
|
discussionId?: string;
|
|
84
118
|
/** @internal */
|
|
85
119
|
signal?: AbortSignal;
|
|
86
|
-
/** @
|
|
120
|
+
/** Revalidated inside the durable write immediately before mutation. @internal */
|
|
121
|
+
beforeWrite?: () => Promise<void>;
|
|
87
122
|
}): Promise<{
|
|
88
123
|
/** @internal */
|
|
89
124
|
marker: string;
|
|
90
125
|
}> {
|
|
91
126
|
const { client, writes, instanceId, projectId, iid, writeKey, signal } = input;
|
|
92
|
-
const
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
127
|
+
const marker = gitLabCommentMarker({
|
|
128
|
+
baseUrl: client.baseUrl,
|
|
129
|
+
projectId,
|
|
130
|
+
iid,
|
|
131
|
+
instanceId,
|
|
132
|
+
writeKey,
|
|
133
|
+
discussionId: input.discussionId,
|
|
134
|
+
});
|
|
135
|
+
const digest = marker.slice("<!-- leitwerk:gitlab:".length, -4);
|
|
96
136
|
const find = async () => {
|
|
97
137
|
const notes = input.discussionId
|
|
98
138
|
? (await client.getDiscussion(projectId, iid, input.discussionId, signal)).notes
|
|
@@ -103,7 +143,8 @@ export async function ensureGitLabComment(input: {
|
|
|
103
143
|
{ writeType: "gitlab.comment", dedupKey: digest },
|
|
104
144
|
{
|
|
105
145
|
reconcile: () => existingRemote(async () => (await find()) ?? null),
|
|
106
|
-
execute: () => {
|
|
146
|
+
execute: async () => {
|
|
147
|
+
await input.beforeWrite?.();
|
|
107
148
|
const body = `${input.body}\n\n${marker}`;
|
|
108
149
|
return input.discussionId
|
|
109
150
|
? client.replyToDiscussion(projectId, iid, input.discussionId, body, signal)
|
|
@@ -114,6 +155,290 @@ export async function ensureGitLabComment(input: {
|
|
|
114
155
|
);
|
|
115
156
|
return { marker };
|
|
116
157
|
}
|
|
158
|
+
/** Idempotently replace a retry-safe top-level comment while retaining its marker. @public */
|
|
159
|
+
export async function updateGitLabComment(input: {
|
|
160
|
+
/** @public */
|
|
161
|
+
client: GitLabClientLike;
|
|
162
|
+
/** @public */
|
|
163
|
+
instanceId: string;
|
|
164
|
+
/** @public */
|
|
165
|
+
projectId: number;
|
|
166
|
+
/** @public */
|
|
167
|
+
iid: number;
|
|
168
|
+
/** @public */
|
|
169
|
+
writeKey: string;
|
|
170
|
+
/** @public */
|
|
171
|
+
body: string;
|
|
172
|
+
/** @public */
|
|
173
|
+
signal?: AbortSignal;
|
|
174
|
+
/** @public */
|
|
175
|
+
beforeWrite?: () => Promise<void>;
|
|
176
|
+
}): Promise<{
|
|
177
|
+
/** @public */
|
|
178
|
+
marker: string;
|
|
179
|
+
}> {
|
|
180
|
+
const { client, instanceId, projectId, iid, writeKey, body, signal } = input;
|
|
181
|
+
const marker = gitLabCommentMarker({
|
|
182
|
+
baseUrl: client.baseUrl,
|
|
183
|
+
projectId,
|
|
184
|
+
iid,
|
|
185
|
+
instanceId,
|
|
186
|
+
writeKey,
|
|
187
|
+
});
|
|
188
|
+
const note = (await client.listNotes(projectId, iid, signal)).find((item) =>
|
|
189
|
+
item.body.includes(marker),
|
|
190
|
+
);
|
|
191
|
+
if (!note) throw new Error("GitLab comment to update was not found");
|
|
192
|
+
const next = `${body}\n\n${marker}`;
|
|
193
|
+
if (note.body !== next) {
|
|
194
|
+
await input.beforeWrite?.();
|
|
195
|
+
await client.updateNote(projectId, iid, note.id, next, signal);
|
|
196
|
+
}
|
|
197
|
+
return { marker };
|
|
198
|
+
}
|
|
199
|
+
/** @internal */
|
|
200
|
+
export async function ensureGitLabInlineComment(input: {
|
|
201
|
+
/** @internal */
|
|
202
|
+
client: GitLabClientLike;
|
|
203
|
+
/** @internal */
|
|
204
|
+
writes: ExternalWrites;
|
|
205
|
+
/** @internal */
|
|
206
|
+
instanceId: string;
|
|
207
|
+
/** @internal */
|
|
208
|
+
projectId: number;
|
|
209
|
+
/** @internal */
|
|
210
|
+
iid: number;
|
|
211
|
+
/** @internal */
|
|
212
|
+
writeKey: string;
|
|
213
|
+
/** @internal */
|
|
214
|
+
body: string;
|
|
215
|
+
/** @internal */
|
|
216
|
+
path: string;
|
|
217
|
+
/** @internal */
|
|
218
|
+
line: number;
|
|
219
|
+
/** @internal */
|
|
220
|
+
side: "new" | "old";
|
|
221
|
+
/** @internal */
|
|
222
|
+
baseSha: string;
|
|
223
|
+
/** @internal */
|
|
224
|
+
startSha: string;
|
|
225
|
+
/** @internal */
|
|
226
|
+
headSha: string;
|
|
227
|
+
/** @internal */
|
|
228
|
+
signal?: AbortSignal;
|
|
229
|
+
}): Promise<{ marker: string; discussionId: string }> {
|
|
230
|
+
const {
|
|
231
|
+
client,
|
|
232
|
+
writes,
|
|
233
|
+
instanceId,
|
|
234
|
+
projectId,
|
|
235
|
+
iid,
|
|
236
|
+
writeKey,
|
|
237
|
+
body,
|
|
238
|
+
path,
|
|
239
|
+
line,
|
|
240
|
+
side,
|
|
241
|
+
baseSha,
|
|
242
|
+
startSha,
|
|
243
|
+
headSha,
|
|
244
|
+
signal,
|
|
245
|
+
} = input;
|
|
246
|
+
const marker = gitLabCommentMarker({
|
|
247
|
+
baseUrl: client.baseUrl,
|
|
248
|
+
projectId,
|
|
249
|
+
iid,
|
|
250
|
+
instanceId,
|
|
251
|
+
writeKey,
|
|
252
|
+
});
|
|
253
|
+
const digest = marker.slice("<!-- leitwerk:gitlab:".length, -4);
|
|
254
|
+
const find = async () => {
|
|
255
|
+
const discussions = await client.listDiscussions(projectId, iid, signal);
|
|
256
|
+
return discussions.find((discussion) =>
|
|
257
|
+
discussion.notes.some((note) => note.body.includes(marker)),
|
|
258
|
+
);
|
|
259
|
+
};
|
|
260
|
+
const diff = (await client.getChanges(projectId, iid, signal)).find((candidate) =>
|
|
261
|
+
side === "new" ? candidate.new_path === path : candidate.old_path === path,
|
|
262
|
+
);
|
|
263
|
+
if (!diff) throw new Error("GitLab inline comment path is not in the merge request diff");
|
|
264
|
+
await writes.ensure(
|
|
265
|
+
{ writeType: "gitlab.discussion", dedupKey: digest },
|
|
266
|
+
{
|
|
267
|
+
reconcile: () => existingRemote(async () => (await find()) ?? null),
|
|
268
|
+
execute: () =>
|
|
269
|
+
client.addDiscussion(
|
|
270
|
+
projectId,
|
|
271
|
+
iid,
|
|
272
|
+
`${body}\n\n${marker}`,
|
|
273
|
+
{
|
|
274
|
+
position_type: "text",
|
|
275
|
+
base_sha: baseSha,
|
|
276
|
+
start_sha: startSha,
|
|
277
|
+
head_sha: headSha,
|
|
278
|
+
old_path: diff.old_path,
|
|
279
|
+
new_path: diff.new_path,
|
|
280
|
+
...(side === "new" ? { new_line: line } : { old_line: line }),
|
|
281
|
+
},
|
|
282
|
+
signal,
|
|
283
|
+
),
|
|
284
|
+
toMetadata: (discussion) => ({
|
|
285
|
+
projectId,
|
|
286
|
+
iid,
|
|
287
|
+
discussionId: discussion.id,
|
|
288
|
+
marker,
|
|
289
|
+
}),
|
|
290
|
+
},
|
|
291
|
+
);
|
|
292
|
+
const discussion = await find();
|
|
293
|
+
if (!discussion) throw new Error("GitLab inline discussion is unavailable after create");
|
|
294
|
+
return { marker, discussionId: discussion.id };
|
|
295
|
+
}
|
|
296
|
+
/** @public */
|
|
297
|
+
export async function ensureGitLabResolveDiscussion(input: {
|
|
298
|
+
/** @public */
|
|
299
|
+
client: GitLabClientLike;
|
|
300
|
+
/** @public */
|
|
301
|
+
writes: ExternalWrites;
|
|
302
|
+
/** @public */
|
|
303
|
+
instanceId: string;
|
|
304
|
+
/** @internal */
|
|
305
|
+
projectId: number;
|
|
306
|
+
/** @internal */
|
|
307
|
+
iid: number;
|
|
308
|
+
/** @public */
|
|
309
|
+
writeKey: string;
|
|
310
|
+
/** @public */
|
|
311
|
+
discussionId: string;
|
|
312
|
+
/** @public */
|
|
313
|
+
signal?: AbortSignal;
|
|
314
|
+
}): Promise<{ discussionId: string; resolved: boolean }> {
|
|
315
|
+
const { client, writes, instanceId, projectId, iid, writeKey, discussionId, signal } = input;
|
|
316
|
+
const digest = hashIdentity([
|
|
317
|
+
client.baseUrl,
|
|
318
|
+
projectId,
|
|
319
|
+
iid,
|
|
320
|
+
instanceId,
|
|
321
|
+
"resolve",
|
|
322
|
+
writeKey,
|
|
323
|
+
discussionId,
|
|
324
|
+
]);
|
|
325
|
+
await writes.ensure(
|
|
326
|
+
{ writeType: "gitlab.discussion.resolve", dedupKey: digest },
|
|
327
|
+
{
|
|
328
|
+
reconcile: () =>
|
|
329
|
+
existingRemote(async () => {
|
|
330
|
+
const discussion = await client.getDiscussion(projectId, iid, discussionId, signal);
|
|
331
|
+
return discussion.notes.some((note) => note.resolved) ? discussion : null;
|
|
332
|
+
}),
|
|
333
|
+
execute: () => client.resolveDiscussion(projectId, iid, discussionId, true, signal),
|
|
334
|
+
toMetadata: () => ({ projectId, iid, discussionId, resolved: true }),
|
|
335
|
+
},
|
|
336
|
+
);
|
|
337
|
+
return { discussionId, resolved: true };
|
|
338
|
+
}
|
|
339
|
+
/** @internal */
|
|
340
|
+
export async function ensureGitLabApproval(input: {
|
|
341
|
+
/** @internal */
|
|
342
|
+
client: GitLabClientLike;
|
|
343
|
+
/** @internal */
|
|
344
|
+
writes: ExternalWrites;
|
|
345
|
+
/** @internal */
|
|
346
|
+
instanceId: string;
|
|
347
|
+
/** @internal */
|
|
348
|
+
projectId: number;
|
|
349
|
+
/** @internal */
|
|
350
|
+
iid: number;
|
|
351
|
+
/** @internal */
|
|
352
|
+
writeKey: string;
|
|
353
|
+
/** @internal */
|
|
354
|
+
sha: string;
|
|
355
|
+
/** @internal */
|
|
356
|
+
signal?: AbortSignal;
|
|
357
|
+
}): Promise<{ sha: string; approved: boolean }> {
|
|
358
|
+
const { client, writes, instanceId, projectId, iid, writeKey, sha, signal } = input;
|
|
359
|
+
if (!/^[a-f0-9]{40}$/i.test(sha)) throw new Error("GitLab approval requires the full source SHA");
|
|
360
|
+
const current = await client.getMergeRequest(projectId, iid, signal);
|
|
361
|
+
if (current.sha !== sha) return { sha, approved: false };
|
|
362
|
+
const digest = hashIdentity([
|
|
363
|
+
client.baseUrl,
|
|
364
|
+
projectId,
|
|
365
|
+
iid,
|
|
366
|
+
instanceId,
|
|
367
|
+
"approve",
|
|
368
|
+
writeKey,
|
|
369
|
+
sha,
|
|
370
|
+
]);
|
|
371
|
+
await writes.ensure(
|
|
372
|
+
{ writeType: "gitlab.approval", dedupKey: digest },
|
|
373
|
+
{
|
|
374
|
+
reconcile: async () => {
|
|
375
|
+
const head = await client.getMergeRequest(projectId, iid, signal);
|
|
376
|
+
const approvals = await client.getMergeRequestApprovals(projectId, iid, signal);
|
|
377
|
+
return head.sha === sha && approvals.user_has_approved ? { sha } : null;
|
|
378
|
+
},
|
|
379
|
+
execute: () => client.approveMergeRequest(projectId, iid, sha, signal),
|
|
380
|
+
toMetadata: (approval) => ({ projectId, iid, sha: approval.sha }),
|
|
381
|
+
},
|
|
382
|
+
);
|
|
383
|
+
return { sha, approved: true };
|
|
384
|
+
}
|
|
385
|
+
/** @internal */
|
|
386
|
+
export async function ensureGitLabUnapproval(input: {
|
|
387
|
+
/** @internal */
|
|
388
|
+
client: GitLabClientLike;
|
|
389
|
+
/** @internal */
|
|
390
|
+
writes: ExternalWrites;
|
|
391
|
+
/** @internal */
|
|
392
|
+
instanceId: string;
|
|
393
|
+
/** @internal */
|
|
394
|
+
projectId: number;
|
|
395
|
+
/** @internal */
|
|
396
|
+
iid: number;
|
|
397
|
+
/** @internal */
|
|
398
|
+
writeKey: string;
|
|
399
|
+
/** @internal */
|
|
400
|
+
sha: string;
|
|
401
|
+
/** @internal */
|
|
402
|
+
signal?: AbortSignal;
|
|
403
|
+
}): Promise<{ sha: string; unapproved: boolean }> {
|
|
404
|
+
const { client, writes, instanceId, projectId, iid, writeKey, sha, signal } = input;
|
|
405
|
+
if (!/^[a-f0-9]{40}$/i.test(sha))
|
|
406
|
+
throw new Error("GitLab unapprove requires the full source SHA");
|
|
407
|
+
const current = await client.getMergeRequest(projectId, iid, signal);
|
|
408
|
+
if (current.sha !== sha) return { sha, unapproved: false };
|
|
409
|
+
const digest = hashIdentity([
|
|
410
|
+
client.baseUrl,
|
|
411
|
+
projectId,
|
|
412
|
+
iid,
|
|
413
|
+
instanceId,
|
|
414
|
+
"unapprove",
|
|
415
|
+
writeKey,
|
|
416
|
+
sha,
|
|
417
|
+
]);
|
|
418
|
+
try {
|
|
419
|
+
await writes.ensure(
|
|
420
|
+
{ writeType: "gitlab.unapproval", dedupKey: digest },
|
|
421
|
+
{
|
|
422
|
+
reconcile: async () => {
|
|
423
|
+
const head = await client.getMergeRequest(projectId, iid, signal);
|
|
424
|
+
const approvals = await client.getMergeRequestApprovals(projectId, iid, signal);
|
|
425
|
+
return head.sha === sha && !approvals.user_has_approved ? { sha } : null;
|
|
426
|
+
},
|
|
427
|
+
execute: async () => {
|
|
428
|
+
const head = await client.getMergeRequest(projectId, iid, signal);
|
|
429
|
+
if (head.sha !== sha) throw new Error("GitLab source SHA is no longer HEAD");
|
|
430
|
+
return client.unapproveMergeRequest(projectId, iid, signal);
|
|
431
|
+
},
|
|
432
|
+
toMetadata: (removed) => ({ projectId, iid, sha: removed.sha }),
|
|
433
|
+
},
|
|
434
|
+
);
|
|
435
|
+
} catch (error) {
|
|
436
|
+
if ((await client.getMergeRequest(projectId, iid, signal)).sha !== sha)
|
|
437
|
+
return { sha, unapproved: false };
|
|
438
|
+
throw error;
|
|
439
|
+
}
|
|
440
|
+
return { sha, unapproved: true };
|
|
441
|
+
}
|
|
117
442
|
/** @public */
|
|
118
443
|
export async function ensureGitLabSeenReaction(input: {
|
|
119
444
|
/** @public */
|
|
@@ -130,11 +455,11 @@ export async function ensureGitLabSeenReaction(input: {
|
|
|
130
455
|
noteId: number;
|
|
131
456
|
/** @public */
|
|
132
457
|
signal?: AbortSignal;
|
|
458
|
+
/** @public */
|
|
459
|
+
beforeWrite?: () => Promise<void>;
|
|
133
460
|
}): Promise<void> {
|
|
134
461
|
const { client, writes, instanceId, projectId, iid, noteId, signal } = input;
|
|
135
|
-
const digest =
|
|
136
|
-
.update(JSON.stringify([client.baseUrl, projectId, iid, instanceId, "eyes", noteId]))
|
|
137
|
-
.digest("hex");
|
|
462
|
+
const digest = hashIdentity([client.baseUrl, projectId, iid, instanceId, "eyes", noteId]);
|
|
138
463
|
await writes.ensure(
|
|
139
464
|
{ writeType: "gitlab.reaction", dedupKey: digest },
|
|
140
465
|
{
|
|
@@ -148,13 +473,101 @@ export async function ensureGitLabSeenReaction(input: {
|
|
|
148
473
|
) ?? null
|
|
149
474
|
);
|
|
150
475
|
}),
|
|
151
|
-
execute: () =>
|
|
476
|
+
execute: async () => {
|
|
477
|
+
await input.beforeWrite?.();
|
|
478
|
+
return client.addNoteReaction(projectId, iid, noteId, "eyes", signal);
|
|
479
|
+
},
|
|
152
480
|
toMetadata: (reaction) => ({ projectId, iid, noteId, reactionId: reaction.id, name: "eyes" }),
|
|
153
481
|
},
|
|
154
482
|
);
|
|
155
483
|
}
|
|
156
484
|
/** @internal */
|
|
157
|
-
export function registerGitLabTools(
|
|
485
|
+
export function registerGitLabTools(
|
|
486
|
+
api: ServerExtensionAPI,
|
|
487
|
+
integration: GitLabIntegration,
|
|
488
|
+
beforeWrite?: (ctx: IntegrationToolExecutionContext) => Promise<void>,
|
|
489
|
+
) {
|
|
490
|
+
for (const name of [
|
|
491
|
+
"gitlab_inspect_project",
|
|
492
|
+
"gitlab_repository_tree",
|
|
493
|
+
"gitlab_repository_file",
|
|
494
|
+
"gitlab_code_search",
|
|
495
|
+
] as const)
|
|
496
|
+
api.tool<Record<string, unknown>>({
|
|
497
|
+
name,
|
|
498
|
+
description:
|
|
499
|
+
name === "gitlab_code_search"
|
|
500
|
+
? "Search blobs in a bound GitLab repository at a full commit. Follow nextPage until null, then read matching files for complete evidence. Search failure is not an empty result."
|
|
501
|
+
: "Inspect a bound GitLab repository. Use the returned commit ID as ref for subsequent tree/file reads.",
|
|
502
|
+
parameters:
|
|
503
|
+
name === "gitlab_code_search"
|
|
504
|
+
? projectParameters(
|
|
505
|
+
{
|
|
506
|
+
ref: { type: "string" },
|
|
507
|
+
search: { type: "string" },
|
|
508
|
+
page: { type: "integer", minimum: 1 },
|
|
509
|
+
perPage: { type: "integer", minimum: 1, maximum: 100 },
|
|
510
|
+
},
|
|
511
|
+
["ref", "search"],
|
|
512
|
+
)
|
|
513
|
+
: projectParameters(
|
|
514
|
+
{ ref: { type: "string" }, path: { type: "string" } },
|
|
515
|
+
name === "gitlab_inspect_project"
|
|
516
|
+
? []
|
|
517
|
+
: name === "gitlab_repository_tree"
|
|
518
|
+
? ["ref"]
|
|
519
|
+
: ["ref", "path"],
|
|
520
|
+
),
|
|
521
|
+
async execute(ctx, args) {
|
|
522
|
+
const binding = resolveGitLabRepositoryBinding(ctx, integration);
|
|
523
|
+
const client = integration.client(binding.profile);
|
|
524
|
+
if (name === "gitlab_inspect_project")
|
|
525
|
+
return {
|
|
526
|
+
project: await client.getProject(binding.projectId, ctx.signal),
|
|
527
|
+
branch: await client.getBranch(binding.projectId, ctx.project!.baseBranch, ctx.signal),
|
|
528
|
+
};
|
|
529
|
+
const ref = stringArg(args, "ref");
|
|
530
|
+
if (!/^[a-f0-9]{40,64}$/i.test(ref))
|
|
531
|
+
throw new Error("Repository reads require a commit ID returned by inspection");
|
|
532
|
+
if (name === "gitlab_code_search") {
|
|
533
|
+
if (!client.searchRepositoryCode)
|
|
534
|
+
throw new Error("GitLab code search unavailable; no search was performed");
|
|
535
|
+
return client.searchRepositoryCode(
|
|
536
|
+
binding.projectId,
|
|
537
|
+
ref,
|
|
538
|
+
stringArg(args, "search"),
|
|
539
|
+
args.page === undefined ? 1 : numberArg(args, "page"),
|
|
540
|
+
args.perPage === undefined ? 20 : numberArg(args, "perPage"),
|
|
541
|
+
ctx.signal,
|
|
542
|
+
);
|
|
543
|
+
}
|
|
544
|
+
if (name === "gitlab_repository_tree") {
|
|
545
|
+
if (!client.listRepositoryTree) throw new Error("Repository tree inspection unavailable");
|
|
546
|
+
return client.listRepositoryTree(
|
|
547
|
+
binding.projectId,
|
|
548
|
+
ref,
|
|
549
|
+
typeof args.path === "string" ? args.path : "",
|
|
550
|
+
ctx.signal,
|
|
551
|
+
);
|
|
552
|
+
}
|
|
553
|
+
if (!client.getRepositoryFile) throw new Error("Repository file inspection unavailable");
|
|
554
|
+
const file = await client.getRepositoryFile(
|
|
555
|
+
binding.projectId,
|
|
556
|
+
ref,
|
|
557
|
+
stringArg(args, "path"),
|
|
558
|
+
ctx.signal,
|
|
559
|
+
);
|
|
560
|
+
return {
|
|
561
|
+
...file,
|
|
562
|
+
content:
|
|
563
|
+
file.encoding === "base64"
|
|
564
|
+
? Buffer.from(file.content, "base64").toString("utf8")
|
|
565
|
+
: file.content,
|
|
566
|
+
encoding: "utf8",
|
|
567
|
+
revision: ref,
|
|
568
|
+
};
|
|
569
|
+
},
|
|
570
|
+
});
|
|
158
571
|
for (const [name, description, required] of [
|
|
159
572
|
["gitlab_observe_merge_request", "Read the current MR source revision and its latest CI", []],
|
|
160
573
|
["gitlab_get_changes", "Read the MR changes", []],
|
|
@@ -166,11 +579,32 @@ export function registerGitLabTools(api: ServerExtensionAPI, integration: GitLab
|
|
|
166
579
|
["pipelineId", "jobId"],
|
|
167
580
|
],
|
|
168
581
|
["gitlab_comment", "Post a retry-safe MR comment", ["body", "writeKey"]],
|
|
582
|
+
["gitlab_update_comment", "Update a retry-safe MR comment", ["body", "writeKey"]],
|
|
583
|
+
[
|
|
584
|
+
"gitlab_inline_comment",
|
|
585
|
+
"Post a retry-safe MR inline discussion",
|
|
586
|
+
["body", "writeKey", "path", "line", "side", "baseSha", "startSha", "headSha"],
|
|
587
|
+
],
|
|
169
588
|
[
|
|
170
589
|
"gitlab_reply",
|
|
171
590
|
"Post a retry-safe reply in an MR discussion",
|
|
172
591
|
["body", "writeKey", "discussionId"],
|
|
173
592
|
],
|
|
593
|
+
[
|
|
594
|
+
"gitlab_resolve_discussion",
|
|
595
|
+
"Resolve a retry-safe MR inline discussion",
|
|
596
|
+
["writeKey", "discussionId"],
|
|
597
|
+
],
|
|
598
|
+
[
|
|
599
|
+
"gitlab_approve_merge_request",
|
|
600
|
+
"Approve the bound MR at an exact source SHA",
|
|
601
|
+
["writeKey", "sha"],
|
|
602
|
+
],
|
|
603
|
+
[
|
|
604
|
+
"gitlab_unapprove_merge_request",
|
|
605
|
+
"Remove the bot approval while the reviewed source SHA is still HEAD",
|
|
606
|
+
["writeKey", "sha"],
|
|
607
|
+
],
|
|
174
608
|
] as const) {
|
|
175
609
|
api.tool<Record<string, unknown>>({
|
|
176
610
|
name,
|
|
@@ -183,30 +617,81 @@ export function registerGitLabTools(api: ServerExtensionAPI, integration: GitLab
|
|
|
183
617
|
body: { type: "string" },
|
|
184
618
|
writeKey: { type: "string" },
|
|
185
619
|
discussionId: { type: "string" },
|
|
620
|
+
path: { type: "string" },
|
|
621
|
+
line: { type: "integer" },
|
|
622
|
+
side: { type: "string" },
|
|
623
|
+
baseSha: { type: "string" },
|
|
624
|
+
startSha: { type: "string" },
|
|
625
|
+
headSha: { type: "string" },
|
|
626
|
+
sha: { type: "string" },
|
|
186
627
|
},
|
|
187
628
|
[...required],
|
|
188
629
|
),
|
|
189
630
|
async execute(ctx, args) {
|
|
190
631
|
if (name === "gitlab_get_identity")
|
|
191
632
|
return integration
|
|
192
|
-
.client(resolveGitLabRepositoryBinding(ctx).profile)
|
|
633
|
+
.client(resolveGitLabRepositoryBinding(ctx, integration).profile)
|
|
193
634
|
.resolveGitIdentity(ctx.signal);
|
|
194
|
-
const b = resolveGitLabBinding(ctx);
|
|
635
|
+
const b = resolveGitLabBinding(ctx, integration);
|
|
195
636
|
const client = integration.client(b.profile);
|
|
196
637
|
if (name === "gitlab_observe_merge_request")
|
|
197
638
|
return observeMergeRequest(client, b.projectId, b.iid, ctx.signal);
|
|
198
639
|
if (name === "gitlab_get_changes") return client.getChanges(b.projectId, b.iid, ctx.signal);
|
|
640
|
+
const writeContext = {
|
|
641
|
+
client,
|
|
642
|
+
writes: ctx.externalWrites,
|
|
643
|
+
instanceId: ctx.process.id,
|
|
644
|
+
projectId: b.projectId,
|
|
645
|
+
iid: b.iid,
|
|
646
|
+
signal: ctx.signal,
|
|
647
|
+
};
|
|
648
|
+
if (name === "gitlab_update_comment")
|
|
649
|
+
return updateGitLabComment({
|
|
650
|
+
...writeContext,
|
|
651
|
+
beforeWrite: beforeWrite ? () => beforeWrite(ctx) : undefined,
|
|
652
|
+
writeKey: stringArg(args, "writeKey"),
|
|
653
|
+
body: stringArg(args, "body"),
|
|
654
|
+
});
|
|
655
|
+
if (name === "gitlab_inline_comment") {
|
|
656
|
+
const side = stringArg(args, "side");
|
|
657
|
+
if (side !== "new" && side !== "old")
|
|
658
|
+
throw new Error("GitLab inline comment side must be new or old");
|
|
659
|
+
return ensureGitLabInlineComment({
|
|
660
|
+
...writeContext,
|
|
661
|
+
writeKey: stringArg(args, "writeKey"),
|
|
662
|
+
body: stringArg(args, "body"),
|
|
663
|
+
path: stringArg(args, "path"),
|
|
664
|
+
line: numberArg(args, "line"),
|
|
665
|
+
side,
|
|
666
|
+
baseSha: stringArg(args, "baseSha"),
|
|
667
|
+
startSha: stringArg(args, "startSha"),
|
|
668
|
+
headSha: stringArg(args, "headSha"),
|
|
669
|
+
});
|
|
670
|
+
}
|
|
199
671
|
if (name === "gitlab_comment" || name === "gitlab_reply")
|
|
200
672
|
return ensureGitLabComment({
|
|
201
|
-
|
|
202
|
-
writes: ctx.externalWrites,
|
|
203
|
-
instanceId: ctx.process.id,
|
|
204
|
-
projectId: b.projectId,
|
|
205
|
-
iid: b.iid,
|
|
673
|
+
...writeContext,
|
|
206
674
|
writeKey: stringArg(args, "writeKey"),
|
|
207
675
|
body: stringArg(args, "body"),
|
|
208
676
|
...(name === "gitlab_reply" ? { discussionId: stringArg(args, "discussionId") } : {}),
|
|
209
|
-
|
|
677
|
+
});
|
|
678
|
+
if (name === "gitlab_resolve_discussion")
|
|
679
|
+
return ensureGitLabResolveDiscussion({
|
|
680
|
+
...writeContext,
|
|
681
|
+
writeKey: stringArg(args, "writeKey"),
|
|
682
|
+
discussionId: stringArg(args, "discussionId"),
|
|
683
|
+
});
|
|
684
|
+
if (name === "gitlab_approve_merge_request")
|
|
685
|
+
return ensureGitLabApproval({
|
|
686
|
+
...writeContext,
|
|
687
|
+
writeKey: stringArg(args, "writeKey"),
|
|
688
|
+
sha: stringArg(args, "sha"),
|
|
689
|
+
});
|
|
690
|
+
if (name === "gitlab_unapprove_merge_request")
|
|
691
|
+
return ensureGitLabUnapproval({
|
|
692
|
+
...writeContext,
|
|
693
|
+
writeKey: stringArg(args, "writeKey"),
|
|
694
|
+
sha: stringArg(args, "sha"),
|
|
210
695
|
});
|
|
211
696
|
const observation = await observeMergeRequest(client, b.projectId, b.iid, ctx.signal);
|
|
212
697
|
if (!observation.pipeline || observation.pipeline.id !== numberArg(args, "pipelineId"))
|