0

Update Incident

by
Published 4 days ago

Update an incident's status, title, urgency, priority, escalation policy, or assignments. Set status to "acknowledged" to acknowledge or "resolved" to resolve. Pass assignee_ids to replace the current assignees. Requires the resource's from_email when the API key is account-scoped.

Script pagerduty Verified

The script

Submitted by hugo989 Typescript (fetch-only)
Verified 5 days ago
1
//native
2

3
/**
4
 * Update Incident
5
 * Update an incident's status, title, urgency, priority, escalation policy, or assignments. Set status to "acknowledged" to acknowledge or "resolved" to resolve. Pass assignee_ids to replace the current assignees. Requires the resource's from_email when the API key is account-scoped.
6
 */
7
export async function main(
8
  auth: RT.Pagerduty,
9
  incident_id: string,
10
  status: "acknowledged" | "resolved" | undefined,
11
  title: string | undefined,
12
  urgency: "high" | "low" | undefined,
13
  priority_id: string | undefined,
14
  escalation_policy_id: string | undefined,
15
  assignee_ids: string[] | undefined,
16
) {
17
  const incident: { [key: string]: any } = { type: "incident" }
18
  if (status !== undefined) incident.status = status
19
  if (title !== undefined && title !== "") incident.title = title
20
  if (urgency !== undefined) incident.urgency = urgency
21
  if (priority_id !== undefined && priority_id !== "")
22
    incident.priority = { id: priority_id, type: "priority_reference" }
23
  if (escalation_policy_id !== undefined && escalation_policy_id !== "")
24
    incident.escalation_policy = {
25
      id: escalation_policy_id,
26
      type: "escalation_policy_reference",
27
    }
28
  if (assignee_ids !== undefined)
29
    incident.assignments = assignee_ids.map((id) => ({
30
      assignee: { id, type: "user_reference" },
31
    }))
32

33
  const headers: { [key: string]: string } = {
34
    Authorization: `Token token=${auth.token}`,
35
    "Content-Type": "application/json",
36
    Accept: "application/vnd.pagerduty+json;version=2",
37
  }
38
  if (auth.from_email) headers["From"] = auth.from_email
39

40
  const response = await fetch(
41
    `https://api.pagerduty.com/incidents/${incident_id}`,
42
    {
43
      method: "PUT",
44
      headers,
45
      body: JSON.stringify({ incident }),
46
    },
47
  )
48

49
  if (!response.ok) {
50
    throw new Error(`${response.status} ${await response.text()}`)
51
  }
52

53
  return await response.json()
54
}
55