1 | |
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_transition_id = string |
17 |
|
18 | |
19 | export async function transition_id(auth: RT.Jira, issue_id_or_key: string) { |
20 | if (!issue_id_or_key) return [] |
21 | const { transitions } = await get( |
22 | auth, |
23 | `/rest/api/3/issue/${issue_id_or_key}/transitions` |
24 | ) |
25 | return transitions.map( |
26 | (t: { id: string; name: string; to?: { name: string } }) => ({ |
27 | value: t.id, |
28 | label: t.to?.name ? `${t.name} (to ${t.to.name})` : t.name, |
29 | }) |
30 | ) |
31 | } |
32 |
|
33 | |
34 | * Transition Issue |
35 | * Move an issue through its workflow, e.g. to In Progress or Done. The transition is picked from those currently available on the issue; `fields` and `update` set fields shown on the transition screen (e.g. `{ "resolution": { "name": "Fixed" } }`). |
36 | */ |
37 | export async function main( |
38 | auth: RT.Jira, |
39 | issue_id_or_key: string, |
40 | transition_id: DynSelect_transition_id, |
41 | fields: { [key: string]: any } | undefined, |
42 | update: { [key: string]: any } | undefined |
43 | ) { |
44 | const url = new URL( |
45 | `https://${auth.domain}.atlassian.net/rest/api/3/issue/${issue_id_or_key}/transitions` |
46 | ) |
47 |
|
48 | const body: { [key: string]: any } = { transition: { id: transition_id } } |
49 | if (fields && Object.keys(fields).length) { |
50 | body.fields = fields |
51 | } |
52 | if (update && Object.keys(update).length) { |
53 | body.update = update |
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(body), |
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 |
|