diff --git a/lib/pact_broker/index/service.rb b/lib/pact_broker/index/service.rb index 1522eb0cd..aeb3d12fb 100644 --- a/lib/pact_broker/index/service.rb +++ b/lib/pact_broker/index/service.rb @@ -11,21 +11,26 @@ class Service extend PactBroker::Services extend PactBroker::Logging - def self.find_relationships - pact_repository.find_latest_pacts - .collect do | pact| - latest_relationship = build_latest_pact_relationship(pact) - prod_relationship = build_relationship_for_tagged_pact(pact, 'prod') - production_relationship = build_relationship_for_tagged_pact(pact, 'production') - [latest_relationship, prod_relationship, production_relationship].compact - end.flatten + def self.find_relationships options = {} + pact_repository + .find_latest_pacts + .collect { | pact| build_relationship_rows(pact, options[:tags] || []) } + .flatten end - def self.build_latest_pact_relationship pact + def self.build_relationship_rows(pact, tags) + relationships = [build_latest_pact_relationship(pact, tags)] + tags.each do | tag | + relationships << build_relationship_for_tagged_pact(pact, tag) + end + relationships.compact + end + + def self.build_latest_pact_relationship pact, tags latest_verification = verification_service.find_latest_verification_for(pact.consumer, pact.provider) webhooks = webhook_service.find_by_consumer_and_provider pact.consumer, pact.provider triggered_webhooks = webhook_service.find_latest_triggered_webhooks pact.consumer, pact.provider - tag_names = pact.consumer_version_tag_names.select{ |name| name == 'prod' || name == 'production' } + tag_names = pact.consumer_version_tag_names.select{ |name| tags.include?(name) } PactBroker::Domain::Relationship.create pact.consumer, pact.provider, pact, true, latest_verification, webhooks, triggered_webhooks, tag_names end diff --git a/lib/pact_broker/ui/controllers/relationships.rb b/lib/pact_broker/ui/controllers/relationships.rb index 6c9f802e2..32a5727f1 100644 --- a/lib/pact_broker/ui/controllers/relationships.rb +++ b/lib/pact_broker/ui/controllers/relationships.rb @@ -10,8 +10,9 @@ class Relationships < Base include PactBroker::Services get "/" do - view_model = ViewDomain::Relationships.new(index_service.find_relationships) - page = params[:showProdPacts] == 'true' ? :'relationships/show-prod-tags' : :'relationships/show' + tags = [*params[:tags]].compact + view_model = ViewDomain::Relationships.new(index_service.find_relationships(tags: tags)) + page = tags.any? ? :'relationships/show-prod-tags' : :'relationships/show' haml page, {locals: {relationships: view_model, title: "Pacts"}, layout: :'layouts/main'} end diff --git a/lib/pact_broker/ui/view_models/relationship.rb b/lib/pact_broker/ui/view_models/relationship.rb index d57762f4b..08d7b2232 100644 --- a/lib/pact_broker/ui/view_models/relationship.rb +++ b/lib/pact_broker/ui/view_models/relationship.rb @@ -30,7 +30,8 @@ def provider_version_number end def tag_names - @relationship.tag_names.any? ? " (#{@relationship.tag_names.join(', ')}) ": "" + latest_overall = @relationship.latest? ? "latest & " : "" + @relationship.tag_names.any? ? " (#{latest_overall}latest #{@relationship.tag_names.join(', ')}) ": " (latest) " end def consumer_group_url diff --git a/spec/lib/pact_broker/index/service_spec.rb b/spec/lib/pact_broker/index/service_spec.rb index 302b417f1..8435c90e3 100644 --- a/spec/lib/pact_broker/index/service_spec.rb +++ b/spec/lib/pact_broker/index/service_spec.rb @@ -8,6 +8,8 @@ module PactBroker module Index describe Service do let(:td) { TestDataBuilder.new } + let(:tags) { ['prod', 'production'] } + let(:options) { { tags: tags } } subject{ Service } @@ -30,16 +32,16 @@ module Index it "retrieves the webhooks for the pact" do expect(PactBroker::Webhooks::Service).to receive(:find_by_consumer_and_provider).with(consumer, provider) - subject.find_relationships + subject.find_relationships(options) end it "retrieves the latest verification for the pact" do expect(PactBroker::Verifications::Service).to receive(:find_latest_verification_for).with(consumer, provider) - subject.find_relationships + subject.find_relationships(options) end it "returns a list of relationships" do - expect(subject.find_relationships).to eq([PactBroker::Domain::Relationship.create(consumer, provider, pact, true, verification, webhooks)]) + expect(subject.find_relationships(options)).to eq([PactBroker::Domain::Relationship.create(consumer, provider, pact, true, verification, webhooks)]) end end @@ -56,12 +58,20 @@ module Index .create_verification(provider_version: "2.1.0") end - let(:rows) { subject.find_relationships } + let(:rows) { subject.find_relationships(options) } it "returns both rows" do expect(rows.count).to eq 2 end + context "when the tags are not specified" do + let(:options) { {} } + + it "only returns the latest row" do + expect(rows.count).to eq 1 + end + end + it "returns the latest row first" do expect(rows.first.consumer_version_number).to eq "1.2.4" expect(rows.last.consumer_version_number).to eq "1.2.3" @@ -97,7 +107,7 @@ module Index .create_verification(provider_version: "2.0.0") end - let(:rows) { subject.find_relationships } + let(:rows) { subject.find_relationships(options) } it "returns one row" do expect(rows.count).to eq 1 diff --git a/spec/lib/pact_broker/ui/view_models/relationship_spec.rb b/spec/lib/pact_broker/ui/view_models/relationship_spec.rb index 000236cd1..45356d24f 100644 --- a/spec/lib/pact_broker/ui/view_models/relationship_spec.rb +++ b/spec/lib/pact_broker/ui/view_models/relationship_spec.rb @@ -1,5 +1,6 @@ require 'spec_helper' require 'pact_broker/ui/view_models/relationship' +require 'pact_broker/domain/relationship' module PactBroker module UI @@ -10,7 +11,9 @@ module ViewDomain let(:provider) { instance_double("PactBroker::Domain::Pacticipant", name: 'Provider Name')} let(:latest_pact) { instance_double("PactBroker::Domain::Pact") } let(:latest_verification) { instance_double("PactBroker::Domain::Verification") } - let(:domain_relationship) { PactBroker::Domain::Relationship.new(consumer, provider, latest_pact, latest_verification)} + let(:domain_relationship) { PactBroker::Domain::Relationship.new(consumer, provider, latest_pact, latest, latest_verification, [], [], tags)} + let(:tags) { [] } + let(:latest) { true } subject { Relationship.new(domain_relationship) } @@ -107,6 +110,24 @@ module ViewDomain end end + describe "tag_names" do + context "when the pact is the overall latest and it has no tag names" do + its(:tag_names) { is_expected.to eq " (latest) " } + end + + context "when the pact is the overall latest and also has tag names" do + let(:tags) { ["master", "prod"] } + its(:tag_names) { is_expected.to eq " (latest & latest master, prod) " } + end + + context "when the pact is not the latest and has tag names" do + let(:latest) { false } + let(:tags) { ["master", "prod"] } + its(:tag_names) { is_expected.to eq " (latest master, prod) " } + end + + end + describe "<=>" do let(:relationship_model_4) { double("PactBroker::Domain::Relationship", consumer_name: "A", provider_name: "X") }