From 110578dc49c97179851f8eaf60a61e96806e6771 Mon Sep 17 00:00:00 2001 From: Beth Skurrie Date: Wed, 24 Mar 2021 07:06:26 +1100 Subject: [PATCH] feat(matrix): support querying by branch --- lib/pact_broker/domain/version.rb | 5 +++ lib/pact_broker/matrix/parse_query.rb | 10 +++--- lib/pact_broker/matrix/resolved_selector.rb | 14 ++++++++ lib/pact_broker/matrix/service.rb | 1 + lib/pact_broker/matrix/unresolved_selector.rb | 10 +++++- lib/pact_broker/test/test_data_builder.rb | 2 +- spec/lib/pact_broker/domain/version_spec.rb | 34 +++++++++++++++++++ 7 files changed, 69 insertions(+), 7 deletions(-) diff --git a/lib/pact_broker/domain/version.rb b/lib/pact_broker/domain/version.rb index 023bbfbad..43a14313b 100644 --- a/lib/pact_broker/domain/version.rb +++ b/lib/pact_broker/domain/version.rb @@ -103,6 +103,10 @@ def where_tag(tag) end end + def where_branch(branch) + where(branch: branch) + end + def where_number(number) where(name_like(:number, number)) end @@ -130,6 +134,7 @@ def for_selector(selector) query = query.where_pacticipant_name(selector.pacticipant_name) if selector.pacticipant_name query = query.currently_deployed_to_environment(selector.environment_name, selector.pacticipant_name) if selector.environment_name query = query.where_tag(selector.tag) if selector.tag + query = query.where_branch(selector.branch) if selector.branch query = query.where_number(selector.pacticipant_version_number) if selector.pacticipant_version_number query = query.where_age_less_than(selector.max_age) if selector.max_age diff --git a/lib/pact_broker/matrix/parse_query.rb b/lib/pact_broker/matrix/parse_query.rb index eb68e932f..4ac565f97 100644 --- a/lib/pact_broker/matrix/parse_query.rb +++ b/lib/pact_broker/matrix/parse_query.rb @@ -8,11 +8,11 @@ def self.call query params = Rack::Utils.parse_nested_query(query) selectors = (params['q'] || []).collect do |i| p = PactBroker::Matrix::UnresolvedSelector.new - p[:pacticipant_name] = i['pacticipant'] if i['pacticipant'] && i['pacticipant'] != '' - p[:pacticipant_version_number] = i['version'] if i['version'] && i['version'] != '' - p[:latest] = true if i['latest'] == 'true' - p[:branch] = i['branch'] if i['branch'] && i['branch'] != '' - p[:tag] = i['tag'] if i['tag'] && i['tag'] != '' + p.pacticipant_name = i['pacticipant'] if i['pacticipant'] && i['pacticipant'] != '' + p.pacticipant_version_number = i['version'] if i['version'] && i['version'] != '' + p.latest = true if i['latest'] == 'true' + p.branch = i['branch'] if i['branch'] && i['branch'] != '' + p.tag = i['tag'] if i['tag'] && i['tag'] != '' p end options = {} diff --git a/lib/pact_broker/matrix/resolved_selector.rb b/lib/pact_broker/matrix/resolved_selector.rb index fdb219353..ae05d601e 100644 --- a/lib/pact_broker/matrix/resolved_selector.rb +++ b/lib/pact_broker/matrix/resolved_selector.rb @@ -31,6 +31,7 @@ def self.for_pacticipant_and_version(pacticipant, version, original_selector, ty pacticipant_version_number: version.number, latest: original_selector[:latest], tag: original_selector[:tag], + branch: original_selector[:branch], environment_name: original_selector[:environment_name], type: type, one_of_many: one_of_many @@ -45,6 +46,7 @@ def self.for_pacticipant_and_non_existing_version(pacticipant, original_selector pacticipant_version_number: original_selector[:pacticipant_version_number], latest: original_selector[:latest], tag: original_selector[:tag], + branch: original_selector[:branch], environment_name: original_selector[:environment_name], type: type ) @@ -74,6 +76,10 @@ def tag self[:tag] end + def branch + self[:branch] + end + def environment_name self[:environment_name] end @@ -94,6 +100,10 @@ def latest_tagged? latest? && tag end + def latest_from_branch? + latest? && branch + end + def version_does_not_exist? !version_exists? end @@ -130,6 +140,10 @@ def description "the latest version of #{pacticipant_name} with tag #{tag} (#{pacticipant_version_number})" elsif latest_tagged? "the latest version of #{pacticipant_name} with tag #{tag} (no such version exists)" + elsif latest_from_branch? && pacticipant_version_number + "the latest version of #{pacticipant_name} from branch #{branch} (#{pacticipant_version_number})" + elsif latest_from_branch? + "the latest version of #{pacticipant_name} from branch #{branch} (no such version exists)" elsif latest? && pacticipant_version_number "the latest version of #{pacticipant_name} (#{pacticipant_version_number})" elsif latest? diff --git a/lib/pact_broker/matrix/service.rb b/lib/pact_broker/matrix/service.rb index ac18e5610..9b6a28815 100644 --- a/lib/pact_broker/matrix/service.rb +++ b/lib/pact_broker/matrix/service.rb @@ -59,6 +59,7 @@ def validate_selectors selectors, options = {} if s[:pacticipant_name].nil? error_messages << "Please specify the pacticipant name" else + # TODO a bunch more validation if s.key?(:pacticipant_version_number) && s.key?(:latest) error_messages << "A version number and latest flag cannot both be specified for #{s[:pacticipant_name]}" end diff --git a/lib/pact_broker/matrix/unresolved_selector.rb b/lib/pact_broker/matrix/unresolved_selector.rb index 1f68d406e..75892aff6 100644 --- a/lib/pact_broker/matrix/unresolved_selector.rb +++ b/lib/pact_broker/matrix/unresolved_selector.rb @@ -10,7 +10,7 @@ def initialize(params = {}) end def self.from_hash(hash) - new(hash.symbolize_keys.snakecase_keys.slice(:pacticipant_name, :pacticipant_version_number, :latest, :tag, :environment_name, :max_age)) + new(hash.symbolize_keys.snakecase_keys.slice(:pacticipant_name, :pacticipant_version_number, :latest, :tag, :branch, :environment_name, :max_age)) end def pacticipant_name @@ -37,6 +37,10 @@ def tag self[:tag] end + def branch + self[:branch] + end + def environment_name self[:environment_name] end @@ -49,6 +53,10 @@ def tag= tag self[:tag] = tag end + def branch= branch + self[:branch] = branch + end + def environment_name= environment_name self[:environment_name] = environment_name end diff --git a/lib/pact_broker/test/test_data_builder.rb b/lib/pact_broker/test/test_data_builder.rb index c56a32c15..f36a3c8f4 100644 --- a/lib/pact_broker/test/test_data_builder.rb +++ b/lib/pact_broker/test/test_data_builder.rb @@ -184,7 +184,7 @@ def create_consumer_version version_number = "1.0.#{model_counter}", params = {} def create_provider_version version_number = "1.0.#{model_counter}", params = {} params.delete(:comment) tag_names = [params.delete(:tag_names), params.delete(:tag_name)].flatten.compact - @version = PactBroker::Domain::Version.create(:number => version_number, :pacticipant => @provider) + @version = PactBroker::Domain::Version.create(:number => version_number, :pacticipant => @provider, branch: params[:branch]) @provider_version = @version tag_names.each do | tag_name | tag = PactBroker::Domain::Tag.create(name: tag_name, version: provider_version) diff --git a/spec/lib/pact_broker/domain/version_spec.rb b/spec/lib/pact_broker/domain/version_spec.rb index fbc7222a9..292fcac94 100644 --- a/spec/lib/pact_broker/domain/version_spec.rb +++ b/spec/lib/pact_broker/domain/version_spec.rb @@ -191,6 +191,40 @@ def version_numbers expect(version_numbers).to eq %w{2 10} end end + + context "selecting versions for a branch" do + before do + td.create_consumer("Foo") + .create_consumer_version("1", branch: "main") + .create_consumer_version("2", branch: "feat/foo") + .create_consumer_version("3", branch: "main") + .create_provider("Bar") + .create_provider_version("10", branch: "main") + end + + let(:selector) { PactBroker::Matrix::UnresolvedSelector.new(branch: "main") } + + it "returns the versions with the matching branch" do + expect(version_numbers).to eq %w{1 3 10} + end + end + + context "selecting latest version for a branch" do + before do + td.create_consumer("Foo") + .create_consumer_version("1", branch: "main") + .create_consumer_version("2", branch: "feat/foo") + .create_consumer_version("3", branch: "main") + .create_provider("Bar") + .create_provider_version("10", branch: "main") + end + + let(:selector) { PactBroker::Matrix::UnresolvedSelector.new(branch: "main", latest: true) } + + it "returns the latest versions for each matching branch" do + expect(version_numbers).to eq %w{3 10} + end + end end describe "latest_for_pacticipant?" do