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_project_id = string |
17 |
|
18 | |
19 | export async function project_id(auth: RT.Jira) { |
20 | const { values } = await get( |
21 | auth, |
22 | "/rest/api/3/project/search?maxResults=100" |
23 | ) |
24 | return values.map((p: { id: string; key: string; name: string }) => ({ |
25 | value: p.id, |
26 | label: `${p.name} (${p.key})`, |
27 | })) |
28 | } |
29 |
|
30 | |
31 | * Get Issue Types |
32 | * List the issue types of a project, or every issue type the user can see when no project is chosen. |
33 | */ |
34 | export async function main( |
35 | auth: RT.Jira, |
36 | project_id: DynSelect_project_id | undefined |
37 | ) { |
38 | const url = new URL( |
39 | `https://${auth.domain}.atlassian.net/rest/api/3/issuetype` |
40 | ) |
41 | if (project_id != null && project_id !== "") { |
42 | url.pathname += "/project" |
43 | url.searchParams.append("projectId", project_id) |
44 | } |
45 |
|
46 | const response = await fetch(url, { |
47 | method: "GET", |
48 | headers: { |
49 | Authorization: `Basic ${btoa(`${auth.username}:${auth.password}`)}`, |
50 | Accept: "application/json", |
51 | }, |
52 | }) |
53 |
|
54 | if (!response.ok) { |
55 | throw new Error(`${response.status} ${await response.text()}`) |
56 | } |
57 |
|
58 | return await response.json() |
59 | } |
60 |
|