Add gadget to dashboard

Adds a gadget to a dashboard. **[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
 * Add gadget to dashboard
8
 * Adds a gadget to a dashboard.
9

10
**[Permissions](#permissions) required:** None.
11
 */
12
export async function main(
13
  auth: Jira,
14
  dashboardId: string,
15
  body: {
16
    color?: string;
17
    ignoreUriAndModuleKeyValidation?: boolean;
18
    moduleKey?: string;
19
    position?: {
20
      "The column position of the gadget.": number;
21
      "The row position of the gadget.": number;
22
    };
23
    title?: string;
24
    uri?: string;
25
  }
26
) {
27
  const url = new URL(
28
    `https://${auth.domain}.atlassian.net/rest/api/2/dashboard/${dashboardId}/gadget`
29
  );
30

31
  const response = await fetch(url, {
32
    method: "POST",
33
    headers: {
34
      "Content-Type": "application/json",
35
      Authorization: "Basic " + btoa(`${auth.username}:${auth.password}`),
36
    },
37
    body: JSON.stringify(body),
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