From 0a1f0ee316699f053c5afb8b8765c50e4b647ef5 Mon Sep 17 00:00:00 2001 From: Beth Skurrie Date: Sat, 11 Nov 2017 19:22:02 +1100 Subject: [PATCH] feat(prod pacts in index): allow all tags to be shown on index --- lib/pact_broker/index/service.rb | 12 ++++++- lib/pact_broker/tags/repository.rb | 12 ++++++- lib/pact_broker/tags/service.rb | 3 ++ lib/pact_broker/ui/controllers/index.rb | 4 +-- spec/lib/pact_broker/tags/repository_spec.rb | 24 ++++++++++++- spec/lib/pact_broker/tags/service_spec.rb | 1 - .../pact_broker/ui/controllers/index_spec.rb | 34 ++++++++++++++++++- 7 files changed, 83 insertions(+), 7 deletions(-) diff --git a/lib/pact_broker/index/service.rb b/lib/pact_broker/index/service.rb index 38f71c7e3..dca8d7311 100644 --- a/lib/pact_broker/index/service.rb +++ b/lib/pact_broker/index/service.rb @@ -14,10 +14,20 @@ class Service def self.find_index_items options = {} pact_repository .find_latest_pacts - .collect { | pact| build_index_item_rows(pact, options[:tags] || []) } + .collect { | pact| build_index_item_rows(pact, tags_for(pact, options)) } .flatten end + def self.tags_for(pact, options) + if options[:tags] == true + tag_service.find_all_tag_names_for_pacticipant(pact.consumer_name) + elsif options[:tags].is_a?(Array) + options[:tags] + else + [] + end + end + def self.build_index_item_rows(pact, tags) index_items = [build_latest_pact_index_item(pact, tags)] tags.each do | tag | diff --git a/lib/pact_broker/tags/repository.rb b/lib/pact_broker/tags/repository.rb index 336377f64..e66fe7b32 100644 --- a/lib/pact_broker/tags/repository.rb +++ b/lib/pact_broker/tags/repository.rb @@ -15,7 +15,7 @@ def create args def find args PactBroker::Domain::Tag .select_all_qualified - .join(:versions, {id: :version_id}) + .join(:versions, { id: :version_id }) .join(:pacticipants, {Sequel.qualify("pacticipants", "id") => Sequel.qualify("versions", "pacticipant_id")}) .where(name_like(Sequel.qualify("tags", "name"), args.fetch(:tag_name))) .where(name_like(Sequel.qualify("versions", "number"), args.fetch(:pacticipant_version_number))) @@ -26,6 +26,16 @@ def find args def delete_by_version_id version_id Sequel::Model.db[:tags].where(version_id: version_id).delete end + + def find_all_tag_names_for_pacticipant pacticipant_name + PactBroker::Domain::Tag + .select(Sequel[:tags][:name]) + .join(:versions, { Sequel[:versions][:id] => Sequel[:tags][:version_id] }) + .join(:pacticipants, { Sequel[:pacticipants][:id] => Sequel[:versions][:pacticipant_id] }) + .where(Sequel[:pacticipants][:name] => pacticipant_name) + .distinct + .collect{ |tag| tag[:name] }.sort + end end end end diff --git a/lib/pact_broker/tags/service.rb b/lib/pact_broker/tags/service.rb index 9e9d3f8eb..3fb37dc17 100644 --- a/lib/pact_broker/tags/service.rb +++ b/lib/pact_broker/tags/service.rb @@ -25,6 +25,9 @@ def delete args connection.run("delete from tags where name = '#{args.fetch(:tag_name)}' and version_id = '#{version.id}'") end + def find_all_tag_names_for_pacticipant pacticipant_name + tag_repository.find_all_tag_names_for_pacticipant pacticipant_name + end end end end diff --git a/lib/pact_broker/ui/controllers/index.rb b/lib/pact_broker/ui/controllers/index.rb index 8055ad5cd..1321ee744 100644 --- a/lib/pact_broker/ui/controllers/index.rb +++ b/lib/pact_broker/ui/controllers/index.rb @@ -10,9 +10,9 @@ class Index < Base include PactBroker::Services get "/" do - tags = [*params[:tags]].compact + tags = params[:tags] == 'true' ? true : [*params[:tags]].compact view_model = ViewDomain::IndexItems.new(index_service.find_index_items(tags: tags)) - page = tags.any? ? :'index/show-with-tags' : :'index/show' + page = tags == true || tags.any? ? :'index/show-with-tags' : :'index/show' haml page, {locals: {index_items: view_model, title: "Pacts"}, layout: :'layouts/main'} end diff --git a/spec/lib/pact_broker/tags/repository_spec.rb b/spec/lib/pact_broker/tags/repository_spec.rb index 95f88d106..fc1c09b96 100644 --- a/spec/lib/pact_broker/tags/repository_spec.rb +++ b/spec/lib/pact_broker/tags/repository_spec.rb @@ -5,6 +5,8 @@ module PactBroker module Tags describe Repository do + let(:td) { TestDataBuilder.new } + describe ".find" do let(:pacticipant_name) { "test_pacticipant" } @@ -80,9 +82,29 @@ module Tags it "deletes the tag" do expect{ subject }.to change { PactBroker::Domain::Tag.count }.by(-2) end - end + + describe "find_all_tag_names_for_pacticipant" do + before do + td.create_consumer("Foo") + .create_consumer_version("1") + .create_consumer_version_tag("prod") + .create_consumer_version_tag("master") + .create_consumer_version("2") + .create_consumer_version_tag("prod") + .create_consumer_version_tag("dev") + .create_consumer("Bar") + .create_consumer_version("1") + .create_consumer_version_tag("ignore") + end + + subject { Repository.new.find_all_tag_names_for_pacticipant("Foo") } + + it "returns all the tag names for the pacticipant" do + expect(subject).to eq ["dev", "master", "prod"] + end + end end end end diff --git a/spec/lib/pact_broker/tags/service_spec.rb b/spec/lib/pact_broker/tags/service_spec.rb index 23ca17edd..b698f7ef6 100644 --- a/spec/lib/pact_broker/tags/service_spec.rb +++ b/spec/lib/pact_broker/tags/service_spec.rb @@ -45,7 +45,6 @@ module Tags }.by(-1) end end - end end end \ No newline at end of file diff --git a/spec/lib/pact_broker/ui/controllers/index_spec.rb b/spec/lib/pact_broker/ui/controllers/index_spec.rb index 35c532998..45feb59e1 100644 --- a/spec/lib/pact_broker/ui/controllers/index_spec.rb +++ b/spec/lib/pact_broker/ui/controllers/index_spec.rb @@ -31,9 +31,41 @@ module Controllers expect(last_response.status).to eq(200) end + context "with tags=true" do + before do + allow(PactBroker::Index::Service).to receive(:find_index_items).and_return([]) + end + + it "passes tags: true to the IndexService" do + expect(PactBroker::Index::Service).to receive(:find_index_items).with(tags: true) + get "/", { tags: 'true' } + end + end + + context "with tags[]=prod" do + before do + allow(PactBroker::Index::Service).to receive(:find_index_items).and_return([]) + end + + it "passes tags: ['prod'] to the IndexService" do + expect(PactBroker::Index::Service).to receive(:find_index_items).with(tags: ["prod"]) + get "/", { tags: ["prod"] } + end + end + + context "with tags=prod" do + before do + allow(PactBroker::Index::Service).to receive(:find_index_items).and_return([]) + end + + it "passes tags: ['prod'] to the IndexService" do + expect(PactBroker::Index::Service).to receive(:find_index_items).with(tags: ["prod"]) + get "/", { tags: "prod" } + end + end end end end end end -end \ No newline at end of file +end