Skip to content

Commit

Permalink
feat(prod pacts in index): show pacts tagged as the 'prod' or 'produc…
Browse files Browse the repository at this point in the history
…tion' versions on the index page
  • Loading branch information
bethesque committed Oct 15, 2017
1 parent 912f5dc commit b58b7a3
Show file tree
Hide file tree
Showing 16 changed files with 297 additions and 39 deletions.
28 changes: 24 additions & 4 deletions lib/pact_broker/domain/relationship.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,26 @@ class Relationship

attr_reader :consumer, :provider, :latest_pact, :latest_verification, :webhooks

def initialize consumer, provider, latest_pact = nil, latest_verification = nil, webhooks = [], triggered_webhooks = []
def initialize consumer, provider, latest_pact = nil, latest = true, latest_verification = nil, webhooks = [], triggered_webhooks = [], tags = []
@consumer = consumer
@provider = provider
@latest_pact = latest_pact
@latest = latest
@latest_verification = latest_verification
@webhooks = webhooks
@triggered_webhooks = triggered_webhooks
@tags = tags
end

def self.create consumer, provider, latest_pact, latest_verification, webhooks = [], triggered_webhooks = []
new consumer, provider, latest_pact, latest_verification, webhooks, triggered_webhooks
def self.create consumer, provider, latest_pact, latest, latest_verification, webhooks = [], triggered_webhooks = [], tags = []
new consumer, provider, latest_pact, latest, latest_verification, webhooks, triggered_webhooks, tags
end

def eq? other
Relationship === other && other.consumer == consumer && other.provider == provider &&
other.latest_pact == latest_pact && other.latest_verification == latest_verification &&
other.latest_pact == latest_pact &&
other.latest? == latest? &&
other.latest_verification == latest_verification &&
other.webhooks == webhooks
end

Expand All @@ -42,6 +46,22 @@ def latest_pact
@latest_pact
end

def latest?
@latest
end

def consumer_version_number
@latest_pact.consumer_version_number
end

def provider_version_number
@latest_verification ? @latest_verification.provider_version_number : nil
end

def tag_names
@tags
end

def any_webhooks?
@webhooks.any?
end
Expand Down
26 changes: 21 additions & 5 deletions lib/pact_broker/pacticipants/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,27 @@ def self.find_pacticipant_repository_url_by_pacticipant_name name
def self.find_relationships
pact_repository.find_latest_pacts
.collect do | pact|
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
PactBroker::Domain::Relationship.create pact.consumer, pact.provider, pact, latest_verification, webhooks, triggered_webhooks
end
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
end

def self.build_latest_pact_relationship pact
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' }
PactBroker::Domain::Relationship.create pact.consumer, pact.provider, pact, true, latest_verification, webhooks, triggered_webhooks, tag_names
end

def self.build_relationship_for_tagged_pact latest_pact, tag
pact = pact_service.find_latest_pact consumer_name: latest_pact.consumer_name, provider_name: latest_pact.provider_name, tag: tag
return nil unless pact
return nil if pact.id == latest_pact.id
verification = verification_repository.find_latest_verification_for pact.consumer_name, pact.provider_name, tag
PactBroker::Domain::Relationship.create pact.consumer, pact.provider, pact, false, verification, [], [], [tag]
end

def self.update params
Expand Down
4 changes: 4 additions & 0 deletions lib/pact_broker/pacts/pact_publication.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ def before_create
self.revision_number ||= 1
end

def latest_tag_names
LatestTaggedPactPublications.where(id: id).select(:tag_name).collect{|t| t[:tag_name]}
end

def to_domain
PactBroker::Domain::Pact.new(
id: id,
Expand Down
2 changes: 1 addition & 1 deletion lib/pact_broker/pacts/repository.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def find_by_version_and_provider version_id, provider_id
end

def find_latest_pacts
LatestPactPublications.order(:consumer_name, :provider_name).collect(&:to_domain_without_tags)
LatestPactPublications.order(:consumer_name, :provider_name).collect(&:to_domain)
end

def find_latest_pact(consumer_name, provider_name, tag = nil)
Expand Down
1 change: 0 additions & 1 deletion lib/pact_broker/tags/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,4 @@ def delete args

end
end

end
4 changes: 2 additions & 2 deletions lib/pact_broker/ui/controllers/relationships.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ class Relationships < Base

get "/" do
view_model = ViewDomain::Relationships.new(pacticipant_service.find_relationships)

haml :'relationships/show', {locals: {relationships: view_model, title: "Pacts"}, layout: :'layouts/main'}
page = params[:showProdPacts] == 'true' ? :'relationships/show-prod-tags' : :'relationships/show'
haml page, {locals: {relationships: view_model, title: "Pacts"}, layout: :'layouts/main'}
end

end
Expand Down
20 changes: 19 additions & 1 deletion lib/pact_broker/ui/view_models/relationship.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,18 @@ def provider_name
@relationship.provider_name
end

def consumer_version_number
@relationship.consumer_version_number
end

def provider_version_number
@relationship.provider_version_number
end

def tag_names
@relationship.tag_names.any? ? " (#{@relationship.tag_names.join(', ')}) ": ""
end

def consumer_group_url
Helpers::URLHelper.group_url consumer_name
end
Expand All @@ -29,7 +41,7 @@ def provider_group_url
Helpers::URLHelper.group_url provider_name
end

def latest_pact_url
def pact_url
"#{pactigration_base_url('', @relationship)}/latest"
end

Expand All @@ -38,6 +50,7 @@ def any_webhooks?
end

def webhook_label
return "" unless show_webhook_status?
case @relationship.webhook_status
when :none then "Create"
when :success, :failure then webhook_last_execution_date
Expand All @@ -47,6 +60,7 @@ def webhook_label
end

def webhook_status
return "" unless show_webhook_status?
case @relationship.webhook_status
when :success then "success"
when :failure then "danger"
Expand All @@ -55,6 +69,10 @@ def webhook_status
end
end

def show_webhook_status?
@relationship.latest?
end

def webhook_last_execution_date
PactBroker::DateHelper.distance_of_time_in_words(@relationship.last_webhook_execution_date, DateTime.now) + " ago"
end
Expand Down
83 changes: 83 additions & 0 deletions lib/pact_broker/ui/views/relationships/show-prod-tags.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
%body
%link{rel: 'stylesheet', href: '/css/bootstrap.min.css'}
%link{rel: 'stylesheet', href: '/stylesheets/relationships.css'}
%script{type: 'text/javascript', src:'/javascripts/jquery-2.1.1.min.js'}
%script{type: 'text/javascript', src:'/javascripts/jquery.tablesorter.min.js'}
%script{type: 'text/javascript', src:'/js/bootstrap.min.js'}
%nav.navbase-default.navbar-right{role: "navigation"}
.container
%ul
%li.navbar-right
%a{href: '/hal-browser/browser.html'}
HAL Browser
.container
%h1.page-header
Pacts
%table.table.table-bordered.table-striped{id: 'relationships'}
%thead
%th.consumer
Consumer
%span.glyphicon.glyphicon-sort.relationships-sort
%th.tag
Version
%span.glyphicon.glyphicon-sort.relationships-sort
%th.pact
%th.provider
Provider
%span.glyphicon.glyphicon-sort.relationships-sort
%th.tag
Version
%span.glyphicon.glyphicon-sort.relationships-sort
%th
Published
%th
Webhook<br>status
%th
Last<br>verified
%tbody

- relationships.each do | relationship |
%tr
%td.consumer
%a{:href => relationship.consumer_group_url}
= relationship.consumer_name
%td
= relationship.consumer_version_number
%span{style: 'color:gray'}
= relationship.tag_names
%td.pact
%a{:href => relationship.pact_url, :title => "View pact"}
%span.pact
%td.provider
%a{:href => relationship.provider_group_url}
= relationship.provider_name
%td
= relationship.provider_version_number
%td
= relationship.publication_date_of_latest_pact.gsub("about ", "")
%td{class: relationship.webhook_status}
- if relationship.show_webhook_status?
%a{:href => relationship.webhook_url}
= relationship.webhook_label

%td{class: relationship.verification_status, title: relationship.verification_tooltip, "data-toggle": "tooltip", "data-placement": "left"}
%div
= relationship.last_verified_date.gsub("about ", "")
- if relationship.warning?
%span.glyphicon.glyphicon-warning-sign{'aria-hidden':true}
%div.relationships-size
= relationships.size_label
:javascript
$(function(){
$("#relationships").tablesorter();
});

$(document).ready(function(){
$("span.pact").load("/images/doc-text.svg");
$('td[data-toggle="tooltip"]').each(function(index, td){
//appended tooltip div screws up table if it's appended after a
//td, so need to append it to a div
$(td).tooltip({container: $(td).first()});
});
});
2 changes: 1 addition & 1 deletion lib/pact_broker/ui/views/relationships/show.haml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
%a{:href => relationship.consumer_group_url}
= relationship.consumer_name
%td.pact
%a{:href => relationship.latest_pact_url, :title => "View pact"}
%a{:href => relationship.pact_url, :title => "View pact"}
%span.pact
%td.provider
%a{:href => relationship.provider_group_url}
Expand Down
3 changes: 3 additions & 0 deletions lib/pact_broker/verifications/repository.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ def find_latest_verifications_for_consumer_version consumer_name, consumer_versi
.all
end

# The most recent verification for the latest revision of the pact
# belonging to the version with the largest consumer_version_order.

def find_latest_verification_for consumer_name, provider_name, tag = nil
query = LatestVerificationsByConsumerVersion
.select_all_qualified
Expand Down
5 changes: 5 additions & 0 deletions script/seed.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,12 @@ def publish_pact params = {}
.create_verification(provider_version: "1.4.234", success: true, execution_date: DateTime.now - 15)
.revise_pact
.create_consumer_version("1.2.101")
.create_consumer_version_tag('prod')
.publish_pact
.create_verification(provider_version: "9.9.10", success: false, execution_date: DateTime.now - 15)
.create_consumer_version("1.2.102")
.publish_pact(created_at: (Date.today - 7).to_datetime)
.create_verification(provider_version: "9.9.9", success: true, execution_date: DateTime.now - 14)
.create_provider("Animals")
.create_webhook(method: 'GET', url: 'http://localhost:9393/')
.publish_pact(created_at: (Time.now - 140).to_datetime)
Expand All @@ -76,6 +79,8 @@ def publish_pact params = {}
.create_provider("The back end")
.create_webhook(method: 'GET', url: 'http://localhost:9393/')
.create_consumer_version("1.2.106")
.create_consumer_version_tag("production")
.create_consumer_version_tag("feat-x")
.publish_pact
.create_consumer("Some other app")
.create_provider("A service")
Expand Down
2 changes: 1 addition & 1 deletion spec/lib/pact_broker/domain/relationship_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ module Domain
allow(webhook_executions).to receive(:sort).and_return(webhook_executions)
end

subject { Relationship.create(nil, nil, nil, nil, [], webhook_executions) }
subject { Relationship.create(nil, nil, nil, true, nil, [], webhook_executions) }

it "returns the created_at date of the last execution" do
expect(subject.last_webhook_execution_date).to eq DateTime.new(2015)
Expand Down
Loading

0 comments on commit b58b7a3

Please sign in to comment.