diff --git a/lib/pact_broker/matrix/repository.rb b/lib/pact_broker/matrix/repository.rb index fe836eb8b..bdecfc43b 100644 --- a/lib/pact_broker/matrix/repository.rb +++ b/lib/pact_broker/matrix/repository.rb @@ -60,7 +60,7 @@ def find_all selectors def look_up_versions_for_tags(selectors) selectors.collect do | selector | if selector[:latest_tag] - version = version_repository.find_by_latest_tag(selector[:pacticipant_name], selector[:latest_tag]) + version = version_repository.find_by_pacticpant_name_and_latest_tag(selector[:pacticipant_name], selector[:latest_tag]) # validation in resource should ensure we always have a version { pacticipant_name: selector[:pacticipant_name], diff --git a/lib/pact_broker/matrix/service.rb b/lib/pact_broker/matrix/service.rb index 6f03c32fa..dcf0dc339 100644 --- a/lib/pact_broker/matrix/service.rb +++ b/lib/pact_broker/matrix/service.rb @@ -23,25 +23,32 @@ def find_compatible_pacticipant_versions criteria def validate_selectors selectors error_messages = [] - selectors.each do | selector | - if selector[:pacticipant_name].nil? && selector[:pacticipant_version_number].nil? + selectors.each do | s | + if s[:pacticipant_name].nil? && s[:pacticipant_version_number].nil? error_messages << "Please specify the pacticipant name and version" - elsif selector[:pacticipant_name].nil? + elsif s[:pacticipant_name].nil? error_messages << "Please specify the pacticipant name" + else + if s.key?(:pacticipant_version_number) && s.key?(:latest_tag) + error_messages << "A version and a latest tag cannot both be specified for #{s[:pacticipant_name]}" + end end end selectors.collect{ |selector| selector[:pacticipant_name] }.compact.each do | pacticipant_name | unless pacticipant_service.find_pacticipant_by_name(pacticipant_name) - error_messages << "Pacticipant '#{pacticipant_name}' not found" + error_messages << "Pacticipant #{pacticipant_name} not found" end end if error_messages.empty? - selectors.each do | selector | - if selector[:pacticipant_version_number] - version = version_service.find_by_pacticipant_name_and_number(pacticipant_name: selector[:pacticipant_name], pacticipant_version_number: selector[:pacticipant_version_number]) - error_messages << "No pact or verification found for #{selector[:pacticipant_name]} version #{selector[:pacticipant_version_number]}" if version.nil? + selectors.each do | s | + if s[:pacticipant_version_number] + version = version_service.find_by_pacticipant_name_and_number(pacticipant_name: s[:pacticipant_name], pacticipant_version_number: s[:pacticipant_version_number]) + error_messages << "No pact or verification found for #{s[:pacticipant_name]} version #{s[:pacticipant_version_number]}" if version.nil? + elsif s[:latest_tag] + version = version_service.find_by_pacticpant_name_and_latest_tag(s[:pacticipant_name], s[:latest_tag]) + error_messages << "No version of #{s[:pacticipant_name]} found with tag #{s[:latest_tag]}" if version.nil? end end end diff --git a/lib/pact_broker/versions/repository.rb b/lib/pact_broker/versions/repository.rb index 10553c324..e4312b4fb 100644 --- a/lib/pact_broker/versions/repository.rb +++ b/lib/pact_broker/versions/repository.rb @@ -12,7 +12,7 @@ def find_by_pacticipant_id_and_number pacticipant_id, number PactBroker::Domain::Version.where(number: number, pacticipant_id: pacticipant_id).single_record end - def find_by_latest_tag pacticipant_name, tag + def find_by_pacticpant_name_and_latest_tag pacticipant_name, tag PactBroker::Domain::Version .select_all_qualified .join(:pacticipants, {id: :pacticipant_id}, {implicit_qualifier: :versions}) diff --git a/lib/pact_broker/versions/service.rb b/lib/pact_broker/versions/service.rb index c21002925..5bc3d5ad3 100644 --- a/lib/pact_broker/versions/service.rb +++ b/lib/pact_broker/versions/service.rb @@ -11,6 +11,10 @@ def self.find_by_pacticipant_name_and_number params version_repository.find_by_pacticipant_name_and_number params.fetch(:pacticipant_name), params.fetch(:pacticipant_version_number) end + def self.find_by_pacticpant_name_and_latest_tag(pacticipant_name, tag) + version_repository.find_by_pacticpant_name_and_latest_tag(pacticipant_name, tag) + end + def self.delete version tag_repository.delete_by_version_id version.id pact_repository.delete_by_version_id version.id diff --git a/spec/lib/pact_broker/matrix/service_spec.rb b/spec/lib/pact_broker/matrix/service_spec.rb index 3b5003e46..b28d675a9 100644 --- a/spec/lib/pact_broker/matrix/service_spec.rb +++ b/spec/lib/pact_broker/matrix/service_spec.rb @@ -36,7 +36,7 @@ module Matrix let(:selectors) { [{ pacticipant_name: "Foo", pacticipant_version_number: "1" }] } it "returns error messages" do - expect(subject.first).to eq "Pacticipant 'Foo' not found" + expect(subject.first).to eq "Pacticipant Foo not found" end end @@ -70,6 +70,49 @@ module Matrix expect(subject.first).to eq "Please specify the pacticipant name and version" end end + + context "when the latest_tag is used instead of a version" do + before do + td.create_pacticipant("Foo") + .create_version("1") + .create_tag("prod") + .create_pacticipant("Bar") + .create_version("2") + end + + let(:selectors) { [{ pacticipant_name: "Foo", latest_tag: "prod" }, { pacticipant_name: "Bar", pacticipant_version_number: "2" }] } + + context "when there is a version for the tag" do + it "returns no error messages" do + expect(subject).to eq [] + end + end + + context "when there is not a version for the tag" do + + let(:selectors) { [{ pacticipant_name: "Foo", latest_tag: "wiffle" }, { pacticipant_name: "Bar", pacticipant_version_number: "2" }] } + + it "returns an error message" do + expect(subject).to eq ["No version of Foo found with tag wiffle"] + end + end + end + + context "when the latest_tag is used as well as a version" do + before do + td.create_pacticipant("Foo") + .create_version("1") + .create_tag("prod") + .create_pacticipant("Bar") + .create_version("2") + end + + let(:selectors) { [{ pacticipant_name: "Foo", pacticipant_version_number: "1", latest_tag: "prod" }, { pacticipant_name: "Bar", pacticipant_version_number: "2" }] } + + it "returns an error message" do + expect(subject).to eq ["A version and a latest tag cannot both be specified for Foo"] + end + end end end end diff --git a/spec/lib/pact_broker/versions/repository_spec.rb b/spec/lib/pact_broker/versions/repository_spec.rb index 313a95521..5f8053552 100644 --- a/spec/lib/pact_broker/versions/repository_spec.rb +++ b/spec/lib/pact_broker/versions/repository_spec.rb @@ -10,7 +10,7 @@ module Versions let(:version_number) { "1.2.3" } - describe "#find_by_latest_tag" do + describe "#find_by_pacticpant_name_and_latest_tag" do before do td.create_consumer("Bar") .create_consumer_version("2.3.4") @@ -23,7 +23,7 @@ module Versions .create_consumer_version("5.6.7") end - subject { Repository.new.find_by_latest_tag("Foo", "prod") } + subject { Repository.new.find_by_pacticpant_name_and_latest_tag("Foo", "prod") } it "returns the most recent version that has the specified tag" do