From 9fe8d479cc06488bde1620f13b489fc049efe78b Mon Sep 17 00:00:00 2001 From: Beth Skurrie Date: Tue, 21 Nov 2017 08:27:15 +1100 Subject: [PATCH] feat(verification webhooks): default to contract changed event when no events specified, for backwards compatibility --- lib/pact_broker/api/contracts/webhook_contract.rb | 3 +-- lib/pact_broker/api/decorators/webhook_decorator.rb | 8 ++++++++ lib/pact_broker/webhooks/webhook_event.rb | 2 ++ .../api/contracts/webhook_contract_spec.rb | 4 ++-- .../api/decorators/webhook_decorator_spec.rb | 11 +++++++++++ 5 files changed, 24 insertions(+), 4 deletions(-) diff --git a/lib/pact_broker/api/contracts/webhook_contract.rb b/lib/pact_broker/api/contracts/webhook_contract.rb index 0d9dfabf8..549c28bfa 100644 --- a/lib/pact_broker/api/contracts/webhook_contract.rb +++ b/lib/pact_broker/api/contracts/webhook_contract.rb @@ -12,7 +12,7 @@ class WebhookContract < Reform::Form end required(:request).filled - required(:events).maybe(min_size?: 1) + optional(:events).maybe(min_size?: 1) end property :request do @@ -47,7 +47,6 @@ def valid_url?(value) required(:name).filled end end - end end end diff --git a/lib/pact_broker/api/decorators/webhook_decorator.rb b/lib/pact_broker/api/decorators/webhook_decorator.rb index 151acdc92..09833826c 100644 --- a/lib/pact_broker/api/decorators/webhook_decorator.rb +++ b/lib/pact_broker/api/decorators/webhook_decorator.rb @@ -66,6 +66,14 @@ class WebhookEventDecorator < BaseDecorator href: webhooks_url(options[:base_url]) } end + + def from_json represented + super.tap do | webhook | + if webhook.events == nil + webhook.events = [PactBroker::Webhooks::WebhookEvent.new(name: PactBroker::Webhooks::WebhookEvent::DEFAULT_EVENT_NAME)] + end + end + end end end end diff --git a/lib/pact_broker/webhooks/webhook_event.rb b/lib/pact_broker/webhooks/webhook_event.rb index ebfecfa54..4b473c3a7 100644 --- a/lib/pact_broker/webhooks/webhook_event.rb +++ b/lib/pact_broker/webhooks/webhook_event.rb @@ -5,6 +5,8 @@ module PactBroker module Webhooks class WebhookEvent < Sequel::Model + DEFAULT_EVENT_NAME = 'contract_changed' + dataset_module do include PactBroker::Repositories::Helpers end diff --git a/spec/lib/pact_broker/api/contracts/webhook_contract_spec.rb b/spec/lib/pact_broker/api/contracts/webhook_contract_spec.rb index 87b934ad9..39fe4a278 100644 --- a/spec/lib/pact_broker/api/contracts/webhook_contract_spec.rb +++ b/spec/lib/pact_broker/api/contracts/webhook_contract_spec.rb @@ -40,8 +40,8 @@ def valid_webhook_with context "with no events defined" do let(:json) { {}.to_json } - it "contains an error for missing request, I wish I could work out how not to have the second error" do - expect(subject.errors[:events]).to eq ["is missing", "size cannot be less than 1"] + it "does not contain an error for missing event, as it will be defaulted" do + expect(subject.errors.messages[:events]).to be nil end end diff --git a/spec/lib/pact_broker/api/decorators/webhook_decorator_spec.rb b/spec/lib/pact_broker/api/decorators/webhook_decorator_spec.rb index 4855e168a..6f0aa317c 100644 --- a/spec/lib/pact_broker/api/decorators/webhook_decorator_spec.rb +++ b/spec/lib/pact_broker/api/decorators/webhook_decorator_spec.rb @@ -114,8 +114,19 @@ module Decorators end it 'parses the events' do + expect(parsed_object.events.size).to eq 1 expect(parsed_object.events.first.name).to eq 'something_happened' end + + context "when no events are specified" do + let(:hash) { { request: request } } + let(:webhook) { Domain::Webhook.new } + + it "defaults to a single contract_changed event for backwards compatibility" do + expect(parsed_object.events.size).to eq 1 + expect(parsed_object.events.first.name).to eq 'contract_changed' + end + end end end end