@leitwerk-dev/gitlab 0.2.1 → 0.3.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 +27 -5
- package/dist/chunk-TQIZKDAM.js +1160 -0
- package/dist/chunk-TQIZKDAM.js.map +1 -0
- package/dist/client.d.ts +80 -15
- 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 +58 -6
- package/dist/external.d.ts.map +1 -1
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +7 -45
- package/dist/index.js.map +1 -1
- package/dist/issue-watcher.d.ts +40 -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/setup.d.ts.map +1 -1
- package/dist/testing.d.ts +17 -2
- package/dist/testing.d.ts.map +1 -1
- package/dist/testing.js +129 -1
- package/dist/testing.js.map +1 -1
- package/dist/tools.d.ts +9 -2
- package/dist/tools.d.ts.map +1 -1
- package/package.json +7 -6
- package/src/client.ts +169 -22
- package/src/delivery-tools.ts +226 -0
- package/src/external.ts +161 -9
- package/src/index.ts +3 -0
- package/src/issue-watcher.ts +145 -0
- package/src/preflight.ts +70 -0
- package/src/setup.ts +2 -0
- package/src/testing.ts +140 -1
- package/src/tools.ts +32 -9
- package/dist/chunk-64FNBSKH.js +0 -605
- package/dist/chunk-64FNBSKH.js.map +0 -1
package/src/external.ts
CHANGED
|
@@ -1,11 +1,40 @@
|
|
|
1
|
+
import { type ConflictEvidence, conflictKey } from "@leitwerk-dev/coding/repository-rebase";
|
|
1
2
|
import {
|
|
2
3
|
type CoreServerSetupDeps,
|
|
3
4
|
createExternalSourcePollReporter,
|
|
4
5
|
type ExternalActionSource,
|
|
6
|
+
type ExternalSourceArmingLike,
|
|
5
7
|
} from "@leitwerk-dev/process-sdk";
|
|
6
8
|
import { emptyPollResult, parseDurationMs } from "@leitwerk-dev/watcher-utils";
|
|
7
9
|
import type { GitLabIntegration } from "./capability.js";
|
|
8
10
|
import { type GitLabFeedback, type GitLabObservation, observeMergeRequest } from "./client.js";
|
|
11
|
+
import { createGitLabIssueDiscovery } from "./issue-watcher.js";
|
|
12
|
+
/** @public */
|
|
13
|
+
export interface GitLabDeliveryObservation extends GitLabObservation {
|
|
14
|
+
/** @public */
|
|
15
|
+
observationKey?: string;
|
|
16
|
+
/** @public */
|
|
17
|
+
feedback?: GitLabFeedback[];
|
|
18
|
+
/** @public */
|
|
19
|
+
conflict?: ConflictEvidence;
|
|
20
|
+
}
|
|
21
|
+
/** @public */
|
|
22
|
+
export const GITLAB_ISSUE_CANCELLED_KIND = "@leitwerk-dev/gitlab.issue-cancelled";
|
|
23
|
+
/** @public */
|
|
24
|
+
export interface GitLabIssueCancelledConfig {
|
|
25
|
+
/** @public */
|
|
26
|
+
profile: string;
|
|
27
|
+
/** @public */
|
|
28
|
+
projectId: number;
|
|
29
|
+
/** @public */
|
|
30
|
+
issueIid: number;
|
|
31
|
+
/** @public */
|
|
32
|
+
iid: number;
|
|
33
|
+
/** @public */
|
|
34
|
+
triggerLabel: string;
|
|
35
|
+
/** @public */
|
|
36
|
+
pollInterval?: string;
|
|
37
|
+
}
|
|
9
38
|
/** @internal */
|
|
10
39
|
export const GITLAB_MR_KIND = "@leitwerk-dev/gitlab.merge-request";
|
|
11
40
|
/** @public */
|
|
@@ -20,9 +49,25 @@ export interface GitLabSourceConfig {
|
|
|
20
49
|
pollInterval?: string;
|
|
21
50
|
/** @public */
|
|
22
51
|
afterKey?: string;
|
|
23
|
-
/** Optional timer also wakes retry work when GitLab facts have not changed.
|
|
52
|
+
/** Optional timer also wakes retry work when GitLab facts have not changed. */
|
|
53
|
+
/** @public */
|
|
24
54
|
wakeAt?: number;
|
|
25
55
|
/** @public */
|
|
56
|
+
delivery?: {
|
|
57
|
+
/** @public */
|
|
58
|
+
headSha: string;
|
|
59
|
+
/** @public */
|
|
60
|
+
owner: string;
|
|
61
|
+
/** @public */
|
|
62
|
+
repo: string;
|
|
63
|
+
/** @public */
|
|
64
|
+
headBranch: string;
|
|
65
|
+
/** @public */
|
|
66
|
+
baseBranch: string;
|
|
67
|
+
/** @public */
|
|
68
|
+
lastConflictKey?: string | null;
|
|
69
|
+
};
|
|
70
|
+
/** @public */
|
|
26
71
|
feedback?: {
|
|
27
72
|
/** @public */
|
|
28
73
|
afterId: number;
|
|
@@ -32,7 +77,8 @@ export interface GitLabSourceConfig {
|
|
|
32
77
|
quietPeriodMs: number;
|
|
33
78
|
};
|
|
34
79
|
}
|
|
35
|
-
/** A trailing quiet period survives restarts because it uses the newest unseen note's timestamp.
|
|
80
|
+
/** A trailing quiet period survives restarts because it uses the newest unseen note's timestamp. */
|
|
81
|
+
/** @public */
|
|
36
82
|
export function pendingGitLabFeedback(
|
|
37
83
|
items: GitLabFeedback[],
|
|
38
84
|
afterId: number,
|
|
@@ -70,19 +116,38 @@ export const observationKey = ({ mr, pipeline, targetHead }: GitLabObservation):
|
|
|
70
116
|
]);
|
|
71
117
|
/** @public */
|
|
72
118
|
export const gitlabExternal = {
|
|
119
|
+
/** @public */
|
|
120
|
+
issueCancelled<P, S>(
|
|
121
|
+
/** @public */
|
|
122
|
+
resolve: (ctx: {
|
|
123
|
+
/** @public */
|
|
124
|
+
params: P;
|
|
125
|
+
/** @public */
|
|
126
|
+
state: S;
|
|
127
|
+
}) => GitLabIssueCancelledConfig,
|
|
128
|
+
): ExternalActionSource<P, S, unknown> {
|
|
129
|
+
return {
|
|
130
|
+
kind: GITLAB_ISSUE_CANCELLED_KIND,
|
|
131
|
+
label: "GitLab source issue cancelled",
|
|
132
|
+
config: {},
|
|
133
|
+
inputMode: "none",
|
|
134
|
+
resolve,
|
|
135
|
+
};
|
|
136
|
+
},
|
|
73
137
|
/** @public */
|
|
74
138
|
mergeRequest<P, S>(
|
|
139
|
+
/** @public */
|
|
75
140
|
resolve: (ctx: {
|
|
76
141
|
/** @public */
|
|
77
142
|
params: P;
|
|
78
143
|
/** @public */
|
|
79
144
|
state: S;
|
|
80
145
|
}) => GitLabSourceConfig,
|
|
81
|
-
): ExternalActionSource<P, S,
|
|
146
|
+
): ExternalActionSource<P, S, GitLabDeliveryObservation> {
|
|
82
147
|
return {
|
|
83
148
|
kind: GITLAB_MR_KIND,
|
|
84
149
|
label: "GitLab merge request",
|
|
85
|
-
description: "Observe MR state, source
|
|
150
|
+
description: "Observe MR state, current source head and revision-correlated CI",
|
|
86
151
|
config: {},
|
|
87
152
|
inputMode: "none",
|
|
88
153
|
resolve,
|
|
@@ -93,13 +158,14 @@ export const gitlabExternal = {
|
|
|
93
158
|
export function createGitLabProvider(
|
|
94
159
|
deps: CoreServerSetupDeps,
|
|
95
160
|
integration: GitLabIntegration,
|
|
161
|
+
/** @public */
|
|
96
162
|
options: {
|
|
97
|
-
/** @internal */
|
|
98
|
-
now?: () => number;
|
|
163
|
+
/** @internal */ now?: () => number;
|
|
99
164
|
} = {},
|
|
100
165
|
) {
|
|
101
166
|
const schedule = new Map<string, { at: number; failures: number }>();
|
|
102
167
|
const now = options.now ?? Date.now;
|
|
168
|
+
const discover = createGitLabIssueDiscovery(deps, integration, now);
|
|
103
169
|
return deps.polling.create({
|
|
104
170
|
id: "gitlab",
|
|
105
171
|
pollInterval: () => "5s",
|
|
@@ -107,7 +173,27 @@ export function createGitLabProvider(
|
|
|
107
173
|
defaultIntervalMs: 5000,
|
|
108
174
|
async pollOnce() {
|
|
109
175
|
const result = emptyPollResult();
|
|
110
|
-
|
|
176
|
+
try {
|
|
177
|
+
await discover(result);
|
|
178
|
+
} catch (error) {
|
|
179
|
+
result.errors.push(error instanceof Error ? error.message : "GitLab discovery failed");
|
|
180
|
+
}
|
|
181
|
+
const reporter = createExternalSourcePollReporter(deps.externalSources, result, {
|
|
182
|
+
forwardGeneration: true,
|
|
183
|
+
});
|
|
184
|
+
const current = (armed: ExternalSourceArmingLike) =>
|
|
185
|
+
[GITLAB_MR_KIND, GITLAB_ISSUE_CANCELLED_KIND].some((kind) =>
|
|
186
|
+
reporter.isCurrent(kind, armed),
|
|
187
|
+
);
|
|
188
|
+
const report = {
|
|
189
|
+
...reporter,
|
|
190
|
+
fire(armed: ExternalSourceArmingLike, event: Record<string, unknown>, mergeKey: string) {
|
|
191
|
+
return current(armed) ? reporter.fire(armed, event, mergeKey) : Promise.resolve(false);
|
|
192
|
+
},
|
|
193
|
+
observe(armed: ExternalSourceArmingLike, input: Parameters<typeof reporter.observe>[1]) {
|
|
194
|
+
if (current(armed)) return reporter.observe(armed, input);
|
|
195
|
+
},
|
|
196
|
+
};
|
|
111
197
|
await report.poll(GITLAB_MR_KIND, async (armed) => {
|
|
112
198
|
const c = armed.resolved as unknown as GitLabSourceConfig;
|
|
113
199
|
if (
|
|
@@ -126,7 +212,40 @@ export function createGitLabProvider(
|
|
|
126
212
|
c.projectId,
|
|
127
213
|
c.iid,
|
|
128
214
|
);
|
|
129
|
-
|
|
215
|
+
let conflict: ConflictEvidence | undefined;
|
|
216
|
+
const client = integration.client(c.profile);
|
|
217
|
+
if (
|
|
218
|
+
c.delivery &&
|
|
219
|
+
observation.mr.state === "opened" &&
|
|
220
|
+
observation.mr.sha === c.delivery.headSha &&
|
|
221
|
+
(observation.mr.has_conflicts === true ||
|
|
222
|
+
observation.mr.detailed_merge_status === "conflict")
|
|
223
|
+
) {
|
|
224
|
+
const mr = observation.mr;
|
|
225
|
+
const d = c.delivery;
|
|
226
|
+
if (
|
|
227
|
+
mr.source_project_id !== c.projectId ||
|
|
228
|
+
mr.target_project_id !== c.projectId ||
|
|
229
|
+
mr.source_branch !== d.headBranch ||
|
|
230
|
+
mr.target_branch !== d.baseBranch
|
|
231
|
+
)
|
|
232
|
+
throw new Error("GitLab merge request branches changed");
|
|
233
|
+
const base = await client.getBranch(c.projectId, d.baseBranch);
|
|
234
|
+
const evidence: ConflictEvidence = {
|
|
235
|
+
owner: d.owner,
|
|
236
|
+
repo: d.repo,
|
|
237
|
+
prNumber: c.iid,
|
|
238
|
+
headBranch: d.headBranch,
|
|
239
|
+
baseBranch: d.baseBranch,
|
|
240
|
+
headSha: mr.sha,
|
|
241
|
+
baseSha: base.commit.id,
|
|
242
|
+
url: mr.web_url,
|
|
243
|
+
};
|
|
244
|
+
if (conflictKey(evidence) !== d.lastConflictKey) conflict = evidence;
|
|
245
|
+
}
|
|
246
|
+
const observedKey =
|
|
247
|
+
observationKey(observation) +
|
|
248
|
+
(c.delivery ? `:${conflict ? conflictKey(conflict) : ""}` : "");
|
|
130
249
|
const feedback =
|
|
131
250
|
c.feedback && observation.mr.state === "opened"
|
|
132
251
|
? pendingGitLabFeedback(
|
|
@@ -140,6 +259,10 @@ export function createGitLabProvider(
|
|
|
140
259
|
c.feedback?.quietPeriodMs ?? 120_000,
|
|
141
260
|
);
|
|
142
261
|
const feedbackReady = feedbackReadyAt !== null && feedbackReadyAt <= now();
|
|
262
|
+
if (c.delivery) {
|
|
263
|
+
const fresh = await client.getMergeRequest(c.projectId, c.iid);
|
|
264
|
+
if (fresh.sha !== observation.mr.sha || fresh.state !== observation.mr.state) return;
|
|
265
|
+
}
|
|
143
266
|
await report.observe(armed, {
|
|
144
267
|
observation: {
|
|
145
268
|
summary: `${observation.mr.state} · ${observation.pipeline?.status ?? "no current pipeline"}`,
|
|
@@ -155,7 +278,16 @@ export function createGitLabProvider(
|
|
|
155
278
|
)
|
|
156
279
|
await report.fire(
|
|
157
280
|
armed,
|
|
158
|
-
{
|
|
281
|
+
{
|
|
282
|
+
...observation,
|
|
283
|
+
...(c.delivery
|
|
284
|
+
? {
|
|
285
|
+
observationKey: observedKey,
|
|
286
|
+
feedback: feedbackReady ? feedback : [],
|
|
287
|
+
...(conflict ? { conflict } : {}),
|
|
288
|
+
}
|
|
289
|
+
: {}),
|
|
290
|
+
},
|
|
159
291
|
`${observedKey}:${c.wakeAt ?? ""}:${feedbackReady ? feedback.map((item) => item.id).join(",") : ""}`,
|
|
160
292
|
);
|
|
161
293
|
schedule.set(key, {
|
|
@@ -174,6 +306,26 @@ export function createGitLabProvider(
|
|
|
174
306
|
throw error;
|
|
175
307
|
}
|
|
176
308
|
});
|
|
309
|
+
await report.poll(GITLAB_ISSUE_CANCELLED_KIND, async (armed) => {
|
|
310
|
+
const c = armed.resolved as unknown as GitLabIssueCancelledConfig;
|
|
311
|
+
const key = `${armed.instanceId}:${armed.id}:${armed.generation ?? ""}`;
|
|
312
|
+
if ((schedule.get(key)?.at ?? 0) > now()) return;
|
|
313
|
+
const client = integration.client(c.profile);
|
|
314
|
+
const issue = await client.getIssue(c.projectId, c.issueIid);
|
|
315
|
+
if (issue.state !== "opened" || !issue.labels.includes(c.triggerLabel)) {
|
|
316
|
+
const mr = await client.getMergeRequest(c.projectId, c.iid);
|
|
317
|
+
if (mr.state !== "opened") return;
|
|
318
|
+
await report.fire(
|
|
319
|
+
armed,
|
|
320
|
+
{ issue },
|
|
321
|
+
`${issue.iid}:${issue.state}:${issue.labels.includes(c.triggerLabel)}`,
|
|
322
|
+
);
|
|
323
|
+
}
|
|
324
|
+
schedule.set(key, {
|
|
325
|
+
at: now() + parseDurationMs(c.pollInterval ?? "30s", 30000),
|
|
326
|
+
failures: 0,
|
|
327
|
+
});
|
|
328
|
+
});
|
|
177
329
|
return result;
|
|
178
330
|
},
|
|
179
331
|
});
|
package/src/index.ts
CHANGED
|
@@ -53,12 +53,15 @@ export type {
|
|
|
53
53
|
GitLabProject,
|
|
54
54
|
} from "./client.js";
|
|
55
55
|
export { observeMergeRequest } from "./client.js";
|
|
56
|
+
export type { GitLabDeliveryObservation } from "./external.js";
|
|
56
57
|
export {
|
|
57
58
|
gitLabFeedbackReadyAt,
|
|
58
59
|
gitlabExternal,
|
|
59
60
|
observationKey,
|
|
60
61
|
pendingGitLabFeedback,
|
|
61
62
|
} from "./external.js";
|
|
63
|
+
export type { GitLabIssueWatcherEvent } from "./issue-watcher.js";
|
|
64
|
+
export { gitlabIssueExternalId, gitlabIssueWatcherSource } from "./issue-watcher.js";
|
|
62
65
|
export type { GitLabSelection } from "./selection.js";
|
|
63
66
|
export {
|
|
64
67
|
parseGitLabSelection,
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
import { asUnknownRecord } from "@leitwerk-dev/domain";
|
|
2
|
+
import {
|
|
3
|
+
type CoreServerSetupDeps,
|
|
4
|
+
defineProcessWatcherSource,
|
|
5
|
+
parseProcessWatcherLaunchModelConfig,
|
|
6
|
+
} from "@leitwerk-dev/process-sdk";
|
|
7
|
+
import { type emptyPollResult, parseDurationMs } from "@leitwerk-dev/watcher-utils";
|
|
8
|
+
import type { GitLabIntegration } from "./capability.js";
|
|
9
|
+
import type { GitLabIssue, GitLabProject } from "./client.js";
|
|
10
|
+
import { type GitLabSelection, parseGitLabSelection, selectGitLabProjects } from "./selection.js";
|
|
11
|
+
/** @public */
|
|
12
|
+
export interface GitLabIssueWatcherConfig extends GitLabSelection {
|
|
13
|
+
/** @public */
|
|
14
|
+
profile: string;
|
|
15
|
+
/** @public */
|
|
16
|
+
pollInterval: string;
|
|
17
|
+
/** @public */
|
|
18
|
+
labels: {
|
|
19
|
+
/** @public */ trigger: string /** @public */;
|
|
20
|
+
/** @public */
|
|
21
|
+
done: string;
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
/** @public */
|
|
25
|
+
export interface GitLabIssueWatcherEvent {
|
|
26
|
+
/** @public */
|
|
27
|
+
profile: string;
|
|
28
|
+
/** @public */
|
|
29
|
+
repository: GitLabProject;
|
|
30
|
+
/** @public */
|
|
31
|
+
issue: GitLabIssue;
|
|
32
|
+
/** @public */
|
|
33
|
+
labels: {
|
|
34
|
+
/** @public */ trigger: string /** @public */;
|
|
35
|
+
/** @public */
|
|
36
|
+
done: string;
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
/** @public */
|
|
40
|
+
export const gitlabIssueExternalId = (origin: string, projectId: number, iid: number) =>
|
|
41
|
+
`gitlab:${origin}:${projectId}#${iid}`;
|
|
42
|
+
/** @public */
|
|
43
|
+
export const gitlabIssueWatcherSource = defineProcessWatcherSource<
|
|
44
|
+
GitLabIssueWatcherConfig,
|
|
45
|
+
GitLabIssueWatcherEvent
|
|
46
|
+
>({
|
|
47
|
+
id: "@leitwerk-dev/gitlab.issue",
|
|
48
|
+
label: "GitLab issue",
|
|
49
|
+
parseConfig(raw) {
|
|
50
|
+
const c = asUnknownRecord(raw);
|
|
51
|
+
if (!c || typeof c.enabled !== "boolean") throw new Error("GitLab watcher requires enabled");
|
|
52
|
+
const text = (value: unknown, name: string) => {
|
|
53
|
+
if (typeof value !== "string" || !value.trim())
|
|
54
|
+
throw new Error(`GitLab watcher requires ${name}`);
|
|
55
|
+
return value.trim();
|
|
56
|
+
};
|
|
57
|
+
const labels = asUnknownRecord(c.labels) ?? {};
|
|
58
|
+
const trigger = text(labels.trigger, "labels.trigger");
|
|
59
|
+
const done = text(labels.done, "labels.done");
|
|
60
|
+
if (trigger === done || trigger.includes(",") || done.includes(","))
|
|
61
|
+
throw new Error("GitLab trigger/done labels must be distinct and contain no comma");
|
|
62
|
+
const pollInterval = text(c.poll_interval ?? "30s", "poll_interval");
|
|
63
|
+
if (parseDurationMs(pollInterval, -1) <= 0) throw new Error("Invalid GitLab polling interval");
|
|
64
|
+
return {
|
|
65
|
+
enabled: c.enabled,
|
|
66
|
+
config: {
|
|
67
|
+
...parseGitLabSelection(c),
|
|
68
|
+
profile: text(c.profile, "profile"),
|
|
69
|
+
pollInterval,
|
|
70
|
+
labels: { trigger, done },
|
|
71
|
+
},
|
|
72
|
+
launchModelConfig: parseProcessWatcherLaunchModelConfig(c.launch),
|
|
73
|
+
};
|
|
74
|
+
},
|
|
75
|
+
presentConfig(c) {
|
|
76
|
+
return {
|
|
77
|
+
targetSummary: `Profile ${c.profile} · trigger ${c.labels.trigger}`,
|
|
78
|
+
details: [
|
|
79
|
+
{ label: "Projects", value: c.projects.include.join(", ") || "selected groups" },
|
|
80
|
+
{ label: "Groups", value: c.groups.include.join(", ") || "none" },
|
|
81
|
+
],
|
|
82
|
+
};
|
|
83
|
+
},
|
|
84
|
+
});
|
|
85
|
+
/** @public */
|
|
86
|
+
export function createGitLabIssueDiscovery(
|
|
87
|
+
deps: CoreServerSetupDeps,
|
|
88
|
+
integration: GitLabIntegration,
|
|
89
|
+
now: () => number,
|
|
90
|
+
) {
|
|
91
|
+
const due = new Map<string, number>();
|
|
92
|
+
return async (result: ReturnType<typeof emptyPollResult>) => {
|
|
93
|
+
for (const watcher of deps.processWatchers?.listBySource(gitlabIssueWatcherSource) ?? []) {
|
|
94
|
+
if (!watcher.enabled) continue;
|
|
95
|
+
const key = `${watcher.processId}:${watcher.watcherId}`;
|
|
96
|
+
const c = watcher.config;
|
|
97
|
+
const eligible = (issue: GitLabIssue) =>
|
|
98
|
+
issue.state === "opened" &&
|
|
99
|
+
issue.labels.includes(c.labels.trigger) &&
|
|
100
|
+
!issue.labels.includes(c.labels.done);
|
|
101
|
+
if ((due.get(key) ?? 0) > now()) continue;
|
|
102
|
+
due.set(key, now() + parseDurationMs(c.pollInterval, 30000));
|
|
103
|
+
const client = integration.client(c.profile);
|
|
104
|
+
for (const repository of await selectGitLabProjects(client, c))
|
|
105
|
+
for (const issue of await client.listIssues(repository.id)) {
|
|
106
|
+
if (!eligible(issue)) continue;
|
|
107
|
+
const id = gitlabIssueExternalId(client.baseUrl, repository.id, issue.iid);
|
|
108
|
+
if (deps.processes.listAll().some((p) => p.externalId === id)) {
|
|
109
|
+
result.skipped.push(id);
|
|
110
|
+
continue;
|
|
111
|
+
}
|
|
112
|
+
const guarded = {
|
|
113
|
+
...watcher,
|
|
114
|
+
async resolveLaunchAttempt(...args: Parameters<typeof watcher.resolveLaunchAttempt>) {
|
|
115
|
+
const attempt = await watcher.resolveLaunchAttempt(...args);
|
|
116
|
+
if (!attempt) return null;
|
|
117
|
+
return {
|
|
118
|
+
...attempt,
|
|
119
|
+
preparationChecks: [
|
|
120
|
+
...attempt.preparationChecks,
|
|
121
|
+
{
|
|
122
|
+
id: "gitlab-issue-eligibility",
|
|
123
|
+
label: "Recheck GitLab source issue",
|
|
124
|
+
async run() {
|
|
125
|
+
const fresh = await client.getIssue(repository.id, issue.iid);
|
|
126
|
+
if (!eligible(fresh))
|
|
127
|
+
throw new Error("GitLab source issue eligibility changed before launch");
|
|
128
|
+
},
|
|
129
|
+
},
|
|
130
|
+
],
|
|
131
|
+
};
|
|
132
|
+
},
|
|
133
|
+
};
|
|
134
|
+
const launched = await deps.launchRuns.startWatcher(
|
|
135
|
+
guarded,
|
|
136
|
+
{ profile: c.profile, repository, issue, labels: c.labels },
|
|
137
|
+
{ idempotencyKey: id },
|
|
138
|
+
);
|
|
139
|
+
if (launched.error) result.errors.push(`${id}:launch_failed`);
|
|
140
|
+
else if (launched.process) result.created.push(id);
|
|
141
|
+
else result.skipped.push(id);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
};
|
|
145
|
+
}
|
package/src/preflight.ts
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { execFile } from "node:child_process";
|
|
2
|
+
import { mkdtemp, rm, writeFile } from "node:fs/promises";
|
|
3
|
+
import { tmpdir } from "node:os";
|
|
4
|
+
import { join } from "node:path";
|
|
5
|
+
import { promisify } from "node:util";
|
|
6
|
+
import { repositoryHttpsUrl } from "@leitwerk-dev/process-sdk";
|
|
7
|
+
|
|
8
|
+
const exec = promisify(execFile);
|
|
9
|
+
/** Credentials stay in the child environment and never appear in URLs or error output. */
|
|
10
|
+
export async function preflightGitLabRepository(input: {
|
|
11
|
+
url: string;
|
|
12
|
+
origin: string;
|
|
13
|
+
token: string;
|
|
14
|
+
baseBranch: string;
|
|
15
|
+
workBranch: string;
|
|
16
|
+
signal?: AbortSignal;
|
|
17
|
+
}) {
|
|
18
|
+
const url = repositoryHttpsUrl(input.url);
|
|
19
|
+
if (url.origin !== input.origin)
|
|
20
|
+
throw new Error("GitLab repository origin does not match its profile");
|
|
21
|
+
const directory = await mkdtemp(join(tmpdir(), "leitwerk-gitlab-preflight-"));
|
|
22
|
+
try {
|
|
23
|
+
const askpass = join(directory, "askpass");
|
|
24
|
+
await writeFile(
|
|
25
|
+
askpass,
|
|
26
|
+
'#!/bin/sh\ncase "$1" in *Username*) printf "%s\\n" oauth2;; *) printf "%s\\n" "$LEITWERK_GITLAB_PREFLIGHT_TOKEN";; esac\n',
|
|
27
|
+
{ mode: 0o700 },
|
|
28
|
+
);
|
|
29
|
+
const env = {
|
|
30
|
+
...process.env,
|
|
31
|
+
GIT_CONFIG_NOSYSTEM: "1",
|
|
32
|
+
GIT_CONFIG_GLOBAL: "/dev/null",
|
|
33
|
+
GIT_TERMINAL_PROMPT: "0",
|
|
34
|
+
GIT_ASKPASS: askpass,
|
|
35
|
+
LEITWERK_GITLAB_PREFLIGHT_TOKEN: input.token,
|
|
36
|
+
};
|
|
37
|
+
const git = (args: string[]) =>
|
|
38
|
+
exec("git", ["-c", "credential.helper=", "-c", "http.followRedirects=false", ...args], {
|
|
39
|
+
env,
|
|
40
|
+
signal: input.signal,
|
|
41
|
+
});
|
|
42
|
+
await git(["check-ref-format", `refs/heads/${input.baseBranch}`]);
|
|
43
|
+
await git(["check-ref-format", `refs/heads/${input.workBranch}`]);
|
|
44
|
+
await git(["ls-remote", "--exit-code", input.url, `refs/heads/${input.baseBranch}`]);
|
|
45
|
+
await git(["init", "--quiet", directory]);
|
|
46
|
+
await git([
|
|
47
|
+
"-C",
|
|
48
|
+
directory,
|
|
49
|
+
"fetch",
|
|
50
|
+
"--quiet",
|
|
51
|
+
"--depth=1",
|
|
52
|
+
input.url,
|
|
53
|
+
`refs/heads/${input.baseBranch}`,
|
|
54
|
+
]);
|
|
55
|
+
await git([
|
|
56
|
+
"-C",
|
|
57
|
+
directory,
|
|
58
|
+
"push",
|
|
59
|
+
"--dry-run",
|
|
60
|
+
input.url,
|
|
61
|
+
`FETCH_HEAD:refs/heads/${input.workBranch}`,
|
|
62
|
+
]);
|
|
63
|
+
} catch {
|
|
64
|
+
throw new Error(
|
|
65
|
+
"GitLab HTTPS read/write admission failed; check repository access and write_repository scope",
|
|
66
|
+
);
|
|
67
|
+
} finally {
|
|
68
|
+
await rm(directory, { recursive: true, force: true });
|
|
69
|
+
}
|
|
70
|
+
}
|
package/src/setup.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { coreHostCapabilities, type ServerExtensionAPI } from "@leitwerk-dev/process-sdk";
|
|
2
2
|
import { type GitLabIntegration, gitlabIntegration } from "./capability.js";
|
|
3
|
+
import { registerGitLabDeliveryTools } from "./delivery-tools.js";
|
|
3
4
|
import { createGitLabProvider } from "./external.js";
|
|
4
5
|
import { registerGitLabTools } from "./tools.js";
|
|
5
6
|
|
|
@@ -16,5 +17,6 @@ export function setupGitLabIntegration(
|
|
|
16
17
|
const deps = api.get(coreHostCapabilities.serverSetup);
|
|
17
18
|
if (!deps || Array.isArray(deps)) return;
|
|
18
19
|
registerGitLabTools(api, integration);
|
|
20
|
+
registerGitLabDeliveryTools(api, integration, deps.projects);
|
|
19
21
|
return createGitLabProvider(deps, integration, options);
|
|
20
22
|
}
|
package/src/testing.ts
CHANGED
|
@@ -5,6 +5,7 @@ import type {
|
|
|
5
5
|
GitLabClientLike,
|
|
6
6
|
GitLabDiff,
|
|
7
7
|
GitLabFeedback,
|
|
8
|
+
GitLabIssue,
|
|
8
9
|
GitLabMergeRequest,
|
|
9
10
|
GitLabNote,
|
|
10
11
|
GitLabNoteReaction,
|
|
@@ -12,10 +13,17 @@ import type {
|
|
|
12
13
|
GitLabProject,
|
|
13
14
|
} from "./client.js";
|
|
14
15
|
|
|
16
|
+
export { GITLAB_MR_KIND } from "./external.js";
|
|
15
17
|
export { setupGitLabIntegration } from "./setup.js";
|
|
16
18
|
|
|
17
19
|
/** @public */
|
|
18
20
|
interface LocalState {
|
|
21
|
+
/** @public */
|
|
22
|
+
issues?: GitLabIssue[];
|
|
23
|
+
/** @public */
|
|
24
|
+
issueNotes?: Record<string, GitLabNote[]>;
|
|
25
|
+
/** @public */
|
|
26
|
+
labels?: Record<string, string[]>;
|
|
19
27
|
/** @internal */
|
|
20
28
|
projects: GitLabProject[];
|
|
21
29
|
/** @internal */
|
|
@@ -33,13 +41,16 @@ interface LocalState {
|
|
|
33
41
|
/** @public */
|
|
34
42
|
reactions?: Record<string, GitLabNoteReaction[]>;
|
|
35
43
|
}
|
|
36
|
-
/** Persistent GitLab test boundary. Repository URLs use local Git's file transport.
|
|
44
|
+
/** Persistent GitLab test boundary. Repository URLs use local Git's file transport. */
|
|
45
|
+
/** @public */
|
|
37
46
|
export class LocalGitLabAdapter {
|
|
38
47
|
/** @public */
|
|
39
48
|
state: LocalState;
|
|
40
49
|
/** @public */
|
|
41
50
|
loseNextCommentResponse = false;
|
|
42
51
|
/** @public */
|
|
52
|
+
loseNextMergeRequestResponse = false;
|
|
53
|
+
/** @public */
|
|
43
54
|
loseNextReplyResponse = false;
|
|
44
55
|
/** @public */
|
|
45
56
|
loseNextReactionResponse = false;
|
|
@@ -136,6 +147,67 @@ export class LocalGitLabAdapter {
|
|
|
136
147
|
return value;
|
|
137
148
|
}
|
|
138
149
|
/** @public */
|
|
150
|
+
createIssue(projectId: number, title: string, labels: string[] = ["use-leitwerk"]): GitLabIssue {
|
|
151
|
+
this.state.issues ??= [];
|
|
152
|
+
const project = this.project(projectId);
|
|
153
|
+
const iid = this.state.issues.filter((i) => i.project_id === projectId).length + 1;
|
|
154
|
+
const issue: GitLabIssue = {
|
|
155
|
+
id: this.state.issues.length + 1,
|
|
156
|
+
iid,
|
|
157
|
+
project_id: projectId,
|
|
158
|
+
title,
|
|
159
|
+
description: "",
|
|
160
|
+
web_url: `${project.web_url}/-/issues/${iid}`,
|
|
161
|
+
state: "opened",
|
|
162
|
+
labels,
|
|
163
|
+
};
|
|
164
|
+
this.state.issues.push(issue);
|
|
165
|
+
this.save();
|
|
166
|
+
return issue;
|
|
167
|
+
}
|
|
168
|
+
/** @public */
|
|
169
|
+
merge(request: GitLabMergeRequest) {
|
|
170
|
+
const base = this.git(request.project_id, ["rev-parse", request.target_branch]);
|
|
171
|
+
const head = this.git(request.project_id, ["rev-parse", request.source_branch]);
|
|
172
|
+
const tree = this.git(request.project_id, ["merge-tree", "--write-tree", base, head]);
|
|
173
|
+
const commit = this.git(request.project_id, [
|
|
174
|
+
"-c",
|
|
175
|
+
"user.name=Leitwerk Bot",
|
|
176
|
+
"-c",
|
|
177
|
+
"user.email=bot@gitlab.test",
|
|
178
|
+
"commit-tree",
|
|
179
|
+
tree,
|
|
180
|
+
"-p",
|
|
181
|
+
base,
|
|
182
|
+
"-p",
|
|
183
|
+
head,
|
|
184
|
+
"-m",
|
|
185
|
+
request.title,
|
|
186
|
+
]);
|
|
187
|
+
this.git(request.project_id, [
|
|
188
|
+
"update-ref",
|
|
189
|
+
`refs/heads/${request.target_branch}`,
|
|
190
|
+
commit,
|
|
191
|
+
base,
|
|
192
|
+
]);
|
|
193
|
+
request.state = "merged";
|
|
194
|
+
request.merge_commit_sha = commit;
|
|
195
|
+
this.save();
|
|
196
|
+
}
|
|
197
|
+
private conflicts(request: GitLabMergeRequest): boolean {
|
|
198
|
+
try {
|
|
199
|
+
this.git(request.project_id, [
|
|
200
|
+
"merge-tree",
|
|
201
|
+
"--write-tree",
|
|
202
|
+
request.target_branch,
|
|
203
|
+
request.source_branch,
|
|
204
|
+
]);
|
|
205
|
+
return false;
|
|
206
|
+
} catch {
|
|
207
|
+
return true;
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
/** @public */
|
|
139
211
|
client(): GitLabClientLike {
|
|
140
212
|
const mr = (id: number, iid: number) => {
|
|
141
213
|
const m = this.state.mrs.find((m) => m.project_id === id && m.iid === iid);
|
|
@@ -149,6 +221,72 @@ export class LocalGitLabAdapter {
|
|
|
149
221
|
});
|
|
150
222
|
return {
|
|
151
223
|
baseUrl: this.baseUrl,
|
|
224
|
+
listIssues: async (id) =>
|
|
225
|
+
structuredClone(
|
|
226
|
+
(this.state.issues ?? []).filter((i) => i.project_id === id && i.state === "opened"),
|
|
227
|
+
),
|
|
228
|
+
getIssue: async (id, iid) => {
|
|
229
|
+
const issue = (this.state.issues ?? []).find((i) => i.project_id === id && i.iid === iid);
|
|
230
|
+
if (!issue) throw new Error("Unknown local GitLab issue");
|
|
231
|
+
return structuredClone(issue);
|
|
232
|
+
},
|
|
233
|
+
updateIssue: async (id, iid, patch) => {
|
|
234
|
+
const issue = (this.state.issues ?? []).find((i) => i.project_id === id && i.iid === iid);
|
|
235
|
+
if (!issue) throw new Error("Unknown local GitLab issue");
|
|
236
|
+
if (patch.labels !== undefined) issue.labels = patch.labels ? patch.labels.split(",") : [];
|
|
237
|
+
if (patch.state_event === "close") issue.state = "closed";
|
|
238
|
+
this.save();
|
|
239
|
+
return structuredClone(issue);
|
|
240
|
+
},
|
|
241
|
+
listIssueNotes: async (id, iid) =>
|
|
242
|
+
structuredClone(this.state.issueNotes?.[`${id}:${iid}`] ?? []),
|
|
243
|
+
addIssueNote: async (id, iid, body) => {
|
|
244
|
+
this.state.issueNotes ??= {};
|
|
245
|
+
this.state.issueNotes[`${id}:${iid}`] ??= [];
|
|
246
|
+
const notes = this.state.issueNotes[`${id}:${iid}`];
|
|
247
|
+
return this.append(notes, { id: notes.length + 1, body }, "Comment");
|
|
248
|
+
},
|
|
249
|
+
listLabels: async (id) => (this.state.labels?.[id] ?? []).map((name) => ({ name })),
|
|
250
|
+
createLabel: async (id, name) => {
|
|
251
|
+
this.state.labels ??= {};
|
|
252
|
+
this.state.labels[id] ??= [];
|
|
253
|
+
const labels = this.state.labels[id];
|
|
254
|
+
if (!labels.includes(name)) labels.push(name);
|
|
255
|
+
this.save();
|
|
256
|
+
return { name };
|
|
257
|
+
},
|
|
258
|
+
listBranchMergeRequests: async (id, source, target) =>
|
|
259
|
+
structuredClone(
|
|
260
|
+
this.state.mrs.filter(
|
|
261
|
+
(m) => m.project_id === id && m.source_branch === source && m.target_branch === target,
|
|
262
|
+
),
|
|
263
|
+
),
|
|
264
|
+
createMergeRequest: async (id, input) => {
|
|
265
|
+
const request = this.openMr(this.project(id), input.source_branch);
|
|
266
|
+
Object.assign(request, {
|
|
267
|
+
title: input.title,
|
|
268
|
+
description: input.description,
|
|
269
|
+
target_branch: input.target_branch,
|
|
270
|
+
labels: [],
|
|
271
|
+
});
|
|
272
|
+
this.save();
|
|
273
|
+
if (this.loseNextMergeRequestResponse) {
|
|
274
|
+
this.loseNextMergeRequestResponse = false;
|
|
275
|
+
throw new Error("Response lost after merge request creation");
|
|
276
|
+
}
|
|
277
|
+
return structuredClone(request);
|
|
278
|
+
},
|
|
279
|
+
preflightRepository: async (id, baseBranch, workBranch) => {
|
|
280
|
+
this.git(id, ["rev-parse", `refs/heads/${baseBranch}`]);
|
|
281
|
+
this.git(id, ["check-ref-format", `refs/heads/${workBranch}`]);
|
|
282
|
+
this.git(id, [
|
|
283
|
+
"push",
|
|
284
|
+
"--dry-run",
|
|
285
|
+
this.project(id).http_url_to_repo,
|
|
286
|
+
`${baseBranch}:refs/heads/${workBranch}`,
|
|
287
|
+
]);
|
|
288
|
+
},
|
|
289
|
+
|
|
152
290
|
getProject: async (id) => structuredClone(this.project(id)),
|
|
153
291
|
listProjects: async () => structuredClone(this.state.projects),
|
|
154
292
|
getGroup: async (id) => ({ id: 1, full_path: String(id) }),
|
|
@@ -158,6 +296,7 @@ export class LocalGitLabAdapter {
|
|
|
158
296
|
),
|
|
159
297
|
getMergeRequest: async (id, iid) => ({
|
|
160
298
|
...structuredClone(mr(id, iid)),
|
|
299
|
+
has_conflicts: this.conflicts(mr(id, iid)),
|
|
161
300
|
sha: commit(mr(id, iid).source_project_id, mr(id, iid).source_branch).id,
|
|
162
301
|
}),
|
|
163
302
|
listMergeRequests: async (id, label) =>
|