0

Get Users

by
Published Nov 2, 2023

Search users by a fragment of their display name or email address, returning their account IDs. Paginated with `start_at` / `max_results`.

Script jira Verified

The script

Submitted by hugo989 Typescript (fetch-only)
Verified 4 hours ago
1
//native
2

3
/**
4
 * Get Users
5
 * Search users by a fragment of their display name or email address, returning their account IDs. Paginated with `start_at` / `max_results`.
6
 */
7
export async function main(
8
  auth: RT.Jira,
9
  query: string | undefined,
10
  start_at: number | undefined,
11
  max_results: number | undefined
12
) {
13
  const url = new URL(
14
    `https://${auth.domain}.atlassian.net/rest/api/3/user/search`
15
  )
16
  if (start_at != null) {
17
    url.searchParams.append("startAt", String(start_at))
18
  }
19
  if (max_results != null) {
20
    url.searchParams.append("maxResults", String(max_results))
21
  }
22

23
  url.searchParams.append("query", query ?? "")
24

25
  const response = await fetch(url, {
26
    method: "GET",
27
    headers: {
28
      Authorization: `Basic ${btoa(`${auth.username}:${auth.password}`)}`,
29
      Accept: "application/json",
30
    },
31
  })
32

33
  if (!response.ok) {
34
    throw new Error(`${response.status} ${await response.text()}`)
35
  }
36

37
  return await response.json()
38
}
39

Other submissions
  • Submitted by hugo697 Typescript (fetch-only)
    Created 525 days ago
    1
    type Jira = {
    2
      username: string;
    3
      password: string;
    4
      domain: string;
    5
    };
    6
    /**
    7
     * Find users
    8
     * Returns a list of active users that match the search string and property.
    9
     */
    10
    export async function main(
    11
      auth: Jira,
    12
      query: string | undefined,
    13
      username: string | undefined,
    14
      accountId: string | undefined,
    15
      startAt: string | undefined,
    16
      maxResults: string | undefined,
    17
      property: string | undefined
    18
    ) {
    19
      const url = new URL(
    20
        `https://${auth.domain}.atlassian.net/rest/api/2/user/search`
    21
      );
    22
      for (const [k, v] of [
    23
        ["query", query],
    24
        ["username", username],
    25
        ["accountId", accountId],
    26
        ["startAt", startAt],
    27
        ["maxResults", maxResults],
    28
        ["property", property],
    29
      ]) {
    30
        if (v !== undefined && v !== "") {
    31
          url.searchParams.append(k, v);
    32
        }
    33
      }
    34
      const response = await fetch(url, {
    35
        method: "GET",
    36
        headers: {
    37
          Authorization: "Basic " + btoa(`${auth.username}:${auth.password}`),
    38
        },
    39
        body: undefined,
    40
      });
    41
      if (!response.ok) {
    42
        const text = await response.text();
    43
        throw new Error(`${response.status} ${text}`);
    44
      }
    45
      return await response.json();
    46
    }
    47