Edits history of script submission #23370 for ' Update Comment (jira)'

  • bunnative
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    
    // Wraps plain text in Atlassian Document Format, one paragraph per line.
    function adf(text: string) {
      return {
        type: "doc",
        version: 1,
        content: text.split("\n").map((line) => ({
          type: "paragraph",
          content: line ? [{ type: "text", text: line }] : [],
        })),
      }
    }
    
    /**
     * Update Comment
     * 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.
     */
    export async function main(
      auth: RT.Jira,
      issue_id_or_key: string,
      comment_id: string,
      comment: string | undefined,
      adf_body: { [key: string]: any } | undefined,
      visibility:
        { type: "group" | "role"; value?: string; identifier?: string } | undefined,
      skip_notifications: boolean | undefined,
      render_body: boolean | undefined
    ) {
      const url = new URL(
        `https://${auth.domain}.atlassian.net/rest/api/3/issue/${issue_id_or_key}/comment/${comment_id}`
      )
    
      if (skip_notifications) {
        url.searchParams.append("notifyUsers", "false")
      }
      if (render_body) {
        url.searchParams.append("expand", "renderedBody")
      }
    
      if (!adf_body && (comment == null || comment === "")) {
        throw new Error("Either comment or adf_body is required")
      }
      const body: { [key: string]: any } = { body: adf_body ?? adf(comment!) }
      if (visibility && Object.keys(visibility).length) {
        body.visibility = visibility
      }
    
      const response = await fetch(url, {
        method: "PUT",
        headers: {
          Authorization: `Basic ${btoa(`${auth.username}:${auth.password}`)}`,
          "Content-Type": "application/json",
          Accept: "application/json",
        },
        body: JSON.stringify(body),
      })
    
      if (!response.ok) {
        throw new Error(`${response.status} ${await response.text()}`)
      }
    
      return await response.json()
    }
    

    Submitted by hugo989 8 days ago