Skip to content

Commit

Permalink
fix: deduplicate wip pacts by content
Browse files Browse the repository at this point in the history
Fixes: #374
  • Loading branch information
bethesque committed Jan 27, 2021
1 parent 4fc6919 commit 0af9077
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 1 deletion.
11 changes: 10 additions & 1 deletion lib/pact_broker/pacts/repository.rb
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ def find_wip_pact_versions_for_provider provider_name, provider_tags_names = [],

provider_version_count = scope_for(PactBroker::Domain::Version).where(pacticipant: provider).count

wip_pacts.collect do | pact|
verifiable_pacts = wip_pacts.collect do | pact|
pending_tag_names = find_provider_tags_for_which_pact_publication_id_is_pending(pact, successfully_verified_head_pact_publication_ids_for_each_provider_tag)
pre_existing_tag_names = find_provider_tag_names_that_were_first_used_before_pact_published(pact, provider_tag_collection)

Expand All @@ -229,6 +229,8 @@ def find_wip_pact_versions_for_provider provider_name, provider_tags_names = [],
VerifiablePact.new(pact.to_domain, selectors, true, pre_existing_pending_tags, [], true)
end
end.compact.sort

deduplicate_verifiable_pacts(verifiable_pacts)
end

def find_pact_versions_for_provider provider_name, tag = nil
Expand Down Expand Up @@ -595,6 +597,13 @@ def find_successfully_verified_head_pacts_by_provider_tag(provider_id, provider_
hash[provider_tag] = head_pacts
end
end

def deduplicate_verifiable_pacts(verifiable_pacts)
verifiable_pacts
.group_by { | verifiable_pact | verifiable_pact.pact_version_sha }
.values
.collect { | verifiable_pacts | verifiable_pacts.reduce(&:+) }
end
end
end
end
22 changes: 22 additions & 0 deletions lib/pact_broker/pacts/verifiable_pact.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'delegate'
require 'pact_broker/error'

module PactBroker
module Pacts
Expand All @@ -23,13 +24,34 @@ def wip?
wip
end

def + other
if pact_version_sha != other.pact_version_sha
raise PactBroker::Error.new("Can't merge two verifiable pacts with different pact content")
end

latest_pact = [self, other].sort_by(&:consumer_version_order).last.__getobj__()

VerifiablePact.new(
latest_pact,
selectors + other.selectors,
pending || other.pending,
pending_provider_tags + other.pending_provider_tags,
non_pending_provider_tags + other.non_pending_provider_tags,
wip || other.wip
)
end

def <=> other
if self.consumer_name != other.consumer_name
return self.consumer_name <=> other.consumer_name
else
return self.consumer_version.order <=> other.consumer_version.order
end
end

def consumer_version_order
__getobj__().consumer_version.order
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,32 @@ module Pacts
end
end

context "when there are multiple wip pacts with the same content" do
before do
td.create_provider("bar")
.create_provider_version("333")
.create_provider_version_tag("dev")
.add_day
.create_pact_with_hierarchy("foo", "1", "bar", json_content)
.create_consumer_version_tag("feat-1")
.add_day
.create_pact_with_hierarchy("meep", "2", "bar", json_content)
.create_consumer_version_tag("feat-2")
end

let(:json_content) { { "interactions" => ["foo"] }.to_json }
let(:provider_tags) { %w[dev] }

it "de-duplicates them" do
expect(subject.size).to eq 1
end

it "merges the selectors" do
expect(subject.first.selectors.size).to eq 2
expect(subject.first.wip).to be true
end
end

context "when the latest pact for a tag has been successfully verified by the given provider tag" do
before do
td.create_pact_with_hierarchy("foo", "1", "bar")
Expand Down

0 comments on commit 0af9077

Please sign in to comment.