0

Update destinations for alerts

by
Published Dec 20, 2024

Updates the emails and slack webhook destinations for app alerts. Emails must be associated to a user with access to the app.

Script digitalocean Verified

The script

Submitted by hugo697 Bun
Verified 536 days ago
1
//native
2
type Digitalocean = {
3
  token: string;
4
};
5
/**
6
 * Update destinations for alerts
7
 * Updates the emails and slack webhook destinations for app alerts. Emails must be associated to a user with access to the app.
8
 */
9
export async function main(
10
  auth: Digitalocean,
11
  app_id: string,
12
  alert_id: string,
13
  body: {
14
    emails?: string[];
15
    slack_webhooks?: { url?: string; channel?: string }[];
16
  },
17
) {
18
  const url = new URL(
19
    `https://api.digitalocean.com/v2/apps/${app_id}/alerts/${alert_id}/destinations`,
20
  );
21

22
  const response = await fetch(url, {
23
    method: "POST",
24
    headers: {
25
      "Content-Type": "application/json",
26
      Authorization: "Bearer " + auth.token,
27
    },
28
    body: JSON.stringify(body),
29
  });
30
  if (!response.ok) {
31
    const text = await response.text();
32
    throw new Error(`${response.status} ${text}`);
33
  }
34
  return await response.json();
35
}
36