@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.
- package/README.md +45 -5
- package/dist/capability.d.ts +5 -1
- package/dist/capability.d.ts.map +1 -1
- package/dist/chunk-H4JCQNDY.js +1162 -0
- package/dist/chunk-H4JCQNDY.js.map +1 -0
- package/dist/client.d.ts +230 -3
- package/dist/client.d.ts.map +1 -1
- package/dist/delivery-tools.d.ts +4 -0
- package/dist/delivery-tools.d.ts.map +1 -0
- package/dist/external.d.ts +80 -5
- package/dist/external.d.ts.map +1 -1
- package/dist/index.d.ts +13 -14
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +40 -20
- package/dist/index.js.map +1 -1
- package/dist/issue-watcher.d.ts +42 -0
- package/dist/issue-watcher.d.ts.map +1 -0
- package/dist/preflight.d.ts +10 -0
- package/dist/preflight.d.ts.map +1 -0
- package/dist/selection.d.ts +11 -1
- package/dist/selection.d.ts.map +1 -1
- package/dist/setup.d.ts +11 -0
- package/dist/setup.d.ts.map +1 -0
- package/dist/testing.d.ts +43 -4
- package/dist/testing.d.ts.map +1 -1
- package/dist/testing.js +139 -1
- package/dist/testing.js.map +1 -1
- package/dist/tools.d.ts +35 -4
- package/dist/tools.d.ts.map +1 -1
- package/package.json +7 -6
- package/src/capability.ts +5 -1
- package/src/client.ts +392 -20
- package/src/delivery-tools.ts +226 -0
- package/src/external.ts +200 -8
- package/src/index.ts +42 -26
- package/src/issue-watcher.ts +152 -0
- package/src/preflight.ts +70 -0
- package/src/selection.ts +19 -3
- package/src/setup.ts +22 -0
- package/src/testing.ts +161 -7
- package/src/tools.ts +106 -54
- package/dist/chunk-5GBBPAG5.js +0 -656
- 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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
|
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
|
-
|
|
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
|
|
47
|
+
return { profile: binding.profile, projectId: binding.projectId };
|
|
32
48
|
}
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
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
|
-
|
|
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
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
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
|
-
|
|
108
|
-
|
|
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,
|