@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/external.ts
CHANGED
|
@@ -9,61 +9,93 @@ import { emptyPollResult, parseDurationMs } from "@leitwerk-dev/watcher-utils";
|
|
|
9
9
|
import type { GitLabIntegration } from "./capability.js";
|
|
10
10
|
import { type GitLabFeedback, type GitLabObservation, observeMergeRequest } from "./client.js";
|
|
11
11
|
import { createGitLabIssueDiscovery } from "./issue-watcher.js";
|
|
12
|
+
|
|
12
13
|
/** @public */
|
|
13
14
|
export interface GitLabDeliveryObservation extends GitLabObservation {
|
|
15
|
+
/** @public */
|
|
16
|
+
projectKey?: string;
|
|
17
|
+
/** Target ancestry suppresses stale conflict/rebase reports after publication. @public */
|
|
18
|
+
targetIntegrated?: boolean;
|
|
19
|
+
|
|
14
20
|
/** @public */
|
|
15
21
|
observationKey?: string;
|
|
22
|
+
|
|
16
23
|
/** @public */
|
|
17
24
|
feedback?: GitLabFeedback[];
|
|
25
|
+
|
|
18
26
|
/** @public */
|
|
19
27
|
conflict?: ConflictEvidence;
|
|
20
28
|
}
|
|
29
|
+
|
|
21
30
|
/** @public */
|
|
22
31
|
export const GITLAB_ISSUE_CANCELLED_KIND = "@leitwerk-dev/gitlab.issue-cancelled";
|
|
32
|
+
|
|
23
33
|
/** @public */
|
|
24
34
|
export interface GitLabIssueCancelledConfig {
|
|
35
|
+
/** Pinned provider installation. @public */
|
|
36
|
+
origin?: string;
|
|
25
37
|
/** @public */
|
|
26
38
|
profile: string;
|
|
39
|
+
|
|
27
40
|
/** @public */
|
|
28
41
|
projectId: number;
|
|
42
|
+
|
|
29
43
|
/** @public */
|
|
30
44
|
issueIid: number;
|
|
45
|
+
|
|
31
46
|
/** @public */
|
|
32
47
|
iid: number;
|
|
48
|
+
|
|
33
49
|
/** @public */
|
|
34
50
|
triggerLabel: string;
|
|
51
|
+
|
|
35
52
|
/** @public */
|
|
36
53
|
pollInterval?: string;
|
|
37
54
|
}
|
|
55
|
+
|
|
38
56
|
/** @internal */
|
|
39
57
|
export const GITLAB_MR_KIND = "@leitwerk-dev/gitlab.merge-request";
|
|
58
|
+
|
|
40
59
|
/** @public */
|
|
41
60
|
export interface GitLabSourceConfig {
|
|
61
|
+
/** Pinned provider installation. @public */
|
|
62
|
+
origin?: string;
|
|
42
63
|
/** @public */
|
|
43
64
|
profile: string;
|
|
65
|
+
|
|
44
66
|
/** @public */
|
|
45
67
|
projectId: number;
|
|
68
|
+
|
|
46
69
|
/** @public */
|
|
47
70
|
iid: number;
|
|
71
|
+
|
|
48
72
|
/** @public */
|
|
49
73
|
pollInterval?: string;
|
|
74
|
+
|
|
50
75
|
/** @public */
|
|
51
76
|
afterKey?: string;
|
|
52
77
|
/** Optional timer also wakes retry work when GitLab facts have not changed. */
|
|
78
|
+
|
|
53
79
|
/** @public */
|
|
54
80
|
wakeAt?: number;
|
|
81
|
+
|
|
55
82
|
/** @public */
|
|
56
83
|
delivery?: {
|
|
57
84
|
/** @public */
|
|
58
85
|
headSha: string;
|
|
86
|
+
|
|
59
87
|
/** @public */
|
|
60
88
|
owner: string;
|
|
89
|
+
|
|
61
90
|
/** @public */
|
|
62
91
|
repo: string;
|
|
92
|
+
|
|
63
93
|
/** @public */
|
|
64
94
|
headBranch: string;
|
|
95
|
+
|
|
65
96
|
/** @public */
|
|
66
97
|
baseBranch: string;
|
|
98
|
+
|
|
67
99
|
/** @public */
|
|
68
100
|
lastConflictKey?: string | null;
|
|
69
101
|
};
|
|
@@ -71,13 +103,16 @@ export interface GitLabSourceConfig {
|
|
|
71
103
|
feedback?: {
|
|
72
104
|
/** @public */
|
|
73
105
|
afterId: number;
|
|
106
|
+
|
|
74
107
|
/** @public */
|
|
75
108
|
since?: string;
|
|
109
|
+
|
|
76
110
|
/** @public */
|
|
77
111
|
quietPeriodMs: number;
|
|
78
112
|
};
|
|
79
113
|
}
|
|
80
114
|
/** A trailing quiet period survives restarts because it uses the newest unseen note's timestamp. */
|
|
115
|
+
|
|
81
116
|
/** @public */
|
|
82
117
|
export function pendingGitLabFeedback(
|
|
83
118
|
items: GitLabFeedback[],
|
|
@@ -87,6 +122,7 @@ export function pendingGitLabFeedback(
|
|
|
87
122
|
const start = since ? Date.parse(since) : 0;
|
|
88
123
|
return items.filter((item) => item.id > afterId && Date.parse(item.createdAt) >= start);
|
|
89
124
|
}
|
|
125
|
+
|
|
90
126
|
/** @public */
|
|
91
127
|
export function gitLabFeedbackReadyAt(
|
|
92
128
|
items: GitLabFeedback[],
|
|
@@ -96,6 +132,7 @@ export function gitLabFeedbackReadyAt(
|
|
|
96
132
|
? Math.max(...items.map((item) => Date.parse(item.createdAt))) + quietPeriodMs
|
|
97
133
|
: null;
|
|
98
134
|
}
|
|
135
|
+
|
|
99
136
|
/** @public */
|
|
100
137
|
export const observationKey = ({ mr, pipeline, targetHead }: GitLabObservation): string =>
|
|
101
138
|
JSON.stringify([
|
|
@@ -114,14 +151,40 @@ export const observationKey = ({ mr, pipeline, targetHead }: GitLabObservation):
|
|
|
114
151
|
mr.merge_status,
|
|
115
152
|
mr.detailed_merge_status,
|
|
116
153
|
]);
|
|
154
|
+
|
|
117
155
|
/** @public */
|
|
118
156
|
export const gitlabExternal = {
|
|
157
|
+
/** Observe coordinated repositories with the same revision and feedback policy. @public */
|
|
158
|
+
mergeRequests<P, S>(
|
|
159
|
+
resolve: (ctx: {
|
|
160
|
+
/** @internal */
|
|
161
|
+
params: P;
|
|
162
|
+
/** @internal */
|
|
163
|
+
state: S;
|
|
164
|
+
}) => {
|
|
165
|
+
/** @internal */
|
|
166
|
+
repositories: (GitLabSourceConfig & {
|
|
167
|
+
/** @internal */
|
|
168
|
+
projectKey: string;
|
|
169
|
+
})[];
|
|
170
|
+
},
|
|
171
|
+
): ExternalActionSource<P, S, GitLabDeliveryObservation> {
|
|
172
|
+
return {
|
|
173
|
+
kind: GITLAB_MR_KIND,
|
|
174
|
+
label: "GitLab merge requests",
|
|
175
|
+
config: {},
|
|
176
|
+
inputMode: "none",
|
|
177
|
+
resolve,
|
|
178
|
+
};
|
|
179
|
+
},
|
|
180
|
+
|
|
119
181
|
/** @public */
|
|
120
182
|
issueCancelled<P, S>(
|
|
121
183
|
/** @public */
|
|
122
184
|
resolve: (ctx: {
|
|
123
185
|
/** @public */
|
|
124
186
|
params: P;
|
|
187
|
+
|
|
125
188
|
/** @public */
|
|
126
189
|
state: S;
|
|
127
190
|
}) => GitLabIssueCancelledConfig,
|
|
@@ -134,12 +197,14 @@ export const gitlabExternal = {
|
|
|
134
197
|
resolve,
|
|
135
198
|
};
|
|
136
199
|
},
|
|
200
|
+
|
|
137
201
|
/** @public */
|
|
138
202
|
mergeRequest<P, S>(
|
|
139
203
|
/** @public */
|
|
140
204
|
resolve: (ctx: {
|
|
141
205
|
/** @public */
|
|
142
206
|
params: P;
|
|
207
|
+
|
|
143
208
|
/** @public */
|
|
144
209
|
state: S;
|
|
145
210
|
}) => GitLabSourceConfig,
|
|
@@ -154,13 +219,15 @@ export const gitlabExternal = {
|
|
|
154
219
|
};
|
|
155
220
|
},
|
|
156
221
|
};
|
|
222
|
+
|
|
157
223
|
/** @internal */
|
|
158
224
|
export function createGitLabProvider(
|
|
159
225
|
deps: CoreServerSetupDeps,
|
|
160
226
|
integration: GitLabIntegration,
|
|
161
227
|
/** @public */
|
|
162
228
|
options: {
|
|
163
|
-
/** @internal */
|
|
229
|
+
/** @internal */
|
|
230
|
+
now?: () => number;
|
|
164
231
|
} = {},
|
|
165
232
|
) {
|
|
166
233
|
const schedule = new Map<string, { at: number; failures: number }>();
|
|
@@ -178,24 +245,14 @@ export function createGitLabProvider(
|
|
|
178
245
|
} catch (error) {
|
|
179
246
|
result.errors.push(error instanceof Error ? error.message : "GitLab discovery failed");
|
|
180
247
|
}
|
|
181
|
-
const
|
|
248
|
+
const report = createExternalSourcePollReporter(deps.externalSources, result, {
|
|
182
249
|
forwardGeneration: true,
|
|
250
|
+
currentKinds: [GITLAB_MR_KIND, GITLAB_ISSUE_CANCELLED_KIND],
|
|
183
251
|
});
|
|
184
|
-
const
|
|
185
|
-
|
|
186
|
-
|
|
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
|
-
};
|
|
197
|
-
await report.poll(GITLAB_MR_KIND, async (armed) => {
|
|
198
|
-
const c = armed.resolved as unknown as GitLabSourceConfig;
|
|
252
|
+
const pollRepository = async (
|
|
253
|
+
armed: ExternalSourceArmingLike,
|
|
254
|
+
c: GitLabSourceConfig & { projectKey?: string },
|
|
255
|
+
) => {
|
|
199
256
|
if (
|
|
200
257
|
!c ||
|
|
201
258
|
typeof c.profile !== "string" ||
|
|
@@ -203,10 +260,12 @@ export function createGitLabProvider(
|
|
|
203
260
|
!Number.isInteger(c.iid)
|
|
204
261
|
)
|
|
205
262
|
throw new Error("Invalid GitLab external source");
|
|
206
|
-
const key = `${armed.instanceId}:${armed.id}:${armed.generation ?? ""}`;
|
|
263
|
+
const key = `${armed.instanceId}:${armed.id}:${armed.generation ?? ""}:${c.projectKey ?? ""}`;
|
|
207
264
|
const previous = schedule.get(key);
|
|
208
265
|
if (previous && previous.at > now()) return;
|
|
209
266
|
try {
|
|
267
|
+
if (c.origin && integration.client(c.profile).baseUrl !== c.origin)
|
|
268
|
+
throw new Error("GitLab observation installation changed");
|
|
210
269
|
const observation = await observeMergeRequest(
|
|
211
270
|
integration.client(c.profile),
|
|
212
271
|
c.projectId,
|
|
@@ -280,6 +339,7 @@ export function createGitLabProvider(
|
|
|
280
339
|
armed,
|
|
281
340
|
{
|
|
282
341
|
...observation,
|
|
342
|
+
...(c.projectKey ? { projectKey: c.projectKey } : {}),
|
|
283
343
|
...(c.delivery
|
|
284
344
|
? {
|
|
285
345
|
observationKey: observedKey,
|
|
@@ -288,7 +348,7 @@ export function createGitLabProvider(
|
|
|
288
348
|
}
|
|
289
349
|
: {}),
|
|
290
350
|
},
|
|
291
|
-
`${observedKey}:${c.wakeAt ?? ""}:${feedbackReady ? feedback.map((item) => item.id).join(",") : ""}`,
|
|
351
|
+
`${c.projectKey ?? ""}:${observedKey}:${c.wakeAt ?? ""}:${feedbackReady ? feedback.map((item) => item.id).join(",") : ""}`,
|
|
292
352
|
);
|
|
293
353
|
schedule.set(key, {
|
|
294
354
|
at: now() + parseDurationMs(c.pollInterval ?? "30s", 30000),
|
|
@@ -305,12 +365,23 @@ export function createGitLabProvider(
|
|
|
305
365
|
});
|
|
306
366
|
throw error;
|
|
307
367
|
}
|
|
368
|
+
};
|
|
369
|
+
await report.poll(GITLAB_MR_KIND, async (armed) => {
|
|
370
|
+
const config = armed.resolved as unknown as GitLabSourceConfig & {
|
|
371
|
+
repositories?: (GitLabSourceConfig & { projectKey: string })[];
|
|
372
|
+
};
|
|
373
|
+
for (const source of config.repositories ?? [config]) {
|
|
374
|
+
if (!report.isCurrent(GITLAB_MR_KIND, armed)) break;
|
|
375
|
+
await pollRepository(armed, source);
|
|
376
|
+
}
|
|
308
377
|
});
|
|
309
378
|
await report.poll(GITLAB_ISSUE_CANCELLED_KIND, async (armed) => {
|
|
310
379
|
const c = armed.resolved as unknown as GitLabIssueCancelledConfig;
|
|
311
380
|
const key = `${armed.instanceId}:${armed.id}:${armed.generation ?? ""}`;
|
|
312
381
|
if ((schedule.get(key)?.at ?? 0) > now()) return;
|
|
313
382
|
const client = integration.client(c.profile);
|
|
383
|
+
if (c.origin && client.baseUrl !== c.origin)
|
|
384
|
+
throw new Error("GitLab source issue installation changed");
|
|
314
385
|
const issue = await client.getIssue(c.projectId, c.issueIid);
|
|
315
386
|
if (issue.state !== "opened" || !issue.labels.includes(c.triggerLabel)) {
|
|
316
387
|
const mr = await client.getMergeRequest(c.projectId, c.iid);
|
package/src/index.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { LeitwerkExtensionModule } from "@leitwerk-dev/process-sdk";
|
|
2
|
-
import { coreHostCapabilities } from "@leitwerk-dev/process-sdk";
|
|
2
|
+
import { coreHostCapabilities, parseTicketCreationConfig } from "@leitwerk-dev/process-sdk";
|
|
3
3
|
import { GitLabClient, parseGitLabProfiles } from "./client.js";
|
|
4
4
|
import { setupGitLabIntegration } from "./setup.js";
|
|
5
5
|
|
|
@@ -16,14 +16,18 @@ const extension: LeitwerkExtensionModule = {
|
|
|
16
16
|
setupServer(api, config) {
|
|
17
17
|
const profiles = parseGitLabProfiles(config);
|
|
18
18
|
const clients = new Map([...profiles].map(([id, profile]) => [id, new GitLabClient(profile)]));
|
|
19
|
-
setupGitLabIntegration(
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
19
|
+
setupGitLabIntegration(
|
|
20
|
+
api,
|
|
21
|
+
{
|
|
22
|
+
profiles: () => [...profiles.keys()],
|
|
23
|
+
client: (profile) => {
|
|
24
|
+
const client = clients.get(profile);
|
|
25
|
+
if (!client) throw new Error(`Unknown GitLab profile '${profile}'`);
|
|
26
|
+
return client;
|
|
27
|
+
},
|
|
25
28
|
},
|
|
26
|
-
|
|
29
|
+
{ ticketCreation: parseTicketCreationConfig(config, "GitLab") },
|
|
30
|
+
);
|
|
27
31
|
const deps = api.get(coreHostCapabilities.serverSetup);
|
|
28
32
|
if (!deps || Array.isArray(deps)) return;
|
|
29
33
|
deps.repositoryCredentials.register({
|
|
@@ -44,15 +48,17 @@ export {
|
|
|
44
48
|
gitlabRepositoryCredentials,
|
|
45
49
|
} from "./capability.js";
|
|
46
50
|
export type {
|
|
51
|
+
GitLabClientLike,
|
|
47
52
|
GitLabDiff,
|
|
48
53
|
GitLabFeedback,
|
|
49
54
|
GitLabIdentity,
|
|
50
55
|
GitLabJob,
|
|
56
|
+
GitLabLabelEvent,
|
|
51
57
|
GitLabMergeRequest,
|
|
52
58
|
GitLabObservation,
|
|
53
59
|
GitLabProject,
|
|
54
60
|
} from "./client.js";
|
|
55
|
-
export { observeMergeRequest } from "./client.js";
|
|
61
|
+
export { GitLabError, observeMergeRequest } from "./client.js";
|
|
56
62
|
export type { GitLabDeliveryObservation } from "./external.js";
|
|
57
63
|
export {
|
|
58
64
|
gitLabFeedbackReadyAt,
|
|
@@ -62,9 +68,50 @@ export {
|
|
|
62
68
|
} from "./external.js";
|
|
63
69
|
export type { GitLabIssueWatcherEvent } from "./issue-watcher.js";
|
|
64
70
|
export { gitlabIssueExternalId, gitlabIssueWatcherSource } from "./issue-watcher.js";
|
|
71
|
+
export { resolveGitLabLaunchProject } from "./launch.js";
|
|
72
|
+
export {
|
|
73
|
+
GITLAB_MAINTAINED_KIND,
|
|
74
|
+
type GitLabMaintainedBinding,
|
|
75
|
+
type GitLabMaintainedProcess,
|
|
76
|
+
type GitLabMaintenance,
|
|
77
|
+
type GitLabMaintenanceContext,
|
|
78
|
+
type GitLabMaintenanceDecision,
|
|
79
|
+
type GitLabMaintenanceEvent,
|
|
80
|
+
type GitLabMaintenanceMessage,
|
|
81
|
+
type GitLabMaintenanceRecord,
|
|
82
|
+
type GitLabMaintenanceRetry,
|
|
83
|
+
type GitLabMaintenanceSettings,
|
|
84
|
+
gitlabMaintenance,
|
|
85
|
+
gitlabMaintenanceSource,
|
|
86
|
+
registerGitLabMaintainedProcess,
|
|
87
|
+
} from "./maintenance.js";
|
|
88
|
+
export {
|
|
89
|
+
GITLAB_ACTIVE_LABEL,
|
|
90
|
+
GITLAB_DONE_LABEL,
|
|
91
|
+
type GitLabLabelState,
|
|
92
|
+
gitLabActivationStatus,
|
|
93
|
+
readGitLabLabels,
|
|
94
|
+
} from "./maintenance-labels.js";
|
|
95
|
+
export {
|
|
96
|
+
createGitLabPublicationAdapter,
|
|
97
|
+
type GitLabPublicationParams,
|
|
98
|
+
gitlabPublicationEvidenceForRequest,
|
|
99
|
+
gitlabPublicationRequest,
|
|
100
|
+
gitlabPublicationSource,
|
|
101
|
+
} from "./publication.js";
|
|
102
|
+
export {
|
|
103
|
+
createGitLabRepositoryCatalog,
|
|
104
|
+
type GitLabRepositoryCatalog,
|
|
105
|
+
} from "./repository-catalog.js";
|
|
65
106
|
export type { GitLabSelection } from "./selection.js";
|
|
66
107
|
export {
|
|
67
108
|
parseGitLabSelection,
|
|
68
109
|
selectGitLabProjects,
|
|
69
110
|
} from "./selection.js";
|
|
70
|
-
export {
|
|
111
|
+
export {
|
|
112
|
+
ensureGitLabComment,
|
|
113
|
+
ensureGitLabSeenReaction,
|
|
114
|
+
gitLabCommentMarker,
|
|
115
|
+
resolveGitLabBinding,
|
|
116
|
+
resolveGitLabRepositoryBinding,
|
|
117
|
+
} from "./tools.js";
|
package/src/issue-watcher.ts
CHANGED
|
@@ -12,6 +12,7 @@ import { type GitLabSelection, parseGitLabSelection, selectGitLabProjects } from
|
|
|
12
12
|
export interface GitLabIssueWatcherConfig extends GitLabSelection {
|
|
13
13
|
/** @public */
|
|
14
14
|
profile: string;
|
|
15
|
+
/** @public */ gitSshProfile?: string;
|
|
15
16
|
/** @public */
|
|
16
17
|
pollInterval: string;
|
|
17
18
|
/** @public */
|
|
@@ -25,6 +26,7 @@ export interface GitLabIssueWatcherConfig extends GitLabSelection {
|
|
|
25
26
|
export interface GitLabIssueWatcherEvent {
|
|
26
27
|
/** @public */
|
|
27
28
|
profile: string;
|
|
29
|
+
/** @public */ gitSshProfile?: string;
|
|
28
30
|
/** @public */
|
|
29
31
|
repository: GitLabProject;
|
|
30
32
|
/** @public */
|
|
@@ -66,6 +68,7 @@ export const gitlabIssueWatcherSource = defineProcessWatcherSource<
|
|
|
66
68
|
config: {
|
|
67
69
|
...parseGitLabSelection(c),
|
|
68
70
|
profile: text(c.profile, "profile"),
|
|
71
|
+
...(typeof c.git_ssh_profile === "string" ? { gitSshProfile: c.git_ssh_profile } : {}),
|
|
69
72
|
pollInterval,
|
|
70
73
|
labels: { trigger, done },
|
|
71
74
|
},
|
|
@@ -133,7 +136,13 @@ export function createGitLabIssueDiscovery(
|
|
|
133
136
|
};
|
|
134
137
|
const launched = await deps.launchRuns.startWatcher(
|
|
135
138
|
guarded,
|
|
136
|
-
{
|
|
139
|
+
{
|
|
140
|
+
profile: c.profile,
|
|
141
|
+
gitSshProfile: c.gitSshProfile,
|
|
142
|
+
repository,
|
|
143
|
+
issue,
|
|
144
|
+
labels: c.labels,
|
|
145
|
+
},
|
|
137
146
|
{ idempotencyKey: id },
|
|
138
147
|
);
|
|
139
148
|
if (launched.error) result.errors.push(`${id}:launch_failed`);
|
package/src/launch.ts
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { repositoryHttpsUrl } from "@leitwerk-dev/process-sdk";
|
|
2
|
+
import type { GitLabClientLike, GitLabProject } from "./client.js";
|
|
3
|
+
|
|
4
|
+
/** Resolve the shared checkout, provider binding and pinned Git identity. @internal */
|
|
5
|
+
export async function resolveGitLabLaunchProject(
|
|
6
|
+
client: GitLabClientLike,
|
|
7
|
+
profile: string,
|
|
8
|
+
repository: GitLabProject,
|
|
9
|
+
workBranch: string,
|
|
10
|
+
key = "repo",
|
|
11
|
+
transport: "ssh" | "https" = "ssh",
|
|
12
|
+
) {
|
|
13
|
+
const repoLocator =
|
|
14
|
+
transport === "ssh" ? repository.ssh_url_to_repo : repository.http_url_to_repo;
|
|
15
|
+
if (!repoLocator)
|
|
16
|
+
throw new Error(`GitLab repository has no ${transport.toUpperCase()} clone URL`);
|
|
17
|
+
if (transport === "https" && repositoryHttpsUrl(repoLocator).origin !== client.baseUrl)
|
|
18
|
+
throw new Error("GitLab repository origin does not match its profile");
|
|
19
|
+
const split = repository.path_with_namespace.lastIndexOf("/");
|
|
20
|
+
if (split < 1) throw new Error("Invalid GitLab project path");
|
|
21
|
+
const params = {
|
|
22
|
+
/** @internal */ gitlabProfile: profile,
|
|
23
|
+
/** @internal */ gitlabOrigin: client.baseUrl,
|
|
24
|
+
/** @internal */ projectId: repository.id,
|
|
25
|
+
/** @internal */ owner: repository.path_with_namespace.slice(0, split),
|
|
26
|
+
/** @internal */ repo: repository.path_with_namespace.slice(split + 1),
|
|
27
|
+
/** @internal */ repoLocator,
|
|
28
|
+
/** @internal */ baseBranch: repository.default_branch,
|
|
29
|
+
/** @internal */ workBranch,
|
|
30
|
+
};
|
|
31
|
+
const identity = await client.resolveGitIdentity();
|
|
32
|
+
return {
|
|
33
|
+
/** @internal */ params,
|
|
34
|
+
/** @internal */ project: {
|
|
35
|
+
/** @internal */ key,
|
|
36
|
+
/** @internal */ repoLocator: params.repoLocator,
|
|
37
|
+
/** @internal */ baseBranch: params.baseBranch,
|
|
38
|
+
/** @internal */ workBranch,
|
|
39
|
+
/** @internal */ settingsRepository: {
|
|
40
|
+
/** @internal */ origin: client.baseUrl,
|
|
41
|
+
/** @internal */ repositoryId: repository.id,
|
|
42
|
+
/** @internal */ aliases: [
|
|
43
|
+
repository.http_url_to_repo,
|
|
44
|
+
...(repository.ssh_url_to_repo ? [repository.ssh_url_to_repo] : []),
|
|
45
|
+
],
|
|
46
|
+
},
|
|
47
|
+
/** @internal */ metadata: {
|
|
48
|
+
/** @internal */ gitlab: {
|
|
49
|
+
/** @internal */ origin: client.baseUrl,
|
|
50
|
+
/** @internal */ profile,
|
|
51
|
+
/** @internal */ projectId: repository.id,
|
|
52
|
+
},
|
|
53
|
+
/** @internal */ "leitwerk.gitIdentity": {
|
|
54
|
+
/** @internal */ provider: "gitlab",
|
|
55
|
+
/** @internal */ profile,
|
|
56
|
+
/** @internal */ login: identity.username,
|
|
57
|
+
/** @internal */ name: identity.name,
|
|
58
|
+
/** @internal */ email: identity.email,
|
|
59
|
+
},
|
|
60
|
+
},
|
|
61
|
+
},
|
|
62
|
+
};
|
|
63
|
+
}
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
import type { ExternalWrites } from "@leitwerk-dev/external-writes";
|
|
2
|
+
import {
|
|
3
|
+
type GitLabClientLike,
|
|
4
|
+
GitLabError,
|
|
5
|
+
type GitLabLabelEvent,
|
|
6
|
+
type GitLabMergeRequest,
|
|
7
|
+
} from "./client.js";
|
|
8
|
+
|
|
9
|
+
/** @public */
|
|
10
|
+
export const GITLAB_ACTIVE_LABEL = "leitwerk-active";
|
|
11
|
+
/** @public */
|
|
12
|
+
export const GITLAB_DONE_LABEL = "leitwerk-done";
|
|
13
|
+
/** @public */
|
|
14
|
+
export interface GitLabLabelState {
|
|
15
|
+
/** @public */ mr: GitLabMergeRequest;
|
|
16
|
+
/** @public */ events: GitLabLabelEvent[];
|
|
17
|
+
}
|
|
18
|
+
/** Read transitions before the current labels so remove/re-add cannot hide cancellation. @public */
|
|
19
|
+
export async function readGitLabLabels(
|
|
20
|
+
client: GitLabClientLike,
|
|
21
|
+
projectId: number,
|
|
22
|
+
iid: number,
|
|
23
|
+
signal?: AbortSignal,
|
|
24
|
+
): Promise<GitLabLabelState> {
|
|
25
|
+
const events = await client.listMergeRequestLabelEvents(projectId, iid, signal);
|
|
26
|
+
const mr = await client.getMergeRequest(projectId, iid, signal);
|
|
27
|
+
return { mr, events: events.sort((a, b) => a.id - b.id) };
|
|
28
|
+
}
|
|
29
|
+
/** A missing label endpoint alone does not establish deletion; project access must still work. @internal */
|
|
30
|
+
export async function readMaintainedGitLabLabels(
|
|
31
|
+
client: GitLabClientLike,
|
|
32
|
+
projectId: number,
|
|
33
|
+
iid: number,
|
|
34
|
+
signal?: AbortSignal,
|
|
35
|
+
): Promise<GitLabLabelState | null> {
|
|
36
|
+
try {
|
|
37
|
+
return await readGitLabLabels(client, projectId, iid, signal);
|
|
38
|
+
} catch (error) {
|
|
39
|
+
if (!(error instanceof GitLabError) || error.status !== 404) throw error;
|
|
40
|
+
try {
|
|
41
|
+
await client.getMergeRequest(projectId, iid, signal);
|
|
42
|
+
} catch (mrError) {
|
|
43
|
+
if (!(mrError instanceof GitLabError) || mrError.status !== 404) throw mrError;
|
|
44
|
+
await client.getProject(projectId, signal);
|
|
45
|
+
return null;
|
|
46
|
+
}
|
|
47
|
+
throw error;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
/** @public */
|
|
51
|
+
export function gitLabActivationStatus(
|
|
52
|
+
after: number,
|
|
53
|
+
labels: GitLabLabelState,
|
|
54
|
+
): "pending" | "active" | "removed" {
|
|
55
|
+
const events = labels.events.filter(
|
|
56
|
+
(event) => event.label?.name === GITLAB_ACTIVE_LABEL && event.id > after,
|
|
57
|
+
);
|
|
58
|
+
if (events.some((event) => event.action === "remove")) return "removed";
|
|
59
|
+
if (!events.length) return "pending";
|
|
60
|
+
return labels.mr.labels.includes(GITLAB_ACTIVE_LABEL) ? "active" : "removed";
|
|
61
|
+
}
|
|
62
|
+
/** All label updates preserve unrelated labels and reconcile uncertain writes. @internal */
|
|
63
|
+
export async function ensureGitLabMaintenanceLabels(input: {
|
|
64
|
+
client: GitLabClientLike;
|
|
65
|
+
writes: ExternalWrites;
|
|
66
|
+
projectId: number;
|
|
67
|
+
iid: number;
|
|
68
|
+
writeKey: string;
|
|
69
|
+
after: number;
|
|
70
|
+
status: "active" | "merged" | "closed" | "stopped";
|
|
71
|
+
guard(): Promise<boolean>;
|
|
72
|
+
signal?: AbortSignal;
|
|
73
|
+
}): Promise<void> {
|
|
74
|
+
const { client, writes, projectId, iid, status, after, signal } = input;
|
|
75
|
+
const settled = async () => {
|
|
76
|
+
const labels = await readGitLabLabels(client, projectId, iid, signal);
|
|
77
|
+
if (!(await input.guard())) throw new Error("GitLab label write superseded");
|
|
78
|
+
if (status === "active")
|
|
79
|
+
return (
|
|
80
|
+
labels.mr.state !== "opened" ||
|
|
81
|
+
gitLabActivationStatus(after, labels) === "removed" ||
|
|
82
|
+
(labels.mr.labels.includes(GITLAB_ACTIVE_LABEL) &&
|
|
83
|
+
!labels.mr.labels.includes(GITLAB_DONE_LABEL))
|
|
84
|
+
);
|
|
85
|
+
// An operator's newer activation belongs to a future process.
|
|
86
|
+
const active = labels.events.filter(
|
|
87
|
+
(event) => event.label?.name === GITLAB_ACTIVE_LABEL && event.id > after,
|
|
88
|
+
);
|
|
89
|
+
if (
|
|
90
|
+
active.some(
|
|
91
|
+
(event, index) =>
|
|
92
|
+
event.action === "add" &&
|
|
93
|
+
active.slice(0, index).some((before) => before.action === "remove"),
|
|
94
|
+
)
|
|
95
|
+
)
|
|
96
|
+
return true;
|
|
97
|
+
return (
|
|
98
|
+
!labels.mr.labels.includes(GITLAB_ACTIVE_LABEL) &&
|
|
99
|
+
(status !== "merged" ||
|
|
100
|
+
labels.mr.labels.includes(GITLAB_DONE_LABEL) ||
|
|
101
|
+
labels.events.some(
|
|
102
|
+
(event) =>
|
|
103
|
+
event.id > after && event.label?.name === GITLAB_DONE_LABEL && event.action === "add",
|
|
104
|
+
))
|
|
105
|
+
);
|
|
106
|
+
};
|
|
107
|
+
await writes.ensure(
|
|
108
|
+
{ writeType: "gitlab.mr_labels", dedupKey: input.writeKey },
|
|
109
|
+
{
|
|
110
|
+
reconcile: async (phase) => (phase === "already_recorded" || (await settled()) ? true : null),
|
|
111
|
+
execute: async () => {
|
|
112
|
+
if (await settled()) return true;
|
|
113
|
+
await client.updateMergeRequestLabels(
|
|
114
|
+
projectId,
|
|
115
|
+
iid,
|
|
116
|
+
status === "active"
|
|
117
|
+
? { add_labels: GITLAB_ACTIVE_LABEL, remove_labels: GITLAB_DONE_LABEL }
|
|
118
|
+
: {
|
|
119
|
+
...(status === "merged" ? { add_labels: GITLAB_DONE_LABEL } : {}),
|
|
120
|
+
remove_labels: GITLAB_ACTIVE_LABEL,
|
|
121
|
+
},
|
|
122
|
+
signal,
|
|
123
|
+
);
|
|
124
|
+
return true;
|
|
125
|
+
},
|
|
126
|
+
toMetadata: () => ({ projectId, iid, status }),
|
|
127
|
+
},
|
|
128
|
+
);
|
|
129
|
+
}
|