@leitwerk-dev/jira-gitlab-change 0.0.0-stage → 0.4.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 +227 -2
- package/dist/LICENSE +13 -0
- package/dist/index.d.ts +51 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +819 -0
- package/dist/index.js.map +1 -0
- package/dist/launch.d.ts +114 -0
- package/dist/launch.d.ts.map +1 -0
- package/dist/models.d.ts +8 -0
- package/dist/models.d.ts.map +1 -0
- package/package.json +55 -5
- package/process.mmd +43 -0
- package/src/index.ts +285 -0
- package/src/launch.ts +784 -0
- package/src/models.ts +35 -0
package/src/launch.ts
ADDED
|
@@ -0,0 +1,784 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import { isDeepStrictEqual } from "node:util";
|
|
3
|
+
import { asUnknownRecord, type SettingsSource } from "@leitwerk-dev/domain";
|
|
4
|
+
import type { PublicationStore } from "@leitwerk-dev/external-writes";
|
|
5
|
+
import { createGitSshPreparationCheck, type GitSshIntegration } from "@leitwerk-dev/git-ssh";
|
|
6
|
+
import {
|
|
7
|
+
createGitLabRepositoryCatalog,
|
|
8
|
+
GitLabError,
|
|
9
|
+
type GitLabIntegration,
|
|
10
|
+
type GitLabPublicationParams,
|
|
11
|
+
type GitLabRepositoryCatalog,
|
|
12
|
+
resolveGitLabLaunchProject,
|
|
13
|
+
} from "@leitwerk-dev/gitlab";
|
|
14
|
+
import {
|
|
15
|
+
ensureIssueWiki,
|
|
16
|
+
type JiraIntegration,
|
|
17
|
+
type JiraIssue,
|
|
18
|
+
JiraRequestError,
|
|
19
|
+
type JiraWatcherEvent,
|
|
20
|
+
jiraEligible,
|
|
21
|
+
jiraIsEpic,
|
|
22
|
+
jiraIssueExternalId,
|
|
23
|
+
jiraSplitChildMatches,
|
|
24
|
+
jiraSubjectIdentity,
|
|
25
|
+
jiraToMarkdown,
|
|
26
|
+
jiraTriggerLabel,
|
|
27
|
+
} from "@leitwerk-dev/jira";
|
|
28
|
+
import {
|
|
29
|
+
type Codec,
|
|
30
|
+
type LauncherContext,
|
|
31
|
+
type LaunchPreparationCheck,
|
|
32
|
+
type ProcessLaunchConfig,
|
|
33
|
+
RetryableWaitError,
|
|
34
|
+
repositorySettingsIdentity,
|
|
35
|
+
SafeLaunchPreparationError,
|
|
36
|
+
type ScopedSettingsResolver,
|
|
37
|
+
type SettingDefinition,
|
|
38
|
+
type TurnWaitContext,
|
|
39
|
+
} from "@leitwerk-dev/process-sdk";
|
|
40
|
+
import type { TopicWikiStore } from "@leitwerk-dev/wiki";
|
|
41
|
+
|
|
42
|
+
import { selectJiraModel } from "./models.js";
|
|
43
|
+
|
|
44
|
+
class JiraSourceIneligibleError extends Error {
|
|
45
|
+
constructor() {
|
|
46
|
+
super("Jira source issue is no longer eligible");
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function eligibleSource(issue: JiraIssue, event: JiraWatcherEvent): boolean {
|
|
51
|
+
return (
|
|
52
|
+
issue.id === event.issue.id &&
|
|
53
|
+
jiraEligible(issue, event.triggerLabel) &&
|
|
54
|
+
event.projects.includes(issue.fields.project.id)
|
|
55
|
+
);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** @public */
|
|
59
|
+
export interface RepositoryMapping {
|
|
60
|
+
/** @public */
|
|
61
|
+
origin: string;
|
|
62
|
+
|
|
63
|
+
/** @public */
|
|
64
|
+
projectId: number;
|
|
65
|
+
|
|
66
|
+
/** @public */
|
|
67
|
+
gitlabProfile: string;
|
|
68
|
+
|
|
69
|
+
/** @public */
|
|
70
|
+
sshProfile?: string;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** @public */
|
|
74
|
+
export interface RepositoryBinding extends GitLabPublicationParams {
|
|
75
|
+
/** @internal */
|
|
76
|
+
key: string;
|
|
77
|
+
|
|
78
|
+
/** @internal */
|
|
79
|
+
origin: "jira";
|
|
80
|
+
|
|
81
|
+
/** @internal */
|
|
82
|
+
gitlabOrigin: string;
|
|
83
|
+
|
|
84
|
+
/** @internal */
|
|
85
|
+
sshProfile?: string;
|
|
86
|
+
|
|
87
|
+
/** @internal */
|
|
88
|
+
repoLocator: string;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/** @public */
|
|
92
|
+
export interface JiraGitLabParams extends Partial<GitLabPublicationParams> {
|
|
93
|
+
/** Present only until initial repository bindings are resolved. @internal */
|
|
94
|
+
startup?: {
|
|
95
|
+
/** Captured watcher scope; later watcher edits do not retarget this process. @internal */
|
|
96
|
+
projectIds: string[];
|
|
97
|
+
};
|
|
98
|
+
/** @internal */
|
|
99
|
+
jiraTriggerLabel?: string;
|
|
100
|
+
/** @internal */
|
|
101
|
+
modelSelection?: {
|
|
102
|
+
/** @internal */
|
|
103
|
+
label: string;
|
|
104
|
+
/** @internal */
|
|
105
|
+
profileId: string;
|
|
106
|
+
};
|
|
107
|
+
/** @internal */
|
|
108
|
+
wikiTopicId?: string;
|
|
109
|
+
/** @internal */
|
|
110
|
+
origin: "jira";
|
|
111
|
+
|
|
112
|
+
/** @internal */
|
|
113
|
+
jiraProfile: string;
|
|
114
|
+
|
|
115
|
+
/** @internal */
|
|
116
|
+
jiraBaseUrl: string;
|
|
117
|
+
|
|
118
|
+
/** @internal */
|
|
119
|
+
issueId: string;
|
|
120
|
+
|
|
121
|
+
/** @internal */
|
|
122
|
+
issueKey: string;
|
|
123
|
+
|
|
124
|
+
/** @internal */
|
|
125
|
+
issueUrl: string;
|
|
126
|
+
|
|
127
|
+
/** @internal */
|
|
128
|
+
prompt: string;
|
|
129
|
+
|
|
130
|
+
/** @internal */
|
|
131
|
+
issue: JiraIssue;
|
|
132
|
+
|
|
133
|
+
/** @internal */
|
|
134
|
+
repositories: RepositoryBinding[];
|
|
135
|
+
|
|
136
|
+
/** @internal */
|
|
137
|
+
mappings: {
|
|
138
|
+
/** @internal */
|
|
139
|
+
componentId: string;
|
|
140
|
+
/** @internal */ values: string[];
|
|
141
|
+
/** @internal */ sources: SettingsSource[];
|
|
142
|
+
}[];
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/** Builds Markdown from captured Jira evidence. @internal */
|
|
146
|
+
export function jiraChangePrompt(issue: JiraIssue): string {
|
|
147
|
+
return `${issue.key}: ${issue.fields.summary}\n\n${jiraToMarkdown(issue.fields.description)}`;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
function admissionParams(params: JiraGitLabParams) {
|
|
151
|
+
const { id, key, fields } = params.issue;
|
|
152
|
+
// Jira plugin fields can change on every read without an issue revision.
|
|
153
|
+
// Keep the raw issue as evidence, but compare only the launch contract.
|
|
154
|
+
return {
|
|
155
|
+
...params,
|
|
156
|
+
issue: {
|
|
157
|
+
id,
|
|
158
|
+
key,
|
|
159
|
+
fields: {
|
|
160
|
+
updated: fields.updated,
|
|
161
|
+
summary: fields.summary,
|
|
162
|
+
description: fields.description,
|
|
163
|
+
labels: [...fields.labels].sort(),
|
|
164
|
+
project: { id: fields.project.id, key: fields.project.key, name: fields.project.name },
|
|
165
|
+
components: fields.components.map(({ id, name }) => ({ id, name })),
|
|
166
|
+
statusCategory: fields.status.statusCategory.key,
|
|
167
|
+
issuetype: fields.issuetype && {
|
|
168
|
+
id: fields.issuetype.id,
|
|
169
|
+
name: fields.issuetype.name,
|
|
170
|
+
subtask: fields.issuetype.subtask,
|
|
171
|
+
},
|
|
172
|
+
parent: fields.parent && { id: fields.parent.id, key: fields.parent.key },
|
|
173
|
+
},
|
|
174
|
+
},
|
|
175
|
+
};
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/** Decode a retained repository selection without remote discovery. @public */
|
|
179
|
+
export function decodeJiraGitLabRepositoryMapping(value: unknown): RepositoryMapping {
|
|
180
|
+
if (typeof value !== "string") throw new Error("Select a repository and its GitLab profile");
|
|
181
|
+
let r: Record<string, unknown> | null;
|
|
182
|
+
try {
|
|
183
|
+
r = asUnknownRecord(JSON.parse(value));
|
|
184
|
+
} catch {
|
|
185
|
+
throw new Error("Invalid repository mapping; reselect it in Settings");
|
|
186
|
+
}
|
|
187
|
+
return validateMapping(r);
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
function validateMapping(r: Record<string, unknown> | null): RepositoryMapping {
|
|
191
|
+
if (
|
|
192
|
+
!r ||
|
|
193
|
+
typeof r.origin !== "string" ||
|
|
194
|
+
new URL(r.origin).origin !== r.origin ||
|
|
195
|
+
typeof r.projectId !== "number" ||
|
|
196
|
+
!Number.isSafeInteger(r.projectId) ||
|
|
197
|
+
r.projectId <= 0 ||
|
|
198
|
+
typeof r.gitlabProfile !== "string" ||
|
|
199
|
+
!r.gitlabProfile ||
|
|
200
|
+
(r.sshProfile !== undefined && (typeof r.sshProfile !== "string" || !r.sshProfile))
|
|
201
|
+
)
|
|
202
|
+
throw new Error("Invalid repository mapping; reselect it in Settings");
|
|
203
|
+
return r as unknown as RepositoryMapping;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
/** @public */
|
|
207
|
+
export const jiraGitLabParamsCodec: Codec<JiraGitLabParams> = {
|
|
208
|
+
parse(value) {
|
|
209
|
+
const r = asUnknownRecord(value);
|
|
210
|
+
jiraTriggerLabel(r?.jiraTriggerLabel);
|
|
211
|
+
if (
|
|
212
|
+
r?.origin !== "jira" ||
|
|
213
|
+
!Array.isArray(r.repositories) ||
|
|
214
|
+
(!r.repositories.length && !asUnknownRecord(r.startup)) ||
|
|
215
|
+
!Array.isArray(r.mappings) ||
|
|
216
|
+
!asUnknownRecord(r.issue)
|
|
217
|
+
)
|
|
218
|
+
throw new Error("Invalid Jira GitLab launch snapshot");
|
|
219
|
+
for (const key of ["jiraProfile", "jiraBaseUrl", "issueId", "issueKey", "issueUrl", "prompt"])
|
|
220
|
+
if (typeof r[key] !== "string" || !r[key]) throw new Error(`Missing ${key}`);
|
|
221
|
+
if (r.startup !== undefined) {
|
|
222
|
+
const startup = asUnknownRecord(r.startup);
|
|
223
|
+
if (
|
|
224
|
+
r.repositories.length ||
|
|
225
|
+
!Array.isArray(startup?.projectIds) ||
|
|
226
|
+
!startup.projectIds.length ||
|
|
227
|
+
startup.projectIds.some((id) => typeof id !== "string" || !id) ||
|
|
228
|
+
[
|
|
229
|
+
"owner",
|
|
230
|
+
"repo",
|
|
231
|
+
"gitlabProfile",
|
|
232
|
+
"projectId",
|
|
233
|
+
"repoLocator",
|
|
234
|
+
"baseBranch",
|
|
235
|
+
"workBranch",
|
|
236
|
+
].some((key) => r[key] !== undefined)
|
|
237
|
+
)
|
|
238
|
+
throw new Error("Invalid pending Jira repository binding");
|
|
239
|
+
}
|
|
240
|
+
for (const item of r.repositories) {
|
|
241
|
+
const repo = asUnknownRecord(item);
|
|
242
|
+
if (!repo || typeof repo.key !== "string" || !/^[a-zA-Z0-9_-]+$/.test(repo.key))
|
|
243
|
+
throw new Error("Invalid repository key");
|
|
244
|
+
validateMapping({ ...repo, origin: repo.gitlabOrigin });
|
|
245
|
+
for (const key of ["repoLocator", "owner", "repo", "baseBranch", "workBranch"])
|
|
246
|
+
if (typeof repo[key] !== "string" || !repo[key])
|
|
247
|
+
throw new Error(`Missing repository ${key}`);
|
|
248
|
+
}
|
|
249
|
+
if (new Set(r.repositories.map((repo) => repo.key)).size !== r.repositories.length)
|
|
250
|
+
throw new Error("Duplicate repository key");
|
|
251
|
+
return r as unknown as JiraGitLabParams;
|
|
252
|
+
},
|
|
253
|
+
serialize: (value) => value,
|
|
254
|
+
};
|
|
255
|
+
|
|
256
|
+
/** Component-to-repository Settings contract. Register it only through this extension. @public */
|
|
257
|
+
export const jiraGitLabRepositories: SettingDefinition<string[]> = {
|
|
258
|
+
key: "jira-gitlab-change.repositories",
|
|
259
|
+
schemaVersion: 1,
|
|
260
|
+
scopes: ["jira.component"],
|
|
261
|
+
merge: "replace",
|
|
262
|
+
defaultValue: [],
|
|
263
|
+
form: {
|
|
264
|
+
label: "GitLab repositories",
|
|
265
|
+
group: "Repository mapping",
|
|
266
|
+
control: "multiselect",
|
|
267
|
+
overview: "repository-mapping",
|
|
268
|
+
search: { minimumLength: 2, placeholder: "Search GitLab repositories" },
|
|
269
|
+
description: "Use the GitLab profile's token over HTTPS, or select a configured SSH profile.",
|
|
270
|
+
},
|
|
271
|
+
schema: {
|
|
272
|
+
parse(value) {
|
|
273
|
+
if (!Array.isArray(value)) throw new Error("Choose repositories");
|
|
274
|
+
for (const entry of value) decodeJiraGitLabRepositoryMapping(entry);
|
|
275
|
+
return [...new Set(value)] as string[];
|
|
276
|
+
},
|
|
277
|
+
},
|
|
278
|
+
};
|
|
279
|
+
|
|
280
|
+
/** @public */
|
|
281
|
+
export function createJiraGitLabLauncher() {
|
|
282
|
+
/** @internal */
|
|
283
|
+
let services: {
|
|
284
|
+
/** @internal */
|
|
285
|
+
jira: JiraIntegration;
|
|
286
|
+
|
|
287
|
+
/** @internal */
|
|
288
|
+
gitlab: GitLabIntegration;
|
|
289
|
+
|
|
290
|
+
/** @internal */
|
|
291
|
+
ssh?: GitSshIntegration;
|
|
292
|
+
|
|
293
|
+
/** @internal */
|
|
294
|
+
settings: ScopedSettingsResolver;
|
|
295
|
+
/** @internal */
|
|
296
|
+
wiki?: TopicWikiStore;
|
|
297
|
+
/** @internal */
|
|
298
|
+
publications?: PublicationStore;
|
|
299
|
+
/** @internal */
|
|
300
|
+
modelLabels?: Readonly<Record<string, string>>;
|
|
301
|
+
} | null = null;
|
|
302
|
+
let repositoryCatalog: GitLabRepositoryCatalog | null = null;
|
|
303
|
+
const requireServices = () => {
|
|
304
|
+
if (!services) throw new Error("Jira GitLab integration is not configured");
|
|
305
|
+
return services;
|
|
306
|
+
};
|
|
307
|
+
const mapping: SettingDefinition<string[]> = {
|
|
308
|
+
...jiraGitLabRepositories,
|
|
309
|
+
choiceLabel(value) {
|
|
310
|
+
const mapping = decodeJiraGitLabRepositoryMapping(value);
|
|
311
|
+
const { gitlab, settings } = requireServices();
|
|
312
|
+
const retained = settings.findSubject(
|
|
313
|
+
"repository",
|
|
314
|
+
repositorySettingsIdentity(mapping.origin, mapping.projectId),
|
|
315
|
+
);
|
|
316
|
+
let path = retained?.label;
|
|
317
|
+
if (!path) {
|
|
318
|
+
const known = gitlab.profiles().includes(mapping.gitlabProfile)
|
|
319
|
+
? repositoryCatalog?.peek(mapping.gitlabProfile, mapping.projectId)
|
|
320
|
+
: undefined;
|
|
321
|
+
path =
|
|
322
|
+
known && gitlab.client(mapping.gitlabProfile).baseUrl === mapping.origin
|
|
323
|
+
? known.path_with_namespace
|
|
324
|
+
: `Repository ${mapping.projectId}`;
|
|
325
|
+
}
|
|
326
|
+
return `${path} · ${mapping.gitlabProfile} / ${mapping.sshProfile ?? "HTTPS"}`;
|
|
327
|
+
},
|
|
328
|
+
async choices(_context, request = {}) {
|
|
329
|
+
const { gitlab, ssh, settings } = requireServices();
|
|
330
|
+
if (!repositoryCatalog) throw new Error("GitLab repository catalog is not configured");
|
|
331
|
+
const query = (request.search ?? "").trim();
|
|
332
|
+
if (query.length < 2) return [];
|
|
333
|
+
const choices = [];
|
|
334
|
+
for (const gitlabProfile of gitlab.profiles()) {
|
|
335
|
+
const client = gitlab.client(gitlabProfile);
|
|
336
|
+
const projects = [...(await repositoryCatalog.search(gitlabProfile, query))];
|
|
337
|
+
for (const value of request.values ?? []) {
|
|
338
|
+
const selected = decodeJiraGitLabRepositoryMapping(value);
|
|
339
|
+
if (
|
|
340
|
+
selected.gitlabProfile !== gitlabProfile ||
|
|
341
|
+
selected.origin !== client.baseUrl ||
|
|
342
|
+
projects.some((repo) => repo.id === selected.projectId)
|
|
343
|
+
)
|
|
344
|
+
continue;
|
|
345
|
+
const project = repositoryCatalog.peek(gitlabProfile, selected.projectId);
|
|
346
|
+
if (project) projects.push(project);
|
|
347
|
+
}
|
|
348
|
+
for (const repo of projects) {
|
|
349
|
+
if (repo.archived || !repo.default_branch) continue;
|
|
350
|
+
settings.discover({
|
|
351
|
+
scopeType: "repository",
|
|
352
|
+
identity: repositorySettingsIdentity(client.baseUrl, repo.id),
|
|
353
|
+
label: repo.path_with_namespace,
|
|
354
|
+
});
|
|
355
|
+
if (repo.http_url_to_repo)
|
|
356
|
+
choices.push({
|
|
357
|
+
value: JSON.stringify({ origin: client.baseUrl, projectId: repo.id, gitlabProfile }),
|
|
358
|
+
label: `${repo.path_with_namespace} · ${gitlabProfile} / HTTPS`,
|
|
359
|
+
});
|
|
360
|
+
if (!repo.ssh_url_to_repo) continue;
|
|
361
|
+
for (const sshProfile of ssh?.profiles() ?? [])
|
|
362
|
+
choices.push({
|
|
363
|
+
value: JSON.stringify({
|
|
364
|
+
origin: client.baseUrl,
|
|
365
|
+
projectId: repo.id,
|
|
366
|
+
gitlabProfile,
|
|
367
|
+
sshProfile,
|
|
368
|
+
}),
|
|
369
|
+
label: `${repo.path_with_namespace} · ${gitlabProfile} / ${sshProfile}`,
|
|
370
|
+
});
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
for (const value of request.values ?? [])
|
|
374
|
+
if (!choices.some((choice) => choice.value === value))
|
|
375
|
+
choices.push({ value, label: mapping.choiceLabel?.(value) ?? value });
|
|
376
|
+
return choices;
|
|
377
|
+
},
|
|
378
|
+
};
|
|
379
|
+
|
|
380
|
+
/** @internal */
|
|
381
|
+
async function resolve(
|
|
382
|
+
event: JiraWatcherEvent,
|
|
383
|
+
ctx: LauncherContext = {},
|
|
384
|
+
retained?: JiraGitLabParams,
|
|
385
|
+
signal?: AbortSignal,
|
|
386
|
+
): Promise<ProcessLaunchConfig<JiraGitLabParams>> {
|
|
387
|
+
const { jira, gitlab, ssh, settings, wiki, publications } = requireServices();
|
|
388
|
+
const client = jira.client(event.profile);
|
|
389
|
+
const issue = await client.getIssue(event.issue.id, signal);
|
|
390
|
+
if (!eligibleSource(issue, event)) throw new JiraSourceIneligibleError();
|
|
391
|
+
const triggerLabel = jiraTriggerLabel(event.triggerLabel);
|
|
392
|
+
const modelSelection = retained
|
|
393
|
+
? retained.modelSelection
|
|
394
|
+
: selectJiraModel(issue.fields.labels, requireServices().modelLabels ?? {});
|
|
395
|
+
if (
|
|
396
|
+
modelSelection &&
|
|
397
|
+
ctx.modelProfiles &&
|
|
398
|
+
!ctx.modelProfiles.some((p) => p.id === modelSelection.profileId)
|
|
399
|
+
)
|
|
400
|
+
throw new Error(
|
|
401
|
+
`Jira model profile ${modelSelection.profileId} is unavailable for this process`,
|
|
402
|
+
);
|
|
403
|
+
const project = settings.discover({
|
|
404
|
+
scopeType: "jira.project",
|
|
405
|
+
identity: jiraSubjectIdentity(client.baseUrl, issue.fields.project.id),
|
|
406
|
+
label: issue.fields.project.key,
|
|
407
|
+
});
|
|
408
|
+
const source = {
|
|
409
|
+
origin: "jira" as const,
|
|
410
|
+
jiraTriggerLabel: triggerLabel,
|
|
411
|
+
...(modelSelection ? { modelSelection } : {}),
|
|
412
|
+
jiraProfile: event.profile,
|
|
413
|
+
jiraBaseUrl: client.baseUrl,
|
|
414
|
+
issueId: issue.id,
|
|
415
|
+
issueKey: issue.key,
|
|
416
|
+
issueUrl: `${client.baseUrl}/browse/${encodeURIComponent(issue.key)}`,
|
|
417
|
+
prompt: jiraChangePrompt(issue),
|
|
418
|
+
issue,
|
|
419
|
+
};
|
|
420
|
+
const launch = {
|
|
421
|
+
processId: "jira_gitlab_change_process",
|
|
422
|
+
defaultModelProfileId: modelSelection?.profileId,
|
|
423
|
+
title: `${issue.key}: ${issue.fields.summary}`,
|
|
424
|
+
startTurnId: "generate_plan",
|
|
425
|
+
externalId: jiraIssueExternalId(client.baseUrl, issue.id),
|
|
426
|
+
externalUrl: source.issueUrl,
|
|
427
|
+
settingsContext: { "jira.project": project.id },
|
|
428
|
+
};
|
|
429
|
+
const mappings: JiraGitLabParams["mappings"] = [];
|
|
430
|
+
const selected = new Map<string, RepositoryMapping>();
|
|
431
|
+
const publication = publications?.publicationByExternalId(
|
|
432
|
+
jiraIssueExternalId(client.baseUrl, issue.id),
|
|
433
|
+
);
|
|
434
|
+
const componentPublication = publication && typeof publication.binding.componentId === "string";
|
|
435
|
+
if (
|
|
436
|
+
componentPublication &&
|
|
437
|
+
(issue.fields.components.length !== 1 ||
|
|
438
|
+
issue.fields.components[0].id !== publication.binding.componentId)
|
|
439
|
+
)
|
|
440
|
+
throw new Error("Generated ticket's component identity changed");
|
|
441
|
+
for (const component of issue.fields.components) {
|
|
442
|
+
const subject = settings.discover({
|
|
443
|
+
scopeType: "jira.component",
|
|
444
|
+
identity: jiraSubjectIdentity(client.baseUrl, component.id),
|
|
445
|
+
label: `${issue.fields.project.key} / ${component.name}`,
|
|
446
|
+
context: { "jira.project": project.id },
|
|
447
|
+
});
|
|
448
|
+
const resolved = settings.resolve(mapping, {
|
|
449
|
+
"jira.project": project.id,
|
|
450
|
+
"jira.component": subject.id,
|
|
451
|
+
});
|
|
452
|
+
mappings.push({
|
|
453
|
+
componentId: component.id,
|
|
454
|
+
values: resolved.value,
|
|
455
|
+
sources: resolved.sources,
|
|
456
|
+
});
|
|
457
|
+
}
|
|
458
|
+
if (!mappings.length || mappings.some((mapping) => !mapping.values.length)) {
|
|
459
|
+
return {
|
|
460
|
+
...launch,
|
|
461
|
+
projects: [],
|
|
462
|
+
params: {
|
|
463
|
+
...source,
|
|
464
|
+
mappings,
|
|
465
|
+
repositories: [],
|
|
466
|
+
startup: { projectIds: [...event.projects] },
|
|
467
|
+
},
|
|
468
|
+
};
|
|
469
|
+
}
|
|
470
|
+
for (const mapping of mappings) {
|
|
471
|
+
for (const value of mapping.values) {
|
|
472
|
+
const entry = decodeJiraGitLabRepositoryMapping(value);
|
|
473
|
+
if (
|
|
474
|
+
publication &&
|
|
475
|
+
!componentPublication &&
|
|
476
|
+
(entry.origin !== publication.binding.origin ||
|
|
477
|
+
entry.projectId !== publication.binding.projectId)
|
|
478
|
+
)
|
|
479
|
+
continue;
|
|
480
|
+
const key = repositorySettingsIdentity(entry.origin, entry.projectId);
|
|
481
|
+
const prior = selected.get(key);
|
|
482
|
+
if (
|
|
483
|
+
prior &&
|
|
484
|
+
(prior.gitlabProfile !== entry.gitlabProfile || prior.sshProfile !== entry.sshProfile)
|
|
485
|
+
)
|
|
486
|
+
throw new Error(
|
|
487
|
+
`Conflicting profiles for project ${entry.projectId}; correct the component mappings in Settings`,
|
|
488
|
+
);
|
|
489
|
+
selected.set(key, entry);
|
|
490
|
+
}
|
|
491
|
+
}
|
|
492
|
+
if (publication && !componentPublication) {
|
|
493
|
+
const binding = publication.binding;
|
|
494
|
+
const exact = [...selected.entries()].find(
|
|
495
|
+
([, entry]) =>
|
|
496
|
+
entry.origin === binding.origin &&
|
|
497
|
+
entry.projectId === binding.projectId &&
|
|
498
|
+
entry.gitlabProfile === binding.gitlabProfile &&
|
|
499
|
+
(binding.sshProfile === undefined || entry.sshProfile === binding.sshProfile),
|
|
500
|
+
);
|
|
501
|
+
if (!exact)
|
|
502
|
+
throw new Error(
|
|
503
|
+
"Generated ticket's repository binding no longer matches its component mappings",
|
|
504
|
+
);
|
|
505
|
+
selected.clear();
|
|
506
|
+
selected.set(...exact);
|
|
507
|
+
}
|
|
508
|
+
if (!selected.size)
|
|
509
|
+
throw new Error(
|
|
510
|
+
`No repositories mapped for ${issue.key}. Map at least one component in Settings → Jira components.`,
|
|
511
|
+
);
|
|
512
|
+
const repositories: RepositoryBinding[] = [];
|
|
513
|
+
const projects: import("@leitwerk-dev/process-sdk").ProcessLaunchProjectConfig[] = [];
|
|
514
|
+
for (const entry of selected.values()) {
|
|
515
|
+
const provider = gitlab.client(entry.gitlabProfile);
|
|
516
|
+
if (
|
|
517
|
+
provider.baseUrl !== entry.origin ||
|
|
518
|
+
(entry.sshProfile !== undefined && !ssh?.profiles().includes(entry.sshProfile))
|
|
519
|
+
)
|
|
520
|
+
throw new Error(
|
|
521
|
+
`Unavailable mapping for GitLab project ${entry.projectId}; update its profiles in Settings`,
|
|
522
|
+
);
|
|
523
|
+
const repo = await provider.getProject(entry.projectId, signal);
|
|
524
|
+
if (repo.id !== entry.projectId) throw new Error("GitLab repository identity changed");
|
|
525
|
+
const repoLocator = entry.sshProfile ? repo.ssh_url_to_repo : repo.http_url_to_repo;
|
|
526
|
+
if (repo.archived || !repo.default_branch || !repoLocator)
|
|
527
|
+
throw new Error(
|
|
528
|
+
`GitLab project ${entry.projectId} must be active and provide the selected clone URL`,
|
|
529
|
+
);
|
|
530
|
+
if (
|
|
531
|
+
publication &&
|
|
532
|
+
!componentPublication &&
|
|
533
|
+
((publication.binding.repoLocator !== undefined &&
|
|
534
|
+
repoLocator !== publication.binding.repoLocator) ||
|
|
535
|
+
(publication.binding.repository !== undefined &&
|
|
536
|
+
repo.path_with_namespace !== publication.binding.repository) ||
|
|
537
|
+
repo.default_branch !== publication.binding.baseBranch)
|
|
538
|
+
)
|
|
539
|
+
throw new Error("Generated ticket's repository checkout binding changed");
|
|
540
|
+
const key = `repo_${repositories.length + 1}`;
|
|
541
|
+
const { params: binding, project } = await resolveGitLabLaunchProject(
|
|
542
|
+
provider,
|
|
543
|
+
entry.gitlabProfile,
|
|
544
|
+
{ ...repo, id: entry.projectId },
|
|
545
|
+
`leitwerk/jira-${createHash("sha256").update(client.baseUrl).digest("hex").slice(0, 10)}-${issue.id}`,
|
|
546
|
+
key,
|
|
547
|
+
entry.sshProfile ? "ssh" : "https",
|
|
548
|
+
);
|
|
549
|
+
if (
|
|
550
|
+
publication?.binding.workBranch !== undefined &&
|
|
551
|
+
publication.binding.workBranch !== binding.workBranch
|
|
552
|
+
)
|
|
553
|
+
throw new Error("Generated ticket's repository work branch binding changed");
|
|
554
|
+
repositories.push({ ...binding, key, origin: "jira", sshProfile: entry.sshProfile });
|
|
555
|
+
projects.push({
|
|
556
|
+
...project,
|
|
557
|
+
metadata: {
|
|
558
|
+
...project.metadata,
|
|
559
|
+
jira: {
|
|
560
|
+
profile: event.profile,
|
|
561
|
+
baseUrl: client.baseUrl,
|
|
562
|
+
issueId: issue.id,
|
|
563
|
+
triggerLabel,
|
|
564
|
+
},
|
|
565
|
+
},
|
|
566
|
+
});
|
|
567
|
+
}
|
|
568
|
+
const first = repositories[0];
|
|
569
|
+
let sourceIssue: JiraIssue | null = null;
|
|
570
|
+
if (publication) {
|
|
571
|
+
const sourceIssueId = publication.binding.sourceIssueId ?? publication.binding.epicId;
|
|
572
|
+
const relationship = publication.binding.relationship ?? "epic";
|
|
573
|
+
if (
|
|
574
|
+
typeof sourceIssueId !== "string" ||
|
|
575
|
+
(relationship !== "epic" && relationship !== "subtask") ||
|
|
576
|
+
!(await jiraSplitChildMatches(client, issue, sourceIssueId, relationship))
|
|
577
|
+
)
|
|
578
|
+
throw new Error("Generated ticket's source issue binding changed");
|
|
579
|
+
sourceIssue = await client.getIssue(sourceIssueId);
|
|
580
|
+
if (
|
|
581
|
+
sourceIssue.id !== sourceIssueId ||
|
|
582
|
+
sourceIssue.fields.project.id !== issue.fields.project.id ||
|
|
583
|
+
sourceIssue.fields.issuetype?.subtask === true ||
|
|
584
|
+
jiraIsEpic(sourceIssue) !== (relationship === "epic")
|
|
585
|
+
)
|
|
586
|
+
throw new Error("Generated ticket's source issue binding changed");
|
|
587
|
+
} else if (wiki && issue.fields.issuetype?.subtask === true) {
|
|
588
|
+
if (!issue.fields.parent?.id) throw new Error("Jira subtask parent is unavailable");
|
|
589
|
+
sourceIssue = await client.getIssue(issue.fields.parent.id);
|
|
590
|
+
} else if (wiki && client.getEpic) {
|
|
591
|
+
sourceIssue = await client.getEpic(issue);
|
|
592
|
+
}
|
|
593
|
+
const topic =
|
|
594
|
+
wiki && sourceIssue ? ensureIssueWiki(wiki, client, sourceIssue, publication?.topicId) : null;
|
|
595
|
+
if (publication && topic?.id !== publication.topicId)
|
|
596
|
+
throw new Error("Generated ticket's source issue wiki binding changed");
|
|
597
|
+
const params: JiraGitLabParams = {
|
|
598
|
+
...(topic ? { wikiTopicId: topic.id } : {}),
|
|
599
|
+
...first,
|
|
600
|
+
...source,
|
|
601
|
+
mappings,
|
|
602
|
+
repositories,
|
|
603
|
+
};
|
|
604
|
+
return {
|
|
605
|
+
...launch,
|
|
606
|
+
...(topic && sourceIssue
|
|
607
|
+
? {
|
|
608
|
+
metadata: {
|
|
609
|
+
wiki: {
|
|
610
|
+
topicId: topic.id,
|
|
611
|
+
profile: event.profile,
|
|
612
|
+
baseUrl: client.baseUrl,
|
|
613
|
+
issueId: sourceIssue.id,
|
|
614
|
+
},
|
|
615
|
+
},
|
|
616
|
+
}
|
|
617
|
+
: {}),
|
|
618
|
+
params,
|
|
619
|
+
projects,
|
|
620
|
+
};
|
|
621
|
+
}
|
|
622
|
+
const checks = (
|
|
623
|
+
event: JiraWatcherEvent,
|
|
624
|
+
launch: ProcessLaunchConfig<JiraGitLabParams>,
|
|
625
|
+
retained?: JiraGitLabParams,
|
|
626
|
+
): readonly LaunchPreparationCheck<JiraGitLabParams>[] => [
|
|
627
|
+
...launch.params.repositories.map<LaunchPreparationCheck<JiraGitLabParams>>((repo) => ({
|
|
628
|
+
id: `${repo.sshProfile ? "ssh" : "https"}_${repo.key}`,
|
|
629
|
+
label: `Verify ${repo.owner}/${repo.repo} ${repo.sshProfile ? "SSH" : "HTTPS"} read/write access`,
|
|
630
|
+
async run(ctx) {
|
|
631
|
+
const { gitlab, ssh } = requireServices();
|
|
632
|
+
const client = gitlab.client(repo.gitlabProfile);
|
|
633
|
+
const fresh = await client.getProject(repo.projectId, ctx.signal);
|
|
634
|
+
if (
|
|
635
|
+
client.baseUrl !== repo.gitlabOrigin ||
|
|
636
|
+
fresh.id !== repo.projectId ||
|
|
637
|
+
fresh.archived ||
|
|
638
|
+
(repo.sshProfile ? fresh.ssh_url_to_repo : fresh.http_url_to_repo) !== repo.repoLocator ||
|
|
639
|
+
fresh.default_branch !== repo.baseBranch
|
|
640
|
+
)
|
|
641
|
+
throw new SafeLaunchPreparationError(
|
|
642
|
+
"Repository changed",
|
|
643
|
+
"Refresh the component mapping and retry launch.",
|
|
644
|
+
);
|
|
645
|
+
if (!repo.sshProfile) {
|
|
646
|
+
await client.preflightRepository(
|
|
647
|
+
repo.projectId,
|
|
648
|
+
repo.baseBranch,
|
|
649
|
+
repo.workBranch,
|
|
650
|
+
ctx.signal,
|
|
651
|
+
);
|
|
652
|
+
return;
|
|
653
|
+
}
|
|
654
|
+
if (!ssh) throw new Error("Git SSH profile is unavailable");
|
|
655
|
+
await createGitSshPreparationCheck(
|
|
656
|
+
"write",
|
|
657
|
+
{ ...repo, sshCredentialRef: repo.sshProfile },
|
|
658
|
+
() => ssh,
|
|
659
|
+
).run(ctx);
|
|
660
|
+
},
|
|
661
|
+
})),
|
|
662
|
+
{
|
|
663
|
+
id: "jira_eligibility",
|
|
664
|
+
label: "Recheck Jira issue and component mappings",
|
|
665
|
+
async run({ signal }) {
|
|
666
|
+
if (launch.params.startup) {
|
|
667
|
+
const issue = await requireServices()
|
|
668
|
+
.jira.client(event.profile)
|
|
669
|
+
.getIssue(event.issue.id, signal);
|
|
670
|
+
if (!eligibleSource(issue, event))
|
|
671
|
+
throw new SafeLaunchPreparationError(
|
|
672
|
+
"Jira source issue is no longer eligible",
|
|
673
|
+
"Restore the watcher label and project eligibility before launching.",
|
|
674
|
+
);
|
|
675
|
+
return;
|
|
676
|
+
}
|
|
677
|
+
const fresh = await resolve(event, {}, retained, signal);
|
|
678
|
+
if (!isDeepStrictEqual(admissionParams(fresh.params), admissionParams(launch.params)))
|
|
679
|
+
throw new SafeLaunchPreparationError(
|
|
680
|
+
"Jira issue or mappings changed before admission",
|
|
681
|
+
"Retry launch to capture the latest issue and mappings.",
|
|
682
|
+
);
|
|
683
|
+
},
|
|
684
|
+
},
|
|
685
|
+
];
|
|
686
|
+
/** Check startup readiness against the current source and mappings. @internal */
|
|
687
|
+
async function ready(
|
|
688
|
+
context: Pick<
|
|
689
|
+
TurnWaitContext<JiraGitLabParams>,
|
|
690
|
+
"params" | "signal" | "waiting" | "abort" | "bindInitialProjects"
|
|
691
|
+
>,
|
|
692
|
+
) {
|
|
693
|
+
const { params, signal } = context;
|
|
694
|
+
if (!params.startup) return true;
|
|
695
|
+
try {
|
|
696
|
+
const client = requireServices().jira.client(params.jiraProfile);
|
|
697
|
+
if (client.baseUrl !== params.jiraBaseUrl) throw new Error("Jira installation changed");
|
|
698
|
+
const issue = await client.getIssue(params.issueId, signal);
|
|
699
|
+
if (issue.id !== params.issueId) throw new Error("Jira issue identity changed");
|
|
700
|
+
const event: JiraWatcherEvent = {
|
|
701
|
+
profile: params.jiraProfile,
|
|
702
|
+
triggerLabel: params.jiraTriggerLabel,
|
|
703
|
+
projects: params.startup.projectIds,
|
|
704
|
+
issue,
|
|
705
|
+
};
|
|
706
|
+
if (!eligibleSource(issue, event))
|
|
707
|
+
return context.abort("Jira source is no longer eligible; startup cancelled");
|
|
708
|
+
const launch = await resolve(event, {}, params, signal);
|
|
709
|
+
if (launch.params.startup) {
|
|
710
|
+
const missing = launch.params.issue.fields.components.filter(
|
|
711
|
+
(component) =>
|
|
712
|
+
!launch.params.mappings.find((mapping) => mapping.componentId === component.id)?.values
|
|
713
|
+
.length,
|
|
714
|
+
);
|
|
715
|
+
const reason = missing.length
|
|
716
|
+
? `no GitLab repository mapping for ${missing.map((component) => component.name).join(", ")}. Configure GitLab repositories in Settings → Jira components.`
|
|
717
|
+
: "no Jira components. Assign at least one component to the issue and configure its GitLab repositories in Settings → Jira components.";
|
|
718
|
+
return context.waiting(
|
|
719
|
+
`Startup blocked: ${launch.params.issueKey} has ${reason} Startup will resume automatically.`,
|
|
720
|
+
);
|
|
721
|
+
}
|
|
722
|
+
for (const check of checks(event, launch, params)) {
|
|
723
|
+
signal.throwIfAborted();
|
|
724
|
+
await check.run({ signal, launchConfig: launch, logger: { info() {}, warn() {} } });
|
|
725
|
+
}
|
|
726
|
+
return context.bindInitialProjects({
|
|
727
|
+
params: launch.params,
|
|
728
|
+
projects: launch.projects ?? [],
|
|
729
|
+
metadata: launch.metadata,
|
|
730
|
+
settingsContext: launch.settingsContext,
|
|
731
|
+
});
|
|
732
|
+
} catch (error) {
|
|
733
|
+
if (error instanceof JiraSourceIneligibleError)
|
|
734
|
+
return context.abort("Jira source is no longer eligible; startup cancelled");
|
|
735
|
+
if (
|
|
736
|
+
(error instanceof GitLabError && error.retryable) ||
|
|
737
|
+
(error instanceof JiraRequestError &&
|
|
738
|
+
(error.status === 408 || error.status === 429 || error.status >= 500)) ||
|
|
739
|
+
(error instanceof TypeError && /fetch|network/i.test(error.message))
|
|
740
|
+
)
|
|
741
|
+
throw new RetryableWaitError(
|
|
742
|
+
"Jira or GitLab is temporarily unavailable; startup will be checked again.",
|
|
743
|
+
);
|
|
744
|
+
if (
|
|
745
|
+
error instanceof SafeLaunchPreparationError &&
|
|
746
|
+
error.message.includes("changed before admission")
|
|
747
|
+
)
|
|
748
|
+
return context.waiting(
|
|
749
|
+
"Jira issue or mappings changed during startup checks. Checking the latest configuration again.",
|
|
750
|
+
);
|
|
751
|
+
if (error instanceof SafeLaunchPreparationError)
|
|
752
|
+
throw new Error(`${error.message}. ${error.safeSummary}`);
|
|
753
|
+
throw error;
|
|
754
|
+
}
|
|
755
|
+
}
|
|
756
|
+
return {
|
|
757
|
+
/** @internal */
|
|
758
|
+
ready,
|
|
759
|
+
/** @internal */
|
|
760
|
+
mapping,
|
|
761
|
+
|
|
762
|
+
/** @internal */
|
|
763
|
+
resolve,
|
|
764
|
+
|
|
765
|
+
/** @internal */
|
|
766
|
+
checks,
|
|
767
|
+
|
|
768
|
+
/** @internal */
|
|
769
|
+
configure(value: typeof services) {
|
|
770
|
+
if (value?.gitlab !== services?.gitlab)
|
|
771
|
+
repositoryCatalog = value
|
|
772
|
+
? (value.gitlab.repositoryCatalog ?? createGitLabRepositoryCatalog(value.gitlab))
|
|
773
|
+
: null;
|
|
774
|
+
services = value;
|
|
775
|
+
},
|
|
776
|
+
|
|
777
|
+
/** @internal */
|
|
778
|
+
async readIssue(params: JiraGitLabParams) {
|
|
779
|
+
const client = requireServices().jira.client(params.jiraProfile);
|
|
780
|
+
if (client.baseUrl !== params.jiraBaseUrl) throw new Error("Jira installation changed");
|
|
781
|
+
return client.getIssue(params.issueId);
|
|
782
|
+
},
|
|
783
|
+
};
|
|
784
|
+
}
|