Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 30 additions & 13 deletions crates/buzz-webhook-gateway/README.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
# buzz-webhook-gateway

Receives webhooks from providers (GitHub, PagerDuty, Sentry, incident.io),
verifies them, and posts one kind-9 message to each Buzz channel whose
subscription matches.
verifies them, and delivers them to each Buzz channel whose subscription matches.

```text
provider ──POST /webhooks/{integration}──▶ intake: Host check, HMAC, dedupe
│ webhook_gateway_deliveries
▼
fan-out: CEL filter + message template
fan-out: CEL filter + message expression
│ webhook_gateway_messages
▼
forwarder: sign kind 9, POST {scheme}://{community_host}/events
forwarder: sign message, reply or edit, POST {scheme}://{community_host}/events
```

Integrations, their secrets and subscriptions are written through the
Expand Down Expand Up @@ -126,20 +125,38 @@ mgmt() { # METHOD PATH [JSON]

INTEGRATION=$(uuidgen | tr A-Z a-z)
openssl rand -hex 20 > $GW/github.secret
mgmt PUT /v1/integrations/$INTEGRATION \
"{\"operation_id\":\"$(uuidgen | tr A-Z a-z)\",\"revision\":1,\"community_host\":\"localhost\",\"provider\":\"github\"}"
mgmt PUT /v1/integrations/$INTEGRATION "$(jq -nc --arg op $(uuidgen | tr A-Z a-z) '{
operation_id: $op, revision: 1, community_host: "localhost", provider: "github",
message_expr: "\"PR #\" + string(payload.number) + \" opened: \" + payload.pull_request.title"}')"
mgmt PUT /v1/integrations/$INTEGRATION/secret \
"{\"operation_id\":\"$(uuidgen | tr A-Z a-z)\",\"revision\":2,\"secret\":\"$(cat $GW/github.secret)\",\"grace_seconds\":0}"
mgmt PUT /v1/integrations/$INTEGRATION/subscriptions/$(uuidgen | tr A-Z a-z) "$(jq -nc --arg c $CHANNEL '{
revision: 1, channel_id: $c,
filter: "event.type == \"pull_request.opened\"",
message_template: "\"PR #\" + string(payload.number) + \" opened: \" + payload.pull_request.title"}')"
filter: "event.type == \"pull_request.opened\""}')"
```

`filter` and `message_template` are CEL over `event` (`provider` and `type`,
for example `pull_request.opened`) and `payload` (the webhook body). Both are
optional. Without a filter, every delivery matches. Without a template, the
message is `<provider>: <type>`.
The integration's `message_expr` and each subscription's `filter` are CEL
over `event` (`provider` and `type`, for example `pull_request.opened`) and
`payload` (the webhook body). Both are optional. Without a filter, every
delivery matches. Every matching subscription posts the integration's message;
without a message expression, it is `<provider>: <type>`.

An integration also stores `delivery_mode` (`channel_subscriptions`, the
default, or `caller_selects_channel`, which requires `channel_expr`),
`message_data_expr`, `group_key_expr` and `group_key_action`. For deliveries
with the same group key in the same channel, `reply_in_thread` (the default)
selects a reply in the original thread and `update_message` selects an edit
to the original message. For example, a PagerDuty integration can set
`group_key_expr` to `payload.event.data.id` to group an incident's lifecycle.
The first successful delivery starts the thread. Subsequent deliveries with
that key target its original message. Each integration and channel has its
own groups. Idle groups expire after the gateway retention period (14 days by
default). Omitting `group_key_expr` posts each match as a new message.

The worker currently routes via subscriptions and renders `message_expr`.
Routing by `channel_expr` and rendering `message_data_expr` are still pending.
When upgrading, drain workers that predate grouping before enabling grouped
delivery. Those workers send queued messages as top-level posts.

### 7. Send a webhook

Expand Down Expand Up @@ -167,7 +184,7 @@ mgmt GET /v1/integrations/$INTEGRATION/deliveries | jq '.deliveries[0] | {state,

The channel shows `PR #42 opened: Fix the build`. The delivery reports
`messages: {queued, accepted, failed}`. A message the relay refused, or whose
template failed, is counted as failed; its error is in
message expression failed, is counted as failed; its error is in
`webhook_gateway_messages.last_error`:

```bash
Expand Down
60 changes: 57 additions & 3 deletions crates/buzz-webhook-gateway/migrations/0001_webhook_gateway.sql
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
-- Durable state for the webhook webhook gateway. This lives in the gateway's
-- own database, outside relay community tenancy: one gateway serves many
-- communities and only ever hands them signed events.
-- The webhook gateway owns these tables in its dedicated database.
SET LOCAL lock_timeout = '5s';

CREATE TABLE webhook_gateway_integrations (
id UUID PRIMARY KEY,
community_host TEXT NOT NULL CHECK (community_host = lower(community_host) AND length(community_host) BETWEEN 1 AND 253),
provider TEXT NOT NULL CHECK (provider IN ('github', 'pagerduty', 'sentry', 'incident_io')),
delivery_mode TEXT NOT NULL DEFAULT 'channel_subscriptions' CHECK (delivery_mode IN ('channel_subscriptions', 'caller_selects_channel')),
channel_expr TEXT CHECK (channel_expr IS NULL OR length(channel_expr) BETWEEN 1 AND 4096),
message_expr TEXT CHECK (message_expr IS NULL OR length(message_expr) BETWEEN 1 AND 4096),
message_data_expr TEXT CHECK (message_data_expr IS NULL OR length(message_data_expr) BETWEEN 1 AND 4096),
group_key_expr TEXT CHECK (group_key_expr IS NULL OR length(group_key_expr) BETWEEN 1 AND 4096),
group_key_action TEXT NOT NULL DEFAULT 'reply_in_thread' CHECK (group_key_action IN ('reply_in_thread', 'update_message')),
revision BIGINT NOT NULL CHECK (revision > 0),
enabled BOOLEAN NOT NULL DEFAULT true,
disabled_at TIMESTAMPTZ,
Expand All @@ -29,6 +35,7 @@ CREATE TABLE webhook_gateway_applied_operations (
operation_id UUID PRIMARY KEY,
integration_id UUID NOT NULL,
revision BIGINT NOT NULL CHECK (revision > 0),
request_fingerprint TEXT NOT NULL CHECK (length(request_fingerprint) <= 64),
response JSONB NOT NULL,
applied_at TIMESTAMPTZ NOT NULL DEFAULT now()
);
Expand All @@ -52,3 +59,50 @@ CREATE TABLE webhook_gateway_deliveries (
);
CREATE INDEX webhook_gateway_deliveries_due ON webhook_gateway_deliveries (next_attempt_at) WHERE state = 'queued';
CREATE INDEX webhook_gateway_deliveries_retention ON webhook_gateway_deliveries (completed_at) WHERE state <> 'queued';
CREATE INDEX webhook_gateway_deliveries_pending_order ON webhook_gateway_deliveries (integration_id, received_at, delivery_id) WHERE state = 'queued';

CREATE TABLE webhook_gateway_subscriptions (
id UUID PRIMARY KEY,
integration_id UUID NOT NULL,
channel_id UUID NOT NULL,
filter TEXT CHECK (filter IS NULL OR length(filter) BETWEEN 1 AND 4096),
revision BIGINT NOT NULL CHECK (revision > 0),
deleted_at TIMESTAMPTZ,
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
updated_at TIMESTAMPTZ NOT NULL DEFAULT now()
);
CREATE INDEX webhook_gateway_subscriptions_live ON webhook_gateway_subscriptions (integration_id) WHERE deleted_at IS NULL;
CREATE INDEX webhook_gateway_subscriptions_tombstones ON webhook_gateway_subscriptions (deleted_at) WHERE deleted_at IS NOT NULL;

CREATE TABLE webhook_gateway_messages (
integration_id UUID NOT NULL,
delivery_id TEXT NOT NULL CHECK (length(delivery_id) BETWEEN 1 AND 256),
subscription_id UUID NOT NULL,
channel_id UUID NOT NULL,
content TEXT NOT NULL CHECK (length(content) <= 16384),
state TEXT NOT NULL CHECK (state IN ('queued', 'accepted', 'failed')),
event_id TEXT CHECK (event_id IS NULL OR length(event_id) = 64),
attempts INTEGER NOT NULL DEFAULT 0 CHECK (attempts >= 0),
next_attempt_at TIMESTAMPTZ,
last_error TEXT,
received_at TIMESTAMPTZ NOT NULL,
completed_at TIMESTAMPTZ,
signed_at TIMESTAMPTZ,
group_id TEXT,
group_position BIGINT,
group_key_action TEXT,
PRIMARY KEY (integration_id, delivery_id, subscription_id)
);
CREATE INDEX webhook_gateway_messages_due ON webhook_gateway_messages (next_attempt_at) WHERE state = 'queued';
CREATE INDEX webhook_gateway_messages_subscription ON webhook_gateway_messages (subscription_id) WHERE state = 'queued';
CREATE INDEX webhook_gateway_messages_retention ON webhook_gateway_messages (completed_at) WHERE state <> 'queued';
CREATE INDEX webhook_gateway_messages_group_pending ON webhook_gateway_messages (group_id, group_position) WHERE group_id IS NOT NULL AND state = 'queued';

CREATE TABLE webhook_gateway_message_groups (
id TEXT PRIMARY KEY,
last_position BIGINT NOT NULL,
root_event_id TEXT,
last_signed_at TIMESTAMPTZ,
updated_at TIMESTAMPTZ NOT NULL
);
CREATE INDEX webhook_gateway_message_groups_retention ON webhook_gateway_message_groups (updated_at);

This file was deleted.

37 changes: 0 additions & 37 deletions crates/buzz-webhook-gateway/migrations/0003_subscriptions.sql

This file was deleted.

1 change: 1 addition & 0 deletions crates/buzz-webhook-gateway/schema/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@
\i tables/public/webhook_gateway_deliveries.sql
\i tables/public/webhook_gateway_subscriptions.sql
\i tables/public/webhook_gateway_messages.sql
\i tables/public/webhook_gateway_message_groups.sql
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ CREATE TABLE webhook_gateway_deliveries (
);
CREATE INDEX webhook_gateway_deliveries_due ON webhook_gateway_deliveries (next_attempt_at) WHERE state = 'queued';
CREATE INDEX webhook_gateway_deliveries_retention ON webhook_gateway_deliveries (completed_at) WHERE state <> 'queued';
CREATE INDEX webhook_gateway_deliveries_pending_order ON webhook_gateway_deliveries (integration_id, received_at, delivery_id) WHERE state = 'queued';
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@ CREATE TABLE webhook_gateway_integrations (
id UUID PRIMARY KEY,
community_host TEXT NOT NULL CHECK (community_host = lower(community_host) AND length(community_host) BETWEEN 1 AND 253),
provider TEXT NOT NULL CHECK (provider IN ('github', 'pagerduty', 'sentry', 'incident_io')),
delivery_mode TEXT NOT NULL DEFAULT 'channel_subscriptions' CHECK (delivery_mode IN ('channel_subscriptions', 'caller_selects_channel')),
channel_expr TEXT CHECK (channel_expr IS NULL OR length(channel_expr) BETWEEN 1 AND 4096),
message_expr TEXT CHECK (message_expr IS NULL OR length(message_expr) BETWEEN 1 AND 4096),
message_data_expr TEXT CHECK (message_data_expr IS NULL OR length(message_data_expr) BETWEEN 1 AND 4096),
group_key_expr TEXT CHECK (group_key_expr IS NULL OR length(group_key_expr) BETWEEN 1 AND 4096),
group_key_action TEXT NOT NULL DEFAULT 'reply_in_thread' CHECK (group_key_action IN ('reply_in_thread', 'update_message')),
revision BIGINT NOT NULL CHECK (revision > 0),
enabled BOOLEAN NOT NULL DEFAULT true,
disabled_at TIMESTAMPTZ,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
CREATE TABLE webhook_gateway_message_groups (
id TEXT PRIMARY KEY,
last_position BIGINT NOT NULL,
root_event_id TEXT,
last_signed_at TIMESTAMPTZ,
updated_at TIMESTAMPTZ NOT NULL
);
CREATE INDEX webhook_gateway_message_groups_retention ON webhook_gateway_message_groups (updated_at);
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,12 @@ CREATE TABLE webhook_gateway_messages (
received_at TIMESTAMPTZ NOT NULL,
completed_at TIMESTAMPTZ,
signed_at TIMESTAMPTZ,
group_id TEXT,
group_position BIGINT,
group_key_action TEXT,
PRIMARY KEY (integration_id, delivery_id, subscription_id)
);
CREATE INDEX webhook_gateway_messages_due ON webhook_gateway_messages (next_attempt_at) WHERE state = 'queued';
CREATE INDEX webhook_gateway_messages_subscription ON webhook_gateway_messages (subscription_id) WHERE state = 'queued';
CREATE INDEX webhook_gateway_messages_retention ON webhook_gateway_messages (completed_at) WHERE state <> 'queued';
CREATE INDEX webhook_gateway_messages_group_pending ON webhook_gateway_messages (group_id, group_position) WHERE group_id IS NOT NULL AND state = 'queued';
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ CREATE TABLE webhook_gateway_subscriptions (
integration_id UUID NOT NULL,
channel_id UUID NOT NULL,
filter TEXT CHECK (filter IS NULL OR length(filter) BETWEEN 1 AND 4096),
message_template TEXT CHECK (message_template IS NULL OR length(message_template) BETWEEN 1 AND 4096),
revision BIGINT NOT NULL CHECK (revision > 0),
deleted_at TIMESTAMPTZ,
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
Expand Down
Loading
Loading