|
| 1 | +import { z } from "zod"; |
| 2 | + |
| 3 | +// Payload shape for the BetterStack status-page "subscribe via webhook" |
| 4 | +// integration. This fires when a status report is published or updated on the |
| 5 | +// status page (event_type "incident"). Maintenance windows and individual |
| 6 | +// component changes arrive as separate event types that we ignore. |
| 7 | +// |
| 8 | +// Note: this webhook is NOT cryptographically signed by BetterStack, so the |
| 9 | +// route gates access with a shared secret in the URL instead. |
| 10 | + |
| 11 | +// BetterStack sends resource ids as JSON numbers, not strings. Accept either |
| 12 | +// and normalize to a string so the rest of the pipeline (dedup keys, logs) can |
| 13 | +// treat every id uniformly. |
| 14 | +const IdSchema = z.union([z.string(), z.number()]).transform((v) => String(v)); |
| 15 | + |
| 16 | +export const IncidentUpdateSchema = z.object({ |
| 17 | + id: IdSchema, |
| 18 | + status_report_id: IdSchema.optional(), |
| 19 | + body: z.string().nullish(), |
| 20 | + created_at: z.string().nullish(), |
| 21 | + updated_at: z.string().nullish(), |
| 22 | +}); |
| 23 | + |
| 24 | +export const IncidentWebhookSchema = z.object({ |
| 25 | + event_type: z.string(), |
| 26 | + page: z |
| 27 | + .object({ |
| 28 | + id: IdSchema.optional(), |
| 29 | + status_indicator: z.string().nullish(), |
| 30 | + status_description: z.string().nullish(), |
| 31 | + }) |
| 32 | + .optional(), |
| 33 | + // Optional so maintenance/component-update callbacks (which carry no |
| 34 | + // `incident`) still parse and fall through to the ignore path, rather than |
| 35 | + // failing with a 400 that BetterStack would retry. |
| 36 | + incident: z |
| 37 | + .object({ |
| 38 | + id: IdSchema, |
| 39 | + name: z.string().nullish(), |
| 40 | + created_at: z.string().nullish(), |
| 41 | + updated_at: z.string().nullish(), |
| 42 | + shortlink: z.string().nullish(), |
| 43 | + incident_updates: z.array(IncidentUpdateSchema).default([]), |
| 44 | + }) |
| 45 | + .optional(), |
| 46 | +}); |
| 47 | + |
| 48 | +export type IncidentWebhook = z.infer<typeof IncidentWebhookSchema>; |
| 49 | + |
| 50 | +export const NormalizedIncidentUpdateSchema = z.object({ |
| 51 | + incidentId: z.string(), |
| 52 | + updateId: z.string(), |
| 53 | + name: z.string(), |
| 54 | + statusIndicator: z.string(), |
| 55 | + body: z.string(), |
| 56 | + shortlink: z.string().nullable(), |
| 57 | + updatedAt: z.string().nullable(), |
| 58 | +}); |
| 59 | + |
| 60 | +export type NormalizedIncidentUpdate = { |
| 61 | + /** Stable id of the incident itself, for grouping all of its updates. */ |
| 62 | + incidentId: string; |
| 63 | + /** Id of the specific status report update — our idempotency key. */ |
| 64 | + updateId: string; |
| 65 | + /** Incident title shown on the status page. */ |
| 66 | + name: string; |
| 67 | + /** Latest aggregate state of the page: operational | degraded | downtime | maintenance. */ |
| 68 | + statusIndicator: string; |
| 69 | + /** Body of the most recent status report update (markdown). */ |
| 70 | + body: string; |
| 71 | + /** Public shortlink to the incident on the status page. */ |
| 72 | + shortlink: string | null; |
| 73 | + /** ISO timestamp of the most recent update, when provided. */ |
| 74 | + updatedAt: string | null; |
| 75 | +}; |
| 76 | + |
| 77 | +/** |
| 78 | + * Only status-report ("incident") events feed customer notifications. Auto |
| 79 | + * alerting from monitors arrives via a different webhook and never as this |
| 80 | + * event type, which is exactly the separation we want. |
| 81 | + */ |
| 82 | +export function isCustomerNotifiableEvent(payload: IncidentWebhook): boolean { |
| 83 | + return payload.event_type === "incident" && !!payload.incident; |
| 84 | +} |
| 85 | + |
| 86 | +/** |
| 87 | + * Collapse the webhook into the single most-recent update. Returns null when |
| 88 | + * there are no updates to relay (nothing to notify about yet). |
| 89 | + */ |
| 90 | +export function normalizeIncidentUpdate(payload: IncidentWebhook): NormalizedIncidentUpdate | null { |
| 91 | + if (!payload.incident) { |
| 92 | + return null; |
| 93 | + } |
| 94 | + |
| 95 | + const updates = payload.incident.incident_updates; |
| 96 | + if (updates.length === 0) { |
| 97 | + return null; |
| 98 | + } |
| 99 | + |
| 100 | + // BetterStack lists updates newest-first; fall back to the last entry if the |
| 101 | + // ordering ever changes by sorting on created_at when available. |
| 102 | + const mostRecent = [...updates].sort((a, b) => { |
| 103 | + const aTime = a.created_at ? Date.parse(a.created_at) : 0; |
| 104 | + const bTime = b.created_at ? Date.parse(b.created_at) : 0; |
| 105 | + return bTime - aTime; |
| 106 | + })[0]; |
| 107 | + |
| 108 | + return { |
| 109 | + incidentId: payload.incident.id, |
| 110 | + updateId: mostRecent.id, |
| 111 | + name: payload.incident.name?.trim() || "Service incident", |
| 112 | + statusIndicator: payload.page?.status_indicator?.trim() || "downtime", |
| 113 | + body: mostRecent.body?.trim() || "", |
| 114 | + shortlink: payload.incident.shortlink ?? null, |
| 115 | + updatedAt: mostRecent.created_at ?? payload.incident.updated_at ?? null, |
| 116 | + }; |
| 117 | +} |
0 commit comments