Edits history of script submission #23395 for ' Move Issues to Sprint (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_board_id = string
    
    // Resolver: the site's boards (first 50).
    export async function board_id(auth: RT.Jira) {
      const { values } = await get(auth, "/rest/agile/1.0/board?maxResults=50")
      return values.map((b: { id: number; name: string; type: string }) => ({
        value: String(b.id),
        label: `${b.name} (${b.type})`,
      }))
    }
    
    export type DynSelect_sprint_id = string
    
    // Resolver: the chosen board's sprints (first 50).
    export async function sprint_id(auth: RT.Jira, board_id: string) {
      if (!board_id) return []
      const { values } = await get(
        auth,
        `/rest/agile/1.0/board/${board_id}/sprint?maxResults=50`
      )
      return values.map((s: { id: number; name: string; state: string }) => ({
        value: String(s.id),
        label: `${s.name} (${s.state})`,
      }))
    }
    
    /**
     * Move Issues to Sprint
     * Move up to 50 issues (IDs or keys) into an active or future sprint. The board only feeds the sprint dropdown.
     */
    export async function main(
      auth: RT.Jira,
      board_id: DynSelect_board_id | undefined,
      sprint_id: DynSelect_sprint_id,
      issues: string[]
    ) {
      const url = new URL(
        `https://${auth.domain}.atlassian.net/rest/agile/1.0/sprint/${sprint_id}/issue`
      )
    
      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({ issues }),
      })
    
      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