Skip to content

Commit 9fe8d47

Browse files
committed
feat(verification webhooks): default to contract changed event when no events specified, for backwards compatibility
1 parent 5d53722 commit 9fe8d47

File tree

5 files changed

+24
-4
lines changed

5 files changed

+24
-4
lines changed

lib/pact_broker/api/contracts/webhook_contract.rb

+1-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class WebhookContract < Reform::Form
1212
end
1313

1414
required(:request).filled
15-
required(:events).maybe(min_size?: 1)
15+
optional(:events).maybe(min_size?: 1)
1616
end
1717

1818
property :request do
@@ -47,7 +47,6 @@ def valid_url?(value)
4747
required(:name).filled
4848
end
4949
end
50-
5150
end
5251
end
5352
end

lib/pact_broker/api/decorators/webhook_decorator.rb

+8
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,14 @@ class WebhookEventDecorator < BaseDecorator
6666
href: webhooks_url(options[:base_url])
6767
}
6868
end
69+
70+
def from_json represented
71+
super.tap do | webhook |
72+
if webhook.events == nil
73+
webhook.events = [PactBroker::Webhooks::WebhookEvent.new(name: PactBroker::Webhooks::WebhookEvent::DEFAULT_EVENT_NAME)]
74+
end
75+
end
76+
end
6977
end
7078
end
7179
end

lib/pact_broker/webhooks/webhook_event.rb

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ module PactBroker
55
module Webhooks
66
class WebhookEvent < Sequel::Model
77

8+
DEFAULT_EVENT_NAME = 'contract_changed'
9+
810
dataset_module do
911
include PactBroker::Repositories::Helpers
1012
end

spec/lib/pact_broker/api/contracts/webhook_contract_spec.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ def valid_webhook_with
4040
context "with no events defined" do
4141
let(:json) { {}.to_json }
4242

43-
it "contains an error for missing request, I wish I could work out how not to have the second error" do
44-
expect(subject.errors[:events]).to eq ["is missing", "size cannot be less than 1"]
43+
it "does not contain an error for missing event, as it will be defaulted" do
44+
expect(subject.errors.messages[:events]).to be nil
4545
end
4646
end
4747

spec/lib/pact_broker/api/decorators/webhook_decorator_spec.rb

+11
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,19 @@ module Decorators
114114
end
115115

116116
it 'parses the events' do
117+
expect(parsed_object.events.size).to eq 1
117118
expect(parsed_object.events.first.name).to eq 'something_happened'
118119
end
120+
121+
context "when no events are specified" do
122+
let(:hash) { { request: request } }
123+
let(:webhook) { Domain::Webhook.new }
124+
125+
it "defaults to a single contract_changed event for backwards compatibility" do
126+
expect(parsed_object.events.size).to eq 1
127+
expect(parsed_object.events.first.name).to eq 'contract_changed'
128+
end
129+
end
119130
end
120131
end
121132
end

0 commit comments

Comments
 (0)