@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/index.ts
ADDED
|
@@ -0,0 +1,285 @@
|
|
|
1
|
+
import { createRepositoryChangeProcess } from "@leitwerk-dev/coding";
|
|
2
|
+
import {
|
|
3
|
+
createRepositoryChangePublication,
|
|
4
|
+
readPublicationState,
|
|
5
|
+
} from "@leitwerk-dev/coding/repository-change-publication";
|
|
6
|
+
import type { RepositoryChangeState } from "@leitwerk-dev/coding/repository-change-state";
|
|
7
|
+
import { asUnknownRecord } from "@leitwerk-dev/domain";
|
|
8
|
+
import { gitSshIntegration } from "@leitwerk-dev/git-ssh";
|
|
9
|
+
import {
|
|
10
|
+
createGitLabPublicationAdapter,
|
|
11
|
+
type GitLabMergeRequest,
|
|
12
|
+
gitlabIntegration,
|
|
13
|
+
gitlabPublicationRequest,
|
|
14
|
+
gitlabRepositoryCredentials,
|
|
15
|
+
} from "@leitwerk-dev/gitlab";
|
|
16
|
+
import {
|
|
17
|
+
type JiraIssue,
|
|
18
|
+
jiraEligible,
|
|
19
|
+
jiraIntegration,
|
|
20
|
+
jiraIssuePolicy,
|
|
21
|
+
jiraIssueWatcherSource,
|
|
22
|
+
jiraWikiSource,
|
|
23
|
+
} from "@leitwerk-dev/jira";
|
|
24
|
+
import {
|
|
25
|
+
coreHostCapabilities,
|
|
26
|
+
type LeitwerkExtensionModule,
|
|
27
|
+
scopedSettingsCapability,
|
|
28
|
+
} from "@leitwerk-dev/process-sdk";
|
|
29
|
+
import { wikiInstructions } from "@leitwerk-dev/wiki";
|
|
30
|
+
import { topicWikiCapability } from "@leitwerk-dev/wiki/integration";
|
|
31
|
+
import {
|
|
32
|
+
createJiraGitLabLauncher,
|
|
33
|
+
type JiraGitLabParams,
|
|
34
|
+
jiraChangePrompt,
|
|
35
|
+
jiraGitLabParamsCodec,
|
|
36
|
+
} from "./launch.js";
|
|
37
|
+
import { parseJiraModelLabels } from "./models.js";
|
|
38
|
+
|
|
39
|
+
export * from "./launch.js";
|
|
40
|
+
|
|
41
|
+
const namespace = "jiraGitLabChange";
|
|
42
|
+
const remote = (state: RepositoryChangeState, key: string) =>
|
|
43
|
+
readPublicationState(state, `${namespace}:${key}`);
|
|
44
|
+
|
|
45
|
+
/** @public */
|
|
46
|
+
export function createJiraGitLabChange(options: {
|
|
47
|
+
/** @internal */
|
|
48
|
+
docker: boolean;
|
|
49
|
+
/** Opt into the supported Jira verification transition after all changed MRs merge. @internal */
|
|
50
|
+
verificationOnMerge?: boolean;
|
|
51
|
+
}) {
|
|
52
|
+
let verificationOnMerge = options.verificationOnMerge ?? false;
|
|
53
|
+
const launcher = createJiraGitLabLauncher();
|
|
54
|
+
const adapter = createGitLabPublicationAdapter<JiraGitLabParams>([], namespace);
|
|
55
|
+
adapter.ensureRequest = async (ctx) => {
|
|
56
|
+
const { url } = (await ctx.callIntegrationTool("jira_ensure_remote_link", {
|
|
57
|
+
projectKey: "repo",
|
|
58
|
+
kind: "process",
|
|
59
|
+
})) as { url: string };
|
|
60
|
+
const mr = (await ctx.callIntegrationTool("gitlab_ensure_merge_request", {
|
|
61
|
+
projectKey: "repo",
|
|
62
|
+
title: `${ctx.params.issueKey}: ${ctx.params.issue.fields.summary}`,
|
|
63
|
+
body: `Implements [${ctx.params.issueKey}](${ctx.params.issueUrl})\n\n[Leitwerk process](${url})`,
|
|
64
|
+
})) as GitLabMergeRequest;
|
|
65
|
+
return gitlabPublicationRequest(mr);
|
|
66
|
+
};
|
|
67
|
+
adapter.repositories = (params) =>
|
|
68
|
+
params.repositories.map((repo) => ({
|
|
69
|
+
key: repo.key,
|
|
70
|
+
params: { ...params, ...repo, issueUrl: params.issueUrl },
|
|
71
|
+
}));
|
|
72
|
+
adapter.tools.delivery = [
|
|
73
|
+
...adapter.tools.delivery,
|
|
74
|
+
"jira_get_source_issue",
|
|
75
|
+
"jira_ensure_remote_link",
|
|
76
|
+
"jira_transition_source_issue",
|
|
77
|
+
"jira_finalize_source_issue",
|
|
78
|
+
];
|
|
79
|
+
adapter.sourceCancelled = async (ctx) =>
|
|
80
|
+
!jiraEligible(
|
|
81
|
+
(await ctx.callIntegrationTool("jira_get_source_issue", {
|
|
82
|
+
projectKey: ctx.params.repositories[0].key,
|
|
83
|
+
})) as JiraIssue,
|
|
84
|
+
ctx.params.jiraTriggerLabel,
|
|
85
|
+
);
|
|
86
|
+
adapter.linkIssue = async (ctx) => {
|
|
87
|
+
await ctx.callIntegrationTool("jira_ensure_remote_link", {
|
|
88
|
+
projectKey: "repo",
|
|
89
|
+
kind: "merge_request",
|
|
90
|
+
});
|
|
91
|
+
};
|
|
92
|
+
adapter.reconcileTerminal = async () => {};
|
|
93
|
+
adapter.reconcileAll = async (ctx, results, cancelled) => {
|
|
94
|
+
const created = results.filter(({ current }) => !current.noChanges);
|
|
95
|
+
const merged = created.filter(({ current }) => current.delivery.terminalPullRequest?.merged);
|
|
96
|
+
const done = !cancelled && created.length > 0 && merged.length === created.length;
|
|
97
|
+
const projectKey = ctx.params.repositories[0].key;
|
|
98
|
+
if (done && verificationOnMerge)
|
|
99
|
+
await ctx.callIntegrationTool("jira_transition_source_issue", {
|
|
100
|
+
projectKey,
|
|
101
|
+
targetStatus: "In Verification",
|
|
102
|
+
});
|
|
103
|
+
if (!cancelled)
|
|
104
|
+
await ctx.callIntegrationTool("jira_finalize_source_issue", {
|
|
105
|
+
projectKey,
|
|
106
|
+
done,
|
|
107
|
+
writeKey: "delivery-labels",
|
|
108
|
+
});
|
|
109
|
+
};
|
|
110
|
+
const publication = createRepositoryChangePublication(adapter);
|
|
111
|
+
publication.fragment.watcher({
|
|
112
|
+
id: "use_leitwerk",
|
|
113
|
+
label: "Jira use-leitwerk issues",
|
|
114
|
+
description: "Coordinate changes across component-mapped GitLab repositories",
|
|
115
|
+
source: jiraIssueWatcherSource,
|
|
116
|
+
resolveLaunchConfig: launcher.resolve,
|
|
117
|
+
preparationChecks: launcher.checks,
|
|
118
|
+
});
|
|
119
|
+
const { process } = createRepositoryChangeProcess<JiraGitLabParams>({
|
|
120
|
+
processId: "jira_gitlab_change_process",
|
|
121
|
+
displayName: "Jira GitLab Change",
|
|
122
|
+
paramsCodec: jiraGitLabParamsCodec,
|
|
123
|
+
publication,
|
|
124
|
+
workflow: {
|
|
125
|
+
multiRepository: true,
|
|
126
|
+
async planDecision(params) {
|
|
127
|
+
const issue = await launcher.readIssue(params);
|
|
128
|
+
return {
|
|
129
|
+
skip:
|
|
130
|
+
jiraEligible(issue, params.jiraTriggerLabel) &&
|
|
131
|
+
issue.fields.labels.includes("leitwerk-skip-plan-decision"),
|
|
132
|
+
reason: "Jira label",
|
|
133
|
+
};
|
|
134
|
+
},
|
|
135
|
+
async simplification(params) {
|
|
136
|
+
const issue = await launcher.readIssue(params);
|
|
137
|
+
return {
|
|
138
|
+
skip: issue.fields.labels.includes("leitwerk-skip-simplification"),
|
|
139
|
+
reason: "Jira source labels",
|
|
140
|
+
};
|
|
141
|
+
},
|
|
142
|
+
planBypassSource: jiraIssuePolicy(({ params, state }) => ({
|
|
143
|
+
profile: params.jiraProfile,
|
|
144
|
+
baseUrl: params.jiraBaseUrl,
|
|
145
|
+
issueId: params.issueId,
|
|
146
|
+
triggerLabel: params.jiraTriggerLabel,
|
|
147
|
+
planRevision: state.routing?.plan?.planRevision,
|
|
148
|
+
mode: "plan_bypass",
|
|
149
|
+
})),
|
|
150
|
+
},
|
|
151
|
+
repositoryCredentials: ({ params }) =>
|
|
152
|
+
params.repositories.flatMap((repo) =>
|
|
153
|
+
repo.sshProfile
|
|
154
|
+
? [{ projectKey: repo.key, kind: "git_ssh" as const, credentialRef: repo.sshProfile }]
|
|
155
|
+
: gitlabRepositoryCredentials(repo.gitlabProfile, [repo]),
|
|
156
|
+
),
|
|
157
|
+
});
|
|
158
|
+
process.runtime = { ...process.runtime, developmentTools: true, docker: options.docker };
|
|
159
|
+
const plan = process.turns.get("generate_plan")?.definition;
|
|
160
|
+
const delivery = process.turns.get("deliver_change")?.definition;
|
|
161
|
+
if (plan?.kind !== "llm" || delivery?.kind !== "automatic")
|
|
162
|
+
throw new Error("Jira change requires planning and delivery turns");
|
|
163
|
+
plan.waitFor = launcher.ready;
|
|
164
|
+
plan.resolveAskQuestions = ({ prepared }) => asUnknownRecord(prepared)?.skipPlanDecision !== true;
|
|
165
|
+
plan.integrationTools = [
|
|
166
|
+
...(plan.integrationTools ?? []),
|
|
167
|
+
"jira_get_source_issue",
|
|
168
|
+
"jira_ensure_remote_link",
|
|
169
|
+
"jira_transition_source_issue",
|
|
170
|
+
];
|
|
171
|
+
const originalPrepare = plan.prepare;
|
|
172
|
+
plan.prepare = async (ctx) => {
|
|
173
|
+
const projectKey = ctx.params.repositories[0].key;
|
|
174
|
+
const issue = (await ctx
|
|
175
|
+
.callIntegrationTool("jira_get_source_issue", { projectKey })
|
|
176
|
+
.catch(() => null)) as JiraIssue | null;
|
|
177
|
+
await ctx.callIntegrationTool("jira_ensure_remote_link", { projectKey, kind: "process" });
|
|
178
|
+
await ctx.callIntegrationTool("jira_transition_source_issue", {
|
|
179
|
+
projectKey,
|
|
180
|
+
targetStatus: "In Progress",
|
|
181
|
+
});
|
|
182
|
+
return {
|
|
183
|
+
...(asUnknownRecord(await originalPrepare?.(ctx)) ?? {}),
|
|
184
|
+
skipPlanDecision: issue?.fields.labels.includes("leitwerk-skip-plan-decision") ?? false,
|
|
185
|
+
};
|
|
186
|
+
};
|
|
187
|
+
const originalDelivery = delivery.run;
|
|
188
|
+
delivery.run = async (ctx) => {
|
|
189
|
+
const result = await originalDelivery(ctx);
|
|
190
|
+
if (result.outcome === "aborted") return result;
|
|
191
|
+
const state = process.stateCodec.parse(result.params?.nextState);
|
|
192
|
+
const published = ctx.params.repositories.map((repo) => remote(state, repo.key));
|
|
193
|
+
if (published.some((current) => current.prNumber)) {
|
|
194
|
+
if (!ctx.callIntegrationTool) throw new Error("Jira integration tools are unavailable");
|
|
195
|
+
// Resumed runs may have marked issueLinked while delivery still used Jira comments.
|
|
196
|
+
await ctx.callIntegrationTool("jira_ensure_remote_link", {
|
|
197
|
+
projectKey: ctx.params.repositories[0].key,
|
|
198
|
+
kind: "process",
|
|
199
|
+
});
|
|
200
|
+
for (const [index, current] of published.entries())
|
|
201
|
+
if (current.prNumber && current.prUrl)
|
|
202
|
+
await ctx.callIntegrationTool("jira_ensure_remote_link", {
|
|
203
|
+
projectKey: ctx.params.repositories[index].key,
|
|
204
|
+
kind: "merge_request",
|
|
205
|
+
});
|
|
206
|
+
if (published.every((current) => current.noChanges || (current.prNumber && current.prUrl)))
|
|
207
|
+
await ctx.callIntegrationTool("jira_transition_source_issue", {
|
|
208
|
+
projectKey: ctx.params.repositories[0].key,
|
|
209
|
+
targetStatus: "In Review",
|
|
210
|
+
});
|
|
211
|
+
}
|
|
212
|
+
return result;
|
|
213
|
+
};
|
|
214
|
+
for (const binding of process.turns.values()) {
|
|
215
|
+
const turn = binding.definition;
|
|
216
|
+
if (turn.kind !== "llm") continue;
|
|
217
|
+
const originalPrompt = turn.prompt;
|
|
218
|
+
const originalTools = turn.resolveIntegrationTools;
|
|
219
|
+
turn.resolveIntegrationTools = (params, state) => [
|
|
220
|
+
...(originalTools?.(params, state) ?? turn.integrationTools ?? []),
|
|
221
|
+
"jira_get_source_issue",
|
|
222
|
+
"jira_get_issue",
|
|
223
|
+
...(params.wikiTopicId
|
|
224
|
+
? ["wiki_index", "wiki_read", "wiki_share", "wiki_edit", "wiki_delete", "wiki_delete_group"]
|
|
225
|
+
: []),
|
|
226
|
+
];
|
|
227
|
+
turn.prompt = async (ctx) =>
|
|
228
|
+
`${await originalPrompt({ ...ctx, params: { ...ctx.params, prompt: jiraChangePrompt(ctx.params.issue) } })}${ctx.params.wikiTopicId ? `\n\n${wikiInstructions}\nWiki: /wiki/${ctx.params.wikiTopicId}` : ""}`;
|
|
229
|
+
}
|
|
230
|
+
const extension: LeitwerkExtensionModule = {
|
|
231
|
+
manifest: {
|
|
232
|
+
id: "jira-gitlab-change",
|
|
233
|
+
version: "0.3.0",
|
|
234
|
+
requires: ["jira", "gitlab", "coding"],
|
|
235
|
+
},
|
|
236
|
+
scopedSettings: { settings: [launcher.mapping] },
|
|
237
|
+
setupCatalog(api) {
|
|
238
|
+
api.registerProcess(process);
|
|
239
|
+
},
|
|
240
|
+
setupServer(api, config) {
|
|
241
|
+
const verification = asUnknownRecord(config)?.verification_on_merge;
|
|
242
|
+
if (verification !== undefined && typeof verification !== "boolean")
|
|
243
|
+
throw new Error("Jira verification_on_merge must be a boolean");
|
|
244
|
+
verificationOnMerge = verification ?? options.verificationOnMerge ?? false;
|
|
245
|
+
adapter.maintenance?.register(api, process);
|
|
246
|
+
const jira = api.require(jiraIntegration),
|
|
247
|
+
gitlab = api.require(gitlabIntegration),
|
|
248
|
+
settings = api.require(scopedSettingsCapability);
|
|
249
|
+
if (Array.isArray(jira) || Array.isArray(gitlab) || Array.isArray(settings))
|
|
250
|
+
throw new Error("Integration capabilities must be singular");
|
|
251
|
+
const wiki = api.require(topicWikiCapability);
|
|
252
|
+
const host = api.require(coreHostCapabilities.serverSetup);
|
|
253
|
+
if (Array.isArray(wiki) || Array.isArray(host))
|
|
254
|
+
throw new Error("Integration capabilities must be singular");
|
|
255
|
+
wiki.registerProcessSource(process.id, jiraWikiSource(wiki, jira));
|
|
256
|
+
launcher.configure({
|
|
257
|
+
modelLabels: parseJiraModelLabels(config),
|
|
258
|
+
jira,
|
|
259
|
+
gitlab,
|
|
260
|
+
get ssh() {
|
|
261
|
+
const ssh = api.get(gitSshIntegration);
|
|
262
|
+
if (Array.isArray(ssh)) throw new Error("Integration capabilities must be singular");
|
|
263
|
+
return ssh;
|
|
264
|
+
},
|
|
265
|
+
settings,
|
|
266
|
+
wiki,
|
|
267
|
+
publications: host.publications,
|
|
268
|
+
});
|
|
269
|
+
api.onStop(() => launcher.configure(null));
|
|
270
|
+
},
|
|
271
|
+
};
|
|
272
|
+
return {
|
|
273
|
+
/** @internal */
|
|
274
|
+
extension,
|
|
275
|
+
/** @internal */
|
|
276
|
+
process,
|
|
277
|
+
/** @internal */
|
|
278
|
+
launcher,
|
|
279
|
+
};
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
/** @public */
|
|
283
|
+
export const { extension: defaultExtension, process: jiraGitLabChangeProcess } =
|
|
284
|
+
createJiraGitLabChange({ docker: true });
|
|
285
|
+
export default defaultExtension;
|