diff --git a/lib/pact_broker/matrix/repository.rb b/lib/pact_broker/matrix/repository.rb index 49f5905cd..1bee4b10f 100644 --- a/lib/pact_broker/matrix/repository.rb +++ b/lib/pact_broker/matrix/repository.rb @@ -60,6 +60,7 @@ def find_all selectors def look_up_versions_for_tags(selectors) selectors.collect do | selector | # resource validation currently stops tag being specified without latest=true + if selector[:tag] && selector[:latest] version = version_repository.find_by_pacticpant_name_and_latest_tag(selector[:pacticipant_name], selector[:tag]) # validation in resource should ensure we always have a version @@ -67,6 +68,12 @@ def look_up_versions_for_tags(selectors) pacticipant_name: selector[:pacticipant_name], pacticipant_version_number: version.number } + elsif selector[:latest] + version = version_repository.find_latest_by_pacticpant_name(selector[:pacticipant_name]) + { + pacticipant_name: selector[:pacticipant_name], + pacticipant_version_number: version.number + } else selector end diff --git a/lib/pact_broker/versions/repository.rb b/lib/pact_broker/versions/repository.rb index e4312b4fb..2679dd290 100644 --- a/lib/pact_broker/versions/repository.rb +++ b/lib/pact_broker/versions/repository.rb @@ -23,6 +23,15 @@ def find_by_pacticpant_name_and_latest_tag pacticipant_name, tag .first end + def find_latest_by_pacticpant_name pacticipant_name + PactBroker::Domain::Version + .select_all_qualified + .join(:pacticipants, {id: :pacticipant_id}, {implicit_qualifier: :versions}) + .where(name_like(Sequel[:pacticipants][:name], pacticipant_name)) + .reverse_order(:order) + .first + end + def find_by_pacticipant_name_and_number pacticipant_name, number PactBroker::Domain::Version .select(Sequel[:versions][:id], Sequel[:versions][:number], Sequel[:versions][:pacticipant_id], Sequel[:versions][:order], Sequel[:versions][:created_at], Sequel[:versions][:updated_at]) diff --git a/spec/lib/pact_broker/matrix/repository_spec.rb b/spec/lib/pact_broker/matrix/repository_spec.rb index 051003444..875b9de07 100644 --- a/spec/lib/pact_broker/matrix/repository_spec.rb +++ b/spec/lib/pact_broker/matrix/repository_spec.rb @@ -246,6 +246,52 @@ def build_selectors(hash) expect(subject.size).to eq 1 end end + + context "when the latest version is specified for a provider without a tag" do + before do + td.create_pact_with_hierarchy("A", "1.2.3", "B") + .create_verification(provider_version: "1.0.0") + .use_provider_version("1.0.0") + .create_verification(provider_version: "2.0.0", number: 2) + .use_provider_version("2.0.0") + .create_verification(provider_version: "3.0.0", number: 3) + end + + let(:selectors) do + [ + { pacticipant_name: "A", pacticipant_version_number: "1.2.3" }, + { pacticipant_name: "B", latest: true } + ] + end + + subject { Repository.new.find(selectors) } + + it "returns the row for the version " do + expect(subject.first).to include provider_version_number: "3.0.0" + expect(subject.size).to eq 1 + end + end + + context "when the latest version is specified for a provider without a tag but the latest known version for a provider does not have a verification" do + before do + td.create_pact_with_hierarchy("A", "1.2.3", "B") + .create_verification(provider_version: "1.0.0") + .create_provider_version("5.0.0") + end + + let(:selectors) do + [ + { pacticipant_name: "A", pacticipant_version_number: "1.2.3" }, + { pacticipant_name: "B", latest: true } + ] + end + + subject { Repository.new.find(selectors) } + + it "returns no data - this may be confusing. Might need to re-think this logic." do + expect(subject.size).to eq 0 + end + end end describe "#find_for_consumer_and_provider" do