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_account_id = string |
17 |
|
18 | |
19 | export async function account_id(auth: RT.Jira, issue_id_or_key: string) { |
20 | if (!issue_id_or_key) return [] |
21 | const users = await get( |
22 | auth, |
23 | `/rest/api/3/user/assignable/search?issueKey=${issue_id_or_key}&maxResults=100` |
24 | ) |
25 | return users.map((u: { accountId: string; displayName: string }) => ({ |
26 | value: u.accountId, |
27 | label: u.displayName, |
28 | })) |
29 | } |
30 |
|
31 | |
32 | * Assign Issue |
33 | * Assign an issue to a user, picked from the users assignable to that issue. Leave the user empty to unassign the issue. |
34 | */ |
35 | export async function main( |
36 | auth: RT.Jira, |
37 | issue_id_or_key: string, |
38 | account_id: DynSelect_account_id | undefined |
39 | ) { |
40 | const url = new URL( |
41 | `https://${auth.domain}.atlassian.net/rest/api/3/issue/${issue_id_or_key}/assignee` |
42 | ) |
43 |
|
44 | const response = await fetch(url, { |
45 | method: "PUT", |
46 | headers: { |
47 | Authorization: `Basic ${btoa(`${auth.username}:${auth.password}`)}`, |
48 | "Content-Type": "application/json", |
49 | Accept: "application/json", |
50 | }, |
51 | body: JSON.stringify({ accountId: account_id || null }), |
52 | }) |
53 |
|
54 | if (!response.ok) { |
55 | throw new Error(`${response.status} ${await response.text()}`) |
56 | } |
57 |
|
58 | if (response.status === 204) return { success: true } |
59 | return await response.json() |
60 | } |
61 |
|