Skip to content

Commit

Permalink
feat(prod pacts in index): allow tags shown in index to be configured…
Browse files Browse the repository at this point in the history
… via query string
  • Loading branch information
bethesque committed Nov 11, 2017
1 parent ea05fe7 commit 4eca8ee
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 19 deletions.
25 changes: 15 additions & 10 deletions lib/pact_broker/index/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
5 changes: 3 additions & 2 deletions lib/pact_broker/ui/controllers/relationships.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
3 changes: 2 additions & 1 deletion lib/pact_broker/ui/view_models/relationship.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 15 additions & 5 deletions spec/lib/pact_broker/index/service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 }

Expand All @@ -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

Expand All @@ -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"
Expand Down Expand Up @@ -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
Expand Down
23 changes: 22 additions & 1 deletion spec/lib/pact_broker/ui/view_models/relationship_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require 'spec_helper'
require 'pact_broker/ui/view_models/relationship'
require 'pact_broker/domain/relationship'

module PactBroker
module UI
Expand All @@ -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) }

Expand Down Expand Up @@ -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") }
Expand Down

0 comments on commit 4eca8ee

Please sign in to comment.