0

Move Issues to Sprint

by
Published today

Move up to 50 issues (IDs or keys) into an active or future sprint. The board only feeds the sprint dropdown.

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
export type DynSelect_sprint_id = string
28

29
// Resolver: the chosen board's sprints (first 50).
30
export async function sprint_id(auth: RT.Jira, board_id: string) {
31
  if (!board_id) return []
32
  const { values } = await get(
33
    auth,
34
    `/rest/agile/1.0/board/${board_id}/sprint?maxResults=50`
35
  )
36
  return values.map((s: { id: number; name: string; state: string }) => ({
37
    value: String(s.id),
38
    label: `${s.name} (${s.state})`,
39
  }))
40
}
41

42
/**
43
 * Move Issues to Sprint
44
 * Move up to 50 issues (IDs or keys) into an active or future sprint. The board only feeds the sprint dropdown.
45
 */
46
export async function main(
47
  auth: RT.Jira,
48
  board_id: DynSelect_board_id | undefined,
49
  sprint_id: DynSelect_sprint_id,
50
  issues: string[]
51
) {
52
  const url = new URL(
53
    `https://${auth.domain}.atlassian.net/rest/agile/1.0/sprint/${sprint_id}/issue`
54
  )
55

56
  const response = await fetch(url, {
57
    method: "POST",
58
    headers: {
59
      Authorization: `Basic ${btoa(`${auth.username}:${auth.password}`)}`,
60
      "Content-Type": "application/json",
61
      Accept: "application/json",
62
    },
63
    body: JSON.stringify({ issues }),
64
  })
65

66
  if (!response.ok) {
67
    throw new Error(`${response.status} ${await response.text()}`)
68
  }
69

70
  if (response.status === 204) return { success: true }
71
  return await response.json()
72
}
73