Edits history of script submission #23376 for ' Assign 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_account_id = string
    
    // Resolver: the users who can be assigned to the chosen issue (first 100).
    export async function account_id(auth: RT.Jira, issue_id_or_key: string) {
      if (!issue_id_or_key) return []
      const users = await get(
        auth,
        `/rest/api/3/user/assignable/search?issueKey=${issue_id_or_key}&maxResults=100`
      )
      return users.map((u: { accountId: string; displayName: string }) => ({
        value: u.accountId,
        label: u.displayName,
      }))
    }
    
    /**
     * Assign Issue
     * Assign an issue to a user, picked from the users assignable to that issue. Leave the user empty to unassign the issue.
     */
    export async function main(
      auth: RT.Jira,
      issue_id_or_key: string,
      account_id: DynSelect_account_id | undefined
    ) {
      const url = new URL(
        `https://${auth.domain}.atlassian.net/rest/api/3/issue/${issue_id_or_key}/assignee`
      )
    
      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({ accountId: account_id || null }),
      })
    
      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 8 days ago