From 19f7060cf462880c210b878a4e5006783b20d5df Mon Sep 17 00:00:00 2001 From: Beth Skurrie Date: Sun, 17 Jun 2018 14:36:19 +1000 Subject: [PATCH] feat: allow "global" webhooks (matching all consumers and providers) to be triggered by pact or verification publication --- lib/pact_broker/webhooks/repository.rb | 6 ++++-- lib/pact_broker/webhooks/service.rb | 2 +- spec/lib/pact_broker/webhooks/repository_spec.rb | 4 +++- spec/lib/pact_broker/webhooks/service_spec.rb | 4 ++-- spec/support/test_data_builder.rb | 4 ++++ 5 files changed, 14 insertions(+), 6 deletions(-) diff --git a/lib/pact_broker/webhooks/repository.rb b/lib/pact_broker/webhooks/repository.rb index 48206dc52..53842e26e 100644 --- a/lib/pact_broker/webhooks/repository.rb +++ b/lib/pact_broker/webhooks/repository.rb @@ -66,7 +66,8 @@ def find_for_pact pact def find_by_consumer_and_or_provider consumer, provider find_by_consumer_and_provider(consumer, provider) + find_by_consumer_and_provider(nil, provider) + - find_by_consumer_and_provider(consumer, nil) + find_by_consumer_and_provider(consumer, nil) + + find_by_consumer_and_provider(nil, nil) end def find_by_consumer_and_provider consumer, provider @@ -84,7 +85,8 @@ def find_for_pact_and_event_name pact, event_name def find_by_consumer_and_or_provider_and_event_name consumer, provider, event_name find_by_consumer_and_provider_and_event_name(consumer, provider, event_name) + find_by_consumer_and_provider_and_event_name(nil, provider, event_name) + - find_by_consumer_and_provider_and_event_name(consumer, nil, event_name) + find_by_consumer_and_provider_and_event_name(consumer, nil, event_name) + + find_by_consumer_and_provider_and_event_name(nil, nil, event_name) end def find_by_consumer_and_provider_and_event_name consumer, provider, event_name diff --git a/lib/pact_broker/webhooks/service.rb b/lib/pact_broker/webhooks/service.rb index c27512bf1..b5d2e64e3 100644 --- a/lib/pact_broker/webhooks/service.rb +++ b/lib/pact_broker/webhooks/service.rb @@ -94,7 +94,7 @@ def self.find_by_consumer_and_provider consumer, provider end def self.trigger_webhooks pact, verification, event_name - webhooks = webhook_repository.find_by_consumer_and_provider_and_event_name pact.consumer, pact.provider, event_name + webhooks = webhook_repository.find_by_consumer_and_or_provider_and_event_name pact.consumer, pact.provider, event_name if webhooks.any? run_later(webhooks, pact, verification, event_name) diff --git a/spec/lib/pact_broker/webhooks/repository_spec.rb b/spec/lib/pact_broker/webhooks/repository_spec.rb index e56a2949a..59fc8e696 100644 --- a/spec/lib/pact_broker/webhooks/repository_spec.rb +++ b/spec/lib/pact_broker/webhooks/repository_spec.rb @@ -365,12 +365,14 @@ module Webhooks .create_webhook(uuid: '3', events: [{ name: 'something_else_happened' }]) .create_consumer_webhook(uuid: '4', events: [{ name: 'something_happened' }]) .create_provider_webhook(uuid: '5', events: [{ name: 'something_happened' }]) + .create_global_webhook(uuid: '6', events: [{ name: 'something_happened' }]) + .create_global_webhook(uuid: '7', events: [{ name: 'something_else_happened' }]) end subject { Repository.new.find_for_pact_and_event_name(td.pact, 'something_happened') } it "returns an array of webhooks" do - expect(subject.collect(&:uuid).sort).to eq ['1', '2', '4', '5'] + expect(subject.collect(&:uuid).sort).to eq ['1', '2', '4', '5', '6'] end end end diff --git a/spec/lib/pact_broker/webhooks/service_spec.rb b/spec/lib/pact_broker/webhooks/service_spec.rb index 9e1a20c1b..48480e6d2 100644 --- a/spec/lib/pact_broker/webhooks/service_spec.rb +++ b/spec/lib/pact_broker/webhooks/service_spec.rb @@ -39,7 +39,7 @@ module Webhooks let(:triggered_webhook) { instance_double(PactBroker::Webhooks::TriggeredWebhook) } before do - allow_any_instance_of(PactBroker::Webhooks::Repository).to receive(:find_by_consumer_and_provider_and_event_name).and_return(webhooks) + allow_any_instance_of(PactBroker::Webhooks::Repository).to receive(:find_by_consumer_and_or_provider_and_event_name).and_return(webhooks) allow_any_instance_of(PactBroker::Webhooks::Repository).to receive(:create_triggered_webhook).and_return(triggered_webhook) allow(Job).to receive(:perform_in) end @@ -47,7 +47,7 @@ module Webhooks subject { Service.trigger_webhooks pact, verification, PactBroker::Webhooks::WebhookEvent::CONTRACT_CONTENT_CHANGED } it "finds the webhooks" do - expect_any_instance_of(PactBroker::Webhooks::Repository).to receive(:find_by_consumer_and_provider_and_event_name).with(consumer, provider, PactBroker::Webhooks::WebhookEvent::DEFAULT_EVENT_NAME) + expect_any_instance_of(PactBroker::Webhooks::Repository).to receive(:find_by_consumer_and_or_provider_and_event_name).with(consumer, provider, PactBroker::Webhooks::WebhookEvent::DEFAULT_EVENT_NAME) subject end diff --git a/spec/support/test_data_builder.rb b/spec/support/test_data_builder.rb index e9db5862e..300759f28 100644 --- a/spec/support/test_data_builder.rb +++ b/spec/support/test_data_builder.rb @@ -261,6 +261,10 @@ def create_webhook parameters = {} self end + def create_global_webhook parameters = {} + create_webhook(parameters.merge(consumer: nil, provider: nil)) + end + def create_provider_webhook parameters = {} create_webhook(parameters.merge(consumer: nil)) end