Skip to content

Commit c392668

Browse files
committed
feat: add event filtering capability for notifications
- Introduced a new environment variable, FILTER_EVENTS, to specify which event types should trigger notifications. - Updated the webhook processing logic to filter events based on the specified types. - Enhanced documentation to include instructions for setting up event filtering.
1 parent e482ffa commit c392668

File tree

5 files changed

+46
-0
lines changed

5 files changed

+46
-0
lines changed

apps/fumadocs/content/docs/installation/environment.mdx

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,25 @@ Add the variables for at least one provider. See the [Providers](/docs/providers
100100
</Step>
101101
<Step>
102102

103+
### Add Event Filtering (Optional)
104+
105+
<TypeTable
106+
type={{
107+
FILTER_EVENTS: {
108+
description: 'Comma-separated list of event types to allow',
109+
type: 'string',
110+
default: 'all events',
111+
},
112+
}}
113+
/>
114+
115+
Example: `FILTER_EVENTS=deployment.error,deployment.succeeded,deployment.ready`
116+
117+
If set, only the specified events will trigger notifications. If not set, all events pass through.
118+
119+
</Step>
120+
<Step>
121+
103122
### Redeploy
104123

105124
Go to **Deployments** and click the three dots on the latest deployment →

apps/web/src/app/api/hook/route.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { after } from "next/server";
22
import { ZodError } from "zod/v4";
33

44
import HttpStatusCode from "@/enums/http-status-codes";
5+
import { isEventAllowed } from "@/lib/filter";
56
import { sendNotifications } from "@/lib/notify";
67
import { checkRateLimit, getClientIp } from "@/lib/ratelimit";
78
import { verifySignature } from "@/lib/verify";
@@ -33,6 +34,10 @@ export async function POST(req: Request) {
3334
const payload = JSON.parse(rawBody) as unknown;
3435
const webhook = webhookSchema.parse(payload);
3536

37+
if (!isEventAllowed(webhook.type)) {
38+
return Response.json({ success: true, message: "Event filtered" });
39+
}
40+
3641
after(() => sendNotifications(webhook));
3742

3843
return Response.json({ success: true, message: "Webhook processed" });

apps/web/src/env.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ export const env = createEnv({
1515
// Rate limiting
1616
UPSTASH_REDIS_REST_URL: string().url().optional(),
1717
UPSTASH_REDIS_REST_TOKEN: string().optional(),
18+
// Filtering
19+
FILTER_EVENTS: string().optional(),
1820
},
1921
client: {},
2022
runtimeEnv: {
@@ -25,6 +27,7 @@ export const env = createEnv({
2527
SLACK_WEBHOOK_URL: process.env.SLACK_WEBHOOK_URL,
2628
UPSTASH_REDIS_REST_URL: process.env.UPSTASH_REDIS_REST_URL,
2729
UPSTASH_REDIS_REST_TOKEN: process.env.UPSTASH_REDIS_REST_TOKEN,
30+
FILTER_EVENTS: process.env.FILTER_EVENTS,
2831
},
2932
extends: [vercel()],
3033
});

apps/web/src/lib/filter.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { env } from "@/env";
2+
import type { WebhookType } from "@/schemas/vercel";
3+
4+
const allowedEvents: Set<string> | null = env.FILTER_EVENTS
5+
? new Set(env.FILTER_EVENTS.split(",").map((e) => e.trim()))
6+
: null;
7+
8+
/**
9+
* Check if an event type should trigger notifications.
10+
* If FILTER_EVENTS is not set, all events are allowed.
11+
* If set, only events in the comma-separated list are allowed.
12+
*/
13+
export function isEventAllowed(type: WebhookType): boolean {
14+
if (!allowedEvents) {
15+
return true;
16+
}
17+
return allowedEvents.has(type);
18+
}

turbo.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
"SLACK_WEBHOOK_URL",
1414
"UPSTASH_REDIS_REST_TOKEN",
1515
"UPSTASH_REDIS_REST_URL",
16+
"FILTER_EVENTS",
1617
"WEBHOOK_INTEGRATION_SECRET"
1718
]
1819
},

0 commit comments

Comments
 (0)