Skip to content

Commit 0a1f0ee

Browse files
committed
feat(prod pacts in index): allow all tags to be shown on index
1 parent af5c919 commit 0a1f0ee

File tree

7 files changed

+83
-7
lines changed

7 files changed

+83
-7
lines changed

lib/pact_broker/index/service.rb

+11-1
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,20 @@ class Service
1414
def self.find_index_items options = {}
1515
pact_repository
1616
.find_latest_pacts
17-
.collect { | pact| build_index_item_rows(pact, options[:tags] || []) }
17+
.collect { | pact| build_index_item_rows(pact, tags_for(pact, options)) }
1818
.flatten
1919
end
2020

21+
def self.tags_for(pact, options)
22+
if options[:tags] == true
23+
tag_service.find_all_tag_names_for_pacticipant(pact.consumer_name)
24+
elsif options[:tags].is_a?(Array)
25+
options[:tags]
26+
else
27+
[]
28+
end
29+
end
30+
2131
def self.build_index_item_rows(pact, tags)
2232
index_items = [build_latest_pact_index_item(pact, tags)]
2333
tags.each do | tag |

lib/pact_broker/tags/repository.rb

+11-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ def create args
1515
def find args
1616
PactBroker::Domain::Tag
1717
.select_all_qualified
18-
.join(:versions, {id: :version_id})
18+
.join(:versions, { id: :version_id })
1919
.join(:pacticipants, {Sequel.qualify("pacticipants", "id") => Sequel.qualify("versions", "pacticipant_id")})
2020
.where(name_like(Sequel.qualify("tags", "name"), args.fetch(:tag_name)))
2121
.where(name_like(Sequel.qualify("versions", "number"), args.fetch(:pacticipant_version_number)))
@@ -26,6 +26,16 @@ def find args
2626
def delete_by_version_id version_id
2727
Sequel::Model.db[:tags].where(version_id: version_id).delete
2828
end
29+
30+
def find_all_tag_names_for_pacticipant pacticipant_name
31+
PactBroker::Domain::Tag
32+
.select(Sequel[:tags][:name])
33+
.join(:versions, { Sequel[:versions][:id] => Sequel[:tags][:version_id] })
34+
.join(:pacticipants, { Sequel[:pacticipants][:id] => Sequel[:versions][:pacticipant_id] })
35+
.where(Sequel[:pacticipants][:name] => pacticipant_name)
36+
.distinct
37+
.collect{ |tag| tag[:name] }.sort
38+
end
2939
end
3040
end
3141
end

lib/pact_broker/tags/service.rb

+3
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ def delete args
2525
connection.run("delete from tags where name = '#{args.fetch(:tag_name)}' and version_id = '#{version.id}'")
2626
end
2727

28+
def find_all_tag_names_for_pacticipant pacticipant_name
29+
tag_repository.find_all_tag_names_for_pacticipant pacticipant_name
30+
end
2831
end
2932
end
3033
end

lib/pact_broker/ui/controllers/index.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ class Index < Base
1010
include PactBroker::Services
1111

1212
get "/" do
13-
tags = [*params[:tags]].compact
13+
tags = params[:tags] == 'true' ? true : [*params[:tags]].compact
1414
view_model = ViewDomain::IndexItems.new(index_service.find_index_items(tags: tags))
15-
page = tags.any? ? :'index/show-with-tags' : :'index/show'
15+
page = tags == true || tags.any? ? :'index/show-with-tags' : :'index/show'
1616
haml page, {locals: {index_items: view_model, title: "Pacts"}, layout: :'layouts/main'}
1717
end
1818

spec/lib/pact_broker/tags/repository_spec.rb

+23-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ module PactBroker
55
module Tags
66
describe Repository do
77

8+
let(:td) { TestDataBuilder.new }
9+
810
describe ".find" do
911

1012
let(:pacticipant_name) { "test_pacticipant" }
@@ -80,9 +82,29 @@ module Tags
8082
it "deletes the tag" do
8183
expect{ subject }.to change { PactBroker::Domain::Tag.count }.by(-2)
8284
end
83-
8485
end
8586

87+
88+
describe "find_all_tag_names_for_pacticipant" do
89+
before do
90+
td.create_consumer("Foo")
91+
.create_consumer_version("1")
92+
.create_consumer_version_tag("prod")
93+
.create_consumer_version_tag("master")
94+
.create_consumer_version("2")
95+
.create_consumer_version_tag("prod")
96+
.create_consumer_version_tag("dev")
97+
.create_consumer("Bar")
98+
.create_consumer_version("1")
99+
.create_consumer_version_tag("ignore")
100+
end
101+
102+
subject { Repository.new.find_all_tag_names_for_pacticipant("Foo") }
103+
104+
it "returns all the tag names for the pacticipant" do
105+
expect(subject).to eq ["dev", "master", "prod"]
106+
end
107+
end
86108
end
87109
end
88110
end

spec/lib/pact_broker/tags/service_spec.rb

-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ module Tags
4545
}.by(-1)
4646
end
4747
end
48-
4948
end
5049
end
5150
end

spec/lib/pact_broker/ui/controllers/index_spec.rb

+33-1
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,41 @@ module Controllers
3131
expect(last_response.status).to eq(200)
3232
end
3333

34+
context "with tags=true" do
35+
before do
36+
allow(PactBroker::Index::Service).to receive(:find_index_items).and_return([])
37+
end
38+
39+
it "passes tags: true to the IndexService" do
40+
expect(PactBroker::Index::Service).to receive(:find_index_items).with(tags: true)
41+
get "/", { tags: 'true' }
42+
end
43+
end
44+
45+
context "with tags[]=prod" do
46+
before do
47+
allow(PactBroker::Index::Service).to receive(:find_index_items).and_return([])
48+
end
49+
50+
it "passes tags: ['prod'] to the IndexService" do
51+
expect(PactBroker::Index::Service).to receive(:find_index_items).with(tags: ["prod"])
52+
get "/", { tags: ["prod"] }
53+
end
54+
end
55+
56+
context "with tags=prod" do
57+
before do
58+
allow(PactBroker::Index::Service).to receive(:find_index_items).and_return([])
59+
end
60+
61+
it "passes tags: ['prod'] to the IndexService" do
62+
expect(PactBroker::Index::Service).to receive(:find_index_items).with(tags: ["prod"])
63+
get "/", { tags: "prod" }
64+
end
65+
end
3466
end
3567
end
3668
end
3769
end
3870
end
39-
end
71+
end

0 commit comments

Comments
 (0)