Edits history of script submission #23367 for ' Add Comment to Issue (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 }] : [],
        })),
      }
    }
    
    /**
     * Add Comment to Issue
     * 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.
     */
    export async function main(
      auth: RT.Jira,
      issue_id_or_key: string,
      comment: string | undefined,
      adf_body: { [key: string]: any } | undefined,
      visibility:
        { type: "group" | "role"; value?: string; identifier?: string } | undefined,
      render_body: boolean | undefined
    ) {
      const url = new URL(
        `https://${auth.domain}.atlassian.net/rest/api/3/issue/${issue_id_or_key}/comment`
      )
    
      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: "POST",
        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 5 hours ago