@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
package/src/tools.ts
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import { projectParameters, type ServerExtensionAPI, stringArg } from "@leitwerk-dev/process-sdk";
|
|
3
|
+
import { jiraTriggerLabel } from "./client.js";
|
|
4
|
+
import type { JiraIntegration } from "./index.js";
|
|
5
|
+
import { jiraToMarkdown } from "./markdown.js";
|
|
6
|
+
import { registerJiraWorkflowTools, resolveJiraSourceClient } from "./workflow-tools.js";
|
|
7
|
+
|
|
8
|
+
/** @internal */
|
|
9
|
+
export function registerJiraTools(api: ServerExtensionAPI, integration: JiraIntegration) {
|
|
10
|
+
for (const name of [
|
|
11
|
+
"jira_get_source_issue",
|
|
12
|
+
"jira_get_issue",
|
|
13
|
+
"jira_comment",
|
|
14
|
+
"jira_finalize_source_issue",
|
|
15
|
+
] as const)
|
|
16
|
+
api.tool<Record<string, unknown>>({
|
|
17
|
+
name,
|
|
18
|
+
description:
|
|
19
|
+
name === "jira_get_source_issue" || name === "jira_get_issue"
|
|
20
|
+
? name === "jira_get_source_issue"
|
|
21
|
+
? "Read the launch-pinned Jira source issue. Use descriptionMarkdown for its converted Markdown description; fields.description retains the original Jira wiki markup."
|
|
22
|
+
: "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."
|
|
23
|
+
: "Reconcile the launch-pinned Jira source issue",
|
|
24
|
+
parameters:
|
|
25
|
+
name === "jira_get_source_issue"
|
|
26
|
+
? projectParameters()
|
|
27
|
+
: name === "jira_get_issue"
|
|
28
|
+
? projectParameters({ issueKey: { type: "string" } })
|
|
29
|
+
: projectParameters({
|
|
30
|
+
body: { type: "string" },
|
|
31
|
+
writeKey: { type: "string" },
|
|
32
|
+
done: { type: "boolean" },
|
|
33
|
+
}),
|
|
34
|
+
async execute(ctx, args) {
|
|
35
|
+
const { binding, client, id } = resolveJiraSourceClient(ctx, integration);
|
|
36
|
+
if (name === "jira_get_source_issue" || name === "jira_get_issue") {
|
|
37
|
+
const issue = await client.getIssue(
|
|
38
|
+
name === "jira_get_source_issue" ? id : stringArg(args, "issueKey"),
|
|
39
|
+
ctx.signal,
|
|
40
|
+
);
|
|
41
|
+
return { ...issue, descriptionMarkdown: jiraToMarkdown(issue.fields.description) };
|
|
42
|
+
}
|
|
43
|
+
const digest = createHash("sha256")
|
|
44
|
+
.update(JSON.stringify([client.baseUrl, id, ctx.process.id, stringArg(args, "writeKey")]))
|
|
45
|
+
.digest("hex");
|
|
46
|
+
if (name === "jira_comment") {
|
|
47
|
+
const marker = `{noformat}leitwerk:${digest}{noformat}`;
|
|
48
|
+
return ctx.externalWrites.ensure(
|
|
49
|
+
{ writeType: name, dedupKey: digest },
|
|
50
|
+
{
|
|
51
|
+
reconcile: async () =>
|
|
52
|
+
(await client.listComments(id)).find((comment) => comment.body.includes(marker)) ??
|
|
53
|
+
null,
|
|
54
|
+
execute: () => client.addComment(id, `${stringArg(args, "body")}\n\n${marker}`),
|
|
55
|
+
toMetadata: (comment) => ({ commentId: comment.id }),
|
|
56
|
+
},
|
|
57
|
+
);
|
|
58
|
+
}
|
|
59
|
+
const done = args.done === true;
|
|
60
|
+
const triggerLabel = jiraTriggerLabel(binding.triggerLabel);
|
|
61
|
+
return ctx.externalWrites.ensure(
|
|
62
|
+
{ writeType: name, dedupKey: digest },
|
|
63
|
+
{
|
|
64
|
+
reconcile: async () => {
|
|
65
|
+
const issue = await client.getIssue(id);
|
|
66
|
+
return !issue.fields.labels.includes(triggerLabel) &&
|
|
67
|
+
(!done || issue.fields.labels.includes("leitwerk-done"))
|
|
68
|
+
? { issueId: id, done }
|
|
69
|
+
: null;
|
|
70
|
+
},
|
|
71
|
+
execute: async () => {
|
|
72
|
+
await client.updateLabels(id, [triggerLabel], done ? ["leitwerk-done"] : []);
|
|
73
|
+
return { issueId: id, done };
|
|
74
|
+
},
|
|
75
|
+
toMetadata: (result) => result,
|
|
76
|
+
},
|
|
77
|
+
);
|
|
78
|
+
},
|
|
79
|
+
});
|
|
80
|
+
registerJiraWorkflowTools(api, integration);
|
|
81
|
+
}
|
package/src/wiki.ts
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import type { TopicWikiStore } from "@leitwerk-dev/wiki";
|
|
3
|
+
import type { WikiSourceResolver } from "@leitwerk-dev/wiki/integration";
|
|
4
|
+
import { type JiraClientLike, type JiraIssue, jiraIsEpic } from "./client.js";
|
|
5
|
+
import type { JiraIntegration } from "./index.js";
|
|
6
|
+
|
|
7
|
+
/** @internal */
|
|
8
|
+
export function jiraEpicRevision(epic: JiraIssue): string {
|
|
9
|
+
return createHash("sha256")
|
|
10
|
+
.update(
|
|
11
|
+
JSON.stringify([
|
|
12
|
+
epic.id,
|
|
13
|
+
epic.fields.project.id,
|
|
14
|
+
epic.fields.summary,
|
|
15
|
+
epic.fields.description,
|
|
16
|
+
epic.fields.components,
|
|
17
|
+
epic.fields.status,
|
|
18
|
+
]),
|
|
19
|
+
)
|
|
20
|
+
.digest("hex");
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** @internal */
|
|
24
|
+
export function ensureIssueWiki(
|
|
25
|
+
store: TopicWikiStore,
|
|
26
|
+
client: JiraClientLike,
|
|
27
|
+
issue: JiraIssue,
|
|
28
|
+
retainedTopicId?: string,
|
|
29
|
+
) {
|
|
30
|
+
let key = JSON.stringify([
|
|
31
|
+
jiraIsEpic(issue) ? "jira.epic" : "jira.issue",
|
|
32
|
+
client.baseUrl,
|
|
33
|
+
issue.id,
|
|
34
|
+
]);
|
|
35
|
+
if (retainedTopicId !== undefined) {
|
|
36
|
+
// Only callers with a durable publication receipt or process binding may supply an ID.
|
|
37
|
+
// Older publishers used their own namespace for the same installation / issue identity.
|
|
38
|
+
const topic = store.getTopic(retainedTopicId);
|
|
39
|
+
let identity: unknown;
|
|
40
|
+
try {
|
|
41
|
+
identity = topic && JSON.parse(topic.key);
|
|
42
|
+
} catch {
|
|
43
|
+
identity = null;
|
|
44
|
+
}
|
|
45
|
+
if (
|
|
46
|
+
!topic ||
|
|
47
|
+
!Array.isArray(identity) ||
|
|
48
|
+
identity.length !== 3 ||
|
|
49
|
+
typeof identity[0] !== "string" ||
|
|
50
|
+
!identity[0] ||
|
|
51
|
+
identity[1] !== client.baseUrl ||
|
|
52
|
+
identity[2] !== issue.id
|
|
53
|
+
)
|
|
54
|
+
throw new Error("Source issue wiki binding mismatch");
|
|
55
|
+
key = topic.key;
|
|
56
|
+
}
|
|
57
|
+
return store.ensureTopic({
|
|
58
|
+
key,
|
|
59
|
+
title: `${issue.key}: ${issue.fields.summary}`,
|
|
60
|
+
url: `${client.baseUrl}/browse/${encodeURIComponent(issue.key)}`,
|
|
61
|
+
sourceRevision: jiraEpicRevision(issue),
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** Refreshes retained current and legacy issue bindings; credentials stay in the integration. @internal */
|
|
66
|
+
export function jiraWikiSource(
|
|
67
|
+
store: TopicWikiStore,
|
|
68
|
+
integration: JiraIntegration,
|
|
69
|
+
): WikiSourceResolver {
|
|
70
|
+
return async (_ctx, binding) => {
|
|
71
|
+
const issueId = binding.issueId ?? binding.epicId;
|
|
72
|
+
if (
|
|
73
|
+
typeof binding.topicId !== "string" ||
|
|
74
|
+
typeof binding.profile !== "string" ||
|
|
75
|
+
typeof issueId !== "string"
|
|
76
|
+
)
|
|
77
|
+
throw new Error("This process has no source issue wiki binding");
|
|
78
|
+
const client = integration.client(binding.profile);
|
|
79
|
+
if (client.baseUrl !== binding.baseUrl) throw new Error("Jira wiki installation changed");
|
|
80
|
+
const issue = await client.getIssue(issueId);
|
|
81
|
+
if (issue.id !== issueId) throw new Error("Source issue wiki binding mismatch");
|
|
82
|
+
return {
|
|
83
|
+
topic: ensureIssueWiki(store, client, issue, binding.topicId),
|
|
84
|
+
sourceDescription: issue.fields.description ?? "",
|
|
85
|
+
};
|
|
86
|
+
};
|
|
87
|
+
}
|
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import { basename } from "node:path";
|
|
3
|
+
import { asUnknownRecord } from "@leitwerk-dev/domain";
|
|
4
|
+
import {
|
|
5
|
+
coreHostCapabilities,
|
|
6
|
+
type IntegrationToolExecutionContext,
|
|
7
|
+
projectParameters,
|
|
8
|
+
type ServerExtensionAPI,
|
|
9
|
+
stringArg,
|
|
10
|
+
} from "@leitwerk-dev/process-sdk";
|
|
11
|
+
import type { JiraIntegration } from "./index.js";
|
|
12
|
+
|
|
13
|
+
/** @internal */
|
|
14
|
+
export function resolveJiraSourceClient(
|
|
15
|
+
ctx: IntegrationToolExecutionContext,
|
|
16
|
+
integration: JiraIntegration,
|
|
17
|
+
) {
|
|
18
|
+
const binding = asUnknownRecord(ctx.project?.metadata?.jira);
|
|
19
|
+
if (
|
|
20
|
+
typeof binding?.profile !== "string" ||
|
|
21
|
+
typeof binding.issueId !== "string" ||
|
|
22
|
+
typeof binding.baseUrl !== "string"
|
|
23
|
+
)
|
|
24
|
+
throw new Error("Missing Jira source binding");
|
|
25
|
+
const client = integration.client(binding.profile);
|
|
26
|
+
if (client.baseUrl !== binding.baseUrl) throw new Error("Jira profile installation changed");
|
|
27
|
+
return { client, id: binding.issueId, binding };
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const digest = (parts: unknown[]) =>
|
|
31
|
+
createHash("sha256").update(JSON.stringify(parts)).digest("hex");
|
|
32
|
+
const statusName = (name: string | undefined) => name?.trim().toLowerCase();
|
|
33
|
+
|
|
34
|
+
/** @internal */
|
|
35
|
+
export function registerJiraWorkflowTools(api: ServerExtensionAPI, integration: JiraIntegration) {
|
|
36
|
+
api.tool<Record<string, unknown>>({
|
|
37
|
+
name: "jira_ensure_remote_link",
|
|
38
|
+
description: "Link the current Leitwerk process or its pinned merge request to the Jira source",
|
|
39
|
+
parameters: projectParameters(
|
|
40
|
+
{ kind: { type: "string", enum: ["process", "merge_request"] } },
|
|
41
|
+
["kind"],
|
|
42
|
+
),
|
|
43
|
+
async execute(ctx, args) {
|
|
44
|
+
const { client, id } = resolveJiraSourceClient(ctx, integration);
|
|
45
|
+
const kind = stringArg(args, "kind");
|
|
46
|
+
let url: string;
|
|
47
|
+
let title: string;
|
|
48
|
+
let target: string;
|
|
49
|
+
if (kind === "process") {
|
|
50
|
+
const deps = api.get?.(coreHostCapabilities.serverSetup);
|
|
51
|
+
const serverBaseUrl = deps && !Array.isArray(deps) ? deps.serverBaseUrl : undefined;
|
|
52
|
+
if (!serverBaseUrl) throw new Error("Leitwerk public server URL is unavailable");
|
|
53
|
+
url = `${serverBaseUrl.replace(/\/+$/, "")}/processes/${encodeURIComponent(ctx.process.id)}`;
|
|
54
|
+
title = `Leitwerk process ${ctx.process.id}`;
|
|
55
|
+
target = ctx.process.id;
|
|
56
|
+
} else if (kind === "merge_request") {
|
|
57
|
+
const mr = asUnknownRecord(ctx.project?.metadata?.gitlab);
|
|
58
|
+
if (
|
|
59
|
+
!ctx.project?.externalUrl ||
|
|
60
|
+
!ctx.project.externalId ||
|
|
61
|
+
typeof mr?.iid !== "number" ||
|
|
62
|
+
String(mr.iid) !== ctx.project.externalId
|
|
63
|
+
)
|
|
64
|
+
throw new Error("A pinned merge request is required for the Jira link");
|
|
65
|
+
url = ctx.project.externalUrl;
|
|
66
|
+
const repositoryName = basename(ctx.project.repoLocator).replace(/\.git$/, "");
|
|
67
|
+
title = `GitLab ${repositoryName} !${ctx.project.externalId}`;
|
|
68
|
+
target = JSON.stringify([ctx.project.id, ctx.project.externalId]);
|
|
69
|
+
} else throw new Error("Unknown Jira remote link kind");
|
|
70
|
+
const key = digest([client.baseUrl, id, kind, target]);
|
|
71
|
+
const globalId = `leitwerk:${key}`;
|
|
72
|
+
const intended = { globalId, object: { url, title } };
|
|
73
|
+
const find = async () =>
|
|
74
|
+
(await client.listRemoteLinks(id, ctx.signal)).find((link) => link.globalId === globalId) ??
|
|
75
|
+
null;
|
|
76
|
+
const link = await ctx.externalWrites.ensure(
|
|
77
|
+
{ writeType: "jira.remote_link", dedupKey: key },
|
|
78
|
+
{
|
|
79
|
+
reconcile: async (phase) => {
|
|
80
|
+
const link = await find();
|
|
81
|
+
return link &&
|
|
82
|
+
(phase === "already_recorded" ||
|
|
83
|
+
(link.object.url === url && link.object.title === title))
|
|
84
|
+
? link
|
|
85
|
+
: null;
|
|
86
|
+
},
|
|
87
|
+
execute: async () => {
|
|
88
|
+
await client.upsertRemoteLink(id, intended, ctx.signal);
|
|
89
|
+
const link = await find();
|
|
90
|
+
if (!link || link.object.url !== url || link.object.title !== title)
|
|
91
|
+
throw new Error("Jira remote link read-back did not match");
|
|
92
|
+
return link;
|
|
93
|
+
},
|
|
94
|
+
toMetadata: (link) => ({ linkId: link.id, globalId: link.globalId }),
|
|
95
|
+
},
|
|
96
|
+
);
|
|
97
|
+
return { linkId: link.id, url };
|
|
98
|
+
},
|
|
99
|
+
});
|
|
100
|
+
api.tool<Record<string, unknown>>({
|
|
101
|
+
name: "jira_transition_source_issue",
|
|
102
|
+
description: "Reconcile the Jira source ticket's work, review or verification status",
|
|
103
|
+
parameters: projectParameters(
|
|
104
|
+
{
|
|
105
|
+
targetStatus: {
|
|
106
|
+
type: "string",
|
|
107
|
+
enum: ["In Progress", "In Review", "Ready for Verification", "In Verification"],
|
|
108
|
+
},
|
|
109
|
+
},
|
|
110
|
+
["targetStatus"],
|
|
111
|
+
),
|
|
112
|
+
async execute(ctx, args) {
|
|
113
|
+
const { client, id } = resolveJiraSourceClient(ctx, integration);
|
|
114
|
+
const targetStatus = stringArg(args, "targetStatus");
|
|
115
|
+
if (
|
|
116
|
+
!["In Progress", "In Review", "Ready for Verification", "In Verification"].includes(
|
|
117
|
+
targetStatus,
|
|
118
|
+
)
|
|
119
|
+
)
|
|
120
|
+
throw new Error("Unsupported Jira workflow target status");
|
|
121
|
+
const target = statusName(targetStatus);
|
|
122
|
+
const reconcile = async (phase: string) => {
|
|
123
|
+
const { status } = (await client.getIssue(id, ctx.signal)).fields;
|
|
124
|
+
const current = statusName(status.name);
|
|
125
|
+
return phase === "already_recorded" ||
|
|
126
|
+
current === target ||
|
|
127
|
+
status.statusCategory.key === "done" ||
|
|
128
|
+
current === "in verification" ||
|
|
129
|
+
(current === "ready for verification" && target !== "in verification") ||
|
|
130
|
+
(target === "in progress" && current === "in review")
|
|
131
|
+
? { issueId: id, status: status.name ?? status.statusCategory.key }
|
|
132
|
+
: null;
|
|
133
|
+
};
|
|
134
|
+
return ctx.externalWrites.ensure(
|
|
135
|
+
{
|
|
136
|
+
writeType: "jira.transition",
|
|
137
|
+
dedupKey: digest([client.baseUrl, id, ctx.process.id, target]),
|
|
138
|
+
},
|
|
139
|
+
{
|
|
140
|
+
reconcile,
|
|
141
|
+
execute: async () => {
|
|
142
|
+
const current = await reconcile("before_execute");
|
|
143
|
+
if (current) return current;
|
|
144
|
+
const transitions = (await client.listTransitions(id, ctx.signal)).filter(
|
|
145
|
+
(transition) => statusName(transition.to.name) === target,
|
|
146
|
+
);
|
|
147
|
+
if (transitions.length !== 1)
|
|
148
|
+
throw new Error(
|
|
149
|
+
`Jira requires one available transition to ${targetStatus}; found ${transitions.length}`,
|
|
150
|
+
);
|
|
151
|
+
await client.transitionIssue(id, transitions[0].id, ctx.signal);
|
|
152
|
+
const result = await reconcile("after_execute_error");
|
|
153
|
+
if (!result) throw new Error(`Jira did not reach ${targetStatus}`);
|
|
154
|
+
return result;
|
|
155
|
+
},
|
|
156
|
+
toMetadata: (result) => result,
|
|
157
|
+
},
|
|
158
|
+
);
|
|
159
|
+
},
|
|
160
|
+
});
|
|
161
|
+
}
|