Skip to content

Commit

Permalink
fix: correctly trigger contract_content_changed webhooks when first v…
Browse files Browse the repository at this point in the history
…ersion of a pact is published
  • Loading branch information
bethesque committed Jun 9, 2018
1 parent e19c9c9 commit 73a06ff
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
12 changes: 9 additions & 3 deletions lib/pact_broker/pacts/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,10 @@ def find_distinct_pacts_between consumer, options
distinct
end

def pact_has_changed_since_previous_version? pact
# TODO also take into account overridden revisions
def pact_is_new_or_pact_has_changed_since_previous_version? pact
previous_pact = pact_repository.find_previous_pact pact
previous_pact && pact.json_content != previous_pact.json_content
previous_pact.nil? || pact.json_content != previous_pact.json_content
end

private
Expand All @@ -108,6 +109,8 @@ def update_pact params, existing_pact

if existing_pact.json_content != updated_pact.json_content
webhook_service.execute_webhooks updated_pact, nil, PactBroker::Webhooks::WebhookEvent::CONTRACT_CONTENT_CHANGED
else
logger.debug "Pact has not changed since previous revision, not triggering webhooks"
end

updated_pact
Expand All @@ -122,8 +125,11 @@ def create_pact params, version, provider
end

def trigger_webhooks pact
if pact_has_changed_since_previous_version? pact
# TODO add tests for this
if pact_is_new_or_pact_has_changed_since_previous_version?(pact)
webhook_service.execute_webhooks pact, nil, PactBroker::Webhooks::WebhookEvent::CONTRACT_CONTENT_CHANGED
else
logger.debug "Pact has not changed since previous version, not triggering webhooks"
end
end
end
Expand Down
9 changes: 5 additions & 4 deletions spec/lib/pact_broker/pacts/service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@ module Pacts

end

describe "#pact_has_changed_since_previous_version?" do
describe "#pact_is_new_or_pact_has_changed_since_previous_version?" do
let(:json_content) { { 'some' => 'json'}.to_json }
let(:pact) { instance_double(PactBroker::Domain::Pact, json_content: json_content)}

before do
allow_any_instance_of(Pacts::Repository).to receive(:find_previous_pact).and_return(previous_pact)
end

subject { Service.pact_has_changed_since_previous_version? pact }
subject { Service.pact_is_new_or_pact_has_changed_since_previous_version? pact }

context "when a previous pact is found" do
let(:previous_pact) { instance_double(PactBroker::Domain::Pact, json_content: previous_json_content)}
Expand All @@ -60,8 +60,9 @@ module Pacts

context "when a previous pact is not found" do
let(:previous_pact) { nil }
it "returns false" do
expect(subject).to be_falsey

it "returns true" do
expect(subject).to be_truthy
end
end
end
Expand Down

0 comments on commit 73a06ff

Please sign in to comment.