diff --git a/lib/pact_broker/index/service.rb b/lib/pact_broker/index/service.rb index c9b42a8b2..542c7a6a2 100644 --- a/lib/pact_broker/index/service.rb +++ b/lib/pact_broker/index/service.rb @@ -144,8 +144,11 @@ def self.db def self.head_pact_publications(options = {}) base = base_query(options) - ids_query = pact_publication_ids_query(base.overall_latest, options[:tags]) + if options[:pacticipant_name] + base = base.where(Sequel[:pact_publications][:id] => pact_pacticipant_ids_by_name(options[:pacticipant_name])) + end + ids_query = query_pact_publication_ids_by_tags(base, options[:tags]) query = PactBroker::Pacts::PactPublication .select_all_qualified .where(Sequel[:pact_publications][:id] => ids_query) @@ -156,8 +159,6 @@ def self.head_pact_publications(options = {}) { table_alias: :cv } ) - query = query.where(pacticipant_name_query(options[:pacticipant_name])) if options[:pacticipant_name] - order_columns = [ Sequel.asc(Sequel.function(:lower, Sequel[:consumers][:name])), Sequel.desc(Sequel[:cv][:order]), @@ -185,6 +186,14 @@ def self.latest_verifications_for_consumer_version_tags(options) end end + def self.query_pact_publication_ids_by_tags(base, tags) + latest = base.overall_latest + return latest.union(base.latest_for_consumer_tag(tags)) if tags.is_a?(Array) + return latest.union(base.latest_by_consumer_tag) if tags + + latest + end + def self.base_query(options) query = PactBroker::Pacts::PactPublication.select(Sequel[:pact_publications][:id]) @@ -201,22 +210,23 @@ def self.base_query(options) query end - def self.pact_publication_ids_query(query, tags) - return query.union(query.latest_for_consumer_tag(tags)) if tags.is_a?(Array) - return query.union(query.latest_by_consumer_tag) if tags - query - end - - def self.pacticipant_name_query(pacticipant_name) + def self.pact_pacticipant_ids_by_name(pacticipant_name) terms = pacticipant_name.split.map { |v| v.gsub("_", '\\_') } - Sequel.|( + string_match_query = Sequel.|( Sequel.|( *terms.map { |term| Sequel.ilike(Sequel[:providers][:name], "%#{term}%") }), Sequel.|( *terms.map { |term| Sequel.ilike(Sequel[:consumers][:name], "%#{term}%") }) ) + + PactBroker::Pacts::PactPublication + .join_consumers(:consumers) + .join_providers(:providers) + .where(string_match_query) + .select(Sequel[:pact_publications][:id]) + .map(:id) end - private_class_method :base_query, :pacticipant_name_query, :pact_publication_ids_query + private_class_method :base_query, :query_pact_publication_ids_by_tags, :pact_pacticipant_ids_by_name end end end diff --git a/lib/pact_broker/ui/views/index/show-with-tags.haml b/lib/pact_broker/ui/views/index/show-with-tags.haml index 24a0e7c5d..575ad3014 100644 --- a/lib/pact_broker/ui/views/index/show-with-tags.haml +++ b/lib/pact_broker/ui/views/index/show-with-tags.haml @@ -2,23 +2,10 @@ != render :haml, :'index/_css_and_js', :layout => false .container != render :haml, :'index/_navbar', :layout => false, locals: {tag_toggle: false, base_url: base_url} - - if index_items.empty? && pacticipant_name.blank? + - if index_items.empty? != render :haml, :'index/_getting-started', :layout => false %h1.page-header Pacts - - - unless errors.blank? - - errors.each do | error | - %div.alert.alert-danger - = error - - %form{action: "#{base_url}", onsubmit:'return onSubmit()'} - .field - %label{for: 'pacticipant_name'} - Pacticipant name - %input{name: 'pacticipant_name', id: 'pacticipant_name', class: 'pacticipant_name'} - %div.top-of-group - %input{type: 'submit', value: 'Search'} %table.table.table-bordered.table-striped{ id: 'relationships' } %thead %tr diff --git a/spec/lib/pact_broker/ui/controllers/index_spec.rb b/spec/lib/pact_broker/ui/controllers/index_spec.rb index 1fb4daf64..b5b532461 100644 --- a/spec/lib/pact_broker/ui/controllers/index_spec.rb +++ b/spec/lib/pact_broker/ui/controllers/index_spec.rb @@ -97,30 +97,29 @@ module Controllers context "when parameter pacticipant_name presents" do context "when it is blank" do it "ignores it" do - expect(PactBroker::Index::Service).not_to receive(:filter_item_by_pacticipant_name) get "/", { pacticipant_name_name: "" } + + expect(last_response.body).to include("Example App") + expect(last_response.status).to eq(200) end end context "when it is NOT blank and the pacticipant name exist" do - it "filters out the pacticipant which name does not match the pacticipant name" do - expect(PactBroker::Index::Service).to receive(:filter_item_by_pacticipant_name).and_call_original + it "returns the pacticipant which matches the query" do get "/", { pacticipant_name: "example app" } expect(last_response.body).to include("Example App") - expect(last_response.body).not_to include("Test App") expect(last_response.status).to eq(200) end end context "when it is NOT blank but the pacticipant name does NOT exist" do - it "returns all the pact items" do - expect(PactBroker::Index::Service).to receive(:filter_item_by_pacticipant_name).and_call_original + it "returns no pacts" do get "/", { pacticipant_name: "does not exist" } expect(last_response.body).not_to include("does not exit") - expect(last_response.body).to include("Example App") - expect(last_response.body).to include("Test App") + expect(last_response.body).not_to include("Example App") + expect(last_response.body).not_to include("Test App") expect(last_response.status).to eq(200) end end