feat(automations): Notion trigger provider

Merged
SA
3mo ago
+1594-3

Summary

Adds the Notion trigger provider (kind: "notion") — the tier-3 "connection + fetch-on-notify" shape.

Events (exactly three, as briefed)

eventsentencefilters
data_source.content_updatedRows changed in [data sources]dataSources (ScopeChip, required)
comment.createdComment added in [data sources] on [Any page] by [Anyone]dataSources · pages (optional, any by default) · actor
comment.mentionedComment mentions [Me] in [data sources] on [Any page]dataSources · pages · mentionedUser (ActorChip anyone/me/people, defaults to me)

comment.mentioned is ours on top of comment.created: the route fetches the comment and reads rich_text for mention items of type: "user"; the matcher tests those ids against mentionedUser (me resolves through user_identities provider notion). Comments only — no page-body diffing.

Pieces

  • packages/shared: notionTriggerConfigSchema (union on event) + describeTriggerProblems rules; automation-matching/notion.ts + one case "notion" in triggerMatches (context.notion carries dataSourceId / pageId / mentionedUserIds / ownerIds).
  • Desktop: providers/notion/{grammar,notion,useNotionOptions} + registry line + hook in useProviderOptions. Two OptionKeys added (dataSources, notionUsers) — Notion user ids are their own id space so the actor chips can't reuse GitHub's people. (The coordinator is landing a consolidated provider-namespaced options shape; I'll rebase onto it when it lands.)
  • tRPC: integration.notion.{getConnection, disconnect, listDataSources, listUsers}; a plain-fetch Notion client (@superset/trpc/integrations/notion, no SDK, no new package, API version 2025-09-03).
  • API: integrations/notion/{connect,callback,webhook}. Callback upserts integration_connections (externalOrgId = workspace id) and a user_identities row for the authorizing member. Webhook: answers the one-time verification_token handshake (logs the token) → verifies X-Notion-Signature (HMAC-SHA256 of the raw body, timing-safe) → fetches the entity (comment → page → parent data source; or the data source) → automation_events keyed on the Notion event id per connection → dispatch. Permanent Notion API errors (400/401/403/404) acknowledge the delivery as skipped; other errors 500 so Notion retries.
  • Web: /integrations/notion page + card (copy of Slack's).

Deviation from the brief (approved by the coordinator): the verification token is a per-integration secret — the subscription lives in the integration's settings, the handshake carries no workspace id, and one token signs deliveries for every workspace — so it is NOTION_WEBHOOK_VERIFICATION_TOKEN (same shape as LINEAR_WEBHOOK_SECRET), not a column on the connection. The three Notion env vars are optional; the routes answer 503 until they're set, so nothing needs to change in the deploy workflows in this PR.

No migration: automation_triggers.kind / integration_provider already carry notion (#6581).

Verification

Live Notion OAuth was not available (no public integration registered yet), so:

  • Editor over CDP (this worktree's renderer, signed in as the org owner): added all three triggers, configured data sources (Product Roadmap) and me, saved, reloaded → rows persisted with kind = notion and rendered with resolved labels. Empty data source scope blocks Save with the shared banner.
  • Webhook: with a local NOTION_WEBHOOK_VERIFICATION_TOKEN and a seeded connection using an internal-integration token for the same workspace, replayed self-signed payloads for a real comment (mentioning a workspace user) and a real data source:
    • handshake {verification_token} → 200; unsigned / bad signature → 401; unknown workspace → no_subscribers.
    • comment.created → automation_events row with the fetched comment (rich_text incl. the mention's user id), page title/url, resource_key = notion:<pageId>; dispatch selected 2/3 triggers (comment.mentioned via me, comment.created via the page's parent data source), not the rows-changed one.
    • data_source.content_updated → row for the data source; dispatch selected 1/3.
    • Redelivery of the same event id → deduped (row count stays 1). Bad entity id → 400 from Notion → delivery acknowledged as skipped.
    • QStash publish itself cannot be proven locally (invalid destination url: endpoint resolves to a loopback address) — the selection log line is the evidence, same as the other providers.
  • bun run typecheck → exit 0. bun run lint → exit 1 only on TerminalAgentAutoResume.tsx:141 (pre-existing on main, being fixed there); nothing in this diff.

To go live

Register the Notion public integration (capabilities: read content, read comments, read user info; redirect URI <API_URL>/api/integrations/notion/callback), set NOTION_CLIENT_ID / NOTION_CLIENT_SECRET, create the webhook subscription pointing at <API_URL>/api/integrations/notion/webhook with comment.created + data_source.content_updated, copy the token from the API log into NOTION_WEBHOOK_VERIFICATION_TOKEN, then add the three vars to the deploy workflows.


Summary by cubic

Adds a Notion trigger provider (kind: "notion") so automations can run on Notion data‑source updates and comments. Previously Notion events were unsupported; now we handle rows changed, comment created, and comments that mention a user, with OAuth connection and a verified webhook.

  • Events and filters: data_source.content_updated, comment.created (filters: data sources · optional pages · actor), and comment.mentioned (derived from comment.created by scanning rich text; filters: data sources · optional pages · mentioned user). Resolves me via user_identities for provider notion.
  • Webhook ingestion: /api/integrations/notion/webhook verifies X-Notion-Signature (HMAC‑SHA256 with NOTION_WEBHOOK_VERIFICATION_TOKEN), handles the one‑time verification handshake (logs full token only while unset; otherwise a fingerprint), applies 15s timeouts and URI‑encodes ids, fetches the referenced entity (comment → page → parent data source, or the data source), dedupes per Notion event id per connection, acknowledges permanent Notion API errors (400/401/403/404) as skipped, and records/dispatches automation events.
  • Matching and schema in @superset/shared: adds notionTriggerConfigSchema, automation-matching/notion.ts, and a case "notion" in triggerMatches.
  • OAuth connection: /api/integrations/notion/{connect,callback} completes public‑integration OAuth (sends Notion‑Version, survives fetch failures), upserts integration_connections (externalOrgId = workspace id) and a user_identities row for the authorizing member; getConnection ignores disconnected rows.
  • tRPC in @superset/trpc: integration.notion.{getConnection, disconnect, listDataSources, listUsers}; lists are bounded (up to 5×100) and return empty on permanent capability errors.
  • UI: Desktop trigger editor adds Notion with sentence grammar and chips; triggers must select at least one data source to save. Web adds /integrations/notion with connect/disconnect and error handling.
  • Env vars (optional until provisioned): NOTION_CLIENT_ID, NOTION_CLIENT_SECRET, NOTION_WEBHOOK_VERIFICATION_TOKEN. Notion routes return 503 until set. No DB migration.

Rollout

  • Register the Notion public integration with capabilities read content, read comments, read user info; set redirect URI to <API_URL>/api/integrations/notion/callback.
  • Create the webhook subscription to <API_URL>/api/integrations/notion/webhook for comment.created and data_source.content_updated. Paste the logged handshake token into NOTION_WEBHOOK_VERIFICATION_TOKEN.
  • Set NOTION_CLIENT_ID, NOTION_CLIENT_SECRET, and NOTION_WEBHOOK_VERIFICATION_TOKEN in deployment.

Summary by CodeRabbit

  • New Features
    • Added Notion integration with secure connection and disconnection controls.
    • View connected workspace details and select Notion data sources and users.
    • Create automations triggered by data-source updates, comments, and mentions.
    • Added optional filters for pages, actors, and mentioned users.
    • Notion webhook events are processed to trigger automations automatically.
  • Improvements
    • Added clear connection and authorization error messages.
    • Added support for retrieving Notion pages, comments, data sources, and workspace users.