@leitwerk-dev/jira 0.0.0-stage → 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 +122 -2
- package/dist/LICENSE +13 -0
- package/dist/chunk-VUXLYUPG.js +818 -0
- package/dist/chunk-VUXLYUPG.js.map +1 -0
- package/dist/client.d.ts +224 -0
- package/dist/client.d.ts.map +1 -0
- package/dist/index.d.ts +84 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +559 -0
- package/dist/index.js.map +1 -0
- package/dist/markdown.d.ts +5 -0
- package/dist/markdown.d.ts.map +1 -0
- package/dist/testing.d.ts +69 -0
- package/dist/testing.d.ts.map +1 -0
- package/dist/testing.js +389 -0
- package/dist/testing.js.map +1 -0
- package/dist/ticket-creation.d.ts +5 -0
- package/dist/ticket-creation.d.ts.map +1 -0
- package/dist/ticket-text.d.ts +18 -0
- package/dist/ticket-text.d.ts.map +1 -0
- package/dist/tools.d.ts +5 -0
- package/dist/tools.d.ts.map +1 -0
- package/dist/wiki.d.ts +11 -0
- package/dist/wiki.d.ts.map +1 -0
- package/dist/workflow-tools.d.ts +11 -0
- package/dist/workflow-tools.d.ts.map +1 -0
- package/package.json +57 -5
- package/src/client.ts +590 -0
- package/src/index.ts +389 -0
- package/src/markdown.ts +327 -0
- package/src/testing.ts +419 -0
- package/src/ticket-creation.ts +219 -0
- package/src/ticket-text.ts +40 -0
- package/src/tools.ts +81 -0
- package/src/wiki.ts +87 -0
- package/src/workflow-tools.ts +161 -0
|
@@ -0,0 +1,818 @@
|
|
|
1
|
+
// src/client.ts
|
|
2
|
+
import { asUnknownRecord } from "@leitwerk-dev/domain";
|
|
3
|
+
var JiraRequestError = class extends Error {
|
|
4
|
+
/** @internal */
|
|
5
|
+
status;
|
|
6
|
+
/** @internal */
|
|
7
|
+
constructor(status) {
|
|
8
|
+
super(`Jira request failed (${status})`);
|
|
9
|
+
this.status = status;
|
|
10
|
+
}
|
|
11
|
+
};
|
|
12
|
+
function jiraBaseUrl(raw) {
|
|
13
|
+
const url = new URL(raw);
|
|
14
|
+
if (url.protocol !== "https:" || url.username || url.password || url.search || url.hash)
|
|
15
|
+
throw new Error("Jira base_url must be HTTPS, without credentials, query, or fragment");
|
|
16
|
+
return url.href.replace(/\/+$/, "");
|
|
17
|
+
}
|
|
18
|
+
function parseJiraProfiles(raw) {
|
|
19
|
+
const profiles = asUnknownRecord(asUnknownRecord(raw)?.profiles) ?? {};
|
|
20
|
+
return new Map(
|
|
21
|
+
Object.entries(profiles).map(([id, value]) => {
|
|
22
|
+
const profile = asUnknownRecord(value);
|
|
23
|
+
if (typeof profile?.base_url !== "string" || typeof profile.token !== "string" || !profile.token.trim())
|
|
24
|
+
throw new Error(`Jira profile '${id}' requires base_url and a PAT`);
|
|
25
|
+
const token = profile.token.startsWith("env:") ? process.env[profile.token.slice(4)] ?? "" : profile.token;
|
|
26
|
+
if (!token || /[\r\n\0]/.test(token))
|
|
27
|
+
throw new Error(`Jira profile '${id}' requires a valid PAT`);
|
|
28
|
+
if (profile.epic_link_field !== void 0 && (typeof profile.epic_link_field !== "string" || !/^customfield_\d+$/.test(profile.epic_link_field)))
|
|
29
|
+
throw new Error("epic_link_field must identify a Jira custom field");
|
|
30
|
+
if (profile.project !== void 0 && (typeof profile.project !== "string" || !profile.project.trim()))
|
|
31
|
+
throw new Error("project must identify a Jira project by ID or key");
|
|
32
|
+
return [
|
|
33
|
+
id,
|
|
34
|
+
{
|
|
35
|
+
baseUrl: jiraBaseUrl(profile.base_url),
|
|
36
|
+
token,
|
|
37
|
+
...typeof profile.project === "string" ? { project: profile.project.trim() } : {},
|
|
38
|
+
...typeof profile.epic_link_field === "string" ? { epicLinkField: profile.epic_link_field } : {}
|
|
39
|
+
}
|
|
40
|
+
];
|
|
41
|
+
})
|
|
42
|
+
);
|
|
43
|
+
}
|
|
44
|
+
var JiraClient = class {
|
|
45
|
+
/** @internal */
|
|
46
|
+
baseUrl;
|
|
47
|
+
#token;
|
|
48
|
+
#fetch;
|
|
49
|
+
#epicLinkField;
|
|
50
|
+
/** @internal */
|
|
51
|
+
constructor(profile, fetcher = fetch) {
|
|
52
|
+
this.#fetch = fetcher;
|
|
53
|
+
this.baseUrl = jiraBaseUrl(profile.baseUrl);
|
|
54
|
+
this.#token = profile.token;
|
|
55
|
+
this.#epicLinkField = profile.epicLinkField;
|
|
56
|
+
}
|
|
57
|
+
async #request(path, init = {}) {
|
|
58
|
+
init.signal?.throwIfAborted();
|
|
59
|
+
let response;
|
|
60
|
+
try {
|
|
61
|
+
response = await this.#fetch(`${this.baseUrl}/rest/api/2/${path}`, {
|
|
62
|
+
...init,
|
|
63
|
+
redirect: "error",
|
|
64
|
+
signal: init.signal ? AbortSignal.any([init.signal, AbortSignal.timeout(3e4)]) : AbortSignal.timeout(3e4),
|
|
65
|
+
headers: {
|
|
66
|
+
Authorization: `Bearer ${this.#token}`,
|
|
67
|
+
Accept: "application/json",
|
|
68
|
+
"Content-Type": "application/json"
|
|
69
|
+
}
|
|
70
|
+
});
|
|
71
|
+
} catch {
|
|
72
|
+
throw new Error("Jira request unavailable");
|
|
73
|
+
}
|
|
74
|
+
if (!response.ok) throw new JiraRequestError(response.status);
|
|
75
|
+
if (response.status === 204) return void 0;
|
|
76
|
+
try {
|
|
77
|
+
return await response.json();
|
|
78
|
+
} catch {
|
|
79
|
+
throw new Error("Jira returned invalid JSON");
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
async #pages(path, field, signal) {
|
|
83
|
+
const items = [];
|
|
84
|
+
let startAt = 0;
|
|
85
|
+
for (; ; ) {
|
|
86
|
+
const page = await this.#request(
|
|
87
|
+
`${path}${path.includes("?") ? "&" : "?"}startAt=${startAt}&maxResults=100`,
|
|
88
|
+
{ signal }
|
|
89
|
+
);
|
|
90
|
+
if (Array.isArray(page)) return page;
|
|
91
|
+
const batch = page[field];
|
|
92
|
+
if (!Array.isArray(batch)) throw new Error("Jira returned an invalid page");
|
|
93
|
+
const pageStart = page.startAt ?? page.start;
|
|
94
|
+
if (typeof pageStart === "number" && pageStart !== startAt)
|
|
95
|
+
throw new Error("Jira pagination did not advance");
|
|
96
|
+
items.push(...batch);
|
|
97
|
+
startAt += batch.length;
|
|
98
|
+
if (page.isLast === true || page.last === true || typeof page.total === "number" && startAt >= page.total)
|
|
99
|
+
return items;
|
|
100
|
+
if (!batch.length) {
|
|
101
|
+
if (typeof page.total === "number" && startAt < page.total)
|
|
102
|
+
throw new Error("Jira returned an incomplete page");
|
|
103
|
+
return items;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
/** @internal */
|
|
108
|
+
listProjects(signal) {
|
|
109
|
+
return this.#pages("project", "values", signal);
|
|
110
|
+
}
|
|
111
|
+
/** @internal */
|
|
112
|
+
async listCreateProjects(projectId, signal) {
|
|
113
|
+
const projects = projectId ? [await this.#request(`project/${encodeURIComponent(projectId)}`, { signal })] : await this.listProjects(signal);
|
|
114
|
+
return Promise.all(
|
|
115
|
+
projects.map(async (project) => {
|
|
116
|
+
const path = `issue/createmeta/${encodeURIComponent(project.id)}/issuetypes`;
|
|
117
|
+
const issueTypes = await this.#pages(path, "values", signal);
|
|
118
|
+
return {
|
|
119
|
+
...project,
|
|
120
|
+
issuetypes: await Promise.all(
|
|
121
|
+
issueTypes.filter((issueType) => !issueType.subtask).map(async (issueType) => {
|
|
122
|
+
const fields = await this.#pages(`${path}/${encodeURIComponent(issueType.id)}`, "values", signal);
|
|
123
|
+
return {
|
|
124
|
+
...issueType,
|
|
125
|
+
fields: Object.fromEntries(
|
|
126
|
+
fields.map(({ fieldId, ...field }) => {
|
|
127
|
+
if (typeof fieldId !== "string" || !fieldId)
|
|
128
|
+
throw new Error("Jira returned invalid create field metadata");
|
|
129
|
+
return [fieldId, field];
|
|
130
|
+
})
|
|
131
|
+
)
|
|
132
|
+
};
|
|
133
|
+
})
|
|
134
|
+
)
|
|
135
|
+
};
|
|
136
|
+
})
|
|
137
|
+
);
|
|
138
|
+
}
|
|
139
|
+
/** @internal */
|
|
140
|
+
listProjectIssues(projectId, signal) {
|
|
141
|
+
if (!/^\d+$/.test(projectId)) throw new Error("Invalid Jira project ID");
|
|
142
|
+
return this.#pages(
|
|
143
|
+
`search?jql=${encodeURIComponent(`project = ${projectId} ORDER BY id ASC`)}&fields=summary,description,labels,project,components,status`,
|
|
144
|
+
"issues",
|
|
145
|
+
signal
|
|
146
|
+
);
|
|
147
|
+
}
|
|
148
|
+
/** @internal */
|
|
149
|
+
async createIssue(fields, signal) {
|
|
150
|
+
const result = await this.createIssueReceipt(fields, signal);
|
|
151
|
+
return this.getIssue(result.id, signal);
|
|
152
|
+
}
|
|
153
|
+
/** Return the write receipt without a follow-up read that could obscure a successful POST. @internal */
|
|
154
|
+
createIssueReceipt(fields, signal) {
|
|
155
|
+
return this.#request("issue", {
|
|
156
|
+
method: "POST",
|
|
157
|
+
body: JSON.stringify({ fields }),
|
|
158
|
+
signal
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
/** @internal */
|
|
162
|
+
listComponents(projectId) {
|
|
163
|
+
return this.#pages(
|
|
164
|
+
`project/${encodeURIComponent(projectId)}/components`,
|
|
165
|
+
"values"
|
|
166
|
+
);
|
|
167
|
+
}
|
|
168
|
+
/** @internal */
|
|
169
|
+
searchIssues(projectIds, triggerLabel) {
|
|
170
|
+
if (!projectIds.length || projectIds.some((id) => !/^\d+$/.test(id)))
|
|
171
|
+
throw new Error("Select explicit Jira project IDs");
|
|
172
|
+
const jql = `project in (${projectIds.join(",")}) AND labels = ${JSON.stringify(jiraTriggerLabel(triggerLabel))} AND (labels not in ("leitwerk-done")) AND statusCategory != Done ORDER BY id ASC`;
|
|
173
|
+
return this.#pages(
|
|
174
|
+
`search?jql=${encodeURIComponent(jql)}&fields=summary,description,labels,project,components,status`,
|
|
175
|
+
"issues"
|
|
176
|
+
);
|
|
177
|
+
}
|
|
178
|
+
/** @internal */
|
|
179
|
+
getIssue(id, signal) {
|
|
180
|
+
return this.#request(`issue/${encodeURIComponent(id)}?fields=*all`, { signal });
|
|
181
|
+
}
|
|
182
|
+
async #resolveEpicLinkField() {
|
|
183
|
+
if (this.#epicLinkField) return this.#epicLinkField;
|
|
184
|
+
const fields = await this.#request("field");
|
|
185
|
+
const matches = fields.filter(
|
|
186
|
+
(field) => field.schema?.custom === "com.pyxis.greenhopper.jira:gh-epic-link"
|
|
187
|
+
);
|
|
188
|
+
if (!matches.length) return null;
|
|
189
|
+
if (matches.length !== 1)
|
|
190
|
+
throw new Error(
|
|
191
|
+
"Configure this Jira profile's epic_link_field; Epic Link discovery is ambiguous"
|
|
192
|
+
);
|
|
193
|
+
this.#epicLinkField = matches[0].id;
|
|
194
|
+
return this.#epicLinkField;
|
|
195
|
+
}
|
|
196
|
+
/** @internal */
|
|
197
|
+
async getEpic(issue) {
|
|
198
|
+
if (issue.fields.issuetype?.name.toLowerCase() === "epic") return issue;
|
|
199
|
+
const field = await this.#resolveEpicLinkField();
|
|
200
|
+
if (!field) return null;
|
|
201
|
+
const key = issue.fields[field];
|
|
202
|
+
return typeof key === "string" && key ? this.getIssue(key) : null;
|
|
203
|
+
}
|
|
204
|
+
/** @internal */
|
|
205
|
+
searchSplitIssues(projectIds) {
|
|
206
|
+
if (!projectIds.length || projectIds.some((id) => !/^\d+$/.test(id)))
|
|
207
|
+
throw new Error("Select explicit Jira project IDs");
|
|
208
|
+
const jql = `project in (${projectIds.join(",")}) AND labels in ("leitwerk-issue-split", "leitwerk-epic-split") AND issuetype not in subTaskIssueTypes() AND statusCategory != Done ORDER BY id ASC`;
|
|
209
|
+
return this.#pages(`search?jql=${encodeURIComponent(jql)}&fields=*all`, "issues");
|
|
210
|
+
}
|
|
211
|
+
/** @internal */
|
|
212
|
+
async createMetadata(projectId, includeEpicLink = true) {
|
|
213
|
+
const metadata = await this.#request(
|
|
214
|
+
`issue/createmeta?projectIds=${encodeURIComponent(projectId)}&expand=projects.issuetypes.fields`
|
|
215
|
+
);
|
|
216
|
+
const project = metadata.projects.find((candidate) => candidate.id === projectId);
|
|
217
|
+
if (!project) throw new Error("Jira issue creation is unavailable for this project");
|
|
218
|
+
const epicLinkField = includeEpicLink ? await this.#resolveEpicLinkField() : null;
|
|
219
|
+
if (includeEpicLink && !epicLinkField)
|
|
220
|
+
throw new Error("Splitting a Jira epic requires an Epic Link field");
|
|
221
|
+
return { epicLinkField, issueTypes: project.issuetypes };
|
|
222
|
+
}
|
|
223
|
+
/** @internal */
|
|
224
|
+
async findSplitIssue(projectId, marker) {
|
|
225
|
+
if (!/^\d+$/.test(projectId) || !/^leitwerk-split-[a-f0-9]{64}$/.test(marker))
|
|
226
|
+
throw new Error("Invalid split ticket identity");
|
|
227
|
+
const jql = `project = ${projectId} AND labels = "${marker}" ORDER BY id ASC`;
|
|
228
|
+
const matches = await this.#pages(
|
|
229
|
+
`search?jql=${encodeURIComponent(jql)}&fields=*all`,
|
|
230
|
+
"issues"
|
|
231
|
+
);
|
|
232
|
+
if (matches.length > 1)
|
|
233
|
+
throw new Error(
|
|
234
|
+
"Multiple Jira tickets share the split identity; operator reconciliation required"
|
|
235
|
+
);
|
|
236
|
+
return matches[0] ?? null;
|
|
237
|
+
}
|
|
238
|
+
/** @internal */
|
|
239
|
+
listComments(id) {
|
|
240
|
+
return this.#pages(`issue/${encodeURIComponent(id)}/comment`, "comments");
|
|
241
|
+
}
|
|
242
|
+
/** @internal */
|
|
243
|
+
listRemoteLinks(id, signal) {
|
|
244
|
+
return this.#request(`issue/${encodeURIComponent(id)}/remotelink`, {
|
|
245
|
+
signal
|
|
246
|
+
});
|
|
247
|
+
}
|
|
248
|
+
/** @internal */
|
|
249
|
+
upsertRemoteLink(id, link, signal) {
|
|
250
|
+
return this.#request(`issue/${encodeURIComponent(id)}/remotelink`, {
|
|
251
|
+
method: "POST",
|
|
252
|
+
body: JSON.stringify(link),
|
|
253
|
+
signal
|
|
254
|
+
});
|
|
255
|
+
}
|
|
256
|
+
/** @internal */
|
|
257
|
+
async listTransitions(id, signal) {
|
|
258
|
+
const result = await this.#request(
|
|
259
|
+
`issue/${encodeURIComponent(id)}/transitions`,
|
|
260
|
+
{ signal }
|
|
261
|
+
);
|
|
262
|
+
return result.transitions;
|
|
263
|
+
}
|
|
264
|
+
/** @internal */
|
|
265
|
+
transitionIssue(id, transitionId, signal) {
|
|
266
|
+
return this.#request(`issue/${encodeURIComponent(id)}/transitions`, {
|
|
267
|
+
method: "POST",
|
|
268
|
+
body: JSON.stringify({ transition: { id: transitionId } }),
|
|
269
|
+
signal
|
|
270
|
+
});
|
|
271
|
+
}
|
|
272
|
+
/** @internal */
|
|
273
|
+
addComment(id, body) {
|
|
274
|
+
return this.#request(`issue/${encodeURIComponent(id)}/comment`, {
|
|
275
|
+
method: "POST",
|
|
276
|
+
body: JSON.stringify({ body })
|
|
277
|
+
});
|
|
278
|
+
}
|
|
279
|
+
/** @internal */
|
|
280
|
+
updateLabels(id, remove, add) {
|
|
281
|
+
return this.#request(`issue/${encodeURIComponent(id)}`, {
|
|
282
|
+
method: "PUT",
|
|
283
|
+
body: JSON.stringify({
|
|
284
|
+
update: {
|
|
285
|
+
labels: [
|
|
286
|
+
...remove.map((value) => ({ remove: value })),
|
|
287
|
+
...add.map((value) => ({ add: value }))
|
|
288
|
+
]
|
|
289
|
+
}
|
|
290
|
+
})
|
|
291
|
+
});
|
|
292
|
+
}
|
|
293
|
+
};
|
|
294
|
+
function jiraTriggerLabel(value = void 0) {
|
|
295
|
+
if (value === void 0) return "use-leitwerk";
|
|
296
|
+
if (typeof value !== "string" || !/^[A-Za-z0-9_.-]{1,255}$/.test(value))
|
|
297
|
+
throw new Error(
|
|
298
|
+
"Jira trigger label must contain only letters, digits, dots, underscores or hyphens"
|
|
299
|
+
);
|
|
300
|
+
return value;
|
|
301
|
+
}
|
|
302
|
+
var jiraEligible = (issue, triggerLabel) => issue.fields.labels.includes(jiraTriggerLabel(triggerLabel)) && !issue.fields.labels.includes("leitwerk-done") && issue.fields.status.statusCategory.key !== "done";
|
|
303
|
+
var jiraIssueExternalId = (baseUrl, id) => `jira:${JSON.stringify([jiraBaseUrl(baseUrl), id])}`;
|
|
304
|
+
var jiraIsEpic = (issue) => issue.fields.issuetype?.name.toLowerCase() === "epic";
|
|
305
|
+
var jiraSplitEligible = (issue) => Boolean(issue.fields.issuetype?.id) && issue.fields.issuetype?.subtask !== true && issue.fields.status.statusCategory.key !== "done" && issue.fields.labels.some(
|
|
306
|
+
(label) => ["leitwerk-issue-split", "leitwerk-epic-split"].includes(label)
|
|
307
|
+
);
|
|
308
|
+
async function jiraSplitChildMatches(client, issue, sourceIssueId, relationship) {
|
|
309
|
+
if (relationship === "subtask")
|
|
310
|
+
return issue.fields.issuetype?.subtask === true && issue.fields.parent?.id === sourceIssueId;
|
|
311
|
+
return issue.fields.issuetype?.subtask !== true && (await client.getEpic?.(issue))?.id === sourceIssueId;
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
// src/markdown.ts
|
|
315
|
+
import MarkdownIt from "markdown-it";
|
|
316
|
+
var markdownParser = new MarkdownIt({ html: false, linkify: false });
|
|
317
|
+
var normalizeNewlines = (value) => value.replace(/\r\n?/g, "\n");
|
|
318
|
+
var escapeMarkdown = (value) => value.replace(/[\\`*_[\]<>|~]/g, "\\$&");
|
|
319
|
+
var escapeJira = (value) => value.replace(/[\\*_{}[\]|!+^~]/g, "\\$&").replace(/(?<!\w)-(?=\S)|(?<=\S)-(?!\w)/g, "\\-");
|
|
320
|
+
function closingToken(tokens, start) {
|
|
321
|
+
let depth = 0;
|
|
322
|
+
for (let index = start; index < tokens.length; index++) {
|
|
323
|
+
depth += tokens[index]?.nesting ?? 0;
|
|
324
|
+
if (depth === 0) return index;
|
|
325
|
+
}
|
|
326
|
+
return tokens.length;
|
|
327
|
+
}
|
|
328
|
+
function markdownInlineToJira(tokens) {
|
|
329
|
+
const output = [];
|
|
330
|
+
for (let index = 0; index < tokens.length; index++) {
|
|
331
|
+
const token = tokens[index];
|
|
332
|
+
if (!token) continue;
|
|
333
|
+
if (token.type === "link_open") {
|
|
334
|
+
const end = closingToken(tokens, index);
|
|
335
|
+
output.push(
|
|
336
|
+
`[${markdownInlineToJira(tokens.slice(index + 1, end))}|${token.attrGet("href") ?? ""}]`
|
|
337
|
+
);
|
|
338
|
+
index = end;
|
|
339
|
+
} else if (token.type === "code_inline") output.push(`{{${token.content}}}`);
|
|
340
|
+
else if (token.type.startsWith("strong_")) output.push("*");
|
|
341
|
+
else if (token.type.startsWith("em_")) output.push("_");
|
|
342
|
+
else if (token.type.startsWith("s_")) output.push("-");
|
|
343
|
+
else if (token.type === "softbreak") output.push("\n");
|
|
344
|
+
else if (token.type === "hardbreak") output.push("\\\\\n");
|
|
345
|
+
else if (token.type === "image")
|
|
346
|
+
output.push(`[${escapeJira(token.content)}|${token.attrGet("src") ?? ""}]`);
|
|
347
|
+
else
|
|
348
|
+
output.push(
|
|
349
|
+
token.children ? markdownInlineToJira(token.children) : escapeJira(token.content)
|
|
350
|
+
);
|
|
351
|
+
}
|
|
352
|
+
return output.join("");
|
|
353
|
+
}
|
|
354
|
+
function markdownTableToJira(tokens) {
|
|
355
|
+
const rows = [];
|
|
356
|
+
for (let index = 0; index < tokens.length; index++) {
|
|
357
|
+
if (tokens[index]?.type !== "tr_open") continue;
|
|
358
|
+
const end = closingToken(tokens, index);
|
|
359
|
+
const row = tokens.slice(index + 1, end);
|
|
360
|
+
const separator = row.some((token) => token.type === "th_open") ? "||" : "|";
|
|
361
|
+
const cells = row.filter((token) => token.type === "inline").map((token) => markdownInlineToJira(token.children ?? []));
|
|
362
|
+
rows.push(`${separator}${cells.join(separator)}${separator}`);
|
|
363
|
+
index = end;
|
|
364
|
+
}
|
|
365
|
+
return rows.join("\n");
|
|
366
|
+
}
|
|
367
|
+
function markdownBlocksToJira(tokens, listMarkers = "") {
|
|
368
|
+
const output = [];
|
|
369
|
+
for (let index = 0; index < tokens.length; index++) {
|
|
370
|
+
const token = tokens[index];
|
|
371
|
+
if (!token) continue;
|
|
372
|
+
if (token.type === "fence" || token.type === "code_block") {
|
|
373
|
+
const language = /^[\w+-]+/.exec(token.info.trim())?.[0] ?? "";
|
|
374
|
+
output.push(
|
|
375
|
+
`{code${language ? `:${language}` : ""}}
|
|
376
|
+
${token.content.replace(/\n$/, "")}
|
|
377
|
+
{code}`
|
|
378
|
+
);
|
|
379
|
+
} else if (token.type === "hr") output.push("----");
|
|
380
|
+
else if (token.nesting === 1) {
|
|
381
|
+
const end = closingToken(tokens, index);
|
|
382
|
+
const inner = tokens.slice(index + 1, end);
|
|
383
|
+
if (token.type === "bullet_list_open" || token.type === "ordered_list_open") {
|
|
384
|
+
const markers = listMarkers + (token.type === "bullet_list_open" ? "*" : "#");
|
|
385
|
+
const items = [];
|
|
386
|
+
for (let item = 0; item < inner.length; item++) {
|
|
387
|
+
if (inner[item]?.type !== "list_item_open") continue;
|
|
388
|
+
const itemEnd = closingToken(inner, item);
|
|
389
|
+
const blocks = markdownBlocksToJira(inner.slice(item + 1, itemEnd), markers);
|
|
390
|
+
items.push(`${markers} ${blocks.join("\n")}`);
|
|
391
|
+
item = itemEnd;
|
|
392
|
+
}
|
|
393
|
+
output.push(items.join("\n"));
|
|
394
|
+
} else if (token.type === "blockquote_open")
|
|
395
|
+
output.push(`{quote}
|
|
396
|
+
${markdownBlocksToJira(inner, listMarkers).join("\n\n")}
|
|
397
|
+
{quote}`);
|
|
398
|
+
else if (token.type === "table_open") output.push(markdownTableToJira(inner));
|
|
399
|
+
else {
|
|
400
|
+
const text = markdownBlocksToJira(inner, listMarkers).join("\n\n");
|
|
401
|
+
output.push(token.type === "heading_open" ? `${token.tag}. ${text}` : text);
|
|
402
|
+
}
|
|
403
|
+
index = end;
|
|
404
|
+
} else if (token.type === "inline") output.push(markdownInlineToJira(token.children ?? []));
|
|
405
|
+
else if (token.content) output.push(escapeJira(token.content));
|
|
406
|
+
}
|
|
407
|
+
return output;
|
|
408
|
+
}
|
|
409
|
+
function markdownToJira(markdown) {
|
|
410
|
+
return markdownBlocksToJira(markdownParser.parse(normalizeNewlines(markdown ?? ""), {})).join(
|
|
411
|
+
"\n\n"
|
|
412
|
+
);
|
|
413
|
+
}
|
|
414
|
+
function backtickDelimiter(value, minimum) {
|
|
415
|
+
const lengths = [...value.matchAll(/`+/g)].map((match) => match[0].length + 1);
|
|
416
|
+
return "`".repeat(Math.max(minimum, ...lengths));
|
|
417
|
+
}
|
|
418
|
+
function markdownCodeSpan(value) {
|
|
419
|
+
const delimiter = backtickDelimiter(value, 1);
|
|
420
|
+
const pad = /^`|`$|^ .* $/s.test(value) ? " " : "";
|
|
421
|
+
return `${delimiter}${pad}${value}${pad}${delimiter}`;
|
|
422
|
+
}
|
|
423
|
+
function literalEnd(value, start) {
|
|
424
|
+
if (value.startsWith("{{", start)) {
|
|
425
|
+
const closing = /}{2,}/.exec(value.slice(start + 2));
|
|
426
|
+
return closing ? start + 2 + closing.index + closing[0].length : null;
|
|
427
|
+
}
|
|
428
|
+
if (value[start] === "[" || value[start] === "{") {
|
|
429
|
+
const close = value[start] === "[" ? "]" : "}";
|
|
430
|
+
for (let index = start + 1; index < value.length; index++) {
|
|
431
|
+
if (value[index] === "\\") index++;
|
|
432
|
+
else if (value[index] === close) return index + 1;
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
return null;
|
|
436
|
+
}
|
|
437
|
+
function jiraEmphasisEnd(value, start, delimiter) {
|
|
438
|
+
for (let index = start + 1; index < value.length; index++) {
|
|
439
|
+
if (value[index] === "\\") {
|
|
440
|
+
index++;
|
|
441
|
+
continue;
|
|
442
|
+
}
|
|
443
|
+
const protectedEnd = literalEnd(value, index);
|
|
444
|
+
if (protectedEnd !== null) {
|
|
445
|
+
index = protectedEnd - 1;
|
|
446
|
+
continue;
|
|
447
|
+
}
|
|
448
|
+
if (value[index] === delimiter && index > start + 1 && !/\s/.test(value[index - 1] ?? "") && !/[\w]/.test(value[index + 1] ?? ""))
|
|
449
|
+
return index;
|
|
450
|
+
}
|
|
451
|
+
return null;
|
|
452
|
+
}
|
|
453
|
+
function jiraInlineToMarkdown(value) {
|
|
454
|
+
const output = [];
|
|
455
|
+
for (let index = 0; index < value.length; index++) {
|
|
456
|
+
const char = value[index] ?? "";
|
|
457
|
+
if (value.startsWith("\\\\", index)) {
|
|
458
|
+
output.push(" \n");
|
|
459
|
+
index++;
|
|
460
|
+
continue;
|
|
461
|
+
}
|
|
462
|
+
if (char === "\\" && /[\\*_{}[\]|!+\-^~]/.test(value[index + 1] ?? "")) {
|
|
463
|
+
output.push(escapeMarkdown(value[++index] ?? ""));
|
|
464
|
+
continue;
|
|
465
|
+
}
|
|
466
|
+
const end = literalEnd(value, index);
|
|
467
|
+
if (end !== null && value.startsWith("{{", index)) {
|
|
468
|
+
output.push(markdownCodeSpan(value.slice(index + 2, end - 2)));
|
|
469
|
+
index = end - 1;
|
|
470
|
+
continue;
|
|
471
|
+
}
|
|
472
|
+
if (end !== null && char === "[") {
|
|
473
|
+
const link = value.slice(index + 1, end - 1);
|
|
474
|
+
const separator = link.search(/(?<!\\)\|/);
|
|
475
|
+
const target = (separator < 0 ? link : link.slice(separator + 1)).trim();
|
|
476
|
+
if (/^(https?:\/\/|mailto:)/i.test(target)) {
|
|
477
|
+
const label = separator < 0 ? escapeMarkdown(link) : jiraInlineToMarkdown(link.slice(0, separator));
|
|
478
|
+
const url = target.replace(/[<>\s\\]/g, (c) => encodeURIComponent(c));
|
|
479
|
+
output.push(`[${label}](${/[()]/.test(url) ? `<${url}>` : url})`);
|
|
480
|
+
} else output.push(escapeMarkdown(value.slice(index, end)));
|
|
481
|
+
index = end - 1;
|
|
482
|
+
continue;
|
|
483
|
+
}
|
|
484
|
+
if (end !== null && char === "{") {
|
|
485
|
+
output.push(escapeMarkdown(value.slice(index, end)));
|
|
486
|
+
index = end - 1;
|
|
487
|
+
continue;
|
|
488
|
+
}
|
|
489
|
+
const delimiter = char === "*" ? "**" : char === "_" ? "*" : char === "-" ? "~~" : null;
|
|
490
|
+
if (delimiter && !/[\w]/.test(value[index - 1] ?? "") && !/[\s]/.test(value[index + 1] ?? "") && value[index + 1] !== char) {
|
|
491
|
+
const close = jiraEmphasisEnd(value, index, char);
|
|
492
|
+
if (close !== null) {
|
|
493
|
+
output.push(
|
|
494
|
+
`${delimiter}${jiraInlineToMarkdown(value.slice(index + 1, close))}${delimiter}`
|
|
495
|
+
);
|
|
496
|
+
index = close;
|
|
497
|
+
continue;
|
|
498
|
+
}
|
|
499
|
+
}
|
|
500
|
+
output.push(escapeMarkdown(char));
|
|
501
|
+
}
|
|
502
|
+
return output.join("");
|
|
503
|
+
}
|
|
504
|
+
function jiraTableRow(line) {
|
|
505
|
+
const row = line.trim();
|
|
506
|
+
if (!row.startsWith("|") || !row.endsWith("|")) return null;
|
|
507
|
+
const cells = [];
|
|
508
|
+
let cell = "";
|
|
509
|
+
const start = row.startsWith("||") ? 2 : 1;
|
|
510
|
+
const end = row.length - (row.endsWith("||") ? 2 : 1);
|
|
511
|
+
for (let index = start; index < end; index++) {
|
|
512
|
+
if (row[index] === "\\" && index + 1 < end) cell += row[index] + row[++index];
|
|
513
|
+
else {
|
|
514
|
+
const protectedEnd = literalEnd(row, index);
|
|
515
|
+
if (protectedEnd !== null) {
|
|
516
|
+
cell += row.slice(index, protectedEnd);
|
|
517
|
+
index = protectedEnd - 1;
|
|
518
|
+
} else if (row[index] === "|") {
|
|
519
|
+
cells.push(cell.trim());
|
|
520
|
+
cell = "";
|
|
521
|
+
if (row[index + 1] === "|") index++;
|
|
522
|
+
} else cell += row[index];
|
|
523
|
+
}
|
|
524
|
+
}
|
|
525
|
+
cells.push(cell.trim());
|
|
526
|
+
return { cells, header: start === 2 };
|
|
527
|
+
}
|
|
528
|
+
function jiraTextToMarkdown(value) {
|
|
529
|
+
const lines = value.split("\n");
|
|
530
|
+
const output = [];
|
|
531
|
+
for (let index = 0; index < lines.length; index++) {
|
|
532
|
+
const line = lines[index] ?? "";
|
|
533
|
+
const table = jiraTableRow(line);
|
|
534
|
+
if (table) {
|
|
535
|
+
const rows = [table];
|
|
536
|
+
while (index + 1 < lines.length) {
|
|
537
|
+
const next = jiraTableRow(lines[index + 1] ?? "");
|
|
538
|
+
if (!next) break;
|
|
539
|
+
rows.push(next);
|
|
540
|
+
index++;
|
|
541
|
+
}
|
|
542
|
+
const width = Math.max(...rows.map((row) => row.cells.length));
|
|
543
|
+
const renderRow = (cells) => `| ${Array.from(
|
|
544
|
+
{ length: width },
|
|
545
|
+
(_, column) => jiraInlineToMarkdown(cells[column] ?? "").replace(/(?<!\\)\|/g, "\\|")
|
|
546
|
+
).join(" | ")} |`;
|
|
547
|
+
const header = table.header ? rows.shift()?.cells ?? [] : [];
|
|
548
|
+
output.push(
|
|
549
|
+
renderRow(header),
|
|
550
|
+
`| ${Array(width).fill("---").join(" | ")} |`,
|
|
551
|
+
...rows.map((row) => renderRow(row.cells))
|
|
552
|
+
);
|
|
553
|
+
continue;
|
|
554
|
+
}
|
|
555
|
+
const heading = /^h([1-6])\.\s+(.*)$/.exec(line);
|
|
556
|
+
const list = /^\s*([*#]+)\s+(.*)$/.exec(line);
|
|
557
|
+
if (heading)
|
|
558
|
+
output.push(`${"#".repeat(Number(heading[1]))} ${jiraInlineToMarkdown(heading[2] ?? "")}`);
|
|
559
|
+
else if (list) {
|
|
560
|
+
const markers = list[1] ?? "*";
|
|
561
|
+
output.push(
|
|
562
|
+
`${" ".repeat(markers.length - 1)}${markers.endsWith("#") ? "1." : "-"} ${jiraInlineToMarkdown(list[2] ?? "")}`
|
|
563
|
+
);
|
|
564
|
+
} else if (/^bq\.\s?/.test(line))
|
|
565
|
+
output.push(`> ${jiraInlineToMarkdown(line.replace(/^bq\.\s?/, ""))}`);
|
|
566
|
+
else if (/^-{4,}\s*$/.test(line)) output.push("---");
|
|
567
|
+
else output.push(jiraInlineToMarkdown(line));
|
|
568
|
+
}
|
|
569
|
+
return output.join("\n");
|
|
570
|
+
}
|
|
571
|
+
function jiraToMarkdown(markup) {
|
|
572
|
+
const value = normalizeNewlines(markup ?? "");
|
|
573
|
+
const output = [];
|
|
574
|
+
let cursor = 0;
|
|
575
|
+
for (let index = 0; index < value.length; index++) {
|
|
576
|
+
if (value[index] === "\\") {
|
|
577
|
+
index++;
|
|
578
|
+
continue;
|
|
579
|
+
}
|
|
580
|
+
const match = /^\{(code|noformat|quote)(?::([^}\n]*))?}/.exec(value.slice(index));
|
|
581
|
+
if (!match || value.startsWith("{{", index)) {
|
|
582
|
+
const protectedEnd = literalEnd(value, index);
|
|
583
|
+
if (protectedEnd !== null) index = protectedEnd - 1;
|
|
584
|
+
continue;
|
|
585
|
+
}
|
|
586
|
+
const start = index + match[0].length;
|
|
587
|
+
const end = value.indexOf(`{${match[1]}}`, start);
|
|
588
|
+
if (end < 0) continue;
|
|
589
|
+
const before = jiraTextToMarkdown(value.slice(cursor, index)).replace(/^\n+|\n+$/g, "");
|
|
590
|
+
if (before) output.push(before);
|
|
591
|
+
const content = value.slice(start, end).replace(/^\n|\n$/g, "");
|
|
592
|
+
if (match[1] === "quote")
|
|
593
|
+
output.push(
|
|
594
|
+
jiraToMarkdown(content).split("\n").map((line) => `> ${line}`).join("\n")
|
|
595
|
+
);
|
|
596
|
+
else {
|
|
597
|
+
const params = match[2] ?? "";
|
|
598
|
+
const language = /(?:^|\|)language=([\w+-]+)/.exec(params)?.[1] ?? /^([\w+-]+)(?:\||$)/.exec(params)?.[1] ?? "";
|
|
599
|
+
const fence = backtickDelimiter(content, 3);
|
|
600
|
+
output.push(`${fence}${language}
|
|
601
|
+
${content}
|
|
602
|
+
${fence}`);
|
|
603
|
+
}
|
|
604
|
+
cursor = end + `{${match[1]}}`.length;
|
|
605
|
+
index = cursor - 1;
|
|
606
|
+
}
|
|
607
|
+
const after = jiraTextToMarkdown(value.slice(cursor)).replace(/^\n+|\n+$/g, "");
|
|
608
|
+
if (after) output.push(after);
|
|
609
|
+
return output.join("\n\n");
|
|
610
|
+
}
|
|
611
|
+
|
|
612
|
+
// src/tools.ts
|
|
613
|
+
import { createHash as createHash2 } from "crypto";
|
|
614
|
+
import { projectParameters as projectParameters2, stringArg as stringArg2 } from "@leitwerk-dev/process-sdk";
|
|
615
|
+
|
|
616
|
+
// src/workflow-tools.ts
|
|
617
|
+
import { createHash } from "crypto";
|
|
618
|
+
import { basename } from "path";
|
|
619
|
+
import { asUnknownRecord as asUnknownRecord2 } from "@leitwerk-dev/domain";
|
|
620
|
+
import {
|
|
621
|
+
coreHostCapabilities,
|
|
622
|
+
projectParameters,
|
|
623
|
+
stringArg
|
|
624
|
+
} from "@leitwerk-dev/process-sdk";
|
|
625
|
+
function resolveJiraSourceClient(ctx, integration) {
|
|
626
|
+
const binding = asUnknownRecord2(ctx.project?.metadata?.jira);
|
|
627
|
+
if (typeof binding?.profile !== "string" || typeof binding.issueId !== "string" || typeof binding.baseUrl !== "string")
|
|
628
|
+
throw new Error("Missing Jira source binding");
|
|
629
|
+
const client = integration.client(binding.profile);
|
|
630
|
+
if (client.baseUrl !== binding.baseUrl) throw new Error("Jira profile installation changed");
|
|
631
|
+
return { client, id: binding.issueId, binding };
|
|
632
|
+
}
|
|
633
|
+
var digest = (parts) => createHash("sha256").update(JSON.stringify(parts)).digest("hex");
|
|
634
|
+
var statusName = (name) => name?.trim().toLowerCase();
|
|
635
|
+
function registerJiraWorkflowTools(api, integration) {
|
|
636
|
+
api.tool({
|
|
637
|
+
name: "jira_ensure_remote_link",
|
|
638
|
+
description: "Link the current Leitwerk process or its pinned merge request to the Jira source",
|
|
639
|
+
parameters: projectParameters(
|
|
640
|
+
{ kind: { type: "string", enum: ["process", "merge_request"] } },
|
|
641
|
+
["kind"]
|
|
642
|
+
),
|
|
643
|
+
async execute(ctx, args) {
|
|
644
|
+
const { client, id } = resolveJiraSourceClient(ctx, integration);
|
|
645
|
+
const kind = stringArg(args, "kind");
|
|
646
|
+
let url;
|
|
647
|
+
let title;
|
|
648
|
+
let target;
|
|
649
|
+
if (kind === "process") {
|
|
650
|
+
const deps = api.get?.(coreHostCapabilities.serverSetup);
|
|
651
|
+
const serverBaseUrl = deps && !Array.isArray(deps) ? deps.serverBaseUrl : void 0;
|
|
652
|
+
if (!serverBaseUrl) throw new Error("Leitwerk public server URL is unavailable");
|
|
653
|
+
url = `${serverBaseUrl.replace(/\/+$/, "")}/processes/${encodeURIComponent(ctx.process.id)}`;
|
|
654
|
+
title = `Leitwerk process ${ctx.process.id}`;
|
|
655
|
+
target = ctx.process.id;
|
|
656
|
+
} else if (kind === "merge_request") {
|
|
657
|
+
const mr = asUnknownRecord2(ctx.project?.metadata?.gitlab);
|
|
658
|
+
if (!ctx.project?.externalUrl || !ctx.project.externalId || typeof mr?.iid !== "number" || String(mr.iid) !== ctx.project.externalId)
|
|
659
|
+
throw new Error("A pinned merge request is required for the Jira link");
|
|
660
|
+
url = ctx.project.externalUrl;
|
|
661
|
+
const repositoryName = basename(ctx.project.repoLocator).replace(/\.git$/, "");
|
|
662
|
+
title = `GitLab ${repositoryName} !${ctx.project.externalId}`;
|
|
663
|
+
target = JSON.stringify([ctx.project.id, ctx.project.externalId]);
|
|
664
|
+
} else throw new Error("Unknown Jira remote link kind");
|
|
665
|
+
const key = digest([client.baseUrl, id, kind, target]);
|
|
666
|
+
const globalId = `leitwerk:${key}`;
|
|
667
|
+
const intended = { globalId, object: { url, title } };
|
|
668
|
+
const find = async () => (await client.listRemoteLinks(id, ctx.signal)).find((link2) => link2.globalId === globalId) ?? null;
|
|
669
|
+
const link = await ctx.externalWrites.ensure(
|
|
670
|
+
{ writeType: "jira.remote_link", dedupKey: key },
|
|
671
|
+
{
|
|
672
|
+
reconcile: async (phase) => {
|
|
673
|
+
const link2 = await find();
|
|
674
|
+
return link2 && (phase === "already_recorded" || link2.object.url === url && link2.object.title === title) ? link2 : null;
|
|
675
|
+
},
|
|
676
|
+
execute: async () => {
|
|
677
|
+
await client.upsertRemoteLink(id, intended, ctx.signal);
|
|
678
|
+
const link2 = await find();
|
|
679
|
+
if (!link2 || link2.object.url !== url || link2.object.title !== title)
|
|
680
|
+
throw new Error("Jira remote link read-back did not match");
|
|
681
|
+
return link2;
|
|
682
|
+
},
|
|
683
|
+
toMetadata: (link2) => ({ linkId: link2.id, globalId: link2.globalId })
|
|
684
|
+
}
|
|
685
|
+
);
|
|
686
|
+
return { linkId: link.id, url };
|
|
687
|
+
}
|
|
688
|
+
});
|
|
689
|
+
api.tool({
|
|
690
|
+
name: "jira_transition_source_issue",
|
|
691
|
+
description: "Reconcile the Jira source ticket's work, review or verification status",
|
|
692
|
+
parameters: projectParameters(
|
|
693
|
+
{
|
|
694
|
+
targetStatus: {
|
|
695
|
+
type: "string",
|
|
696
|
+
enum: ["In Progress", "In Review", "Ready for Verification", "In Verification"]
|
|
697
|
+
}
|
|
698
|
+
},
|
|
699
|
+
["targetStatus"]
|
|
700
|
+
),
|
|
701
|
+
async execute(ctx, args) {
|
|
702
|
+
const { client, id } = resolveJiraSourceClient(ctx, integration);
|
|
703
|
+
const targetStatus = stringArg(args, "targetStatus");
|
|
704
|
+
if (!["In Progress", "In Review", "Ready for Verification", "In Verification"].includes(
|
|
705
|
+
targetStatus
|
|
706
|
+
))
|
|
707
|
+
throw new Error("Unsupported Jira workflow target status");
|
|
708
|
+
const target = statusName(targetStatus);
|
|
709
|
+
const reconcile = async (phase) => {
|
|
710
|
+
const { status } = (await client.getIssue(id, ctx.signal)).fields;
|
|
711
|
+
const current = statusName(status.name);
|
|
712
|
+
return phase === "already_recorded" || current === target || status.statusCategory.key === "done" || current === "in verification" || current === "ready for verification" && target !== "in verification" || target === "in progress" && current === "in review" ? { issueId: id, status: status.name ?? status.statusCategory.key } : null;
|
|
713
|
+
};
|
|
714
|
+
return ctx.externalWrites.ensure(
|
|
715
|
+
{
|
|
716
|
+
writeType: "jira.transition",
|
|
717
|
+
dedupKey: digest([client.baseUrl, id, ctx.process.id, target])
|
|
718
|
+
},
|
|
719
|
+
{
|
|
720
|
+
reconcile,
|
|
721
|
+
execute: async () => {
|
|
722
|
+
const current = await reconcile("before_execute");
|
|
723
|
+
if (current) return current;
|
|
724
|
+
const transitions = (await client.listTransitions(id, ctx.signal)).filter(
|
|
725
|
+
(transition) => statusName(transition.to.name) === target
|
|
726
|
+
);
|
|
727
|
+
if (transitions.length !== 1)
|
|
728
|
+
throw new Error(
|
|
729
|
+
`Jira requires one available transition to ${targetStatus}; found ${transitions.length}`
|
|
730
|
+
);
|
|
731
|
+
await client.transitionIssue(id, transitions[0].id, ctx.signal);
|
|
732
|
+
const result = await reconcile("after_execute_error");
|
|
733
|
+
if (!result) throw new Error(`Jira did not reach ${targetStatus}`);
|
|
734
|
+
return result;
|
|
735
|
+
},
|
|
736
|
+
toMetadata: (result) => result
|
|
737
|
+
}
|
|
738
|
+
);
|
|
739
|
+
}
|
|
740
|
+
});
|
|
741
|
+
}
|
|
742
|
+
|
|
743
|
+
// src/tools.ts
|
|
744
|
+
function registerJiraTools(api, integration) {
|
|
745
|
+
for (const name of [
|
|
746
|
+
"jira_get_source_issue",
|
|
747
|
+
"jira_get_issue",
|
|
748
|
+
"jira_comment",
|
|
749
|
+
"jira_finalize_source_issue"
|
|
750
|
+
])
|
|
751
|
+
api.tool({
|
|
752
|
+
name,
|
|
753
|
+
description: name === "jira_get_source_issue" || name === "jira_get_issue" ? name === "jira_get_source_issue" ? "Read the launch-pinned Jira source issue. Use descriptionMarkdown for its converted Markdown description; fields.description retains the original Jira wiki markup." : "Read a Jira issue by key or ID using the current process project's bound Jira profile. Use descriptionMarkdown for converted Markdown; fields.description retains Jira wiki markup." : "Reconcile the launch-pinned Jira source issue",
|
|
754
|
+
parameters: name === "jira_get_source_issue" ? projectParameters2() : name === "jira_get_issue" ? projectParameters2({ issueKey: { type: "string" } }) : projectParameters2({
|
|
755
|
+
body: { type: "string" },
|
|
756
|
+
writeKey: { type: "string" },
|
|
757
|
+
done: { type: "boolean" }
|
|
758
|
+
}),
|
|
759
|
+
async execute(ctx, args) {
|
|
760
|
+
const { binding, client, id } = resolveJiraSourceClient(ctx, integration);
|
|
761
|
+
if (name === "jira_get_source_issue" || name === "jira_get_issue") {
|
|
762
|
+
const issue = await client.getIssue(
|
|
763
|
+
name === "jira_get_source_issue" ? id : stringArg2(args, "issueKey"),
|
|
764
|
+
ctx.signal
|
|
765
|
+
);
|
|
766
|
+
return { ...issue, descriptionMarkdown: jiraToMarkdown(issue.fields.description) };
|
|
767
|
+
}
|
|
768
|
+
const digest2 = createHash2("sha256").update(JSON.stringify([client.baseUrl, id, ctx.process.id, stringArg2(args, "writeKey")])).digest("hex");
|
|
769
|
+
if (name === "jira_comment") {
|
|
770
|
+
const marker = `{noformat}leitwerk:${digest2}{noformat}`;
|
|
771
|
+
return ctx.externalWrites.ensure(
|
|
772
|
+
{ writeType: name, dedupKey: digest2 },
|
|
773
|
+
{
|
|
774
|
+
reconcile: async () => (await client.listComments(id)).find((comment) => comment.body.includes(marker)) ?? null,
|
|
775
|
+
execute: () => client.addComment(id, `${stringArg2(args, "body")}
|
|
776
|
+
|
|
777
|
+
${marker}`),
|
|
778
|
+
toMetadata: (comment) => ({ commentId: comment.id })
|
|
779
|
+
}
|
|
780
|
+
);
|
|
781
|
+
}
|
|
782
|
+
const done = args.done === true;
|
|
783
|
+
const triggerLabel = jiraTriggerLabel(binding.triggerLabel);
|
|
784
|
+
return ctx.externalWrites.ensure(
|
|
785
|
+
{ writeType: name, dedupKey: digest2 },
|
|
786
|
+
{
|
|
787
|
+
reconcile: async () => {
|
|
788
|
+
const issue = await client.getIssue(id);
|
|
789
|
+
return !issue.fields.labels.includes(triggerLabel) && (!done || issue.fields.labels.includes("leitwerk-done")) ? { issueId: id, done } : null;
|
|
790
|
+
},
|
|
791
|
+
execute: async () => {
|
|
792
|
+
await client.updateLabels(id, [triggerLabel], done ? ["leitwerk-done"] : []);
|
|
793
|
+
return { issueId: id, done };
|
|
794
|
+
},
|
|
795
|
+
toMetadata: (result) => result
|
|
796
|
+
}
|
|
797
|
+
);
|
|
798
|
+
}
|
|
799
|
+
});
|
|
800
|
+
registerJiraWorkflowTools(api, integration);
|
|
801
|
+
}
|
|
802
|
+
|
|
803
|
+
export {
|
|
804
|
+
JiraRequestError,
|
|
805
|
+
jiraBaseUrl,
|
|
806
|
+
parseJiraProfiles,
|
|
807
|
+
JiraClient,
|
|
808
|
+
jiraTriggerLabel,
|
|
809
|
+
jiraEligible,
|
|
810
|
+
jiraIssueExternalId,
|
|
811
|
+
jiraIsEpic,
|
|
812
|
+
jiraSplitEligible,
|
|
813
|
+
jiraSplitChildMatches,
|
|
814
|
+
markdownToJira,
|
|
815
|
+
jiraToMarkdown,
|
|
816
|
+
registerJiraTools
|
|
817
|
+
};
|
|
818
|
+
//# sourceMappingURL=chunk-VUXLYUPG.js.map
|