@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/setup.ts
CHANGED
|
@@ -1,7 +1,18 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
coreHostCapabilities,
|
|
3
|
+
type IntegrationToolExecutionContext,
|
|
4
|
+
repositorySettingsIdentity,
|
|
5
|
+
type ServerExtensionAPI,
|
|
6
|
+
scopedSettingsCapability,
|
|
7
|
+
type TicketCreationConfig,
|
|
8
|
+
} from "@leitwerk-dev/process-sdk";
|
|
9
|
+
import type { PollResult } from "@leitwerk-dev/watcher-utils";
|
|
2
10
|
import { type GitLabIntegration, gitlabIntegration } from "./capability.js";
|
|
3
11
|
import { registerGitLabDeliveryTools } from "./delivery-tools.js";
|
|
4
12
|
import { createGitLabProvider } from "./external.js";
|
|
13
|
+
import { createGitLabMaintenance } from "./maintenance.js";
|
|
14
|
+
import { createGitLabRepositoryCatalog } from "./repository-catalog.js";
|
|
15
|
+
import { registerGitLabTicketCreation } from "./ticket-creation.js";
|
|
5
16
|
import { registerGitLabTools } from "./tools.js";
|
|
6
17
|
|
|
7
18
|
/** Register shared tools and polling with an explicit integration. @public */
|
|
@@ -11,12 +22,110 @@ export function setupGitLabIntegration(
|
|
|
11
22
|
options: {
|
|
12
23
|
/** @public */
|
|
13
24
|
now?: () => number;
|
|
25
|
+
/** @internal */
|
|
26
|
+
ticketCreation?: TicketCreationConfig;
|
|
14
27
|
} = {},
|
|
15
|
-
)
|
|
16
|
-
|
|
28
|
+
):
|
|
29
|
+
| {
|
|
30
|
+
/** @public */
|
|
31
|
+
poll(): Promise<PollResult>;
|
|
32
|
+
/** @internal */
|
|
33
|
+
maintenance: ReturnType<typeof createGitLabMaintenance>;
|
|
34
|
+
}
|
|
35
|
+
| undefined {
|
|
36
|
+
const catalog = integration.repositoryCatalog ?? createGitLabRepositoryCatalog(integration);
|
|
37
|
+
api.provide(gitlabIntegration, {
|
|
38
|
+
profiles: () => integration.profiles(),
|
|
39
|
+
client: (profile) => integration.client(profile),
|
|
40
|
+
repositoryCatalog: catalog,
|
|
41
|
+
});
|
|
42
|
+
const settings = api.get(scopedSettingsCapability);
|
|
43
|
+
if (settings && !Array.isArray(settings))
|
|
44
|
+
settings.registerDiscovery("repository", async () => {
|
|
45
|
+
const subjects = [];
|
|
46
|
+
for (const profile of integration.profiles()) {
|
|
47
|
+
const client = integration.client(profile);
|
|
48
|
+
for (const project of await catalog.refresh(profile))
|
|
49
|
+
subjects.push({
|
|
50
|
+
scopeType: "repository",
|
|
51
|
+
identity: repositorySettingsIdentity(client.baseUrl, project.id),
|
|
52
|
+
label: project.path_with_namespace,
|
|
53
|
+
aliases: [
|
|
54
|
+
project.http_url_to_repo,
|
|
55
|
+
...(project.ssh_url_to_repo ? [project.ssh_url_to_repo] : []),
|
|
56
|
+
],
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
return subjects;
|
|
60
|
+
});
|
|
17
61
|
const deps = api.get(coreHostCapabilities.serverSetup);
|
|
18
62
|
if (!deps || Array.isArray(deps)) return;
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
63
|
+
const maintenance = createGitLabMaintenance(api, deps, integration, options.now);
|
|
64
|
+
const assertOwnership = async (ctx: IntegrationToolExecutionContext) => {
|
|
65
|
+
if (maintenance.ownsProcess(ctx.process.processId))
|
|
66
|
+
await maintenance.assertOwnership(ctx.process.id, ctx.project?.key ?? "repo", ctx.signal);
|
|
67
|
+
};
|
|
68
|
+
const guardedWrites = new Set([
|
|
69
|
+
"gitlab_ensure_merge_request",
|
|
70
|
+
"gitlab_comment",
|
|
71
|
+
"gitlab_reply",
|
|
72
|
+
"gitlab_inline_comment",
|
|
73
|
+
"gitlab_resolve_discussion",
|
|
74
|
+
"gitlab_acknowledge_feedback",
|
|
75
|
+
]);
|
|
76
|
+
const guardedApi: ServerExtensionAPI = {
|
|
77
|
+
...api,
|
|
78
|
+
tool(definition) {
|
|
79
|
+
api.tool({
|
|
80
|
+
...definition,
|
|
81
|
+
async execute(ctx, args) {
|
|
82
|
+
const guarded =
|
|
83
|
+
maintenance.ownsProcess(ctx.process.processId) && guardedWrites.has(definition.name);
|
|
84
|
+
if (guarded || definition.name === "gitlab_update_comment") await assertOwnership(ctx);
|
|
85
|
+
const result = await definition.execute(
|
|
86
|
+
guarded
|
|
87
|
+
? {
|
|
88
|
+
...ctx,
|
|
89
|
+
externalWrites: {
|
|
90
|
+
...ctx.externalWrites,
|
|
91
|
+
ensure: (identity, operation) =>
|
|
92
|
+
ctx.externalWrites.ensure(identity, {
|
|
93
|
+
...operation,
|
|
94
|
+
execute: async () => {
|
|
95
|
+
await assertOwnership(ctx);
|
|
96
|
+
return operation.execute();
|
|
97
|
+
},
|
|
98
|
+
}),
|
|
99
|
+
},
|
|
100
|
+
}
|
|
101
|
+
: ctx,
|
|
102
|
+
args,
|
|
103
|
+
);
|
|
104
|
+
if (definition.name === "gitlab_ensure_merge_request") await assertOwnership(ctx);
|
|
105
|
+
return result;
|
|
106
|
+
},
|
|
107
|
+
});
|
|
108
|
+
},
|
|
109
|
+
};
|
|
110
|
+
registerGitLabTools(guardedApi, integration, assertOwnership);
|
|
111
|
+
registerGitLabTicketCreation(
|
|
112
|
+
api,
|
|
113
|
+
integration,
|
|
114
|
+
options.ticketCreation ?? { enabled: false, defaultLabels: [] },
|
|
115
|
+
);
|
|
116
|
+
registerGitLabDeliveryTools(guardedApi, integration, deps.projects);
|
|
117
|
+
api.tool({
|
|
118
|
+
name: "gitlab_assert_maintenance",
|
|
119
|
+
description: "Recheck MR maintenance ownership before Git publication",
|
|
120
|
+
parameters: {
|
|
121
|
+
type: "object",
|
|
122
|
+
properties: { projectKey: { type: "string" } },
|
|
123
|
+
required: ["projectKey"],
|
|
124
|
+
},
|
|
125
|
+
async execute(ctx) {
|
|
126
|
+
await assertOwnership(ctx);
|
|
127
|
+
return { ok: true };
|
|
128
|
+
},
|
|
129
|
+
});
|
|
130
|
+
return { ...createGitLabProvider(deps, integration, options), /** @internal */ maintenance };
|
|
22
131
|
}
|
package/src/testing.ts
CHANGED
|
@@ -6,14 +6,17 @@ import type {
|
|
|
6
6
|
GitLabDiff,
|
|
7
7
|
GitLabFeedback,
|
|
8
8
|
GitLabIssue,
|
|
9
|
+
GitLabLabelEvent,
|
|
9
10
|
GitLabMergeRequest,
|
|
10
11
|
GitLabNote,
|
|
11
12
|
GitLabNoteReaction,
|
|
12
13
|
GitLabPipeline,
|
|
13
14
|
GitLabProject,
|
|
14
15
|
} from "./client.js";
|
|
16
|
+
import { GitLabError } from "./client.js";
|
|
15
17
|
|
|
16
18
|
export { GITLAB_MR_KIND } from "./external.js";
|
|
19
|
+
export { createGitLabMaintenance } from "./maintenance.js";
|
|
17
20
|
export { setupGitLabIntegration } from "./setup.js";
|
|
18
21
|
|
|
19
22
|
/** @public */
|
|
@@ -25,6 +28,8 @@ interface LocalState {
|
|
|
25
28
|
/** @public */
|
|
26
29
|
labels?: Record<string, string[]>;
|
|
27
30
|
/** @internal */
|
|
31
|
+
labelEvents?: Record<string, GitLabLabelEvent[]>;
|
|
32
|
+
/** @internal */
|
|
28
33
|
projects: GitLabProject[];
|
|
29
34
|
/** @internal */
|
|
30
35
|
mrs: GitLabMergeRequest[];
|
|
@@ -38,8 +43,12 @@ interface LocalState {
|
|
|
38
43
|
feedback?: Record<string, GitLabFeedback[]>;
|
|
39
44
|
/** @public */
|
|
40
45
|
discussionNotes?: Record<string, GitLabNote[]>;
|
|
46
|
+
/** @internal */
|
|
47
|
+
discussionResolved?: Record<string, boolean>;
|
|
41
48
|
/** @public */
|
|
42
49
|
reactions?: Record<string, GitLabNoteReaction[]>;
|
|
50
|
+
/** SHA the bot approved. Stays set across later commits until unapprove. @public */
|
|
51
|
+
approvedSha?: Record<string, string>;
|
|
43
52
|
}
|
|
44
53
|
/** Persistent GitLab test boundary. Repository URLs use local Git's file transport. */
|
|
45
54
|
/** @public */
|
|
@@ -50,11 +59,17 @@ export class LocalGitLabAdapter {
|
|
|
50
59
|
loseNextCommentResponse = false;
|
|
51
60
|
/** @public */
|
|
52
61
|
loseNextMergeRequestResponse = false;
|
|
62
|
+
/** @internal */
|
|
63
|
+
loseNextIssueResponse = false;
|
|
53
64
|
/** @public */
|
|
54
65
|
loseNextReplyResponse = false;
|
|
55
66
|
/** @public */
|
|
56
67
|
loseNextReactionResponse = false;
|
|
57
68
|
/** @public */
|
|
69
|
+
loseNextLabelResponse = false;
|
|
70
|
+
/** @public */
|
|
71
|
+
loseNextApprovalResponse = false;
|
|
72
|
+
/** @public */
|
|
58
73
|
constructor(
|
|
59
74
|
/** @internal */
|
|
60
75
|
readonly root: string,
|
|
@@ -104,6 +119,7 @@ export class LocalGitLabAdapter {
|
|
|
104
119
|
id: this.state.projects.length + 1,
|
|
105
120
|
path_with_namespace: name,
|
|
106
121
|
http_url_to_repo: bare,
|
|
122
|
+
ssh_url_to_repo: bare,
|
|
107
123
|
web_url: `${this.baseUrl}/${name}`,
|
|
108
124
|
default_branch: "main",
|
|
109
125
|
};
|
|
@@ -211,7 +227,7 @@ export class LocalGitLabAdapter {
|
|
|
211
227
|
client(): GitLabClientLike {
|
|
212
228
|
const mr = (id: number, iid: number) => {
|
|
213
229
|
const m = this.state.mrs.find((m) => m.project_id === id && m.iid === iid);
|
|
214
|
-
if (!m) throw new
|
|
230
|
+
if (!m) throw new GitLabError(404, false);
|
|
215
231
|
return m;
|
|
216
232
|
};
|
|
217
233
|
const commit = (id: number, ref: string) => ({
|
|
@@ -221,10 +237,26 @@ export class LocalGitLabAdapter {
|
|
|
221
237
|
});
|
|
222
238
|
return {
|
|
223
239
|
baseUrl: this.baseUrl,
|
|
224
|
-
listIssues: async (id) =>
|
|
240
|
+
listIssues: async (id, _signal, state = "opened") =>
|
|
225
241
|
structuredClone(
|
|
226
|
-
(this.state.issues ?? []).filter(
|
|
242
|
+
(this.state.issues ?? []).filter(
|
|
243
|
+
(i) => i.project_id === id && (state === "all" || i.state === "opened"),
|
|
244
|
+
),
|
|
227
245
|
),
|
|
246
|
+
createIssue: async (id, input) => {
|
|
247
|
+
const issue = this.createIssue(
|
|
248
|
+
id,
|
|
249
|
+
input.title,
|
|
250
|
+
input.labels ? input.labels.split(",") : [],
|
|
251
|
+
);
|
|
252
|
+
issue.description = input.description;
|
|
253
|
+
this.save();
|
|
254
|
+
if (this.loseNextIssueResponse) {
|
|
255
|
+
this.loseNextIssueResponse = false;
|
|
256
|
+
throw new Error("Response lost after issue write");
|
|
257
|
+
}
|
|
258
|
+
return structuredClone(issue);
|
|
259
|
+
},
|
|
228
260
|
getIssue: async (id, iid) => {
|
|
229
261
|
const issue = (this.state.issues ?? []).find((i) => i.project_id === id && i.iid === iid);
|
|
230
262
|
if (!issue) throw new Error("Unknown local GitLab issue");
|
|
@@ -288,7 +320,32 @@ export class LocalGitLabAdapter {
|
|
|
288
320
|
},
|
|
289
321
|
|
|
290
322
|
getProject: async (id) => structuredClone(this.project(id)),
|
|
323
|
+
listRepositoryTree: async (id, ref, directory = "") =>
|
|
324
|
+
this.git(id, ["ls-tree", ref, ...(directory ? [`${directory}/`] : [])])
|
|
325
|
+
.split("\n")
|
|
326
|
+
.filter(Boolean)
|
|
327
|
+
.map((line) => {
|
|
328
|
+
const [details, filePath] = line.split("\t");
|
|
329
|
+
const [, type, objectId] = details.split(" ");
|
|
330
|
+
return { path: filePath, type, id: objectId };
|
|
331
|
+
}),
|
|
332
|
+
getRepositoryFile: async (id, ref, filePath) => ({
|
|
333
|
+
file_path: filePath,
|
|
334
|
+
content: Buffer.from(this.git(id, ["show", `${ref}:${filePath}`])).toString("base64"),
|
|
335
|
+
encoding: "base64",
|
|
336
|
+
last_commit_id: this.git(id, ["log", "-1", "--format=%H", ref, "--", filePath]),
|
|
337
|
+
}),
|
|
291
338
|
listProjects: async () => structuredClone(this.state.projects),
|
|
339
|
+
searchProjects: async (search) =>
|
|
340
|
+
structuredClone(
|
|
341
|
+
this.state.projects
|
|
342
|
+
.filter(
|
|
343
|
+
(project) =>
|
|
344
|
+
!project.archived &&
|
|
345
|
+
project.path_with_namespace.toLowerCase().includes(search.trim().toLowerCase()),
|
|
346
|
+
)
|
|
347
|
+
.slice(0, 100),
|
|
348
|
+
),
|
|
292
349
|
getGroup: async (id) => ({ id: 1, full_path: String(id) }),
|
|
293
350
|
listGroupProjects: async (group) =>
|
|
294
351
|
structuredClone(
|
|
@@ -297,14 +354,79 @@ export class LocalGitLabAdapter {
|
|
|
297
354
|
getMergeRequest: async (id, iid) => ({
|
|
298
355
|
...structuredClone(mr(id, iid)),
|
|
299
356
|
has_conflicts: this.conflicts(mr(id, iid)),
|
|
300
|
-
sha:
|
|
357
|
+
sha: this.git(mr(id, iid).source_project_id, ["rev-parse", mr(id, iid).source_branch]),
|
|
301
358
|
}),
|
|
359
|
+
getMergeRequestApprovals: async (id, iid) => ({
|
|
360
|
+
user_has_approved: this.state.approvedSha?.[`${id}:${iid}`] !== undefined,
|
|
361
|
+
}),
|
|
362
|
+
approveMergeRequest: async (id, iid, sha) => {
|
|
363
|
+
const request = mr(id, iid);
|
|
364
|
+
const head = commit(request.source_project_id, request.source_branch).id;
|
|
365
|
+
if (head !== sha) throw new Error("SHA does not match HEAD of source branch");
|
|
366
|
+
const key = `${id}:${iid}`;
|
|
367
|
+
this.state.approvedSha ??= {};
|
|
368
|
+
if (this.state.approvedSha[key] === sha) return { sha };
|
|
369
|
+
this.state.approvedSha[key] = sha;
|
|
370
|
+
this.save();
|
|
371
|
+
if (this.loseNextApprovalResponse) {
|
|
372
|
+
this.loseNextApprovalResponse = false;
|
|
373
|
+
throw new Error("Response lost after approval");
|
|
374
|
+
}
|
|
375
|
+
return { sha };
|
|
376
|
+
},
|
|
377
|
+
unapproveMergeRequest: async (id, iid) => {
|
|
378
|
+
const request = mr(id, iid);
|
|
379
|
+
const head = commit(request.source_project_id, request.source_branch).id;
|
|
380
|
+
const key = `${id}:${iid}`;
|
|
381
|
+
if (this.state.approvedSha?.[key] === undefined) return { sha: head };
|
|
382
|
+
delete this.state.approvedSha[key];
|
|
383
|
+
this.save();
|
|
384
|
+
if (this.loseNextApprovalResponse) {
|
|
385
|
+
this.loseNextApprovalResponse = false;
|
|
386
|
+
throw new Error("Response lost after unapprove");
|
|
387
|
+
}
|
|
388
|
+
return { sha: head };
|
|
389
|
+
},
|
|
302
390
|
listMergeRequests: async (id, label) =>
|
|
303
391
|
structuredClone(
|
|
304
392
|
this.state.mrs.filter(
|
|
305
393
|
(m) => m.project_id === id && m.state === "opened" && m.labels.includes(label),
|
|
306
394
|
),
|
|
307
395
|
),
|
|
396
|
+
listMergeRequestLabelEvents: async (id, iid) => {
|
|
397
|
+
mr(id, iid);
|
|
398
|
+
return structuredClone(this.state.labelEvents?.[`${id}:${iid}`] ?? []);
|
|
399
|
+
},
|
|
400
|
+
updateMergeRequestLabels: async (id, iid, patch) => {
|
|
401
|
+
const request = mr(id, iid);
|
|
402
|
+
this.state.labelEvents ??= {};
|
|
403
|
+
this.state.labelEvents[`${id}:${iid}`] ??= [];
|
|
404
|
+
const events = this.state.labelEvents[`${id}:${iid}`];
|
|
405
|
+
for (const [action, labels] of [
|
|
406
|
+
["add", patch.add_labels],
|
|
407
|
+
["remove", patch.remove_labels],
|
|
408
|
+
] as const) {
|
|
409
|
+
for (const name of labels?.split(",").filter(Boolean) ?? []) {
|
|
410
|
+
if (request.labels.includes(name) === (action === "add")) continue;
|
|
411
|
+
request.labels =
|
|
412
|
+
action === "add"
|
|
413
|
+
? [...request.labels, name]
|
|
414
|
+
: request.labels.filter((label) => label !== name);
|
|
415
|
+
events.push({
|
|
416
|
+
id: Math.max(0, ...events.map((event) => event.id)) + 1,
|
|
417
|
+
action,
|
|
418
|
+
label: { name },
|
|
419
|
+
created_at: new Date().toISOString(),
|
|
420
|
+
});
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
this.save();
|
|
424
|
+
if (this.loseNextLabelResponse) {
|
|
425
|
+
this.loseNextLabelResponse = false;
|
|
426
|
+
throw new Error("Response lost after label write");
|
|
427
|
+
}
|
|
428
|
+
return structuredClone(request);
|
|
429
|
+
},
|
|
308
430
|
getBranch: async (id, branch) => ({
|
|
309
431
|
name: branch,
|
|
310
432
|
can_push: true,
|
|
@@ -312,6 +434,7 @@ export class LocalGitLabAdapter {
|
|
|
312
434
|
commit: commit(id, branch),
|
|
313
435
|
}),
|
|
314
436
|
getCommit: async (id, ref) => commit(id, ref),
|
|
437
|
+
getMergeBase: async (id, refs) => commit(id, this.git(id, ["merge-base", ...refs])),
|
|
315
438
|
getChanges: async (id, iid) => structuredClone(this.state.diffs[`${id}:${iid}`] ?? []),
|
|
316
439
|
listMergeRequestPipelines: async (id, iid) =>
|
|
317
440
|
structuredClone(
|
|
@@ -357,10 +480,38 @@ export class LocalGitLabAdapter {
|
|
|
357
480
|
notes: (this.state.discussionNotes?.[`${id}:${iid}:${discussionId}`] ?? []).map((note) => ({
|
|
358
481
|
...structuredClone(note),
|
|
359
482
|
system: false,
|
|
483
|
+
resolved: this.state.discussionResolved?.[`${id}:${iid}:${discussionId}`] === true,
|
|
360
484
|
created_at: new Date().toISOString(),
|
|
361
485
|
author: { username: "bot" },
|
|
362
486
|
})),
|
|
363
487
|
}),
|
|
488
|
+
listDiscussions: async (id, iid) =>
|
|
489
|
+
Object.entries(this.state.discussionNotes ?? {})
|
|
490
|
+
.filter(([key]) => key.startsWith(`${id}:${iid}:`))
|
|
491
|
+
.map(([key, notes]) => ({
|
|
492
|
+
id: key.slice(`${id}:${iid}:`.length),
|
|
493
|
+
notes: notes.map((note) => ({
|
|
494
|
+
...structuredClone(note),
|
|
495
|
+
system: false,
|
|
496
|
+
created_at: new Date().toISOString(),
|
|
497
|
+
author: { username: "bot" },
|
|
498
|
+
})),
|
|
499
|
+
})),
|
|
500
|
+
addDiscussion: async (id, iid, body, position) => {
|
|
501
|
+
this.state.discussionNotes ??= {};
|
|
502
|
+
const discussionId = `discussion-${Object.keys(this.state.discussionNotes).length + 1}`;
|
|
503
|
+
const note = {
|
|
504
|
+
id: 1,
|
|
505
|
+
body,
|
|
506
|
+
system: false,
|
|
507
|
+
created_at: new Date().toISOString(),
|
|
508
|
+
author: { username: "bot" },
|
|
509
|
+
position,
|
|
510
|
+
};
|
|
511
|
+
this.state.discussionNotes[`${id}:${iid}:${discussionId}`] = [note];
|
|
512
|
+
this.save();
|
|
513
|
+
return { id: discussionId, notes: [note] };
|
|
514
|
+
},
|
|
364
515
|
replyToDiscussion: async (id, iid, discussionId, body) => {
|
|
365
516
|
const key = `${id}:${iid}:${discussionId}`;
|
|
366
517
|
this.state.discussionNotes ??= {};
|
|
@@ -368,6 +519,22 @@ export class LocalGitLabAdapter {
|
|
|
368
519
|
const notes = this.state.discussionNotes[key];
|
|
369
520
|
return this.append(notes, { id: notes.length + 1, body }, "Reply");
|
|
370
521
|
},
|
|
522
|
+
resolveDiscussion: async (id, iid, discussionId) => {
|
|
523
|
+
const key = `${id}:${iid}:${discussionId}`;
|
|
524
|
+
this.state.discussionResolved ??= {};
|
|
525
|
+
this.state.discussionResolved[key] = true;
|
|
526
|
+
this.save();
|
|
527
|
+
return {
|
|
528
|
+
id: discussionId,
|
|
529
|
+
notes: (this.state.discussionNotes?.[key] ?? []).map((note) => ({
|
|
530
|
+
...structuredClone(note),
|
|
531
|
+
system: false,
|
|
532
|
+
resolved: true,
|
|
533
|
+
created_at: new Date().toISOString(),
|
|
534
|
+
author: { username: "bot" },
|
|
535
|
+
})),
|
|
536
|
+
};
|
|
537
|
+
},
|
|
371
538
|
listNoteReactions: async (id, iid, noteId) =>
|
|
372
539
|
structuredClone(this.state.reactions?.[`${id}:${iid}:${noteId}`] ?? []),
|
|
373
540
|
addNoteReaction: async (id, iid, noteId, name) => {
|
|
@@ -388,6 +555,13 @@ export class LocalGitLabAdapter {
|
|
|
388
555
|
const notes = this.state.notes[key];
|
|
389
556
|
return this.append(notes, { id: notes.length + 1, body }, "Comment");
|
|
390
557
|
},
|
|
558
|
+
updateNote: async (id, iid, noteId, body) => {
|
|
559
|
+
const note = (this.state.notes[`${id}:${iid}`] ?? []).find((item) => item.id === noteId);
|
|
560
|
+
if (!note) throw new Error("Unknown test note");
|
|
561
|
+
note.body = body;
|
|
562
|
+
this.save();
|
|
563
|
+
return structuredClone(note);
|
|
564
|
+
},
|
|
391
565
|
};
|
|
392
566
|
}
|
|
393
567
|
}
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
import {
|
|
2
|
+
listTicketDestinations,
|
|
3
|
+
markdownTicketCreationDefinition,
|
|
4
|
+
numberArg,
|
|
5
|
+
objectArg,
|
|
6
|
+
parseTicketDestinationId,
|
|
7
|
+
type ServerExtensionAPI,
|
|
8
|
+
stringArg,
|
|
9
|
+
type TicketCreationConfig,
|
|
10
|
+
type TicketCreationDestinationProvider,
|
|
11
|
+
type TicketCreationDestinationSnapshot,
|
|
12
|
+
ticketDestinationId,
|
|
13
|
+
ticketLabelNames,
|
|
14
|
+
} from "@leitwerk-dev/process-sdk";
|
|
15
|
+
import type { GitLabIntegration } from "./capability.js";
|
|
16
|
+
import type { GitLabProject } from "./client.js";
|
|
17
|
+
|
|
18
|
+
function available(project: GitLabProject): boolean {
|
|
19
|
+
return (
|
|
20
|
+
!project.archived &&
|
|
21
|
+
project.issues_enabled !== false &&
|
|
22
|
+
project.issues_access_level !== "disabled"
|
|
23
|
+
);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function destinationData(snapshot: TicketCreationDestinationSnapshot) {
|
|
27
|
+
const data = objectArg(snapshot.data);
|
|
28
|
+
return {
|
|
29
|
+
profile: stringArg(data, "profile"),
|
|
30
|
+
baseUrl: stringArg(data, "baseUrl"),
|
|
31
|
+
projectId: numberArg(data, "projectId"),
|
|
32
|
+
path: stringArg(data, "path"),
|
|
33
|
+
defaultLabels: ticketLabelNames(data.defaultLabels),
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** @internal */
|
|
38
|
+
export function registerGitLabTicketCreation(
|
|
39
|
+
api: ServerExtensionAPI,
|
|
40
|
+
integration: GitLabIntegration,
|
|
41
|
+
config: TicketCreationConfig,
|
|
42
|
+
): void {
|
|
43
|
+
if (!config.enabled) return;
|
|
44
|
+
const summary = (profile: string, project: GitLabProject) => ({
|
|
45
|
+
id: ticketDestinationId(profile, project.id),
|
|
46
|
+
displayName: project.path_with_namespace,
|
|
47
|
+
group: `${profile} · ${new URL(integration.client(profile).baseUrl).hostname}`,
|
|
48
|
+
description: config.defaultLabels.length
|
|
49
|
+
? `Default labels: ${config.defaultLabels.join(", ")}`
|
|
50
|
+
: "No default labels",
|
|
51
|
+
});
|
|
52
|
+
const destinations: TicketCreationDestinationProvider = {
|
|
53
|
+
list() {
|
|
54
|
+
return listTicketDestinations("GitLab", integration.profiles(), async (profile) =>
|
|
55
|
+
(await integration.client(profile).listProjects())
|
|
56
|
+
.filter(available)
|
|
57
|
+
.map((project) => summary(profile, project)),
|
|
58
|
+
);
|
|
59
|
+
},
|
|
60
|
+
async resolve({ destinationId }) {
|
|
61
|
+
const { profile, resourceId } = parseTicketDestinationId(destinationId);
|
|
62
|
+
const client = integration.client(profile);
|
|
63
|
+
const project = await client.getProject(Number(resourceId));
|
|
64
|
+
if (!available(project)) throw new Error("The selected GitLab project cannot accept issues");
|
|
65
|
+
const labels = await client.listLabels(project.id);
|
|
66
|
+
return {
|
|
67
|
+
summary: summary(profile, project),
|
|
68
|
+
data: {
|
|
69
|
+
profile,
|
|
70
|
+
baseUrl: client.baseUrl,
|
|
71
|
+
projectId: project.id,
|
|
72
|
+
path: project.path_with_namespace,
|
|
73
|
+
defaultLabels: config.defaultLabels,
|
|
74
|
+
},
|
|
75
|
+
agentContext: `Create an issue in ${project.path_with_namespace}. Existing optional labels: ${labels.map((label) => label.name).join(", ") || "none"}.`,
|
|
76
|
+
};
|
|
77
|
+
},
|
|
78
|
+
async validate(snapshot) {
|
|
79
|
+
const data = destinationData(snapshot);
|
|
80
|
+
const client = integration.client(data.profile);
|
|
81
|
+
if (client.baseUrl !== data.baseUrl) throw new Error("GitLab profile installation changed");
|
|
82
|
+
const project = await client.getProject(data.projectId);
|
|
83
|
+
if (!available(project) || project.path_with_namespace !== data.path)
|
|
84
|
+
throw new Error("The selected GitLab project changed; choose the destination again");
|
|
85
|
+
},
|
|
86
|
+
};
|
|
87
|
+
api.tool<Record<string, unknown>>({
|
|
88
|
+
name: "gitlab_create_issue",
|
|
89
|
+
description: "Create one issue in the selected GitLab project",
|
|
90
|
+
...markdownTicketCreationDefinition("GitLab", destinations, "description"),
|
|
91
|
+
async execute(ctx, args) {
|
|
92
|
+
if (!ctx.ticketDestination) throw new Error("A GitLab ticket destination is required");
|
|
93
|
+
await destinations.validate(ctx.ticketDestination);
|
|
94
|
+
const target = destinationData(ctx.ticketDestination);
|
|
95
|
+
const client = integration.client(target.profile);
|
|
96
|
+
const title = stringArg(args, "title");
|
|
97
|
+
const description = stringArg(args, "description");
|
|
98
|
+
const requestedLabels = ticketLabelNames(args.labels);
|
|
99
|
+
const names = [...new Set([...target.defaultLabels, ...requestedLabels])];
|
|
100
|
+
if (names.some((name) => name.includes(",")))
|
|
101
|
+
throw new Error("GitLab label names cannot contain commas");
|
|
102
|
+
const labels = await client.listLabels(target.projectId, ctx.signal);
|
|
103
|
+
const unknown = requestedLabels.filter(
|
|
104
|
+
(name) =>
|
|
105
|
+
!target.defaultLabels.includes(name) && !labels.some((label) => label.name === name),
|
|
106
|
+
);
|
|
107
|
+
if (unknown.length) throw new Error(`Unknown GitLab labels: ${unknown.join(", ")}`);
|
|
108
|
+
for (const name of target.defaultLabels) {
|
|
109
|
+
await ctx.externalWrites.ensure(
|
|
110
|
+
{ writeType: "gitlab.ensure_label", dedupKey: `${ctx.idempotencyKey}:label:${name}` },
|
|
111
|
+
{
|
|
112
|
+
reconcile: async () =>
|
|
113
|
+
(await client.listLabels(target.projectId, ctx.signal)).find(
|
|
114
|
+
(label) => label.name === name,
|
|
115
|
+
) ?? null,
|
|
116
|
+
execute: () => client.createLabel(target.projectId, name, ctx.signal),
|
|
117
|
+
toMetadata: (label) => ({ name: label.name }),
|
|
118
|
+
},
|
|
119
|
+
);
|
|
120
|
+
}
|
|
121
|
+
const marker = `<!-- leitwerk-ticket-write:${ctx.idempotencyKey} -->`;
|
|
122
|
+
const issue = await ctx.externalWrites.ensure(
|
|
123
|
+
{ writeType: "gitlab.create_issue", dedupKey: ctx.idempotencyKey },
|
|
124
|
+
{
|
|
125
|
+
reconcile: async () =>
|
|
126
|
+
(await client.listIssues(target.projectId, ctx.signal, "all")).find((issue) =>
|
|
127
|
+
issue.description?.includes(marker),
|
|
128
|
+
) ?? null,
|
|
129
|
+
execute: () =>
|
|
130
|
+
client.createIssue(
|
|
131
|
+
target.projectId,
|
|
132
|
+
{ title, description: `${description}\n\n${marker}`, labels: names.join(",") },
|
|
133
|
+
ctx.signal,
|
|
134
|
+
),
|
|
135
|
+
toMetadata: (issue) => ({
|
|
136
|
+
projectId: issue.project_id,
|
|
137
|
+
iid: issue.iid,
|
|
138
|
+
url: issue.web_url,
|
|
139
|
+
}),
|
|
140
|
+
},
|
|
141
|
+
);
|
|
142
|
+
return { externalId: `${target.path}#${issue.iid}`, url: issue.web_url, result: issue };
|
|
143
|
+
},
|
|
144
|
+
});
|
|
145
|
+
}
|