Get user
One script reply has been approved by the moderators Verified

Returns a user.

Privacy controls are applied to the response based on the user's preferences. This could mean, for example, that the user's email address is hidden. See the Profile visibility overview for more details.

Permissions required: Browse users and groups global permission.

Created by hugo697 879 days ago Picked 66 times
Submitted by hugo697 Typescript (fetch-only)
Verified 327 days ago
1
type Jira = {
2
  username: string;
3
  password: string;
4
  domain: string;
5
};
6
/**
7
 * Get user
8
 * Returns a user.
9

10
Privacy controls are applied to the response based on the user's preferences. This could mean, for example, that the user's email address is hidden. See the [Profile visibility overview](https://developer.atlassian.com/cloud/jira/platform/profile-visibility/) for more details.
11

12
**[Permissions](#permissions) required:** *Browse users and groups* [global permission](https://confluence.atlassian.com/x/x4dKLg).
13
 */
14
export async function main(
15
  auth: Jira,
16
  accountId: string | undefined,
17
  username: string | undefined,
18
  key: string | undefined,
19
  expand: string | undefined
20
) {
21
  const url = new URL(`https://${auth.domain}.atlassian.net/rest/api/2/user`);
22
  for (const [k, v] of [
23
    ["accountId", accountId],
24
    ["username", username],
25
    ["key", key],
26
    ["expand", expand],
27
  ]) {
28
    if (v !== undefined && v !== "") {
29
      url.searchParams.append(k, v);
30
    }
31
  }
32
  const response = await fetch(url, {
33
    method: "GET",
34
    headers: {
35
      Authorization: "Basic " + btoa(`${auth.username}:${auth.password}`),
36
    },
37
    body: undefined,
38
  });
39
  if (!response.ok) {
40
    const text = await response.text();
41
    throw new Error(`${response.status} ${text}`);
42
  }
43
  return await response.json();
44
}
45