Skip to content

Commit 0334e50

Browse files
jackbridgerTrigger.dev RepoOps
authored andcommitted
fix(core,sdk): accept alert.raised webhooks in webhooks.constructEvent
Mono-RevId: 0f5b802ef2efc7d8ccadbf23fa4af7ac9980124c
1 parent 9b443b7 commit 0334e50

4 files changed

Lines changed: 235 additions & 3 deletions

File tree

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@trigger.dev/core": patch
3+
"@trigger.dev/sdk": patch
4+
---
5+
6+
`webhooks.constructEvent` now accepts `alert.raised` webhooks, such as those sent by Limits alerts. Previously it threw a `WebhookError` for them.

‎docs/troubleshooting-alerts.mdx‎

Lines changed: 61 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
title: "Alerts"
3-
description: "Get alerted when runs or deployments fail, or when deployments succeed."
3+
description: "Get alerted when runs or deployments fail, when deployments succeed, or when you hit a limit."
44
---
55

66
import { PrivateBeta } from "/snippets/feature-status.jsx";
@@ -10,8 +10,9 @@ We support receiving alerts for the following events:
1010
- Deployment fails
1111
- Deployment succeeds
1212
- A new error group appears, regresses, or is unignored
13+
- A limit is reached or approached
1314

14-
The first three are created from the **Alerts** page. The fourth — an **Error group** alert — is created from the **Errors** page instead, but appears in the same Alerts table once created. It behaves quite differently from a run failure alert; see [Error group alerts](#error-group-alerts) below.
15+
The first three are created from the **Alerts** page. The fourth — an **Error group** alert — is created from the **Errors** page instead, but appears in the same Alerts table once created. It behaves quite differently from a run failure alert; see [Error group alerts](#error-group-alerts) below. The last one is a [Limits alert](#limits-alerts).
1516

1617
<Note>
1718
If you want to be told about **every** run that fails, choose a **run fails** alert. An Error group
@@ -151,6 +152,13 @@ export async function action({ request }: ActionFunctionArgs) {
151152
console.log("[Webhook Internal Test] Error group alert webhook received", { event });
152153
break;
153154
}
155+
case "alert.raised": {
156+
console.log("[Webhook Internal Test] Alert raised webhook received", {
157+
capabilityKey: event.object.alert.capabilityKey,
158+
facts: event.object.event.facts,
159+
});
160+
break;
161+
}
154162
default: {
155163
console.log("[Webhook Internal Test] Unhandled webhook type", { event });
156164
}
@@ -194,7 +202,7 @@ When you create a webhook alert, you'll receive different payloads depending on
194202
</ParamField>
195203

196204
<ParamField path="type" type="string">
197-
The type of alert webhook. One of: `alert.run.failed`, `alert.deployment.success`, `alert.deployment.failed`, or `alert.error`
205+
The type of alert webhook. One of: `alert.run.failed`, `alert.deployment.success`, `alert.deployment.failed`, `alert.error`, or `alert.raised`
198206
</ParamField>
199207

200208
### Run Failed Alert
@@ -644,3 +652,53 @@ This webhook is sent for an [error group alert](#error-group-alerts). The payloa
644652
<ParamField path="object.dashboardUrl" type="string">
645653
URL to view the error in the dashboard
646654
</ParamField>
655+
656+
### Alert Raised
657+
658+
This webhook is sent when a [Limits alert](#limits-alerts) raises. The payload is available on the `object` property.
659+
660+
Use `@trigger.dev/sdk` 4.9.2 or later. Older versions don't have the `alert.raised` type, and `webhooks.constructEvent` throws a `WebhookError` for this webhook.
661+
662+
<ParamField path="object.alert.id" type="string">
663+
Alert ID
664+
</ParamField>
665+
666+
<ParamField path="object.alert.name" type="string | null">
667+
Alert name. `null` if the alert has no name.
668+
</ParamField>
669+
670+
<ParamField path="object.alert.capabilityKey" type="string">
671+
What the alert watches, for example `limits.api` or `limits.all`
672+
</ParamField>
673+
674+
<ParamField path="object.event.id" type="string">
675+
Event ID
676+
</ParamField>
677+
678+
<ParamField path="object.event.type" type="string">
679+
Event type. This is `raised`.
680+
</ParamField>
681+
682+
<ParamField path="object.event.at" type="datetime">
683+
When the alert raised
684+
</ParamField>
685+
686+
<ParamField path="object.event.facts" type="object">
687+
Details about what raised the alert. The keys vary by alert, so treat this as free-form data.
688+
</ParamField>
689+
690+
<ParamField path="object.event.summary" type="string">
691+
One-line summary of the event. Only set for some alerts.
692+
</ParamField>
693+
694+
<ParamField path="object.environment.id" type="string">
695+
Environment ID
696+
</ParamField>
697+
698+
<ParamField path="object.organization.id" type="string">
699+
Organization ID
700+
</ParamField>
701+
702+
<ParamField path="object.project.id" type="string">
703+
Project ID
704+
</ParamField>

‎packages/core/src/v3/schemas/webhooks.ts‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,49 @@ export const AlertWebhookErrorGroupObject = z.object({
247247

248248
export type AlertWebhookErrorGroupObject = z.infer<typeof AlertWebhookErrorGroupObject>;
249249

250+
/** Represents an alert raised webhook payload, sent by alerts such as Limits alerts */
251+
export const AlertWebhookAlertRaisedObject = z.object({
252+
/** Alert information */
253+
alert: z.object({
254+
/** Alert ID */
255+
id: z.string(),
256+
/** Alert name, if one was set */
257+
name: z.string().nullable(),
258+
/** What the alert watches, for example `limits.api` */
259+
capabilityKey: z.string(),
260+
}),
261+
/** Event information */
262+
event: z.object({
263+
/** Event ID */
264+
id: z.string(),
265+
/** Event type, for example `raised` */
266+
type: z.string(),
267+
/** When the event happened */
268+
at: z.coerce.date(),
269+
/** Details about the event. The keys vary by alert. */
270+
facts: z.record(z.string(), z.unknown()),
271+
/** One-line summary of the event */
272+
summary: z.string().optional(),
273+
}),
274+
/** Environment information */
275+
environment: z.object({
276+
/** Environment ID */
277+
id: z.string(),
278+
}),
279+
/** Organization information */
280+
organization: z.object({
281+
/** Organization ID */
282+
id: z.string(),
283+
}),
284+
/** Project information */
285+
project: z.object({
286+
/** Project ID */
287+
id: z.string(),
288+
}),
289+
});
290+
291+
export type AlertWebhookAlertRaisedObject = z.infer<typeof AlertWebhookAlertRaisedObject>;
292+
250293
/** Common properties for all webhooks */
251294
const commonProperties = {
252295
/** Webhook ID */
@@ -283,13 +326,20 @@ export const Webhook = discriminatedUnion("type", [
283326
type: z.literal("alert.error"),
284327
object: AlertWebhookErrorGroupObject,
285328
}),
329+
/** Alert raised webhook */
330+
z.object({
331+
...commonProperties,
332+
type: z.literal("alert.raised"),
333+
object: AlertWebhookAlertRaisedObject,
334+
}),
286335
]);
287336

288337
export type Webhook = z.infer<typeof Webhook>;
289338
export type RunFailedWebhook = Extract<Webhook, { type: "alert.run.failed" }>;
290339
export type DeploymentSuccessWebhook = Extract<Webhook, { type: "alert.deployment.success" }>;
291340
export type DeploymentFailedWebhook = Extract<Webhook, { type: "alert.deployment.failed" }>;
292341
export type ErrorWebhook = Extract<Webhook, { type: "alert.error" }>;
342+
export type AlertRaisedWebhook = Extract<Webhook, { type: "alert.raised" }>;
293343

294344
// The ingress webhook verification schemas live in ./webhookConfig.ts (a leaf module),
295345
// re-exported via schemas/index.ts. They are deliberately NOT here: this file imports
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
import { createHmac } from "node:crypto";
2+
import { describe, expect, it } from "vitest";
3+
import { WebhookError, webhooks } from "./webhooks.js";
4+
5+
const SECRET = "test-alert-webhook-secret";
6+
7+
function sign(payload: string, secret = SECRET) {
8+
return createHmac("sha256", secret).update(payload).digest("hex");
9+
}
10+
11+
// Matches the payload the webapp builds in deliverAlertEvent.server.ts (deliverWebhookChannel).
12+
const alertRaisedPayload = {
13+
id: "alert-event:alev_123:channel:ch_456",
14+
created: "2026-11-30T12:00:00.000Z",
15+
webhookVersion: "2026-09-17",
16+
type: "alert.raised",
17+
object: {
18+
alert: { id: "alert_123", name: null, capabilityKey: "limits.api" },
19+
event: {
20+
id: "alev_123",
21+
type: "raised",
22+
at: "2026-11-30T12:00:00.000Z",
23+
facts: { component: "limits.api", signal: "approaching", usedPercent: 84 },
24+
},
25+
environment: { id: "env_123" },
26+
organization: { id: "org_123" },
27+
project: { id: "proj_123" },
28+
},
29+
};
30+
31+
const deploymentSuccessPayload = {
32+
id: "wh_deploy_1",
33+
created: "2026-11-30T12:00:00.000Z",
34+
webhookVersion: "2025-07-10",
35+
type: "alert.deployment.success",
36+
object: {
37+
deployment: {
38+
id: "deploy_1",
39+
status: "DEPLOYED",
40+
version: "20261130.1",
41+
shortCode: "abc123",
42+
deployedAt: "2026-11-30T12:00:00.000Z",
43+
},
44+
tasks: [{ id: "hello-world", filePath: "src/trigger/example.ts", triggerSource: "STANDARD" }],
45+
environment: { id: "env_123", type: "PRODUCTION", slug: "prod" },
46+
organization: { id: "org_123", slug: "acme", name: "Acme" },
47+
project: { id: "proj_123", ref: "proj_abc", slug: "hello", name: "Hello" },
48+
},
49+
};
50+
51+
describe("webhooks.constructEvent", () => {
52+
it("accepts an alert.raised webhook from a Limits alert", async () => {
53+
const payload = JSON.stringify(alertRaisedPayload);
54+
55+
const event = await webhooks.constructEvent({ payload, header: sign(payload) }, SECRET);
56+
57+
expect(event.type).toBe("alert.raised");
58+
if (event.type !== "alert.raised") throw new Error("expected alert.raised");
59+
60+
// Narrowed to the alert.raised shape
61+
expect(event.object.alert.name).toBeNull();
62+
expect(event.object.alert.capabilityKey).toBe("limits.api");
63+
expect(event.object.event.type).toBe("raised");
64+
expect(event.object.event.at).toBeInstanceOf(Date);
65+
expect(event.object.event.at.toISOString()).toBe("2026-11-30T12:00:00.000Z");
66+
expect(event.object.event.facts).toEqual(alertRaisedPayload.object.event.facts);
67+
expect(event.object.event.summary).toBeUndefined();
68+
expect(event.object.project.id).toBe("proj_123");
69+
expect(event.created).toBeInstanceOf(Date);
70+
});
71+
72+
it("accepts an alert.raised webhook passed as a Request, with a name and summary", async () => {
73+
const payload = JSON.stringify({
74+
...alertRaisedPayload,
75+
object: {
76+
...alertRaisedPayload.object,
77+
alert: { id: "alert_456", name: "Slow queue", capabilityKey: "custom.query" },
78+
event: { ...alertRaisedPayload.object.event, summary: "p95 duration is 12.4s" },
79+
},
80+
});
81+
82+
const request = new Request("https://example.com/webhooks/alerts", {
83+
method: "POST",
84+
headers: { "x-trigger-signature-hmacsha256": sign(payload) },
85+
body: payload,
86+
});
87+
88+
const event = await webhooks.constructEvent(request, SECRET);
89+
90+
if (event.type !== "alert.raised") throw new Error("expected alert.raised");
91+
expect(event.object.alert.name).toBe("Slow queue");
92+
expect(event.object.event.summary).toBe("p95 duration is 12.4s");
93+
});
94+
95+
it("still parses existing alert webhook types", async () => {
96+
const payload = JSON.stringify(deploymentSuccessPayload);
97+
98+
const event = await webhooks.constructEvent({ payload, header: sign(payload) }, SECRET);
99+
100+
if (event.type !== "alert.deployment.success") {
101+
throw new Error("expected alert.deployment.success");
102+
}
103+
expect(event.object.deployment.deployedAt).toBeInstanceOf(Date);
104+
expect(event.object.tasks[0]?.id).toBe("hello-world");
105+
});
106+
107+
it("still rejects a bad signature and an unknown type", async () => {
108+
const payload = JSON.stringify(alertRaisedPayload);
109+
await expect(
110+
webhooks.constructEvent({ payload, header: sign(payload, "wrong-secret") }, SECRET)
111+
).rejects.toThrow("Invalid signature");
112+
113+
const unknown = JSON.stringify({ ...alertRaisedPayload, type: "alert.unknown" });
114+
await expect(
115+
webhooks.constructEvent({ payload: unknown, header: sign(unknown) }, SECRET)
116+
).rejects.toBeInstanceOf(WebhookError);
117+
});
118+
});

0 commit comments

Comments
 (0)