Edits history of script submission #23361 for ' Transition Issue (jira)'

  • bunnative
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    
    async function get(auth: RT.Jira, path: string) {
      const response = await fetch(`https://${auth.domain}.atlassian.net${path}`, {
        headers: {
          Authorization: `Basic ${btoa(`${auth.username}:${auth.password}`)}`,
          Accept: "application/json",
        },
      })
      if (!response.ok) {
        throw new Error(`${response.status} ${await response.text()}`)
      }
      return await response.json()
    }
    
    export type DynSelect_transition_id = string
    
    // Resolver: the transitions currently available on the chosen issue.
    export async function transition_id(auth: RT.Jira, issue_id_or_key: string) {
      if (!issue_id_or_key) return []
      const { transitions } = await get(
        auth,
        `/rest/api/3/issue/${issue_id_or_key}/transitions`
      )
      return transitions.map(
        (t: { id: string; name: string; to?: { name: string } }) => ({
          value: t.id,
          label: t.to?.name ? `${t.name} (to ${t.to.name})` : t.name,
        })
      )
    }
    
    /**
     * Transition Issue
     * Move an issue through its workflow, e.g. to In Progress or Done. The transition is picked from those currently available on the issue; `fields` and `update` set fields shown on the transition screen (e.g. `{ "resolution": { "name": "Fixed" } }`).
     */
    export async function main(
      auth: RT.Jira,
      issue_id_or_key: string,
      transition_id: DynSelect_transition_id,
      fields: { [key: string]: any } | undefined,
      update: { [key: string]: any } | undefined
    ) {
      const url = new URL(
        `https://${auth.domain}.atlassian.net/rest/api/3/issue/${issue_id_or_key}/transitions`
      )
    
      const body: { [key: string]: any } = { transition: { id: transition_id } }
      if (fields && Object.keys(fields).length) {
        body.fields = fields
      }
      if (update && Object.keys(update).length) {
        body.update = update
      }
    
      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()}`)
      }
    
      if (response.status === 204) return { success: true }
      return await response.json()
    }
    

    Submitted by hugo989 14 days ago