0

List Sprints

by
Published today

List the sprints of a scrum board, optionally filtered by state. Paginated with `start_at` / `max_results`; boards without sprint support (kanban) return 400.

Script jira Verified

The script

Submitted by hugo989 Typescript (fetch-only)
Verified 4 hours ago
1
//native
2

3
async function get(auth: RT.Jira, path: string) {
4
  const response = await fetch(`https://${auth.domain}.atlassian.net${path}`, {
5
    headers: {
6
      Authorization: `Basic ${btoa(`${auth.username}:${auth.password}`)}`,
7
      Accept: "application/json",
8
    },
9
  })
10
  if (!response.ok) {
11
    throw new Error(`${response.status} ${await response.text()}`)
12
  }
13
  return await response.json()
14
}
15

16
export type DynSelect_board_id = string
17

18
// Resolver: the site's boards (first 50).
19
export async function board_id(auth: RT.Jira) {
20
  const { values } = await get(auth, "/rest/agile/1.0/board?maxResults=50")
21
  return values.map((b: { id: number; name: string; type: string }) => ({
22
    value: String(b.id),
23
    label: `${b.name} (${b.type})`,
24
  }))
25
}
26

27
/**
28
 * List Sprints
29
 * List the sprints of a scrum board, optionally filtered by state. Paginated with `start_at` / `max_results`; boards without sprint support (kanban) return 400.
30
 */
31
export async function main(
32
  auth: RT.Jira,
33
  board_id: DynSelect_board_id,
34
  state: ("future" | "active" | "closed")[] | undefined,
35
  start_at: number | undefined,
36
  max_results: number | undefined
37
) {
38
  const url = new URL(
39
    `https://${auth.domain}.atlassian.net/rest/agile/1.0/board/${board_id}/sprint`
40
  )
41
  if (state?.length) {
42
    url.searchParams.append("state", state.join(","))
43
  }
44
  if (start_at != null) {
45
    url.searchParams.append("startAt", String(start_at))
46
  }
47
  if (max_results != null) {
48
    url.searchParams.append("maxResults", String(max_results))
49
  }
50

51
  const response = await fetch(url, {
52
    method: "GET",
53
    headers: {
54
      Authorization: `Basic ${btoa(`${auth.username}:${auth.password}`)}`,
55
      Accept: "application/json",
56
    },
57
  })
58

59
  if (!response.ok) {
60
    throw new Error(`${response.status} ${await response.text()}`)
61
  }
62

63
  return await response.json()
64
}
65