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 | * Create Version |
32 | * Create a version (release) in a project. Dates are `yyyy-mm-dd`. |
33 | */ |
34 | export async function main( |
35 | auth: RT.Jira, |
36 | project_id: DynSelect_project_id, |
37 | name: string, |
38 | description: string | undefined, |
39 | start_date: string | undefined, |
40 | release_date: string | undefined, |
41 | archived: boolean | undefined |
42 | ) { |
43 | const url = new URL(`https://${auth.domain}.atlassian.net/rest/api/3/version`) |
44 |
|
45 | const body: { [key: string]: any } = { projectId: Number(project_id), name } |
46 | if (description != null && description !== "") { |
47 | body.description = description |
48 | } |
49 | if (start_date != null && start_date !== "") { |
50 | body.startDate = start_date |
51 | } |
52 | if (release_date != null && release_date !== "") { |
53 | body.releaseDate = release_date |
54 | } |
55 | if (archived) { |
56 | body.archived = true |
57 | } |
58 |
|
59 | const response = await fetch(url, { |
60 | method: "POST", |
61 | headers: { |
62 | Authorization: `Basic ${btoa(`${auth.username}:${auth.password}`)}`, |
63 | "Content-Type": "application/json", |
64 | Accept: "application/json", |
65 | }, |
66 | body: JSON.stringify(body), |
67 | }) |
68 |
|
69 | if (!response.ok) { |
70 | throw new Error(`${response.status} ${await response.text()}`) |
71 | } |
72 |
|
73 | return await response.json() |
74 | } |
75 |
|