Skip to content

Commit

Permalink
feat(verification webhooks): default to contract changed event when n…
Browse files Browse the repository at this point in the history
…o events specified, for backwards compatibility
  • Loading branch information
bethesque committed Nov 20, 2017
1 parent 5d53722 commit 9fe8d47
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 4 deletions.
3 changes: 1 addition & 2 deletions lib/pact_broker/api/contracts/webhook_contract.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -47,7 +47,6 @@ def valid_url?(value)
required(:name).filled
end
end

end
end
end
Expand Down
8 changes: 8 additions & 0 deletions lib/pact_broker/api/decorators/webhook_decorator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions lib/pact_broker/webhooks/webhook_event.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions spec/lib/pact_broker/api/contracts/webhook_contract_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
11 changes: 11 additions & 0 deletions spec/lib/pact_broker/api/decorators/webhook_decorator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 9fe8d47

Please sign in to comment.