Skip to content

Commit

Permalink
feat: allow "global" webhooks (matching all consumers and providers) …
Browse files Browse the repository at this point in the history
…to be triggered by pact or verification publication
  • Loading branch information
bethesque committed Jun 17, 2018
1 parent 87a8416 commit 19f7060
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 6 deletions.
6 changes: 4 additions & 2 deletions lib/pact_broker/webhooks/repository.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/pact_broker/webhooks/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion spec/lib/pact_broker/webhooks/repository_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions spec/lib/pact_broker/webhooks/service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,15 @@ 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

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

Expand Down
4 changes: 4 additions & 0 deletions spec/support/test_data_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 19f7060

Please sign in to comment.