Edits history of script submission #23364 for ' Update 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 }] : [],
        })),
      }
    }
    
    /**
     * Update Issue
     * Edit an issue's fields. Only what you pass changes: `summary` and `description` (plain text, converted to Atlassian Document Format) are shortcuts, `fields` sets any field by ID, and `update` takes add/remove/set operations (e.g. `{ "labels": [{ "add": "triaged" }] }`). Status changes go through Transition Issue. Skipping notifications needs the Administer Jira or Administer Projects permission.
     */
    export async function main(
      auth: RT.Jira,
      issue_id_or_key: string,
      summary: string | undefined,
      description: string | undefined,
      fields: { [key: string]: any } | undefined,
      update: { [key: string]: any } | undefined,
      skip_notifications: boolean | undefined,
      return_issue: boolean | undefined
    ) {
      const url = new URL(
        `https://${auth.domain}.atlassian.net/rest/api/3/issue/${issue_id_or_key}`
      )
    
      if (skip_notifications) {
        url.searchParams.append("notifyUsers", "false")
      }
      if (return_issue) {
        url.searchParams.append("returnIssue", "true")
      }
    
      const body: { [key: string]: any } = { fields: { ...fields } }
      if (summary != null && summary !== "") {
        body.fields.summary = summary
      }
      if (description != null && description !== "") {
        body.fields.description = adf(description)
      }
      if (update && Object.keys(update).length) {
        body.update = update
      }
    
      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()}`)
      }
    
      if (response.status === 204) return { success: true }
      return await response.json()
    }
    

    Submitted by hugo989 5 hours ago