From c02c8f90a5ca5081a40bfbfae9930022cf0b57b0 Mon Sep 17 00:00:00 2001 From: Kirill Platonov Date: Wed, 22 Mar 2023 15:57:54 +0400 Subject: [PATCH] Fix registration of event_bridge and pub_sub webhooks --- CHANGELOG.md | 1 + docs/shopify_app/webhooks.md | 36 +++++++++++++++++++- lib/shopify_app/managers/webhooks_manager.rb | 5 +-- 3 files changed, 39 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 70876e7f8..8d2a0c707 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ Unreleased * Support Unified Admin [#1658](https://github.com/Shopify/shopify_app/pull/1658) * Set `access_scopes` column to string by default [#1636](https://github.com/Shopify/shopify_app/pull/1636) +* Fix registration of event_bridge and pub_sub webhooks [#1635](https://github.com/Shopify/shopify_app/pull/1635) 21.4.1 (Feb 21, 2023) ---------- diff --git a/docs/shopify_app/webhooks.md b/docs/shopify_app/webhooks.md index ae5723bcd..f7e9c3d20 100644 --- a/docs/shopify_app/webhooks.md +++ b/docs/shopify_app/webhooks.md @@ -82,4 +82,38 @@ We have three mandatory GDPR webhooks The `generate shopify_app` command generated three job templates corresponding to all three of these webhooks. To pass our approval process you will need to set these webhooks in your partner dashboard. -You can read more about that [here](https://shopify.dev/apps/webhooks/configuration/mandatory-webhooks). \ No newline at end of file +You can read more about that [here](https://shopify.dev/apps/webhooks/configuration/mandatory-webhooks). + +## EventBridge and PubSub Webhooks + +You can also register webhooks for delivery to Amazon EventBridge or Google Cloud Pub/Sub. In this case the `path` argument to needs to be of a specific form. + +For EventBridge, the `path` must be the ARN of the partner event source. + +```rb +ShopifyApp.configure do |config| + config.webhooks = [ + { + delivery_method: :event_bridge, + topic: 'carts/update', + path: 'arn:aws:events....' + } + ] +end +``` + +For Pub/Sub, the `path` must be of the form `pubsub://[PROJECT-ID]:[PUB-SUB-TOPIC-ID]`. For example, if you created a topic with id `red` in the project `blue`, then the value of path would be `pubsub://blue:red`. + +```rb +ShopifyApp.configure do |config| + config.webhooks = [ + { + delivery_method: :pub_sub, + topic: 'carts/update', + path: 'pubsub://project-id:pub-sub-topic-id' + } + ] +end +``` + +When registering for an EventBridge or PubSub Webhook you'll need to implement a handler that will fetch webhooks from the queue and process them yourself. diff --git a/lib/shopify_app/managers/webhooks_manager.rb b/lib/shopify_app/managers/webhooks_manager.rb index 777811598..7bc5b2c24 100644 --- a/lib/shopify_app/managers/webhooks_manager.rb +++ b/lib/shopify_app/managers/webhooks_manager.rb @@ -45,12 +45,13 @@ def add_registrations ShopifyApp::Logger.debug("Adding registrations to webhooks") ShopifyApp.configuration.webhooks.each do |attributes| webhook_path = path(attributes) + delivery_method = attributes[:delivery_method] || :http ShopifyAPI::Webhooks::Registry.add_registration( topic: attributes[:topic], - delivery_method: attributes[:delivery_method] || :http, + delivery_method: delivery_method, path: webhook_path, - handler: webhook_job_klass(webhook_path), + handler: delivery_method == :http ? webhook_job_klass(webhook_path) : nil, fields: attributes[:fields], ) end