# Trunk Webhooks

Trunk ([trunk.io](https://trunk.io/)) is CI developer tooling: **Merge Queue** emits `pull_request.*`
and `pull_request_batch.*` lifecycle events, and **Flaky Tests** emits `test_case.*` status, monitor,
quarantine and AI-investigation events. Webhooks are delivered by Svix and
signed per Standard Webhooks with the `svix-*` header names:
`svix-id`, `svix-timestamp`, and `svix-signature`. The JSON body is flat: a top-level `type` and
`timestamp` plus the resource fields (Merge Queue: `action`, `status`, `pr_number`, `repository`;
Flaky Tests: `test_case`, `repository`, `monitor` / `triggered_by`). Collection events add
`test_collection`. The body has no event id (use `svix-id`).

## When to Use This Skill

* How do I receive Trunk webhooks?
* How do I verify Trunk webhook signatures (`svix-signature`)?
* How do I handle `pull_request.merged`, `pull_request.failed` or `pull_request_batch.pending_failure` events?
* How do I react to `v2.test_case.status_changed` when a test becomes flaky?
* Why is my Trunk webhook signature verification failing, or rejecting Hookdeck retries?

## Verification (core)

Signed content is `{svix-id}.{svix-timestamp}.{raw body}`. Svix signs it with HMAC-SHA256 using
the base64-decoded secret (the part after `whsec_`), base64-encodes the result, and sends one or
more space-separated `v1,<signature>` entries. Use the official `svix` library, which compares in
constant time. Read the `svix-*` headers, and when `svix-id` is absent fall back to the `webhook-*`
headers (`webhook-id`, `webhook-timestamp`, `webhook-signature`).

```javascript
import { Webhook } from 'svix'; // svix 2.x is ESM-only

const prefix = req.headers['svix-id'] ? 'svix' : 'webhook';
const id = req.headers[`${prefix}-id`];
const timestamp = req.headers[`${prefix}-timestamp`];
const signature = req.headers[`${prefix}-signature`];
// rawBody: the raw Buffer/string, never re-serialized JSON
new Webhook(process.env.TRUNK_WEBHOOK_SECRET).verify(rawBody, {
  'svix-id': id, 'svix-timestamp': timestamp, 'svix-signature': signature,
  'webhook-id': id, 'webhook-timestamp': timestamp, 'webhook-signature': signature,
}); // throws on a bad signature or a timestamp outside the window
const event = JSON.parse(rawBody.toString('utf8')); // parse only after verify passes

```

```python
from svix.webhooks import Webhook, WebhookVerificationError

wh = Webhook(os.environ["TRUNK_WEBHOOK_SECRET"])  # "whsec_..."
wh.verify(raw_body, {"svix-id": id, "svix-timestamp": ts, "svix-signature": sig,
                     "webhook-id": id, "webhook-timestamp": ts, "webhook-signature": sig})

```

> **For complete handlers with tests**, see [examples/express/](https://github.com/hookdeck/webhook-skills/tree/main/skills/trunk-webhooks/examples/express/), [examples/nextjs/](https://github.com/hookdeck/webhook-skills/tree/main/skills/trunk-webhooks/examples/nextjs/), [examples/fastapi/](https://github.com/hookdeck/webhook-skills/tree/main/skills/trunk-webhooks/examples/fastapi/).

Each endpoint has one signing secret, shown in the Svix App Portal embedded in the Trunk web app.
Trunk doesn't document its format, but Svix secrets look like `whsec_...`. Keep the full value in
`TRUNK_WEBHOOK_SECRET`. Handlers fail closed: with the variable unset (or not a `whsec_` value) they
return 500 instead of accepting anything. The `svix` library used here reads either the `svix-*` or
the `webhook-*` header set, so no mapping is needed beyond the fallback shown.

### Hookdeck retries and replays hit the 5-minute window

The `svix` and `standardwebhooks` libraries always enforce a fixed 5-minute timestamp window, and
it can't be configured. Hookdeck retries and replays resend the **original** `svix-timestamp`, so
the library rejects any retry or replay that happens more than 5 minutes after the original
delivery. If you verify behind Hookdeck (or replay old events), use the manual verifier in
[references/verification.md](https://github.com/hookdeck/webhook-skills/blob/main/skills/trunk-webhooks/references/verification.md), where the window can be dropped or
widened. Deduplicate on `svix-id` instead of relying on the timestamp.

## Common Event Types

20 events are documented (the list may not be exhaustive). The name is the top-level `type` in the
JSON body, and the examples handle all of them:

| Event | Triggered When |
| --- | --- |
| `pull_request.queued` | A PR is added to the Merge Queue (`queued_reason` can be `BISECTION_REQUIRED` or `BISECTION_TEST_RUN_PASSED`) |
| `pull_request.submitted` / `.testing` | A PR is submitted / Trunk starts testing it, alone or in a batch |
| `pull_request.merged` | A PR merged after a clean run |
| `pull_request.failed` | A PR was kicked out of the queue |
| `pull_request.pending_failure` / `.canceled` | A PR has a pending failure / was canceled |
| `pull_request_batch.queued` / `.submitted` / `.testing` / `.merged` / `.failed` / `.canceled` / `.pending_failure` | Same lifecycle for a batch of PRs tested together. Subscribe to this family separately |
| `v2.test_case.status_changed` | A test changes status (for example becomes flaky), triggered by a monitor |
| `test_case.status_changed` | The original event, being phased out in favor of the v2 event |
| `test_case.monitor_status_changed` | A monitor activates or resolves for a test case |
| `test_case.quarantining_setting_changed` / `v2.test_case.quarantining_setting_changed` | A quarantine setting is manually overridden to `ALWAYS_QUARANTINE` or `NEVER_QUARANTINE` (not on auto-quarantine) |
| `test_case.investigation_completed` | An AI flaky-test analysis finishes (beta) |

See [references/overview.md](https://github.com/hookdeck/webhook-skills/blob/main/skills/trunk-webhooks/references/overview.md) for payload shapes and delivery notes.

## Environment Variables

```bash
# Signing secret of the webhook endpoint (Svix format, starts with whsec_)
TRUNK_WEBHOOK_SECRET=whsec_xxxxx

```

## Local Development

```bash
# Start tunnel (no account needed)
npx hookdeck-cli listen 3000 trunk --path /webhooks/trunk

```

Register the tunnel URL as an endpoint in the Svix App Portal inside the Trunk web app (Merge Queue >
Settings > Webhooks, or Settings > Organization > Webhooks), then send a test event from the portal
or trigger a real PR / flaky test event.

## Reference Materials

* [references/overview.md](https://github.com/hookdeck/webhook-skills/blob/main/skills/trunk-webhooks/references/overview.md) - Trunk webhook concepts, events, payload shapes, delivery
* [references/setup.md](https://github.com/hookdeck/webhook-skills/blob/main/skills/trunk-webhooks/references/setup.md) - Register an endpoint in the Svix App Portal, get the secret, subscribe to both Merge Queue families
* [references/verification.md](https://github.com/hookdeck/webhook-skills/blob/main/skills/trunk-webhooks/references/verification.md) - Svix verification details, header fallback, manual verifier, timestamp window