1 | |
2 |
|
3 | export type DynSelect_service_id = string |
4 |
|
5 | |
6 | export async function service_id(auth: RT.Pagerduty) { |
7 | const response = await fetch( |
8 | "https://api.pagerduty.com/services?limit=100", |
9 | { |
10 | headers: { |
11 | Authorization: `Token token=${auth.token}`, |
12 | Accept: "application/vnd.pagerduty+json;version=2", |
13 | }, |
14 | }, |
15 | ) |
16 | if (!response.ok) { |
17 | throw new Error(`${response.status} ${await response.text()}`) |
18 | } |
19 | const { services } = (await response.json()) as { |
20 | services: { id: string; name: string }[] |
21 | } |
22 | return services.map((s) => ({ value: s.id, label: `${s.name} (${s.id})` })) |
23 | } |
24 |
|
25 | |
26 | * Create Incident |
27 | * Open a new incident on a service. Set incident_key to a stable value (e.g. a hash of the source event) to dedupe retries — PagerDuty rejects a second open incident with the same key on the same service. Requires the resource's from_email when the API key is account-scoped. |
28 | */ |
29 | export async function main( |
30 | auth: RT.Pagerduty, |
31 | title: string, |
32 | service_id: DynSelect_service_id, |
33 | urgency: "high" | "low" | undefined, |
34 | incident_key: string | undefined, |
35 | details: string | undefined, |
36 | escalation_policy_id: string | undefined, |
37 | priority_id: string | undefined, |
38 | ) { |
39 | const incident: { [key: string]: any } = { |
40 | type: "incident", |
41 | title, |
42 | service: { id: service_id, type: "service_reference" }, |
43 | } |
44 | if (urgency !== undefined) incident.urgency = urgency |
45 | if (incident_key !== undefined && incident_key !== "") |
46 | incident.incident_key = incident_key |
47 | if (details !== undefined && details !== "") |
48 | incident.body = { type: "incident_body", details } |
49 | if (escalation_policy_id !== undefined && escalation_policy_id !== "") |
50 | incident.escalation_policy = { |
51 | id: escalation_policy_id, |
52 | type: "escalation_policy_reference", |
53 | } |
54 | if (priority_id !== undefined && priority_id !== "") |
55 | incident.priority = { id: priority_id, type: "priority_reference" } |
56 |
|
57 | const headers: { [key: string]: string } = { |
58 | Authorization: `Token token=${auth.token}`, |
59 | "Content-Type": "application/json", |
60 | Accept: "application/vnd.pagerduty+json;version=2", |
61 | } |
62 | if (auth.from_email) headers["From"] = auth.from_email |
63 |
|
64 | const response = await fetch("https://api.pagerduty.com/incidents", { |
65 | method: "POST", |
66 | headers, |
67 | body: JSON.stringify({ incident }), |
68 | }) |
69 |
|
70 | if (!response.ok) { |
71 | throw new Error(`${response.status} ${await response.text()}`) |
72 | } |
73 |
|
74 | return await response.json() |
75 | } |
76 |
|