0

Get All Projects

by
Published Nov 2, 2023

List the projects visible to the user, optionally filtered by a name or key fragment. Paginated with `start_at` / `max_results`; `expand` accepts a comma-separated list of `description`, `issueTypes`, `lead`, `projectKeys`, `insight`.

Script jira Verified

The script

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

3
/**
4
 * Get All Projects
5
 * List the projects visible to the user, optionally filtered by a name or key fragment. Paginated with `start_at` / `max_results`; `expand` accepts a comma-separated list of `description`, `issueTypes`, `lead`, `projectKeys`, `insight`.
6
 */
7
export async function main(
8
  auth: RT.Jira,
9
  query: string | undefined,
10
  expand: string | undefined,
11
  start_at: number | undefined,
12
  max_results: number | undefined
13
) {
14
  const url = new URL(
15
    `https://${auth.domain}.atlassian.net/rest/api/3/project/search`
16
  )
17
  if (query != null && query !== "") {
18
    url.searchParams.append("query", query)
19
  }
20
  if (expand != null && expand !== "") {
21
    url.searchParams.append("expand", expand)
22
  }
23
  if (start_at != null) {
24
    url.searchParams.append("startAt", String(start_at))
25
  }
26
  if (max_results != null) {
27
    url.searchParams.append("maxResults", String(max_results))
28
  }
29

30
  const response = await fetch(url, {
31
    method: "GET",
32
    headers: {
33
      Authorization: `Basic ${btoa(`${auth.username}:${auth.password}`)}`,
34
      Accept: "application/json",
35
    },
36
  })
37

38
  if (!response.ok) {
39
    throw new Error(`${response.status} ${await response.text()}`)
40
  }
41

42
  return await response.json()
43
}
44

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
     * Get projects paginated
    8
     * Returns a [paginated](#pagination) list of projects visible to the user.
    9
     */
    10
    export async function main(
    11
      auth: Jira,
    12
      startAt: string | undefined,
    13
      maxResults: string | undefined,
    14
      orderBy:
    15
        | "category"
    16
        | "-category"
    17
        | "+category"
    18
        | "key"
    19
        | "-key"
    20
        | "+key"
    21
        | "name"
    22
        | "-name"
    23
        | "+name"
    24
        | "owner"
    25
        | "-owner"
    26
        | "+owner"
    27
        | "issueCount"
    28
        | "-issueCount"
    29
        | "+issueCount"
    30
        | "lastIssueUpdatedDate"
    31
        | "-lastIssueUpdatedDate"
    32
        | "+lastIssueUpdatedDate"
    33
        | "archivedDate"
    34
        | "+archivedDate"
    35
        | "-archivedDate"
    36
        | "deletedDate"
    37
        | "+deletedDate"
    38
        | "-deletedDate"
    39
        | undefined,
    40
      id: string | undefined,
    41
      keys: string | undefined,
    42
      query: string | undefined,
    43
      typeKey: string | undefined,
    44
      categoryId: string | undefined,
    45
      action: "view" | "browse" | "edit" | "create" | undefined,
    46
      expand: string | undefined,
    47
      status: string | undefined,
    48
      properties: string | undefined,
    49
      propertyQuery: string | undefined
    50
    ) {
    51
      const url = new URL(
    52
        `https://${auth.domain}.atlassian.net/rest/api/2/project/search`
    53
      );
    54
      for (const [k, v] of [
    55
        ["startAt", startAt],
    56
        ["maxResults", maxResults],
    57
        ["orderBy", orderBy],
    58
        ["id", id],
    59
        ["keys", keys],
    60
        ["query", query],
    61
        ["typeKey", typeKey],
    62
        ["categoryId", categoryId],
    63
        ["action", action],
    64
        ["expand", expand],
    65
        ["status", status],
    66
        ["properties", properties],
    67
        ["propertyQuery", propertyQuery],
    68
      ]) {
    69
        if (v !== undefined && v !== "") {
    70
          url.searchParams.append(k, v);
    71
        }
    72
      }
    73
      const response = await fetch(url, {
    74
        method: "GET",
    75
        headers: {
    76
          Authorization: "Basic " + btoa(`${auth.username}:${auth.password}`),
    77
        },
    78
        body: undefined,
    79
      });
    80
      if (!response.ok) {
    81
        const text = await response.text();
    82
        throw new Error(`${response.status} ${text}`);
    83
      }
    84
      return await response.json();
    85
    }
    86