From 9040bf479043a1b74878199d1e3aebef3468ebe7 Mon Sep 17 00:00:00 2001 From: Kirill Platonov Date: Fri, 6 Jan 2023 13:54:29 +0300 Subject: [PATCH] Fix registration of event_bridge and pub_sub webhooks --- CHANGELOG.md | 2 ++ docs/shopify_app/webhooks.md | 36 +++++++++++++++++++- lib/shopify_app/managers/webhooks_manager.rb | 5 +-- 3 files changed, 40 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f2b1df019..c019068d4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,8 @@ Unreleased ---------- +* Fix registration of event_bridge and pub_sub webhooks [#1635](https://github.com/Shopify/shopify_app/pull/1635) + 21.4.0 (Jan 5, 2023) ---------- * Updated shopify_api to 12.4.0 [#1633](https://github.com/Shopify/shopify_app/pull/1633) 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