Get project components paginated

Returns a [paginated](#pagination) list of all components in a project.

Script jira Verified

by hugo697 ยท 11/2/2023

The script

Submitted by hugo697 Typescript (fetch-only)
Verified 396 days ago
1
type Jira = {
2
  username: string;
3
  password: string;
4
  domain: string;
5
};
6
/**
7
 * Get project components paginated
8
 * Returns a [paginated](#pagination) list of all components in a project.
9
 */
10
export async function main(
11
  auth: Jira,
12
  projectIdOrKey: string,
13
  startAt: string | undefined,
14
  maxResults: string | undefined,
15
  orderBy:
16
    | "description"
17
    | "-description"
18
    | "+description"
19
    | "issueCount"
20
    | "-issueCount"
21
    | "+issueCount"
22
    | "lead"
23
    | "-lead"
24
    | "+lead"
25
    | "name"
26
    | "-name"
27
    | "+name"
28
    | undefined,
29
  componentSource: "jira" | "compass" | "auto" | undefined,
30
  query: string | undefined
31
) {
32
  const url = new URL(
33
    `https://${auth.domain}.atlassian.net/rest/api/2/project/${projectIdOrKey}/component`
34
  );
35
  for (const [k, v] of [
36
    ["startAt", startAt],
37
    ["maxResults", maxResults],
38
    ["orderBy", orderBy],
39
    ["componentSource", componentSource],
40
    ["query", query],
41
  ]) {
42
    if (v !== undefined && v !== "") {
43
      url.searchParams.append(k, v);
44
    }
45
  }
46
  const response = await fetch(url, {
47
    method: "GET",
48
    headers: {
49
      Authorization: "Basic " + btoa(`${auth.username}:${auth.password}`),
50
    },
51
    body: undefined,
52
  });
53
  if (!response.ok) {
54
    const text = await response.text();
55
    throw new Error(`${response.status} ${text}`);
56
  }
57
  return await response.json();
58
}
59