Edits history of script submission #23385 for ' Create Future 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})`,
      }))
    }
    
    /**
     * Create Future Sprint
     * Create a sprint in the future state on a scrum board. Dates are ISO 8601 date-times, e.g. `2026-01-15T09:00:00.000Z`.
     */
    export async function main(
      auth: RT.Jira,
      board_id: DynSelect_board_id,
      name: string,
      goal: string | undefined,
      start_date: string | undefined,
      end_date: string | undefined
    ) {
      const url = new URL(
        `https://${auth.domain}.atlassian.net/rest/agile/1.0/sprint`
      )
    
      const body: { [key: string]: any } = { originBoardId: Number(board_id), name }
      if (goal != null && goal !== "") {
        body.goal = goal
      }
      if (start_date != null && start_date !== "") {
        body.startDate = start_date
      }
      if (end_date != null && end_date !== "") {
        body.endDate = end_date
      }
    
      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