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 | * Update Comment |
17 | * Replace the body of an existing comment. Pass plain text in `comment` (converted to Atlassian Document Format) or a full ADF document in `adf_body`. Skipping notifications needs the Administer Jira or Administer Projects permission. |
18 | */ |
19 | export async function main( |
20 | auth: RT.Jira, |
21 | issue_id_or_key: string, |
22 | comment_id: string, |
23 | comment: string | undefined, |
24 | adf_body: { [key: string]: any } | undefined, |
25 | visibility: |
26 | { type: "group" | "role"; value?: string; identifier?: string } | undefined, |
27 | skip_notifications: boolean | undefined, |
28 | render_body: boolean | undefined |
29 | ) { |
30 | const url = new URL( |
31 | `https://${auth.domain}.atlassian.net/rest/api/3/issue/${issue_id_or_key}/comment/${comment_id}` |
32 | ) |
33 |
|
34 | if (skip_notifications) { |
35 | url.searchParams.append("notifyUsers", "false") |
36 | } |
37 | if (render_body) { |
38 | url.searchParams.append("expand", "renderedBody") |
39 | } |
40 |
|
41 | if (!adf_body && (comment == null || comment === "")) { |
42 | throw new Error("Either comment or adf_body is required") |
43 | } |
44 | const body: { [key: string]: any } = { body: adf_body ?? adf(comment!) } |
45 | if (visibility && Object.keys(visibility).length) { |
46 | body.visibility = visibility |
47 | } |
48 |
|
49 | const response = await fetch(url, { |
50 | method: "PUT", |
51 | headers: { |
52 | Authorization: `Basic ${btoa(`${auth.username}:${auth.password}`)}`, |
53 | "Content-Type": "application/json", |
54 | Accept: "application/json", |
55 | }, |
56 | body: JSON.stringify(body), |
57 | }) |
58 |
|
59 | if (!response.ok) { |
60 | throw new Error(`${response.status} ${await response.text()}`) |
61 | } |
62 |
|
63 | return await response.json() |
64 | } |
65 |
|