1 | |
2 |
|
3 | |
4 | function adf(text: string) { |
5 | return { |
6 | type: "doc", |
7 | version: 1, |
8 | content: text.split("\n").map((line) => ({ |
9 | type: "paragraph", |
10 | content: line ? [{ type: "text", text: line }] : [], |
11 | })), |
12 | } |
13 | } |
14 |
|
15 | |
16 | * Add Comment to Issue |
17 | * Add a comment to an issue. Pass plain text in `comment` (converted to Atlassian Document Format, one paragraph per line) or a full ADF document in `adf_body` for mentions and rich formatting. `visibility` restricts the comment to a group or project role. |
18 | */ |
19 | export async function main( |
20 | auth: RT.Jira, |
21 | issue_id_or_key: string, |
22 | comment: string | undefined, |
23 | adf_body: { [key: string]: any } | undefined, |
24 | visibility: |
25 | { type: "group" | "role"; value?: string; identifier?: string } | undefined, |
26 | render_body: boolean | undefined |
27 | ) { |
28 | const url = new URL( |
29 | `https://${auth.domain}.atlassian.net/rest/api/3/issue/${issue_id_or_key}/comment` |
30 | ) |
31 |
|
32 | if (render_body) { |
33 | url.searchParams.append("expand", "renderedBody") |
34 | } |
35 |
|
36 | if (!adf_body && (comment == null || comment === "")) { |
37 | throw new Error("Either comment or adf_body is required") |
38 | } |
39 | const body: { [key: string]: any } = { body: adf_body ?? adf(comment!) } |
40 | if (visibility && Object.keys(visibility).length) { |
41 | body.visibility = visibility |
42 | } |
43 |
|
44 | const response = await fetch(url, { |
45 | method: "POST", |
46 | headers: { |
47 | Authorization: `Basic ${btoa(`${auth.username}:${auth.password}`)}`, |
48 | "Content-Type": "application/json", |
49 | Accept: "application/json", |
50 | }, |
51 | body: JSON.stringify(body), |
52 | }) |
53 |
|
54 | if (!response.ok) { |
55 | throw new Error(`${response.status} ${await response.text()}`) |
56 | } |
57 |
|
58 | return await response.json() |
59 | } |
60 |
|