diff --git a/lib/pact_broker/pacts/repository.rb b/lib/pact_broker/pacts/repository.rb index bb2ce5172..5cbcb9c45 100644 --- a/lib/pact_broker/pacts/repository.rb +++ b/lib/pact_broker/pacts/repository.rb @@ -130,16 +130,17 @@ def find_latest_pact_versions_for_provider provider_name, tag = nil end end - def find_all_pact_versions_for_provider_with_consumer_version_tags provider_name, consumer_version_tag_name, consumer_name = nil + def find_all_pact_versions_for_provider_with_consumer_version_tags provider_name, selector provider = pacticipant_repository.find_by_name(provider_name) - consumer = consumer_name ? pacticipant_repository.find_by_name(consumer_name) : nil + consumer = selector.consumer ? pacticipant_repository.find_by_name(selector.consumer) : nil + PactPublication .select_all_qualified .select_append(Sequel[:cv][:order].as(:consumer_version_order)) .select_append(Sequel[:ct][:name].as(:consumer_version_tag_name)) .remove_overridden_revisions .join_consumer_versions(:cv) - .join_consumer_version_tags_with_names(consumer_version_tag_name) + .join_consumer_version_tags_with_names(selector.tag) .where(provider: provider) .where_consumer_if_set(consumer) .eager(:consumer) @@ -150,10 +151,8 @@ def find_all_pact_versions_for_provider_with_consumer_version_tags provider_name .group_by(&:pact_version_id) .values .collect do | pact_publications | - selector_tag_names = pact_publications.collect{ | p| p.values.fetch(:consumer_version_tag_name) } latest_pact_publication = pact_publications.sort_by{ |p| p.values.fetch(:consumer_version_order) }.last - selectors = Selectors.create_for_all_of_each_tag(selector_tag_names) - SelectedPact.new(latest_pact_publication.to_domain, selectors) + SelectedPact.new(latest_pact_publication.to_domain, [selector]) end end @@ -461,7 +460,7 @@ def find_pacts_for_which_all_versions_for_the_tag_are_required(provider_name, co selectors = consumer_version_selectors.select(&:all_for_tag?) selectors.flat_map do | selector | - find_all_pact_versions_for_provider_with_consumer_version_tags(provider_name, selector.tag, selector.consumer) + find_all_pact_versions_for_provider_with_consumer_version_tags(provider_name, selector) end end diff --git a/lib/pact_broker/pacts/selector.rb b/lib/pact_broker/pacts/selector.rb index 3ad385edc..4f6ce26c6 100644 --- a/lib/pact_broker/pacts/selector.rb +++ b/lib/pact_broker/pacts/selector.rb @@ -78,6 +78,10 @@ def latest_for_tag? potential_tag = nil end end + def all_for_tag_and_consumer? + !!(tag && !latest? && consumer) + end + def all_for_tag? !!(tag && !latest?) end @@ -95,6 +99,12 @@ def <=> other else latest_for_tag? ? -1 : 1 end + elsif consumer || other.consumer + if consumer == other.consumer + tag <=> other.tag + else + consumer ? -1 : 1 + end else tag <=> other.tag end diff --git a/lib/pact_broker/pacts/verifiable_pact_messages.rb b/lib/pact_broker/pacts/verifiable_pact_messages.rb index a655b6621..92233c609 100644 --- a/lib/pact_broker/pacts/verifiable_pact_messages.rb +++ b/lib/pact_broker/pacts/verifiable_pact_messages.rb @@ -138,7 +138,9 @@ def selector_description selector else "latest pact for a consumer version tagged '#{selector.tag}'" end - elsif selector.tag + elsif selector.all_for_tag_and_consumer? + "pacts for all #{selector.consumer} versions tagged '#{selector.tag}'" + elsif selector.all_for_tag? "pacts for all consumer versions tagged '#{selector.tag}'" else selector.to_json diff --git a/spec/lib/pact_broker/pacts/repository_find_for_verification_spec.rb b/spec/lib/pact_broker/pacts/repository_find_for_verification_spec.rb index abb222d71..4d37f2168 100644 --- a/spec/lib/pact_broker/pacts/repository_find_for_verification_spec.rb +++ b/spec/lib/pact_broker/pacts/repository_find_for_verification_spec.rb @@ -127,6 +127,11 @@ def find_by_consumer_name_and_consumer_version_number(consumer_name, consumer_ve expect(find_by_consumer_version_number("prod-version-2")).to_not be nil expect(find_by_consumer_version_number("prod-version-3")).to be nil end + + it "sets the selectors" do + expect(find_by_consumer_version_number("prod-version-1").selectors.first.tag).to eq 'prod' + expect(find_by_consumer_version_number("prod-version-1").selectors.first.consumer).to eq 'Foo2' + end end context "when a pact version has been selected by two different selectors" do diff --git a/spec/lib/pact_broker/pacts/selector_spec.rb b/spec/lib/pact_broker/pacts/selector_spec.rb index 88794c71c..27f2fdcc3 100644 --- a/spec/lib/pact_broker/pacts/selector_spec.rb +++ b/spec/lib/pact_broker/pacts/selector_spec.rb @@ -8,15 +8,18 @@ module Pacts let(:overall_latest_2) { Selector.overall_latest } let(:latest_for_tag_prod) { Selector.latest_for_tag('prod') } let(:latest_for_tag_dev) { Selector.latest_for_tag('dev') } + let(:all_prod_for_consumer_1) { Selector.all_for_tag_and_consumer('prod', 'Foo') } + let(:all_prod_for_consumer_2) { Selector.all_for_tag_and_consumer('prod', 'Bar') } + let(:all_dev_for_consumer_1) { Selector.all_for_tag_and_consumer('dev', 'Bar') } let(:all_prod) { Selector.all_for_tag('prod') } let(:all_dev) { Selector.all_for_tag('dev') } let(:unsorted_selectors) do - [all_prod, all_dev, latest_for_tag_prod, overall_latest_1, overall_latest_1, latest_for_tag_dev] + [all_prod, all_dev, all_dev_for_consumer_1, latest_for_tag_prod, overall_latest_1, overall_latest_1, latest_for_tag_dev, all_prod_for_consumer_2, all_prod_for_consumer_1] end let(:expected_sorted_selectors) do - [overall_latest_1, overall_latest_1, latest_for_tag_dev, latest_for_tag_prod, all_dev, all_prod] + [overall_latest_1, overall_latest_1, latest_for_tag_dev, latest_for_tag_prod, all_dev_for_consumer_1, all_prod_for_consumer_2, all_prod_for_consumer_1, all_dev, all_prod] end it "sorts the selectors" do diff --git a/spec/lib/pact_broker/pacts/verifiable_pact_messages_spec.rb b/spec/lib/pact_broker/pacts/verifiable_pact_messages_spec.rb index 38e754b5d..26e056592 100644 --- a/spec/lib/pact_broker/pacts/verifiable_pact_messages_spec.rb +++ b/spec/lib/pact_broker/pacts/verifiable_pact_messages_spec.rb @@ -67,6 +67,12 @@ module Pacts its(:inclusion_reason) { is_expected.to include "The pact at http://pact is being verified because it matches the following configured selection criterion: pacts for all consumer versions tagged 'prod'"} end + + context "when the pact is one of all versions for a tag and consumer" do + let(:selectors) { Selectors.new(Selector.all_for_tag_and_consumer('prod', 'Foo')) } + + its(:inclusion_reason) { is_expected.to include "The pact at http://pact is being verified because it matches the following configured selection criterion: pacts for all Foo versions tagged 'prod'"} + end end describe "#pending_reason" do