0

Update Comment

by
Published Nov 2, 2023

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.

Script jira Verified

The script

Submitted by hugo989 Typescript (fetch-only)
Verified 8 days ago
1
//native
2

3
// Wraps plain text in Atlassian Document Format, one paragraph per line.
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

Other submissions
  • Submitted by hugo697 Typescript (fetch-only)
    Created 532 days ago
    1
    type Jira = {
    2
      username: string;
    3
      password: string;
    4
      domain: string;
    5
    };
    6
    /**
    7
     * Update comment
    8
     * Updates a comment.
    9
     */
    10
    export async function main(
    11
      auth: Jira,
    12
      issueIdOrKey: string,
    13
      id: string,
    14
      notifyUsers: string | undefined,
    15
      overrideEditableFlag: string | undefined,
    16
      expand: string | undefined,
    17
      body: {
    18
        author?: {
    19
          accountId?: string;
    20
          accountType?: string;
    21
          active?: boolean;
    22
          avatarUrls?: {
    23
            "16x16"?: string;
    24
            "24x24"?: string;
    25
            "32x32"?: string;
    26
            "48x48"?: string;
    27
          };
    28
          displayName?: string;
    29
          emailAddress?: string;
    30
          key?: string;
    31
          name?: string;
    32
          self?: string;
    33
          timeZone?: string;
    34
        };
    35
        body?: string;
    36
        created?: string;
    37
        id?: string;
    38
        jsdAuthorCanSeeRequest?: boolean;
    39
        jsdPublic?: boolean;
    40
        properties?: { key?: string; value?: { [k: string]: unknown } }[];
    41
        renderedBody?: string;
    42
        self?: string;
    43
        updateAuthor?: {
    44
          accountId?: string;
    45
          accountType?: string;
    46
          active?: boolean;
    47
          avatarUrls?: {
    48
            "16x16"?: string;
    49
            "24x24"?: string;
    50
            "32x32"?: string;
    51
            "48x48"?: string;
    52
          };
    53
          displayName?: string;
    54
          emailAddress?: string;
    55
          key?: string;
    56
          name?: string;
    57
          self?: string;
    58
          timeZone?: string;
    59
        };
    60
        updated?: string;
    61
        visibility?: {
    62
          identifier?: string;
    63
          type?: "group" | "role";
    64
          value?: string;
    65
          [k: string]: unknown;
    66
        };
    67
        [k: string]: unknown;
    68
      }
    69
    ) {
    70
      const url = new URL(
    71
        `https://${auth.domain}.atlassian.net/rest/api/2/issue/${issueIdOrKey}/comment/${id}`
    72
      );
    73
      for (const [k, v] of [
    74
        ["notifyUsers", notifyUsers],
    75
        ["overrideEditableFlag", overrideEditableFlag],
    76
        ["expand", expand],
    77
      ]) {
    78
        if (v !== undefined && v !== "") {
    79
          url.searchParams.append(k, v);
    80
        }
    81
      }
    82
      const response = await fetch(url, {
    83
        method: "PUT",
    84
        headers: {
    85
          "Content-Type": "application/json",
    86
          Authorization: "Basic " + btoa(`${auth.username}:${auth.password}`),
    87
        },
    88
        body: JSON.stringify(body),
    89
      });
    90
      if (!response.ok) {
    91
        const text = await response.text();
    92
        throw new Error(`${response.status} ${text}`);
    93
      }
    94
      return await response.json();
    95
    }
    96