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 | * Delete Project |
32 | * Delete a project. By default it goes to the Jira recycle bin, where an admin can restore it; set `permanent` to delete it outright. Requires the Administer Jira permission. |
33 | */ |
34 | export async function main( |
35 | auth: RT.Jira, |
36 | project_id: DynSelect_project_id, |
37 | permanent: boolean | undefined |
38 | ) { |
39 | const url = new URL( |
40 | `https://${auth.domain}.atlassian.net/rest/api/3/project/${project_id}` |
41 | ) |
42 |
|
43 | url.searchParams.append("enableUndo", permanent ? "false" : "true") |
44 |
|
45 | const response = await fetch(url, { |
46 | method: "DELETE", |
47 | headers: { |
48 | Authorization: `Basic ${btoa(`${auth.username}:${auth.password}`)}`, |
49 | Accept: "application/json", |
50 | }, |
51 | }) |
52 |
|
53 | if (!response.ok) { |
54 | throw new Error(`${response.status} ${await response.text()}`) |
55 | } |
56 |
|
57 | if (response.status === 204) return { success: true } |
58 | return await response.json() |
59 | } |
60 |
|