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_field_id = string |
17 |
|
18 | |
19 | export async function field_id(auth: RT.Jira) { |
20 | const { values } = await get( |
21 | auth, |
22 | "/rest/api/3/field/search?type=custom&maxResults=100" |
23 | ) |
24 | return values.map((f: { id: string; name: string }) => ({ |
25 | value: f.id, |
26 | label: `${f.name} (${f.id})`, |
27 | })) |
28 | } |
29 |
|
30 | export type DynSelect_context_id = string |
31 |
|
32 | |
33 | export async function context_id(auth: RT.Jira, field_id: string) { |
34 | if (!field_id) return [] |
35 | const { values } = await get(auth, `/rest/api/3/field/${field_id}/context`) |
36 | return values.map((c: { id: string; name: string }) => ({ |
37 | value: c.id, |
38 | label: c.name, |
39 | })) |
40 | } |
41 |
|
42 | |
43 | * Create Custom Field Options |
44 | * Add options to a select-type custom field in one of its contexts. Requires the Administer Jira permission. |
45 | */ |
46 | export async function main( |
47 | auth: RT.Jira, |
48 | field_id: DynSelect_field_id, |
49 | context_id: DynSelect_context_id, |
50 | options: { value: string; disabled?: boolean; optionId?: string }[] |
51 | ) { |
52 | const url = new URL( |
53 | `https://${auth.domain}.atlassian.net/rest/api/3/field/${field_id}/context/${context_id}/option` |
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({ options }), |
64 | }) |
65 |
|
66 | if (!response.ok) { |
67 | throw new Error(`${response.status} ${await response.text()}`) |
68 | } |
69 |
|
70 | return await response.json() |
71 | } |
72 |
|