Skip to content

Commit 4eca8ee

Browse files
committed
feat(prod pacts in index): allow tags shown in index to be configured via query string
1 parent ea05fe7 commit 4eca8ee

File tree

5 files changed

+57
-19
lines changed

5 files changed

+57
-19
lines changed

lib/pact_broker/index/service.rb

+15-10
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,26 @@ class Service
1111
extend PactBroker::Services
1212
extend PactBroker::Logging
1313

14-
def self.find_relationships
15-
pact_repository.find_latest_pacts
16-
.collect do | pact|
17-
latest_relationship = build_latest_pact_relationship(pact)
18-
prod_relationship = build_relationship_for_tagged_pact(pact, 'prod')
19-
production_relationship = build_relationship_for_tagged_pact(pact, 'production')
20-
[latest_relationship, prod_relationship, production_relationship].compact
21-
end.flatten
14+
def self.find_relationships options = {}
15+
pact_repository
16+
.find_latest_pacts
17+
.collect { | pact| build_relationship_rows(pact, options[:tags] || []) }
18+
.flatten
2219
end
2320

24-
def self.build_latest_pact_relationship pact
21+
def self.build_relationship_rows(pact, tags)
22+
relationships = [build_latest_pact_relationship(pact, tags)]
23+
tags.each do | tag |
24+
relationships << build_relationship_for_tagged_pact(pact, tag)
25+
end
26+
relationships.compact
27+
end
28+
29+
def self.build_latest_pact_relationship pact, tags
2530
latest_verification = verification_service.find_latest_verification_for(pact.consumer, pact.provider)
2631
webhooks = webhook_service.find_by_consumer_and_provider pact.consumer, pact.provider
2732
triggered_webhooks = webhook_service.find_latest_triggered_webhooks pact.consumer, pact.provider
28-
tag_names = pact.consumer_version_tag_names.select{ |name| name == 'prod' || name == 'production' }
33+
tag_names = pact.consumer_version_tag_names.select{ |name| tags.include?(name) }
2934
PactBroker::Domain::Relationship.create pact.consumer, pact.provider, pact, true, latest_verification, webhooks, triggered_webhooks, tag_names
3035
end
3136

lib/pact_broker/ui/controllers/relationships.rb

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

1212
get "/" do
13-
view_model = ViewDomain::Relationships.new(index_service.find_relationships)
14-
page = params[:showProdPacts] == 'true' ? :'relationships/show-prod-tags' : :'relationships/show'
13+
tags = [*params[:tags]].compact
14+
view_model = ViewDomain::Relationships.new(index_service.find_relationships(tags: tags))
15+
page = tags.any? ? :'relationships/show-prod-tags' : :'relationships/show'
1516
haml page, {locals: {relationships: view_model, title: "Pacts"}, layout: :'layouts/main'}
1617
end
1718

lib/pact_broker/ui/view_models/relationship.rb

+2-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ def provider_version_number
3030
end
3131

3232
def tag_names
33-
@relationship.tag_names.any? ? " (#{@relationship.tag_names.join(', ')}) ": ""
33+
latest_overall = @relationship.latest? ? "latest & " : ""
34+
@relationship.tag_names.any? ? " (#{latest_overall}latest #{@relationship.tag_names.join(', ')}) ": " (latest) "
3435
end
3536

3637
def consumer_group_url

spec/lib/pact_broker/index/service_spec.rb

+15-5
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ module PactBroker
88
module Index
99
describe Service do
1010
let(:td) { TestDataBuilder.new }
11+
let(:tags) { ['prod', 'production'] }
12+
let(:options) { { tags: tags } }
1113

1214
subject{ Service }
1315

@@ -30,16 +32,16 @@ module Index
3032

3133
it "retrieves the webhooks for the pact" do
3234
expect(PactBroker::Webhooks::Service).to receive(:find_by_consumer_and_provider).with(consumer, provider)
33-
subject.find_relationships
35+
subject.find_relationships(options)
3436
end
3537

3638
it "retrieves the latest verification for the pact" do
3739
expect(PactBroker::Verifications::Service).to receive(:find_latest_verification_for).with(consumer, provider)
38-
subject.find_relationships
40+
subject.find_relationships(options)
3941
end
4042

4143
it "returns a list of relationships" do
42-
expect(subject.find_relationships).to eq([PactBroker::Domain::Relationship.create(consumer, provider, pact, true, verification, webhooks)])
44+
expect(subject.find_relationships(options)).to eq([PactBroker::Domain::Relationship.create(consumer, provider, pact, true, verification, webhooks)])
4345
end
4446
end
4547

@@ -56,12 +58,20 @@ module Index
5658
.create_verification(provider_version: "2.1.0")
5759
end
5860

59-
let(:rows) { subject.find_relationships }
61+
let(:rows) { subject.find_relationships(options) }
6062

6163
it "returns both rows" do
6264
expect(rows.count).to eq 2
6365
end
6466

67+
context "when the tags are not specified" do
68+
let(:options) { {} }
69+
70+
it "only returns the latest row" do
71+
expect(rows.count).to eq 1
72+
end
73+
end
74+
6575
it "returns the latest row first" do
6676
expect(rows.first.consumer_version_number).to eq "1.2.4"
6777
expect(rows.last.consumer_version_number).to eq "1.2.3"
@@ -97,7 +107,7 @@ module Index
97107
.create_verification(provider_version: "2.0.0")
98108
end
99109

100-
let(:rows) { subject.find_relationships }
110+
let(:rows) { subject.find_relationships(options) }
101111

102112
it "returns one row" do
103113
expect(rows.count).to eq 1

spec/lib/pact_broker/ui/view_models/relationship_spec.rb

+22-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
require 'spec_helper'
22
require 'pact_broker/ui/view_models/relationship'
3+
require 'pact_broker/domain/relationship'
34

45
module PactBroker
56
module UI
@@ -10,7 +11,9 @@ module ViewDomain
1011
let(:provider) { instance_double("PactBroker::Domain::Pacticipant", name: 'Provider Name')}
1112
let(:latest_pact) { instance_double("PactBroker::Domain::Pact") }
1213
let(:latest_verification) { instance_double("PactBroker::Domain::Verification") }
13-
let(:domain_relationship) { PactBroker::Domain::Relationship.new(consumer, provider, latest_pact, latest_verification)}
14+
let(:domain_relationship) { PactBroker::Domain::Relationship.new(consumer, provider, latest_pact, latest, latest_verification, [], [], tags)}
15+
let(:tags) { [] }
16+
let(:latest) { true }
1417

1518
subject { Relationship.new(domain_relationship) }
1619

@@ -107,6 +110,24 @@ module ViewDomain
107110
end
108111
end
109112

113+
describe "tag_names" do
114+
context "when the pact is the overall latest and it has no tag names" do
115+
its(:tag_names) { is_expected.to eq " (latest) " }
116+
end
117+
118+
context "when the pact is the overall latest and also has tag names" do
119+
let(:tags) { ["master", "prod"] }
120+
its(:tag_names) { is_expected.to eq " (latest & latest master, prod) " }
121+
end
122+
123+
context "when the pact is not the latest and has tag names" do
124+
let(:latest) { false }
125+
let(:tags) { ["master", "prod"] }
126+
its(:tag_names) { is_expected.to eq " (latest master, prod) " }
127+
end
128+
129+
end
130+
110131
describe "<=>" do
111132

112133
let(:relationship_model_4) { double("PactBroker::Domain::Relationship", consumer_name: "A", provider_name: "X") }

0 commit comments

Comments
 (0)