Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: ShopifyAPI::Webhooks::Registry doesn't update webhooks if metafield_namespaces has changed #1344

Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
cd5ad77
remove dead code in test
andy-liuu Oct 29, 2024
0d411c5
make the do registration test helper more granular, update how it's c…
andy-liuu Oct 30, 2024
b2f136e
fill out all test scenarios, temp set check to true
andy-liuu Oct 30, 2024
bd368ce
remove unused comments and temporary bypass that made unit tests pass
andy-liuu Oct 30, 2024
fc2103c
the dumb solution
andy-liuu Oct 30, 2024
94e9c93
add no registration done tests for http, pubsub, eventbridge
andy-liuu Oct 30, 2024
53a6741
make parse type untyped and possibly sort arrays when checking for eq…
andy-liuu Oct 30, 2024
30c046e
invert order of attribute arrays for testing equality test
andy-liuu Oct 31, 2024
71ae2f1
changelog
andy-liuu Oct 31, 2024
ce31b6a
make type conversions more explicit, add unit test for edgecase of co…
andy-liuu Oct 31, 2024
b4a0338
remove removing duplicate code
andy-liuu Nov 4, 2024
4171aee
initial unit test refactoring: put everything in a loop
andy-liuu Nov 4, 2024
9c272c0
rename to registry test queries
andy-liuu Nov 4, 2024
b8382a5
pull out adding and registring webhooks into seperate fn
andy-liuu Nov 4, 2024
072e46a
allow more addresses in the loop, remove hardcoding 2 other tests
andy-liuu Nov 4, 2024
059b586
rename constant
andy-liuu Nov 4, 2024
cf49964
make test names shorter for rubocop
andy-liuu Nov 4, 2024
f85a612
bring in http/pubsub/eventbridge registration error into loop
andy-liuu Nov 4, 2024
ee5ba2c
rename test
andy-liuu Nov 4, 2024
c5919e2
refactor away no registration test helper function because it's only …
andy-liuu Nov 4, 2024
1fd2e90
rename test again
andy-liuu Nov 4, 2024
a0c039d
remove only instance of check error test to abstract less
andy-liuu Nov 4, 2024
6dcfdf7
consolidate adding new webhook test, reduces redundancy
andy-liuu Nov 4, 2024
9bb2ff7
helper function to setup tests and expected queries
andy-liuu Nov 5, 2024
783b5c4
totally refactor away do registration test, and use setup queries hel…
andy-liuu Nov 5, 2024
98433b3
rename test
andy-liuu Nov 5, 2024
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
Note: For changes to the API, see https://shopify.dev/changelog?filter=api
## Unreleased

- [#1344](https://github.com/Shopify/shopify-api-ruby/pull/1344) Allow ShopifyAPI::Webhooks::Registry to update a webhook when fields or metafield_namespaces are changed.
- [#1343](https://github.com/Shopify/shopify-api-ruby/pull/1343) Make ShopifyAPI::Context::scope parameter optional. `scope` defaults to empty list `[]`.

## 14.6.0
Expand Down
24 changes: 22 additions & 2 deletions lib/shopify_api/webhooks/registration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,28 @@ def mutation_name(webhook_id); end
sig { abstract.returns(String) }
def build_check_query; end

sig { abstract.params(body: T::Hash[String, T.untyped]).returns(T::Hash[Symbol, String]) }
def parse_check_result(body); end
sig do
params(body: T::Hash[String, T.untyped]).returns({
webhook_id: T.nilable(String),
current_address: T.nilable(String),
fields: T::Array[String],
metafield_namespaces: T::Array[String],
})
end
def parse_check_result(body)
andy-liuu marked this conversation as resolved.
Show resolved Hide resolved
edges = body.dig("data", "webhookSubscriptions", "edges") || {}
webhook_id = nil
fields = []
metafield_namespaces = []
unless edges.empty?
node = edges[0]["node"]
webhook_id = node["id"].to_s
fields = node["includeFields"] || []
metafield_namespaces = node["metafieldNamespaces"] || []
end
{ webhook_id: webhook_id, current_address: nil, fields: fields,
metafield_namespaces: metafield_namespaces, }
end

sig { params(webhook_id: T.nilable(String)).returns(String) }
def build_register_query(webhook_id: nil)
Expand Down
19 changes: 13 additions & 6 deletions lib/shopify_api/webhooks/registrations/event_bridge.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ def build_check_query
edges {
node {
id
includeFields
metafieldNamespaces
endpoint {
__typename
... on WebhookEventBridgeEndpoint {
Expand All @@ -43,17 +45,22 @@ def build_check_query
QUERY
end

sig { override.params(body: T::Hash[String, T.untyped]).returns(T::Hash[Symbol, String]) }
sig do
params(body: T::Hash[String, T.untyped]).returns({
webhook_id: T.nilable(String),
current_address: T.nilable(String),
fields: T::Array[String],
metafield_namespaces: T::Array[String],
})
end
def parse_check_result(body)
parse_results = super(body)
edges = body.dig("data", "webhookSubscriptions", "edges") || {}
webhook_id = nil
current_address = nil
unless edges.empty?
node = edges[0]["node"]
webhook_id = node["id"].to_s
current_address = node["endpoint"]["arn"].to_s
parse_results[:current_address] = node["endpoint"]["arn"].to_s
end
{ webhook_id: webhook_id, current_address: current_address }
parse_results
end
end
end
Expand Down
19 changes: 13 additions & 6 deletions lib/shopify_api/webhooks/registrations/http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ def build_check_query
edges {
node {
id
includeFields
metafieldNamespaces
endpoint {
__typename
... on WebhookHttpEndpoint {
Expand All @@ -49,22 +51,27 @@ def build_check_query
QUERY
end

sig { override.params(body: T::Hash[String, T.untyped]).returns(T::Hash[Symbol, String]) }
sig do
params(body: T::Hash[String, T.untyped]).returns({
webhook_id: T.nilable(String),
current_address: T.nilable(String),
fields: T::Array[String],
metafield_namespaces: T::Array[String],
})
end
def parse_check_result(body)
parse_results = super(body)
edges = body.dig("data", "webhookSubscriptions", "edges") || {}
webhook_id = nil
current_address = nil
unless edges.empty?
node = edges[0]["node"]
webhook_id = node["id"].to_s
current_address =
parse_results[:current_address] =
if node.key?("endpoint")
node["endpoint"]["callbackUrl"].to_s
else
node["callbackUrl"].to_s
end
end
{ webhook_id: webhook_id, current_address: current_address }
parse_results
end
end
end
Expand Down
20 changes: 14 additions & 6 deletions lib/shopify_api/webhooks/registrations/pub_sub.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ def build_check_query
edges {
node {
id
includeFields
metafieldNamespaces
endpoint {
__typename
... on WebhookPubSubEndpoint {
Expand All @@ -48,17 +50,23 @@ def build_check_query
QUERY
end

sig { override.params(body: T::Hash[String, T.untyped]).returns(T::Hash[Symbol, String]) }
sig do
params(body: T::Hash[String, T.untyped]).returns({
webhook_id: T.nilable(String),
current_address: T.nilable(String),
fields: T::Array[String],
metafield_namespaces: T::Array[String],
})
end
def parse_check_result(body)
parse_results = super(body)
edges = body.dig("data", "webhookSubscriptions", "edges") || {}
webhook_id = nil
current_address = nil
unless edges.empty?
node = edges[0]["node"]
webhook_id = node["id"].to_s
current_address = "pubsub://#{node["endpoint"]["pubSubProject"]}:#{node["endpoint"]["pubSubTopic"]}"
parse_results[:current_address] =
"pubsub://#{node["endpoint"]["pubSubProject"]}:#{node["endpoint"]["pubSubTopic"]}"
end
{ webhook_id: webhook_id, current_address: current_address }
parse_results
end
end
end
Expand Down
8 changes: 7 additions & 1 deletion lib/shopify_api/webhooks/registry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -219,8 +219,14 @@ def webhook_registration_needed?(client, registration)
check_response = client.query(query: registration.build_check_query, response_as_struct: false)
raise Errors::WebhookRegistrationError,
"Failed to check if webhook was already registered" unless check_response.ok?

parsed_check_result = registration.parse_check_result(T.cast(check_response.body, T::Hash[String, T.untyped]))
must_register = parsed_check_result[:current_address] != registration.callback_address
registration_fields = registration.fields || []
registration_metafield_namespaces = registration.metafield_namespaces || []

must_register = parsed_check_result[:current_address] != registration.callback_address ||
parsed_check_result[:fields].sort != registration_fields.sort ||
parsed_check_result[:metafield_namespaces].sort != registration_metafield_namespaces.sort

{ webhook_id: parsed_check_result[:webhook_id], must_register: must_register }
end
Expand Down
Loading
Loading