Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

feat(webhooks): implement real webhook filters (#4670)

Will Hutchinsoncbeb05970044
diff --git a/.gitignore b/.gitignore
index 719b7eae49..5ed399641c 100644
--- a/.gitignore
+++ b/.gitignore
@@ -18,3 +18,4 @@ run-local.fish
# Per-instance generated local-stack artifacts (compose override, env, kickstart, Caddyfile).
infra/local/generated/
**/target/*
+.pi/outputs/
diff --git a/js/app/packages/service-clients/service-storage/generated/schemas/createWebhookRequest.ts b/js/app/packages/service-clients/service-storage/generated/schemas/createWebhookRequest.ts
index bb58d79aca..a6f79b6eb7 100644
--- a/js/app/packages/service-clients/service-storage/generated/schemas/createWebhookRequest.ts
+++ b/js/app/packages/service-clients/service-storage/generated/schemas/createWebhookRequest.ts
@@ -4,7 +4,9 @@
* document_storage_service
* OpenAPI spec version: 0.1.0
*/
+
import type { CreateWebhookRequestHeaders } from './createWebhookRequestHeaders';
+import type { Vec } from './vec';
import type { WebhookScope } from './webhookScope';
/**
@@ -13,11 +15,11 @@ import type { WebhookScope } from './webhookScope';
export interface CreateWebhookRequest {
/** HTTPS endpoint URL. */
endpoint_url: string;
+ /** Typed filters used to match events and optional entity ids. */
+ filters: Vec;
headers?: CreateWebhookRequestHeaders;
/** Display name. */
name: string;
- /** Rule definition used to match events. */
- rule: unknown;
/** Scope that owns the webhook. */
scope: WebhookScope;
}
diff --git a/js/app/packages/service-clients/service-storage/generated/schemas/index.ts b/js/app/packages/service-clients/service-storage/generated/schemas/index.ts
index 1e562bdb4e..df085981f0 100644
--- a/js/app/packages/service-clients/service-storage/generated/schemas/index.ts
+++ b/js/app/packages/service-clients/service-storage/generated/schemas/index.ts
@@ -678,6 +678,7 @@ export * from './patchThreadRequestV2ProjectId';
export * from './patchThreadRequestV2SharePermission';
export * from './patchWebhookRequest';
export * from './patchWebhookRequestEndpointUrl';
+export * from './patchWebhookRequestFilters';
export * from './patchWebhookRequestHeaders';
export * from './patchWebhookRequestName';
export * from './patchWebhookRequestStatus';
@@ -1004,12 +1005,17 @@ export * from './userViewsResponse';
export * from './validateWebhookResponse';
export * from './validateWebhookResponseMessage';
export * from './validateWebhookResponseResponseStatus';
+export * from './vec';
+export * from './vecItem';
+export * from './vecItemIds';
export * from './view';
export * from './viewPatch';
export * from './viewPatchName';
export * from './viewsResponse';
export * from './webhook';
export * from './webhookDeletedAt';
+export * from './webhookFilter';
+export * from './webhookFilterIds';
export * from './webhookScope';
export * from './webhookStatus';
export * from './withCallId';
diff --git a/js/app/packages/service-clients/service-storage/generated/schemas/patchWebhookRequest.ts b/js/app/packages/service-clients/service-storage/generated/schemas/patchWebhookRequest.ts
index 8da69bf410..d6fb1467e2 100644
--- a/js/app/packages/service-clients/service-storage/generated/schemas/patchWebhookRequest.ts
+++ b/js/app/packages/service-clients/service-storage/generated/schemas/patchWebhookRequest.ts
@@ -5,6 +5,7 @@
* OpenAPI spec version: 0.1.0
*/
import type { PatchWebhookRequestEndpointUrl } from './patchWebhookRequestEndpointUrl';
+import type { PatchWebhookRequestFilters } from './patchWebhookRequestFilters';
import type { PatchWebhookRequestHeaders } from './patchWebhookRequestHeaders';
import type { PatchWebhookRequestName } from './patchWebhookRequestName';
import type { PatchWebhookRequestStatus } from './patchWebhookRequestStatus';
@@ -15,10 +16,9 @@ import type { PatchWebhookRequestStatus } from './patchWebhookRequestStatus';
export interface PatchWebhookRequest {
/** HTTPS endpoint URL. */
endpoint_url?: PatchWebhookRequestEndpointUrl;
+ filters?: PatchWebhookRequestFilters;
headers?: PatchWebhookRequestHeaders;
/** Display name. */
name?: PatchWebhookRequestName;
- /** Rule definition used to match events. */
- rule?: unknown;
status?: PatchWebhookRequestStatus;
}
diff --git a/js/app/packages/service-clients/service-storage/generated/schemas/patchWebhookRequestFilters.ts b/js/app/packages/service-clients/service-storage/generated/schemas/patchWebhookRequestFilters.ts
new file mode 100644
index 0000000000..19c0f4095d
--- /dev/null
+++ b/js/app/packages/service-clients/service-storage/generated/schemas/patchWebhookRequestFilters.ts
@@ -0,0 +1,9 @@
+/**
+ * Generated by orval v7.21.0 🍺
+ * Do not edit manually.
+ * document_storage_service
+ * OpenAPI spec version: 0.1.0
+ */
+import type { Vec } from './vec';
+
+export type PatchWebhookRequestFilters = null | Vec;
diff --git a/js/app/packages/service-clients/service-storage/generated/schemas/vec.ts b/js/app/packages/service-clients/service-storage/generated/schemas/vec.ts
new file mode 100644
index 0000000000..70810d29be
--- /dev/null
+++ b/js/app/packages/service-clients/service-storage/generated/schemas/vec.ts
@@ -0,0 +1,9 @@
+/**
+ * Generated by orval v7.21.0 🍺
+ * Do not edit manually.
+ * document_storage_service
+ * OpenAPI spec version: 0.1.0
+ */
+import type { VecItem } from './vecItem';
+
+export type Vec = VecItem[];
diff --git a/js/app/packages/service-clients/service-storage/generated/schemas/vecItem.ts b/js/app/packages/service-clients/service-storage/generated/schemas/vecItem.ts
new file mode 100644
index 0000000000..1b242045c0
--- /dev/null
+++ b/js/app/packages/service-clients/service-storage/generated/schemas/vecItem.ts
@@ -0,0 +1,17 @@
+/**
+ * Generated by orval v7.21.0 🍺
+ * Do not edit manually.
+ * document_storage_service
+ * OpenAPI spec version: 0.1.0
+ */
+import type { VecItemIds } from './vecItemIds';
+
+/**
+ * Event and optional entity-id constraints used to match webhook deliveries.
+ */
+export type VecItem = {
+ /** Event names matched by this filter. */
+ events: string[];
+ /** Entity ids matched by this filter. When absent, the filter matches all entity ids. */
+ ids?: VecItemIds;
+};
diff --git a/js/app/packages/service-clients/service-storage/generated/schemas/vecItemIds.ts b/js/app/packages/service-clients/service-storage/generated/schemas/vecItemIds.ts
new file mode 100644
index 0000000000..2533685e16
--- /dev/null
+++ b/js/app/packages/service-clients/service-storage/generated/schemas/vecItemIds.ts
@@ -0,0 +1,11 @@
+/**
+ * Generated by orval v7.21.0 🍺
+ * Do not edit manually.
+ * document_storage_service
+ * OpenAPI spec version: 0.1.0
+ */
+
+/**
+ * Entity ids matched by this filter. When absent, the filter matches all entity ids.
+ */
+export type VecItemIds = string[] | null;
diff --git a/js/app/packages/service-clients/service-storage/generated/schemas/webhook.ts b/js/app/packages/service-clients/service-storage/generated/schemas/webhook.ts
index a6db02e5bb..57bf5efc13 100644
--- a/js/app/packages/service-clients/service-storage/generated/schemas/webhook.ts
+++ b/js/app/packages/service-clients/service-storage/generated/schemas/webhook.ts
@@ -7,6 +7,7 @@
import type { BTreeMap } from './bTreeMap';
import type { String } from './string';
+import type { Vec } from './vec';
import type { WebhookDeletedAt } from './webhookDeletedAt';
import type { WebhookStatus } from './webhookStatus';
@@ -22,6 +23,8 @@ export interface Webhook {
deleted_at?: WebhookDeletedAt;
/** HTTPS endpoint URL. */
endpoint_url: string;
+ /** Typed filters used to match events and optional entity ids. */
+ filters: Vec;
/** Custom delivery headers, after decryption by the repository. */
headers: BTreeMap;
/** Webhook id. */
@@ -30,8 +33,6 @@ export interface Webhook {
is_valid: boolean;
/** Display name. */
name: string;
- /** Event matching rule. */
- rule: unknown;
/** Webhook lifecycle status. */
status: WebhookStatus;
/** Update timestamp. */
diff --git a/js/app/packages/service-clients/service-storage/generated/schemas/webhookFilter.ts b/js/app/packages/service-clients/service-storage/generated/schemas/webhookFilter.ts
new file mode 100644
index 0000000000..929495e584
--- /dev/null
+++ b/js/app/packages/service-clients/service-storage/generated/schemas/webhookFilter.ts
@@ -0,0 +1,17 @@
+/**
+ * Generated by orval v7.21.0 🍺
+ * Do not edit manually.
+ * document_storage_service
+ * OpenAPI spec version: 0.1.0
+ */
+import type { WebhookFilterIds } from './webhookFilterIds';
+
+/**
+ * Event and optional entity-id constraints used to match webhook deliveries.
+ */
+export interface WebhookFilter {
+ /** Event names matched by this filter. */
+ events: string[];
+ /** Entity ids matched by this filter. When absent, the filter matches all entity ids. */
+ ids?: WebhookFilterIds;
+}
diff --git a/js/app/packages/service-clients/service-storage/generated/schemas/webhookFilterIds.ts b/js/app/packages/service-clients/service-storage/generated/schemas/webhookFilterIds.ts
new file mode 100644
index 0000000000..8b6cdafc72
--- /dev/null
+++ b/js/app/packages/service-clients/service-storage/generated/schemas/webhookFilterIds.ts
@@ -0,0 +1,11 @@
+/**
+ * Generated by orval v7.21.0 🍺
+ * Do not edit manually.
+ * document_storage_service
+ * OpenAPI spec version: 0.1.0
+ */
+
+/**
+ * Entity ids matched by this filter. When absent, the filter matches all entity ids.
+ */
+export type WebhookFilterIds = string[] | null;
diff --git a/js/app/packages/service-clients/service-storage/generated/zod.ts b/js/app/packages/service-clients/service-storage/generated/zod.ts
index a90a84e3d3..d932075b01 100644
--- a/js/app/packages/service-clients/service-storage/generated/zod.ts
+++ b/js/app/packages/service-clients/service-storage/generated/zod.ts
@@ -20026,11 +20026,27 @@ export const editProjectV2Response = zod.object({
export const createWebhookBody = zod
.object({
endpoint_url: zod.string().describe('HTTPS endpoint URL.'),
+ filters: zod.array(
+ zod
+ .object({
+ events: zod
+ .array(zod.string())
+ .describe('Event names matched by this filter.'),
+ ids: zod
+ .array(zod.string())
+ .nullish()
+ .describe(
+ 'Entity ids matched by this filter. When absent, the filter matches all entity ids.'
+ ),
+ })
+ .describe(
+ 'Event and optional entity-id constraints used to match webhook deliveries.'
+ )
+ ),
headers: zod
.union([zod.null(), zod.record(zod.string(), zod.string())])
.optional(),
name: zod.string().describe('Display name.'),
- rule: zod.unknown().describe('Rule definition used to match events.'),
scope: zod
.enum(['user', 'team'])
.describe('Scope that owns a newly-created webhook.'),
@@ -20054,14 +20070,32 @@ export const patchWebhookParams = zod.object({
export const patchWebhookBody = zod
.object({
endpoint_url: zod.string().nullish().describe('HTTPS endpoint URL.'),
+ filters: zod
+ .union([
+ zod.null(),
+ zod.array(
+ zod
+ .object({
+ events: zod
+ .array(zod.string())
+ .describe('Event names matched by this filter.'),
+ ids: zod
+ .array(zod.string())
+ .nullish()
+ .describe(
+ 'Entity ids matched by this filter. When absent, the filter matches all entity ids.'
+ ),
+ })
+ .describe(
+ 'Event and optional entity-id constraints used to match webhook deliveries.'
+ )
+ ),
+ ])
+ .optional(),
headers: zod
.union([zod.null(), zod.record(zod.string(), zod.string())])
.optional(),
name: zod.string().nullish().describe('Display name.'),
- rule: zod
- .unknown()
- .optional()
- .describe('Rule definition used to match events.'),
status: zod
.union([
zod.null(),
@@ -20082,6 +20116,23 @@ export const patchWebhookResponse = zod
.nullish()
.describe('Soft-delete timestamp.'),
endpoint_url: zod.string().describe('HTTPS endpoint URL.'),
+ filters: zod.array(
+ zod
+ .object({
+ events: zod
+ .array(zod.string())
+ .describe('Event names matched by this filter.'),
+ ids: zod
+ .array(zod.string())
+ .nullish()
+ .describe(
+ 'Entity ids matched by this filter. When absent, the filter matches all entity ids.'
+ ),
+ })
+ .describe(
+ 'Event and optional entity-id constraints used to match webhook deliveries.'
+ )
+ ),
headers: zod.record(zod.string(), zod.string()),
id: zod.string(),
is_valid: zod
@@ -20090,7 +20141,6 @@ export const patchWebhookResponse = zod
'Whether the current endpoint configuration has passed validation.'
),
name: zod.string().describe('Display name.'),
- rule: zod.unknown().describe('Event matching rule.'),
status: zod
.enum(['active', 'paused', 'disabled'])
.describe('Webhook lifecycle status.'),
diff --git a/js/app/packages/service-clients/service-storage/openapi.json b/js/app/packages/service-clients/service-storage/openapi.json
index 1d168b4ef3..79b3edbdaf 100644
--- a/js/app/packages/service-clients/service-storage/openapi.json
+++ b/js/app/packages/service-clients/service-storage/openapi.json
@@ -12623,12 +12623,16 @@
"CreateWebhookRequest": {
"type": "object",
"description": "Request to create a webhook.",
- "required": ["scope", "name", "endpoint_url", "rule"],
+ "required": ["scope", "name", "endpoint_url", "filters"],
"properties": {
"endpoint_url": {
"type": "string",
"description": "HTTPS endpoint URL."
},
+ "filters": {
+ "$ref": "#/components/schemas/Vec",
+ "description": "Typed filters used to match events and optional entity ids."
+ },
"headers": {
"oneOf": [
{
@@ -12644,9 +12648,6 @@
"type": "string",
"description": "Display name."
},
- "rule": {
- "description": "Rule definition used to match events."
- },
"scope": {
"$ref": "#/components/schemas/WebhookScope",
"description": "Scope that owns the webhook."
@@ -16338,6 +16339,17 @@
"type": ["string", "null"],
"description": "HTTPS endpoint URL."
},
+ "filters": {
+ "oneOf": [
+ {
+ "type": "null"
+ },
+ {
+ "$ref": "#/components/schemas/Vec",
+ "description": "Typed filters used to match events and optional entity ids."
+ }
+ ]
+ },
"headers": {
"oneOf": [
{
@@ -16353,9 +16365,6 @@
"type": ["string", "null"],
"description": "Display name."
},
- "rule": {
- "description": "Rule definition used to match events."
- },
"status": {
"oneOf": [
{
@@ -19693,6 +19702,31 @@
}
}
},
+ "Vec": {
+ "type": "array",
+ "items": {
+ "type": "object",
+ "description": "Event and optional entity-id constraints used to match webhook deliveries.",
+ "required": ["events"],
+ "properties": {
+ "events": {
+ "type": "array",
+ "items": {
+ "type": "string"
+ },
+ "description": "Event names matched by this filter."
+ },
+ "ids": {
+ "type": ["array", "null"],
+ "items": {
+ "type": "string"
+ },
+ "description": "Entity ids matched by this filter. When absent, the filter matches all entity ids."
+ }
+ },
+ "additionalProperties": false
+ }
+ },
"View": {
"type": "object",
"required": [
@@ -19768,7 +19802,7 @@
"created_by_user_id",
"created_at",
"updated_at",
- "rule"
+ "filters"
],
"properties": {
"created_at": {
@@ -19789,6 +19823,10 @@
"type": "string",
"description": "HTTPS endpoint URL."
},
+ "filters": {
+ "$ref": "#/components/schemas/Vec",
+ "description": "Typed filters used to match events and optional entity ids."
+ },
"headers": {
"$ref": "#/components/schemas/BTreeMap",
"description": "Custom delivery headers, after decryption by the repository."
@@ -19805,9 +19843,6 @@
"type": "string",
"description": "Display name."
},
- "rule": {
- "description": "Event matching rule."
- },
"status": {
"$ref": "#/components/schemas/WebhookStatus",
"description": "Webhook lifecycle status."
@@ -19823,6 +19858,28 @@
}
}
},
+ "WebhookFilter": {
+ "type": "object",
+ "description": "Event and optional entity-id constraints used to match webhook deliveries.",
+ "required": ["events"],
+ "properties": {
+ "events": {
+ "type": "array",
+ "items": {
+ "type": "string"
+ },
+ "description": "Event names matched by this filter."
+ },
+ "ids": {
+ "type": ["array", "null"],
+ "items": {
+ "type": "string"
+ },
+ "description": "Entity ids matched by this filter. When absent, the filter matches all entity ids."
+ }
+ },
+ "additionalProperties": false
+ },
"WebhookScope": {
"type": "string",
"description": "Scope that owns a newly-created webhook.",
diff --git a/rust/cloud-storage/.sqlx/query-909a58ec4730ff19d1c4609b4142b95c458a83d75e7f04aeecaeafa4dd65e705.json b/rust/cloud-storage/.sqlx/query-026ae701b71d898643ea8ff1b711313571856233dc180495ba72a1d6d4fe27bb.json
similarity index 51%
rename from rust/cloud-storage/.sqlx/query-909a58ec4730ff19d1c4609b4142b95c458a83d75e7f04aeecaeafa4dd65e705.json
rename to rust/cloud-storage/.sqlx/query-026ae701b71d898643ea8ff1b711313571856233dc180495ba72a1d6d4fe27bb.json
index 2970a74d10..38b4e8edc9 100644
--- a/rust/cloud-storage/.sqlx/query-909a58ec4730ff19d1c4609b4142b95c458a83d75e7f04aeecaeafa4dd65e705.json
+++ b/rust/cloud-storage/.sqlx/query-026ae701b71d898643ea8ff1b711313571856233dc180495ba72a1d6d4fe27bb.json
@@ -1,6 +1,6 @@
{
"db_name": "PostgreSQL",
- "query": "\n UPDATE webhook\n SET\n name = COALESCE($2, name),\n endpoint_url = COALESCE($3, endpoint_url),\n headers = COALESCE($4, headers),\n rule = COALESCE($5, rule),\n status = COALESCE($6, status),\n is_valid = CASE WHEN $3::TEXT IS NOT NULL OR $4::JSONB IS NOT NULL THEN false ELSE is_valid END,\n updated_at = now()\n WHERE id = $1 AND deleted_at IS NULL\n ",
+ "query": "\n UPDATE webhook\n SET\n name = COALESCE($2, name),\n endpoint_url = COALESCE($3, endpoint_url),\n headers = COALESCE($4, headers),\n filters = COALESCE($5, filters),\n status = COALESCE($6, status),\n is_valid = CASE WHEN $3::TEXT IS NOT NULL OR $4::JSONB IS NOT NULL THEN false ELSE is_valid END,\n updated_at = now()\n WHERE id = $1 AND deleted_at IS NULL\n ",
"describe": {
"columns": [],
"parameters": {
@@ -15,5 +15,5 @@
},
"nullable": []
},
- "hash": "909a58ec4730ff19d1c4609b4142b95c458a83d75e7f04aeecaeafa4dd65e705"
+ "hash": "026ae701b71d898643ea8ff1b711313571856233dc180495ba72a1d6d4fe27bb"
}
diff --git a/rust/cloud-storage/.sqlx/query-72a8a4591d37bad783027764bfe1da50aa233032cef86a6ed61db543ef416f9c.json b/rust/cloud-storage/.sqlx/query-199c0cb77a4f34154fc85bb27392e1a89b9cc268f3a5b239bd628efba4e667a0.json
similarity index 63%
rename from rust/cloud-storage/.sqlx/query-72a8a4591d37bad783027764bfe1da50aa233032cef86a6ed61db543ef416f9c.json
rename to rust/cloud-storage/.sqlx/query-199c0cb77a4f34154fc85bb27392e1a89b9cc268f3a5b239bd628efba4e667a0.json
index e669513d1f..9dbc4f27be 100644
--- a/rust/cloud-storage/.sqlx/query-72a8a4591d37bad783027764bfe1da50aa233032cef86a6ed61db543ef416f9c.json
+++ b/rust/cloud-storage/.sqlx/query-199c0cb77a4f34154fc85bb27392e1a89b9cc268f3a5b239bd628efba4e667a0.json
@@ -1,6 +1,6 @@
{
"db_name": "PostgreSQL",
- "query": "\n INSERT INTO webhook (\n id, workspace_id, name, endpoint_url, signing_secret, headers,\n rule, status, is_valid, created_by_user_id\n )\n VALUES ($1, $2, $3, $4, $5, $6, $7, $8, false, $9)\n ",
+ "query": "\n INSERT INTO webhook (\n id, workspace_id, name, endpoint_url, signing_secret, headers,\n filters, status, is_valid, created_by_user_id\n )\n VALUES ($1, $2, $3, $4, $5, $6, $7, $8, false, $9)\n ",
"describe": {
"columns": [],
"parameters": {
@@ -18,5 +18,5 @@
},
"nullable": []
},
- "hash": "72a8a4591d37bad783027764bfe1da50aa233032cef86a6ed61db543ef416f9c"
+ "hash": "199c0cb77a4f34154fc85bb27392e1a89b9cc268f3a5b239bd628efba4e667a0"
}
diff --git a/rust/cloud-storage/.sqlx/query-848c7af0d60329aaee7bca5ad6c771d971197200816018eabb15ae456a448b1d.json b/rust/cloud-storage/.sqlx/query-cc37bd1f7c61dc4f15e72880577d363ca24b1abe3c8cd286735a26db5bc856d5.json
similarity index 88%
rename from rust/cloud-storage/.sqlx/query-848c7af0d60329aaee7bca5ad6c771d971197200816018eabb15ae456a448b1d.json
rename to rust/cloud-storage/.sqlx/query-cc37bd1f7c61dc4f15e72880577d363ca24b1abe3c8cd286735a26db5bc856d5.json
index 310e74c5dc..28943e067d 100644
--- a/rust/cloud-storage/.sqlx/query-848c7af0d60329aaee7bca5ad6c771d971197200816018eabb15ae456a448b1d.json
+++ b/rust/cloud-storage/.sqlx/query-cc37bd1f7c61dc4f15e72880577d363ca24b1abe3c8cd286735a26db5bc856d5.json
@@ -1,6 +1,6 @@
{
"db_name": "PostgreSQL",
- "query": "\n SELECT\n w.id,\n w.workspace_id,\n w.name,\n w.endpoint_url,\n w.signing_secret,\n w.headers,\n w.status,\n w.is_valid,\n w.created_by_user_id,\n w.created_at,\n w.updated_at,\n w.deleted_at,\n w.rule\n FROM webhook w\n WHERE w.id = $1\n AND w.deleted_at IS NULL\n ",
+ "query": "\n SELECT\n w.id,\n w.workspace_id,\n w.name,\n w.endpoint_url,\n w.signing_secret,\n w.headers,\n w.status,\n w.is_valid,\n w.created_by_user_id,\n w.created_at,\n w.updated_at,\n w.deleted_at,\n w.filters\n FROM webhook w\n WHERE w.id = $1\n AND w.deleted_at IS NULL\n ",
"describe": {
"columns": [
{
@@ -65,7 +65,7 @@
},
{
"ordinal": 12,
- "name": "rule",
+ "name": "filters",
"type_info": "Jsonb"
}
],
@@ -90,5 +90,5 @@
false
]
},
- "hash": "848c7af0d60329aaee7bca5ad6c771d971197200816018eabb15ae456a448b1d"
+ "hash": "cc37bd1f7c61dc4f15e72880577d363ca24b1abe3c8cd286735a26db5bc856d5"
}
diff --git a/rust/cloud-storage/.sqlx/query-f151dbffae8ee031399d76f39e78b3e008eb2a93a170a4c6764e988502738570.json b/rust/cloud-storage/.sqlx/query-f151dbffae8ee031399d76f39e78b3e008eb2a93a170a4c6764e988502738570.json
new file mode 100644
index 0000000000..6756af2ac7
--- /dev/null
+++ b/rust/cloud-storage/.sqlx/query-f151dbffae8ee031399d76f39e78b3e008eb2a93a170a4c6764e988502738570.json
@@ -0,0 +1,96 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n SELECT\n w.id,\n w.workspace_id,\n w.name,\n w.endpoint_url,\n w.signing_secret,\n w.headers,\n w.status,\n w.is_valid,\n w.created_by_user_id,\n w.created_at,\n w.updated_at,\n w.deleted_at,\n w.filters\n FROM webhook w\n WHERE w.workspace_id = ANY($1)\n AND w.deleted_at IS NULL\n AND w.status = 'active'\n AND w.is_valid\n AND w.filters @> jsonb_build_array(jsonb_build_object('events', jsonb_build_array($2::text)))\n AND jsonb_path_exists(\n w.filters,\n '$[*] ? (@.events[*] == $event && (!exists (@.ids) || @.ids == null || @.ids[*] == $id))',\n jsonb_build_object('event', $2::text, 'id', $3::text)\n )\n ORDER BY w.id\n ",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "id",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 1,
+ "name": "workspace_id",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 2,
+ "name": "name",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 3,
+ "name": "endpoint_url",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 4,
+ "name": "signing_secret",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 5,
+ "name": "headers",
+ "type_info": "Jsonb"
+ },
+ {
+ "ordinal": 6,
+ "name": "status",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 7,
+ "name": "is_valid",
+ "type_info": "Bool"
+ },
+ {
+ "ordinal": 8,
+ "name": "created_by_user_id",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 9,
+ "name": "created_at",
+ "type_info": "Timestamptz"
+ },
+ {
+ "ordinal": 10,
+ "name": "updated_at",
+ "type_info": "Timestamptz"
+ },
+ {
+ "ordinal": 11,
+ "name": "deleted_at",
+ "type_info": "Timestamptz"
+ },
+ {
+ "ordinal": 12,
+ "name": "filters",
+ "type_info": "Jsonb"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "TextArray",
+ "Text",
+ "Text"
+ ]
+ },
+ "nullable": [
+ false,
+ false,
+ false,
+ false,
+ false,
+ false,
+ false,
+ false,
+ false,
+ false,
+ false,
+ true,
+ false
+ ]
+ },
+ "hash": "f151dbffae8ee031399d76f39e78b3e008eb2a93a170a4c6764e988502738570"
+}
diff --git a/rust/cloud-storage/document_storage_service/src/api/swagger.rs b/rust/cloud-storage/document_storage_service/src/api/swagger.rs
index 3bb255e2ec..06d6a4d63c 100644
--- a/rust/cloud-storage/document_storage_service/src/api/swagger.rs
+++ b/rust/cloud-storage/document_storage_service/src/api/swagger.rs
@@ -527,6 +527,7 @@ use utoipa::OpenApi;
webhook::domain::models::PatchWebhookRequest,
webhook::domain::models::ValidateWebhookResponse,
webhook::domain::models::Webhook,
+ webhook::domain::models::WebhookFilter,
webhook::domain::models::WebhookStatus,
DocumentSubType,
diff --git a/rust/cloud-storage/macro_db_client/migrations/20260709151655_webhook_filters_rename_and_indexes.sql b/rust/cloud-storage/macro_db_client/migrations/20260709151655_webhook_filters_rename_and_indexes.sql
new file mode 100644
index 0000000000..f2f0341d86
--- /dev/null
+++ b/rust/cloud-storage/macro_db_client/migrations/20260709151655_webhook_filters_rename_and_indexes.sql
@@ -0,0 +1,17 @@
+ALTER TABLE webhook RENAME COLUMN rule TO filters;
+
+UPDATE webhook
+SET filters = jsonb_build_array(filters)
+WHERE jsonb_typeof(filters) = 'object';
+
+ALTER TABLE webhook
+ADD CONSTRAINT webhook_filters_is_array CHECK (jsonb_typeof(filters) = 'array');
+
+DROP INDEX IF EXISTS webhook_events_gin_idx;
+
+-- Canonical index-backed filter queries:
+-- filters @> '[{"events": ["<event>"]}]'
+-- filters @> '[{"ids": ["<id>"]}]'
+-- filters @> '[{"events": ["<event>"], "ids": ["<id>"]}]'
+CREATE INDEX webhook_filters_gin_idx
+ON webhook USING GIN (filters jsonb_path_ops);
diff --git a/rust/cloud-storage/webhook/README.md b/rust/cloud-storage/webhook/README.md
index 28dbd90650..394c74abd5 100644
--- a/rust/cloud-storage/webhook/README.md
+++ b/rust/cloud-storage/webhook/README.md
@@ -5,7 +5,7 @@ This crate currently contains the core application-layer webhook pieces for the
## Current scope
- Database tables are present for event ingestion, webhooks, deliveries, and delivery attempts.
-- The Postgres repository supports creating, loading, patching, and updating webhook validity through the `is_valid` field.
+- The Postgres repository supports creating, loading, patching, and updating webhook validity through the `is_valid` field. It also lists active webhooks whose typed `filters` match an event.
- The inbound Axum API supports:
- `POST /webhooks`
- `PATCH /webhooks/{webhook_id}`
@@ -13,6 +13,40 @@ This crate currently contains the core application-layer webhook pieces for the
- Validation sends a signed `webhook.validation.test` event to the webhook endpoint and persists the result by updating `is_valid`.
- Validation attempts are rate limited with the existing `rate_limit` crate. The validation key is `per-user-validate-webhook:{macro_user_id}:{webhook_id}` and the current limit is 10 attempts per 3600-second window.
+## Typed filters
+
+Webhooks use a typed `filters` array to decide which events are eligible for delivery. Each filter contains event names and may also restrict delivery to specific entity ids:
+
+```json
+[
+ {
+ "events": ["document.created"],
+ "ids": ["doc_123"]
+ }
+]
+```
+
+`events` is required on every filter. `ids` is optional; when it is absent, that filter matches all entity ids for its events.
+
+## Database storage and matching
+
+Filters are stored in the `webhook.filters` `JSONB NOT NULL` column. The column has the `webhook_filters_is_array` check constraint so stored filter data is always an array:
+
+```sql
+CHECK (jsonb_typeof(filters) = 'array')
+```
+
+The `webhook_filters_gin_idx` index is a GIN index using `jsonb_path_ops` on `filters`. It supports containment probes on keys inside filter elements, including `events` and `ids`. The canonical matching query used by `WebhookRepo::list_active_webhooks_matching_event` combines an index-backed containment predicate for the event with an exact JSONPath recheck for the same filter element's event and entity-id semantics:
+
+```sql
+WHERE w.filters @> jsonb_build_array(jsonb_build_object('events', jsonb_build_array($2::text)))
+ AND jsonb_path_exists(
+ w.filters,
+ '$[*] ? (@.events[*] == $event && (!exists (@.ids) || @.ids == null || @.ids[*] == $id))',
+ jsonb_build_object('event', $2::text, 'id', $3::text)
+ )
+```
+
## Temporary limitations
- Webhook IDs currently use prefixed UUIDv7 strings (`wh_<uuid_v7>`). They can be swapped for true ULIDs later.
diff --git a/rust/cloud-storage/webhook/src/domain/ingestion.rs b/rust/cloud-storage/webhook/src/domain/ingestion.rs
index 752c2ff444..3d6834a0e2 100644
--- a/rust/cloud-storage/webhook/src/domain/ingestion.rs
+++ b/rust/cloud-storage/webhook/src/domain/ingestion.rs
@@ -1,17 +1,18 @@
//! Webhook event ingestion: broker events in, matching webhooks out.
//!
-//! [`WebhookEventIngestionService`] is the inbound port driven by broker
+//! `WebhookEventIngestionService` is the inbound port driven by broker
//! consumers (see `crate::inbound::kafka_consumer`). It is intentionally
-//! separate from [`WebhookService`](super::ports::WebhookService), which is
-//! the CRUD surface for webhook creation and edits.
+//! separate from `WebhookService`, which is the CRUD surface for webhook
+//! creation and edits.
//!
//! Event handling is not implemented yet: every handler below is a stub that
//! only logs the event. The planned flow for each event is:
//! 1. resolve the users that can see the event's entity via
-//! [`EntityAccessService::get_users_by_entity`] (see
-//! [`WebhookEventIngestionServiceImpl::users_with_access`]);
-//! 2. load those users' active webhooks and match each webhook's `rule`
-//! against the event;
+//! `EntityAccessService::get_users_by_entity` (see
+//! `WebhookEventIngestionServiceImpl::users_with_access`);
+//! 2. call `WebhookRepo::list_active_webhooks_matching_event` to load those
+//! users' active webhooks whose typed `filters` match the event name and
+//! entity id;
//! 3. enqueue a delivery for every match.
#[cfg(test)]
diff --git a/rust/cloud-storage/webhook/src/domain/mod.rs b/rust/cloud-storage/webhook/src/domain/mod.rs
index 2aecc98c77..ea2b4e5c8f 100644
--- a/rust/cloud-storage/webhook/src/domain/mod.rs
+++ b/rust/cloud-storage/webhook/src/domain/mod.rs
@@ -5,6 +5,8 @@
pub mod ingestion;
/// Webhook domain models.
pub mod models;
+#[cfg(test)]
+mod models_test;
#[cfg(feature = "ports")]
/// Webhook ports.
pub mod ports;
diff --git a/rust/cloud-storage/webhook/src/domain/models.rs b/rust/cloud-storage/webhook/src/domain/models.rs
index 8c782a3ac5..e2edd489fd 100644
--- a/rust/cloud-storage/webhook/src/domain/models.rs
+++ b/rust/cloud-storage/webhook/src/domain/models.rs
@@ -2,7 +2,6 @@
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
-use serde_json::Value;
use std::collections::BTreeMap;
/// Webhook id. Webhook ids are stored with a `wh_` prefix.
@@ -11,6 +10,21 @@ pub type WebhookId = String;
/// Custom headers supplied for webhook delivery.
pub type WebhookHeaders = BTreeMap<String, String>;
+/// Event and optional entity-id constraints used to match webhook deliveries.
+#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
+#[cfg_attr(feature = "inbound", derive(utoipa::ToSchema))]
+#[serde(deny_unknown_fields)]
+pub struct WebhookFilter {
+ /// Event names matched by this filter.
+ pub events: Vec<String>,
+ /// Entity ids matched by this filter. When absent, the filter matches all entity ids.
+ #[serde(default, skip_serializing_if = "Option::is_none")]
+ pub ids: Option<Vec<String>>,
+}
+
+/// Collection of webhook filters used to decide delivery eligibility.
+pub type WebhookFilters = Vec<WebhookFilter>;
+
/// Webhook lifecycle status.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "inbound", derive(utoipa::ToSchema))]
@@ -71,8 +85,8 @@ pub struct CreateWebhookRequest {
pub endpoint_url: String,
/// Optional custom delivery headers.
pub headers: Option<WebhookHeaders>,
- /// Rule definition used to match events.
- pub rule: Value,
+ /// Typed filters used to match events and optional entity ids.
+ pub filters: WebhookFilters,
}
/// Request to patch a webhook.
@@ -85,8 +99,8 @@ pub struct PatchWebhookRequest {
pub endpoint_url: Option<String>,
/// Optional custom delivery headers. When present, replaces existing headers.
pub headers: Option<WebhookHeaders>,
- /// Rule definition used to match events.
- pub rule: Option<Value>,
+ /// Typed filters used to match events and optional entity ids.
+ pub filters: Option<WebhookFilters>,
/// Webhook lifecycle status.
pub status: Option<WebhookStatus>,
}
@@ -120,8 +134,8 @@ pub struct Webhook {
pub updated_at: DateTime<Utc>,
/// Soft-delete timestamp.
pub deleted_at: Option<DateTime<Utc>>,
- /// Event matching rule.
- pub rule: Value,
+ /// Typed filters used to match events and optional entity ids.
+ pub filters: WebhookFilters,
}
/// Sanitized result of validating a webhook endpoint.
diff --git a/rust/cloud-storage/webhook/src/domain/models_test.rs b/rust/cloud-storage/webhook/src/domain/models_test.rs
new file mode 100644
index 0000000000..5e1154d331
--- /dev/null
+++ b/rust/cloud-storage/webhook/src/domain/models_test.rs
@@ -0,0 +1,76 @@
+use super::models::{WebhookFilter, WebhookFilters};
+use serde_json::json;
+
+#[test]
+fn deserializes_filter_with_events_and_ids() {
+ let filters: WebhookFilters =
+ serde_json::from_value(json!([{ "events": ["document.created"], "ids": ["doc_1"] }]))
+ .unwrap();
+
+ assert_eq!(
+ filters,
+ vec![WebhookFilter {
+ events: vec!["document.created".to_string()],
+ ids: Some(vec!["doc_1".to_string()]),
+ }]
+ );
+}
+
+#[test]
+fn deserializes_absent_ids_as_none() {
+ let filters: WebhookFilters = serde_json::from_value(json!([{ "events": ["e"] }])).unwrap();
+
+ assert_eq!(
+ filters,
+ vec![WebhookFilter {
+ events: vec!["e".to_string()],
+ ids: None,
+ }]
+ );
+}
+
+#[test]
+fn deserializes_null_ids_as_none() {
+ let filters: WebhookFilters =
+ serde_json::from_value(json!([{ "events": ["e"], "ids": null }])).unwrap();
+
+ assert_eq!(
+ filters,
+ vec![WebhookFilter {
+ events: vec!["e".to_string()],
+ ids: None,
+ }]
+ );
+}
+
+#[test]
+fn serializes_none_ids_without_ids_key() {
+ let filter = WebhookFilter {
+ events: vec!["e".to_string()],
+ ids: None,
+ };
+
+ let value = serde_json::to_value(filter).unwrap();
+
+ assert_eq!(value, json!({ "events": ["e"] }));
+ assert!(value.get("ids").is_none());
+}
+
+#[test]
+fn rejects_unknown_filter_keys() {
+ let result = serde_json::from_value::<WebhookFilters>(json!([{ "events": ["e"], "extra": 1 }]));
+
+ assert!(result.is_err());
+}
+
+#[test]
+fn rejects_non_string_array_entries() {
+ for invalid_filters in [
+ json!([{ "events": [1] }]),
+ json!([{ "events": ["e"], "ids": [1] }]),
+ ] {
+ let result = serde_json::from_value::<WebhookFilters>(invalid_filters);
+
+ assert!(result.is_err());
+ }
+}
diff --git a/rust/cloud-storage/webhook/src/domain/ports.rs b/rust/cloud-storage/webhook/src/domain/ports.rs
index 44ccb897b5..09507ff053 100644
--- a/rust/cloud-storage/webhook/src/domain/ports.rs
+++ b/rust/cloud-storage/webhook/src/domain/ports.rs
@@ -28,6 +28,17 @@ pub trait WebhookRepo: Clone + Send + Sync + 'static {
webhook_id: WebhookId,
) -> impl Future<Output = Result<Option<Webhook>, Self::Err>> + Send;
+ /// List delivery-eligible webhooks whose typed filters match an event and entity id.
+ ///
+ /// Delivery-eligible webhooks are active, valid, and not soft-deleted. A filter without
+ /// `ids` matches every entity id.
+ fn list_active_webhooks_matching_event(
+ &self,
+ workspace_ids: Vec<String>,
+ event: String,
+ entity_id: String,
+ ) -> impl Future<Output = Result<Vec<Webhook>, Self::Err>> + Send;
+
/// Patch an active webhook.
fn patch_webhook(
&self,
diff --git a/rust/cloud-storage/webhook/src/domain/service.rs b/rust/cloud-storage/webhook/src/domain/service.rs
index ed10907a0c..b90f9ccfd4 100644
--- a/rust/cloud-storage/webhook/src/domain/service.rs
+++ b/rust/cloud-storage/webhook/src/domain/service.rs
@@ -2,8 +2,8 @@
use super::{
models::{
- CreateWebhookRequest, PatchWebhookRequest, ValidateWebhookResponse, Webhook, WebhookId,
- WebhookScope, WebhookValidationResult,
+ CreateWebhookRequest, PatchWebhookRequest, ValidateWebhookResponse, Webhook,
+ WebhookFilters, WebhookId, WebhookScope, WebhookValidationResult,
},
ports::{WebhookError, WebhookRepo, WebhookService, WebhookValidationClient},
};
@@ -13,6 +13,10 @@ use std::net::IpAddr;
use url::Url;
const MAX_NAME_LEN: usize = 128;
+const MAX_FILTERS: usize = 50;
+const MAX_FILTER_EVENTS: usize = 100;
+const MAX_FILTER_IDS: usize = 100;
+const MAX_FILTER_VALUE_LEN: usize = 256;
const VALIDATION_EVENT_NAME: &str = "webhook.validation.test";
/// Webhook service implementation.
@@ -95,25 +99,82 @@ fn is_blocked_endpoint_ip(ip: IpAddr) -> bool {
}
}
-fn validate_rule(rule: &serde_json::Value) -> Result<(), WebhookError> {
- let events = rule
- .get("events")
- .and_then(serde_json::Value::as_array)
- .ok_or_else(|| WebhookError::BadRequest("rule.events must be an array".to_string()))?;
+fn validate_filter_value(value: &str) -> bool {
+ let value = value.trim();
+ !value.is_empty() && value.len() <= MAX_FILTER_VALUE_LEN
+}
+
+fn validate_filter_events(events: &[String]) -> Result<(), WebhookError> {
+ if events.is_empty() {
+ return Err(WebhookError::BadRequest(
+ "filters[].events must be a non-empty array of strings".to_string(),
+ ));
+ }
+
+ if events.len() > MAX_FILTER_EVENTS {
+ return Err(WebhookError::BadRequest(format!(
+ "filters[].events must include at most {MAX_FILTER_EVENTS} entries"
+ )));
+ }
+
+ if !events.iter().all(|event| validate_filter_value(event)) {
+ return Err(WebhookError::BadRequest(format!(
+ "filters[].events entries must be non-empty strings at most {MAX_FILTER_VALUE_LEN} characters"
+ )));
+ }
+
+ Ok(())
+}
+
+fn validate_filter_ids(ids: &[String]) -> Result<(), WebhookError> {
+ if ids.is_empty() {
+ return Err(WebhookError::BadRequest(
+ "filters[].ids must be a non-empty array of strings when present".to_string(),
+ ));
+ }
+
+ if ids.len() > MAX_FILTER_IDS {
+ return Err(WebhookError::BadRequest(format!(
+ "filters[].ids must include at most {MAX_FILTER_IDS} entries"
+ )));
+ }
- if events.is_empty() || !events.iter().all(|event| event.as_str().is_some()) {
+ if !ids.iter().all(|id| validate_filter_value(id)) {
+ return Err(WebhookError::BadRequest(format!(
+ "filters[].ids entries must be non-empty strings at most {MAX_FILTER_VALUE_LEN} characters"
+ )));
+ }
+
+ Ok(())
+}
+
+fn validate_filters(filters: &WebhookFilters) -> Result<(), WebhookError> {
+ if filters.is_empty() {
return Err(WebhookError::BadRequest(
- "rule.events must be a non-empty array of strings".to_string(),
+ "filters must be a non-empty array".to_string(),
));
}
+ if filters.len() > MAX_FILTERS {
+ return Err(WebhookError::BadRequest(format!(
+ "filters must include at most {MAX_FILTERS} entries"
+ )));
+ }
+
+ for filter in filters {
+ validate_filter_events(&filter.events)?;
+ if let Some(ids) = &filter.ids {
+ validate_filter_ids(ids)?;
+ }
+ }
+
Ok(())
}
fn validate_create_request(request: &CreateWebhookRequest) -> Result<(), WebhookError> {
validate_name(&request.name)?;
validate_endpoint_url(&request.endpoint_url)?;
- validate_rule(&request.rule)
+ validate_filters(&request.filters)
}
fn validate_patch_request(request: &PatchWebhookRequest) -> Result<(), WebhookError> {
@@ -123,8 +184,8 @@ fn validate_patch_request(request: &PatchWebhookRequest) -> Result<(), WebhookEr
if let Some(endpoint_url) = &request.endpoint_url {
validate_endpoint_url(endpoint_url)?;
}
- if let Some(rule) = &request.rule {
- validate_rule(rule)?;
+ if let Some(filters) = &request.filters {
+ validate_filters(filters)?;
}
Ok(())
}
diff --git a/rust/cloud-storage/webhook/src/domain/service_test.rs b/rust/cloud-storage/webhook/src/domain/service_test.rs
index e459d3260f..52572e7bb4 100644
--- a/rust/cloud-storage/webhook/src/domain/service_test.rs
+++ b/rust/cloud-storage/webhook/src/domain/service_test.rs
@@ -1,7 +1,7 @@
use super::{
models::{
- CreateWebhookRequest, PatchWebhookRequest, ValidateWebhookResponse, Webhook, WebhookScope,
- WebhookStatus, WebhookValidationResult,
+ CreateWebhookRequest, PatchWebhookRequest, ValidateWebhookResponse, Webhook, WebhookFilter,
+ WebhookFilters, WebhookScope, WebhookStatus, WebhookValidationResult,
},
ports::{WebhookError, WebhookRepo, WebhookService, WebhookValidationClient},
service::WebhookServiceImpl,
@@ -34,6 +34,17 @@ impl FakeRepo {
}
}
+fn filter_matches_event(filter: &WebhookFilter, event: &str, entity_id: &str) -> bool {
+ if !filter.events.iter().any(|candidate| candidate == event) {
+ return false;
+ }
+
+ match &filter.ids {
+ Some(ids) => ids.iter().any(|id| id == entity_id),
+ None => true,
+ }
+}
+
impl WebhookRepo for FakeRepo {
type Err = anyhow::Error;
@@ -60,6 +71,35 @@ impl WebhookRepo for FakeRepo {
.filter(|webhook| webhook.id == webhook_id))
}
+ async fn list_active_webhooks_matching_event(
+ &self,
+ workspace_ids: Vec<String>,
+ event: String,
+ entity_id: String,
+ ) -> Result<Vec<Webhook>, Self::Err> {
+ let Some(webhook) = self.state.lock().await.webhook.clone() else {
+ return Ok(Vec::new());
+ };
+
+ if webhook.deleted_at.is_some()
+ || webhook.status != WebhookStatus::Active
+ || !webhook.is_valid
+ || !workspace_ids.contains(&webhook.workspace_id)
+ {
+ return Ok(Vec::new());
+ }
+
+ if webhook
+ .filters
+ .iter()
+ .any(|filter| filter_matches_event(filter, &event, &entity_id))
+ {
+ return Ok(vec![webhook]);
+ }
+
+ Ok(Vec::new())
+ }
+
async fn patch_webhook(
&self,
webhook_id: String,
@@ -82,8 +122,8 @@ impl WebhookRepo for FakeRepo {
if let Some(headers) = request.headers {
webhook.headers = headers;
}
- if let Some(rule) = request.rule {
- webhook.rule = rule;
+ if let Some(filters) = request.filters {
+ webhook.filters = filters;
}
if let Some(status) = request.status {
webhook.status = status;
@@ -187,8 +227,11 @@ fn caller() -> MacroUserIdStr<'static> {
MacroUserIdStr::try_from_email("[email protected]").unwrap()
}
-fn valid_rule() -> serde_json::Value {
- serde_json::json!({ "events": ["file.created"] })
+fn valid_filters() -> WebhookFilters {
+ vec![WebhookFilter {
+ events: vec!["file.created".to_string()],
+ ids: None,
+ }]
}
fn create_request() -> CreateWebhookRequest {
@@ -200,7 +243,7 @@ fn create_request() -> CreateWebhookRequest {
"X-Custom".to_string(),
"value".to_string(),
)])),
- rule: valid_rule(),
+ filters: valid_filters(),
}
}
@@ -209,7 +252,7 @@ fn patch_request() -> PatchWebhookRequest {
name: Some("Updated".to_string()),
endpoint_url: None,
headers: None,
- rule: None,
+ filters: None,
status: None,
}
}
@@ -233,7 +276,7 @@ fn webhook_from_create(
created_at: now,
updated_at: now,
deleted_at: None,
- rule: request.rule,
+ filters: request.filters,
}
}
@@ -330,15 +373,70 @@ async fn private_and_link_local_endpoints_are_rejected() {
}
#[tokio::test]
-async fn empty_events_array_is_rejected() {
+async fn empty_filters_list_is_rejected() {
+ let repo = FakeRepo::default();
+ let service = WebhookServiceImpl::new(repo, FakeValidationClient::default());
+ let mut request = create_request();
+ request.filters = Vec::new();
+
+ assert_bad_request(service.create_webhook(caller(), request).await);
+}
+
+#[tokio::test]
+async fn filter_with_empty_events_is_rejected() {
let repo = FakeRepo::default();
let service = WebhookServiceImpl::new(repo, FakeValidationClient::default());
let mut request = create_request();
- request.rule = serde_json::json!({ "events": [] });
+ request.filters = vec![WebhookFilter {
+ events: Vec::new(),
+ ids: None,
+ }];
assert_bad_request(service.create_webhook(caller(), request).await);
}
+#[tokio::test]
+async fn filter_with_empty_ids_is_rejected() {
+ let repo = FakeRepo::default();
+ let service = WebhookServiceImpl::new(repo, FakeValidationClient::default());
+ let mut request = create_request();
+ request.filters = vec![WebhookFilter {
+ events: vec!["file.created".to_string()],
+ ids: Some(Vec::new()),
+ }];
+
+ assert_bad_request(service.create_webhook(caller(), request).await);
+}
+
+#[tokio::test]
+async fn filter_without_ids_is_accepted() {
+ let repo = FakeRepo::default();
+ let service = WebhookServiceImpl::new(repo, FakeValidationClient::default());
+
+ let webhook = service
+ .create_webhook(caller(), create_request())
+ .await
+ .unwrap();
+
+ assert_eq!(webhook.filters, valid_filters());
+}
+
+#[tokio::test]
+async fn filter_with_valid_ids_is_accepted() {
+ let repo = FakeRepo::default();
+ let service = WebhookServiceImpl::new(repo, FakeValidationClient::default());
+ let mut request = create_request();
+ request.filters = vec![WebhookFilter {
+ events: vec!["file.created".to_string()],
+ ids: Some(vec!["file_1".to_string()]),
+ }];
+ let expected_filters = request.filters.clone();
+
+ let webhook = service.create_webhook(caller(), request).await.unwrap();
+
+ assert_eq!(webhook.filters, expected_filters);
+}
+
#[tokio::test]
async fn validate_succeeds_and_sets_webhook_validity() {
let repo = FakeRepo::with_webhook(existing_webhook(), None).await;
diff --git a/rust/cloud-storage/webhook/src/inbound/axum_router_test.rs b/rust/cloud-storage/webhook/src/inbound/axum_router_test.rs
index 403cd4af7f..edb95914d9 100644
--- a/rust/cloud-storage/webhook/src/inbound/axum_router_test.rs
+++ b/rust/cloud-storage/webhook/src/inbound/axum_router_test.rs
@@ -399,7 +399,7 @@ fn create_body() -> serde_json::Value {
"name": "Events",
"endpoint_url": "https://example.com/webhook",
"headers": {"x-custom": "value"},
- "rule": {"events": ["document.created"]}
+ "filters": [{"events": ["document.created"]}]
})
}
@@ -425,7 +425,7 @@ fn webhook() -> Webhook {
"created_at": "2026-06-29T00:00:00Z",
"updated_at": "2026-06-29T00:00:00Z",
"deleted_at": null,
- "rule": {"events": ["document.created"]}
+ "filters": [{"events": ["document.created"]}]
}))
.unwrap()
}
diff --git a/rust/cloud-storage/webhook/src/inbound/kafka_consumer.rs b/rust/cloud-storage/webhook/src/inbound/kafka_consumer.rs
index 03c4b330fd..58491055d5 100644
--- a/rust/cloud-storage/webhook/src/inbound/kafka_consumer.rs
+++ b/rust/cloud-storage/webhook/src/inbound/kafka_consumer.rs
@@ -227,7 +227,7 @@ async fn ingest_with_retry<S: WebhookEventIngestionService>(
/// Connects to `brokers`, subscribes to [`MacroDocumentsTopic`] and
/// [`MacroChannelsTopic`] under the `webhook-event-ingestion` consumer group,
/// and feeds every decoded event to `service`, committing each offset only
-/// after ingestion succeeds (see [`ingest_with_retry`] for the retry policy).
+/// after ingestion succeeds (see `ingest_with_retry` for the retry policy).
/// Returns an error when the consumer cannot be created or subscribed, or when
/// a transient ingestion failure exhausts its in-process retries; callers
/// should treat that as fatal and restart, which redelivers the uncommitted
diff --git a/rust/cloud-storage/webhook/src/lib.rs b/rust/cloud-storage/webhook/src/lib.rs
index 0804f14701..770873f2fa 100644
--- a/rust/cloud-storage/webhook/src/lib.rs
+++ b/rust/cloud-storage/webhook/src/lib.rs
@@ -6,8 +6,14 @@
//!
//! - database tables for event ingestion, webhooks, deliveries, and delivery
//! attempts;
+//! - a typed `filters` model for matching webhook deliveries, shaped as an
+//! array of objects such as
+//! `[{"events": ["document.created"], "ids": ["doc_123"]}]`, where
+//! `ids` is optional and an absent `ids` field matches all entity ids;
//! - a repository that can create, get, patch, and update webhook validity via
-//! `is_valid`;
+//! `is_valid`; it can also list active webhooks whose typed `filters` match
+//! an event with
+//! `domain::ports::WebhookRepo::list_active_webhooks_matching_event`;
//! - inbound handlers for `POST /webhooks`, `PATCH /webhooks/{webhook_id}`, and
//! `POST /webhooks/{webhook_id}/validate`;
//! - validation that sends a signed `webhook.validation.test` event and persists
diff --git a/rust/cloud-storage/webhook/src/outbound/pg_repository.rs b/rust/cloud-storage/webhook/src/outbound/pg_repository.rs
index e234197fb1..9e8e333504 100644
--- a/rust/cloud-storage/webhook/src/outbound/pg_repository.rs
+++ b/rust/cloud-storage/webhook/src/outbound/pg_repository.rs
@@ -5,8 +5,8 @@ mod pg_repository_test;
use crate::domain::{
models::{
- CreateWebhookRequest, PatchWebhookRequest, Webhook, WebhookHeaders, WebhookId,
- WebhookStatus,
+ CreateWebhookRequest, PatchWebhookRequest, Webhook, WebhookFilters, WebhookHeaders,
+ WebhookId, WebhookStatus,
},
ports::WebhookRepo,
};
@@ -42,7 +42,7 @@ struct WebhookRow {
created_at: chrono::DateTime<chrono::Utc>,
updated_at: chrono::DateTime<chrono::Utc>,
deleted_at: Option<chrono::DateTime<chrono::Utc>>,
- rule: Value,
+ filters: Value,
}
fn new_webhook_id() -> String {
@@ -61,9 +61,16 @@ fn row_to_webhook(row: WebhookRow) -> Result<Webhook, sqlx::Error> {
source: message.into(),
})?;
+ let filters = serde_json::from_value::<WebhookFilters>(row.filters).map_err(|err| {
+ sqlx::Error::ColumnDecode {
+ index: "filters".to_string(),
+ source: err.into(),
+ }
+ })?;
+
Ok(Webhook {
- id: row.id.clone(),
- workspace_id: row.workspace_id.clone(),
+ id: row.id,
+ workspace_id: row.workspace_id,
name: row.name,
endpoint_url: row.endpoint_url,
signing_secret: row.signing_secret,
@@ -74,7 +81,7 @@ fn row_to_webhook(row: WebhookRow) -> Result<Webhook, sqlx::Error> {
created_at: row.created_at,
updated_at: row.updated_at,
deleted_at: row.deleted_at,
- rule: row.rule,
+ filters,
})
}
@@ -95,7 +102,7 @@ async fn fetch_webhook(pool: &PgPool, webhook_id: &str) -> Result<Option<Webhook
w.created_at,
w.updated_at,
w.deleted_at,
- w.rule
+ w.filters
FROM webhook w
WHERE w.id = $1
AND w.deleted_at IS NULL
@@ -123,11 +130,13 @@ impl WebhookRepo for PgRepository {
let webhook_id = new_webhook_id();
let status = WebhookStatus::Active.as_str();
let created_by_user_id = created_by_user_id.as_ref();
+ let filters = serde_json::to_value(&request.filters)
+ .map_err(|err| sqlx::Error::Encode(err.into()))?;
sqlx::query!(
r#"
INSERT INTO webhook (
id, workspace_id, name, endpoint_url, signing_secret, headers,
- rule, status, is_valid, created_by_user_id
+ filters, status, is_valid, created_by_user_id
)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, false, $9)
"#,
@@ -137,7 +146,7 @@ impl WebhookRepo for PgRepository {
request.endpoint_url,
signing_secret,
headers,
- request.rule,
+ filters,
status,
created_by_user_id
)
@@ -153,6 +162,53 @@ impl WebhookRepo for PgRepository {
fetch_webhook(&self.pool, &webhook_id).await
}
+ #[tracing::instrument(skip(self, workspace_ids), err)]
+ async fn list_active_webhooks_matching_event(
+ &self,
+ workspace_ids: Vec<String>,
+ event: String,
+ entity_id: String,
+ ) -> Result<Vec<Webhook>, Self::Err> {
+ let rows = sqlx::query_as!(
+ WebhookRow,
+ r#"
+ SELECT
+ w.id,
+ w.workspace_id,
+ w.name,
+ w.endpoint_url,
+ w.signing_secret,
+ w.headers,
+ w.status,
+ w.is_valid,
+ w.created_by_user_id,
+ w.created_at,
+ w.updated_at,
+ w.deleted_at,
+ w.filters
+ FROM webhook w
+ WHERE w.workspace_id = ANY($1)
+ AND w.deleted_at IS NULL
+ AND w.status = 'active'
+ AND w.is_valid
+ AND w.filters @> jsonb_build_array(jsonb_build_object('events', jsonb_build_array($2::text)))
+ AND jsonb_path_exists(
+ w.filters,
+ '$[*] ? (@.events[*] == $event && (!exists (@.ids) || @.ids == null || @.ids[*] == $id))',
+ jsonb_build_object('event', $2::text, 'id', $3::text)
+ )
+ ORDER BY w.id
+ "#,
+ &workspace_ids,
+ &event,
+ &entity_id
+ )
+ .fetch_all(&self.pool)
+ .await?;
+
+ rows.into_iter().map(row_to_webhook).collect()
+ }
+
#[tracing::instrument(skip(self, request), err)]
async fn patch_webhook(
&self,
@@ -163,6 +219,12 @@ impl WebhookRepo for PgRepository {
serde_json::to_value(headers).unwrap_or_else(|_| Value::Object(Default::default()))
});
let status = request.status.map(WebhookStatus::as_str);
+ let filters = request
+ .filters
+ .as_ref()
+ .map(serde_json::to_value)
+ .transpose()
+ .map_err(|err| sqlx::Error::Encode(err.into()))?;
let updated = sqlx::query!(
r#"
UPDATE webhook
@@ -170,7 +232,7 @@ impl WebhookRepo for PgRepository {
name = COALESCE($2, name),
endpoint_url = COALESCE($3, endpoint_url),
headers = COALESCE($4, headers),
- rule = COALESCE($5, rule),
+ filters = COALESCE($5, filters),
status = COALESCE($6, status),
is_valid = CASE WHEN $3::TEXT IS NOT NULL OR $4::JSONB IS NOT NULL THEN false ELSE is_valid END,
updated_at = now()
@@ -180,7 +242,7 @@ impl WebhookRepo for PgRepository {
request.name,
request.endpoint_url,
headers,
- request.rule,
+ filters,
status
)
.execute(&self.pool)
diff --git a/rust/cloud-storage/webhook/src/outbound/pg_repository_test.rs b/rust/cloud-storage/webhook/src/outbound/pg_repository_test.rs
index 433beca8e2..d9bba1d9b6 100644
--- a/rust/cloud-storage/webhook/src/outbound/pg_repository_test.rs
+++ b/rust/cloud-storage/webhook/src/outbound/pg_repository_test.rs
@@ -1,5 +1,8 @@
use super::*;
-use crate::domain::ports::WebhookRepo;
+use crate::domain::{
+ models::{WebhookFilter, WebhookStatus},
+ ports::WebhookRepo,
+};
use macro_db_migrator::MACRO_DB_MIGRATIONS;
use macro_user_id::user_id::MacroUserIdStr;
use serde_json::json;
@@ -7,6 +10,7 @@ use sqlx::{PgPool, types::Uuid};
const USER_ID: &str = "macro|[email protected]";
const TEAM_ID: &str = "11111111-1111-1111-1111-111111111111";
+const OTHER_WORKSPACE_ID: &str = "workspace_other";
fn user_id() -> MacroUserIdStr<'static> {
MacroUserIdStr::try_from(USER_ID.to_string()).expect("valid macro user id")
@@ -18,7 +22,10 @@ fn create_request() -> CreateWebhookRequest {
name: "Build events".to_string(),
endpoint_url: "https://example.com/webhook".to_string(),
headers: None,
- rule: json!({ "events": ["build.created"] }),
+ filters: vec![WebhookFilter {
+ events: vec!["build.created".to_string()],
+ ids: None,
+ }],
}
}
@@ -67,19 +74,295 @@ async fn create_webhook(repo: &PgRepository) -> Webhook {
.expect("create webhook")
}
+fn webhook_filter(events: &[&str], ids: Option<&[&str]>) -> WebhookFilter {
+ WebhookFilter {
+ events: events.iter().map(|event| (*event).to_string()).collect(),
+ ids: ids.map(|ids| ids.iter().map(|id| (*id).to_string()).collect()),
+ }
+}
+
+fn webhook_ids(webhooks: &[Webhook]) -> Vec<&str> {
+ webhooks.iter().map(|webhook| webhook.id.as_str()).collect()
+}
+
+async fn insert_webhook_for_matching(
+ pool: &PgPool,
+ id: &str,
+ workspace_id: &str,
+ filters: Vec<WebhookFilter>,
+ status: WebhookStatus,
+ is_valid: bool,
+ deleted: bool,
+) -> anyhow::Result<()> {
+ let deleted_at = deleted.then(chrono::Utc::now);
+ let endpoint_url = format!("https://example.com/{id}");
+ let filters = serde_json::to_value(filters)?;
+ let name = format!("{id} webhook");
+ let status = status.as_str();
+
+ sqlx::query!(
+ r#"
+ INSERT INTO webhook (
+ id, workspace_id, name, endpoint_url, signing_secret,
+ filters, status, is_valid, created_by_user_id, deleted_at
+ )
+ VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)
+ "#,
+ id,
+ workspace_id,
+ name,
+ endpoint_url,
+ "signing-secret",
+ filters,
+ status,
+ is_valid,
+ USER_ID,
+ deleted_at
+ )
+ .execute(pool)
+ .await?;
+
+ Ok(())
+}
+
#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
-async fn create_inserts_webhook_with_rule(pool: PgPool) -> anyhow::Result<()> {
+async fn create_inserts_webhook_with_filters(pool: PgPool) -> anyhow::Result<()> {
insert_user(&pool).await?;
let repo = PgRepository::new(pool.clone());
let webhook = create_webhook(&repo).await;
assert!(webhook.id.starts_with("wh_"));
- assert_eq!(webhook.rule, json!({ "events": ["build.created"] }));
+ assert_eq!(
+ webhook.filters,
+ vec![WebhookFilter {
+ events: vec!["build.created".to_string()],
+ ids: None,
+ }]
+ );
assert!(!webhook.is_valid);
Ok(())
}
+#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
+async fn object_shaped_filters_are_rejected_by_check_constraint(
+ pool: PgPool,
+) -> anyhow::Result<()> {
+ let result = sqlx::query!(
+ r#"
+ INSERT INTO webhook (
+ id, workspace_id, name, endpoint_url, signing_secret,
+ filters, created_by_user_id
+ )
+ VALUES ($1, $2, $3, $4, $5, $6, $7)
+ "#,
+ "wh_object_filters",
+ USER_ID,
+ "Object filters",
+ "https://example.com/webhook",
+ "signing-secret",
+ json!({ "events": ["x"] }),
+ USER_ID
+ )
+ .execute(&pool)
+ .await;
+
+ let err = result.expect_err("object-shaped filters should violate array check");
+ assert_eq!(
+ err.as_database_error().and_then(|db| db.constraint()),
+ Some("webhook_filters_is_array")
+ );
+ Ok(())
+}
+
+#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
+async fn containment_query_uses_filters_gin_index(pool: PgPool) -> anyhow::Result<()> {
+ let mut connection = pool.acquire().await?;
+ sqlx::query("SET enable_seqscan = off")
+ .execute(&mut *connection)
+ .await?;
+
+ // EXPLAIN output is planner text rather than application data, so the SQLx
+ // macros do not add useful type validation for this assertion.
+ let plan_lines = sqlx::query_scalar::<_, String>(
+ r#"
+ EXPLAIN SELECT id
+ FROM webhook
+ WHERE filters @> '[{"events": ["document.created"]}]'
+ "#,
+ )
+ .fetch_all(&mut *connection)
+ .await?;
+ let plan_text = plan_lines.join("\n");
+
+ assert!(
+ plan_text.contains("webhook_filters_gin_idx"),
+ "expected webhook_filters_gin_idx in plan:\n{plan_text}"
+ );
+ Ok(())
+}
+
+#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
+async fn list_matching_event_treats_missing_ids_as_wildcard(pool: PgPool) -> anyhow::Result<()> {
+ insert_webhook_for_matching(
+ &pool,
+ "wh_all_ids",
+ USER_ID,
+ vec![webhook_filter(&["document.created"], None)],
+ WebhookStatus::Active,
+ true,
+ false,
+ )
+ .await?;
+ let repo = PgRepository::new(pool);
+
+ let doc_1_webhooks = repo
+ .list_active_webhooks_matching_event(
+ vec![USER_ID.to_string()],
+ "document.created".to_string(),
+ "doc_1".to_string(),
+ )
+ .await?;
+ let doc_2_webhooks = repo
+ .list_active_webhooks_matching_event(
+ vec![USER_ID.to_string()],
+ "document.created".to_string(),
+ "doc_2".to_string(),
+ )
+ .await?;
+ let non_matching_event_webhooks = repo
+ .list_active_webhooks_matching_event(
+ vec![USER_ID.to_string()],
+ "document.deleted".to_string(),
+ "doc_1".to_string(),
+ )
+ .await?;
+
+ assert_eq!(webhook_ids(&doc_1_webhooks), vec!["wh_all_ids"]);
+ assert_eq!(webhook_ids(&doc_2_webhooks), vec!["wh_all_ids"]);
+ assert!(non_matching_event_webhooks.is_empty());
+ Ok(())
+}
+
+#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
+async fn list_matching_event_requires_id_in_same_filter_element(
+ pool: PgPool,
+) -> anyhow::Result<()> {
+ insert_webhook_for_matching(
+ &pool,
+ "wh_doc_1_only",
+ USER_ID,
+ vec![webhook_filter(&["document.created"], Some(&["doc_1"]))],
+ WebhookStatus::Active,
+ true,
+ false,
+ )
+ .await?;
+ insert_webhook_for_matching(
+ &pool,
+ "wh_same_element",
+ USER_ID,
+ vec![
+ webhook_filter(&["channel.created"], None),
+ webhook_filter(&["document.created"], Some(&["doc_1"])),
+ ],
+ WebhookStatus::Active,
+ true,
+ false,
+ )
+ .await?;
+ let repo = PgRepository::new(pool);
+
+ let doc_1_webhooks = repo
+ .list_active_webhooks_matching_event(
+ vec![USER_ID.to_string()],
+ "document.created".to_string(),
+ "doc_1".to_string(),
+ )
+ .await?;
+ let doc_2_webhooks = repo
+ .list_active_webhooks_matching_event(
+ vec![USER_ID.to_string()],
+ "document.created".to_string(),
+ "doc_2".to_string(),
+ )
+ .await?;
+
+ assert_eq!(
+ webhook_ids(&doc_1_webhooks),
+ vec!["wh_doc_1_only", "wh_same_element"]
+ );
+ assert!(doc_2_webhooks.is_empty());
+ Ok(())
+}
+
+#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
+async fn list_matching_event_excludes_ineligible_webhooks(pool: PgPool) -> anyhow::Result<()> {
+ let matching_filter = vec![webhook_filter(&["document.created"], None)];
+ insert_webhook_for_matching(
+ &pool,
+ "wh_active_valid",
+ USER_ID,
+ matching_filter.clone(),
+ WebhookStatus::Active,
+ true,
+ false,
+ )
+ .await?;
+ insert_webhook_for_matching(
+ &pool,
+ "wh_paused",
+ USER_ID,
+ matching_filter.clone(),
+ WebhookStatus::Paused,
+ true,
+ false,
+ )
+ .await?;
+ insert_webhook_for_matching(
+ &pool,
+ "wh_invalid",
+ USER_ID,
+ matching_filter.clone(),
+ WebhookStatus::Active,
+ false,
+ false,
+ )
+ .await?;
+ insert_webhook_for_matching(
+ &pool,
+ "wh_deleted",
+ USER_ID,
+ matching_filter.clone(),
+ WebhookStatus::Active,
+ true,
+ true,
+ )
+ .await?;
+ insert_webhook_for_matching(
+ &pool,
+ "wh_other_workspace",
+ OTHER_WORKSPACE_ID,
+ matching_filter,
+ WebhookStatus::Active,
+ true,
+ false,
+ )
+ .await?;
+ let repo = PgRepository::new(pool);
+
+ let webhooks = repo
+ .list_active_webhooks_matching_event(
+ vec![USER_ID.to_string()],
+ "document.created".to_string(),
+ "doc_1".to_string(),
+ )
+ .await?;
+
+ assert_eq!(webhook_ids(&webhooks), vec!["wh_active_valid"]);
+ Ok(())
+}
+
#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
async fn patch_updates_endpoint_and_resets_validity(pool: PgPool) -> anyhow::Result<()> {
insert_user(&pool).await?;
@@ -94,7 +377,7 @@ async fn patch_updates_endpoint_and_resets_validity(pool: PgPool) -> anyhow::Res
name: Some("Deploy events".to_string()),
endpoint_url: Some("https://example.com/deploy".to_string()),
headers: None,
- rule: None,
+ filters: None,
status: None,
},
)