Get gadgets

Returns a list of dashboard gadgets on a dashboard. This operation returns: * Gadgets from a list of IDs, when `id` is set. * Gadgets with a module key, when `moduleKey` is set. * Gadgets from a list of URIs, when `uri` is set. * All gadgets, when no other parameters are set. This operation can be accessed anonymously. **[Permissions](#permissions) required:** None.

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 gadgets
8
 * Returns a list of dashboard gadgets on a dashboard.
9

10
This operation returns:
11

12
 *  Gadgets from a list of IDs, when `id` is set.
13
 *  Gadgets with a module key, when `moduleKey` is set.
14
 *  Gadgets from a list of URIs, when `uri` is set.
15
 *  All gadgets, when no other parameters are set.
16

17
This operation can be accessed anonymously.
18

19
**[Permissions](#permissions) required:** None.
20
 */
21
export async function main(
22
  auth: Jira,
23
  dashboardId: string,
24
  moduleKey: string | undefined,
25
  uri: string | undefined,
26
  gadgetId: string | undefined
27
) {
28
  const url = new URL(
29
    `https://${auth.domain}.atlassian.net/rest/api/2/dashboard/${dashboardId}/gadget`
30
  );
31
  for (const [k, v] of [
32
    ["moduleKey", moduleKey],
33
    ["uri", uri],
34
    ["gadgetId", gadgetId],
35
  ]) {
36
    if (v !== undefined && v !== "") {
37
      url.searchParams.append(k, v);
38
    }
39
  }
40
  const response = await fetch(url, {
41
    method: "GET",
42
    headers: {
43
      Authorization: "Basic " + btoa(`${auth.username}:${auth.password}`),
44
    },
45
    body: undefined,
46
  });
47
  if (!response.ok) {
48
    const text = await response.text();
49
    throw new Error(`${response.status} ${text}`);
50
  }
51
  return await response.json();
52
}
53