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