Skip to content

Commit b58b7a3

Browse files
committed
feat(prod pacts in index): show pacts tagged as the 'prod' or 'production' versions on the index page
1 parent 912f5dc commit b58b7a3

File tree

16 files changed

+297
-39
lines changed

16 files changed

+297
-39
lines changed

lib/pact_broker/domain/relationship.rb

+24-4
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,26 @@ class Relationship
77

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

10-
def initialize consumer, provider, latest_pact = nil, latest_verification = nil, webhooks = [], triggered_webhooks = []
10+
def initialize consumer, provider, latest_pact = nil, latest = true, latest_verification = nil, webhooks = [], triggered_webhooks = [], tags = []
1111
@consumer = consumer
1212
@provider = provider
1313
@latest_pact = latest_pact
14+
@latest = latest
1415
@latest_verification = latest_verification
1516
@webhooks = webhooks
1617
@triggered_webhooks = triggered_webhooks
18+
@tags = tags
1719
end
1820

19-
def self.create consumer, provider, latest_pact, latest_verification, webhooks = [], triggered_webhooks = []
20-
new consumer, provider, latest_pact, latest_verification, webhooks, triggered_webhooks
21+
def self.create consumer, provider, latest_pact, latest, latest_verification, webhooks = [], triggered_webhooks = [], tags = []
22+
new consumer, provider, latest_pact, latest, latest_verification, webhooks, triggered_webhooks, tags
2123
end
2224

2325
def eq? other
2426
Relationship === other && other.consumer == consumer && other.provider == provider &&
25-
other.latest_pact == latest_pact && other.latest_verification == latest_verification &&
27+
other.latest_pact == latest_pact &&
28+
other.latest? == latest? &&
29+
other.latest_verification == latest_verification &&
2630
other.webhooks == webhooks
2731
end
2832

@@ -42,6 +46,22 @@ def latest_pact
4246
@latest_pact
4347
end
4448

49+
def latest?
50+
@latest
51+
end
52+
53+
def consumer_version_number
54+
@latest_pact.consumer_version_number
55+
end
56+
57+
def provider_version_number
58+
@latest_verification ? @latest_verification.provider_version_number : nil
59+
end
60+
61+
def tag_names
62+
@tags
63+
end
64+
4565
def any_webhooks?
4666
@webhooks.any?
4767
end

lib/pact_broker/pacticipants/service.rb

+21-5
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,27 @@ def self.find_pacticipant_repository_url_by_pacticipant_name name
6262
def self.find_relationships
6363
pact_repository.find_latest_pacts
6464
.collect do | pact|
65-
latest_verification = verification_service.find_latest_verification_for(pact.consumer, pact.provider)
66-
webhooks = webhook_service.find_by_consumer_and_provider pact.consumer, pact.provider
67-
triggered_webhooks = webhook_service.find_latest_triggered_webhooks pact.consumer, pact.provider
68-
PactBroker::Domain::Relationship.create pact.consumer, pact.provider, pact, latest_verification, webhooks, triggered_webhooks
69-
end
65+
latest_relationship = build_latest_pact_relationship(pact)
66+
prod_relationship = build_relationship_for_tagged_pact(pact, 'prod')
67+
production_relationship = build_relationship_for_tagged_pact(pact, 'production')
68+
[latest_relationship, prod_relationship, production_relationship].compact
69+
end.flatten
70+
end
71+
72+
def self.build_latest_pact_relationship pact
73+
latest_verification = verification_service.find_latest_verification_for(pact.consumer, pact.provider)
74+
webhooks = webhook_service.find_by_consumer_and_provider pact.consumer, pact.provider
75+
triggered_webhooks = webhook_service.find_latest_triggered_webhooks pact.consumer, pact.provider
76+
tag_names = pact.consumer_version_tag_names.select{ |name| name == 'prod' || name == 'production' }
77+
PactBroker::Domain::Relationship.create pact.consumer, pact.provider, pact, true, latest_verification, webhooks, triggered_webhooks, tag_names
78+
end
79+
80+
def self.build_relationship_for_tagged_pact latest_pact, tag
81+
pact = pact_service.find_latest_pact consumer_name: latest_pact.consumer_name, provider_name: latest_pact.provider_name, tag: tag
82+
return nil unless pact
83+
return nil if pact.id == latest_pact.id
84+
verification = verification_repository.find_latest_verification_for pact.consumer_name, pact.provider_name, tag
85+
PactBroker::Domain::Relationship.create pact.consumer, pact.provider, pact, false, verification, [], [], [tag]
7086
end
7187

7288
def self.update params

lib/pact_broker/pacts/pact_publication.rb

+4
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ def before_create
2222
self.revision_number ||= 1
2323
end
2424

25+
def latest_tag_names
26+
LatestTaggedPactPublications.where(id: id).select(:tag_name).collect{|t| t[:tag_name]}
27+
end
28+
2529
def to_domain
2630
PactBroker::Domain::Pact.new(
2731
id: id,

lib/pact_broker/pacts/repository.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ def find_by_version_and_provider version_id, provider_id
8686
end
8787

8888
def find_latest_pacts
89-
LatestPactPublications.order(:consumer_name, :provider_name).collect(&:to_domain_without_tags)
89+
LatestPactPublications.order(:consumer_name, :provider_name).collect(&:to_domain)
9090
end
9191

9292
def find_latest_pact(consumer_name, provider_name, tag = nil)

lib/pact_broker/tags/service.rb

-1
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,4 @@ def delete args
2727

2828
end
2929
end
30-
3130
end

lib/pact_broker/ui/controllers/relationships.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ class Relationships < Base
1111

1212
get "/" do
1313
view_model = ViewDomain::Relationships.new(pacticipant_service.find_relationships)
14-
15-
haml :'relationships/show', {locals: {relationships: view_model, title: "Pacts"}, layout: :'layouts/main'}
14+
page = params[:showProdPacts] == 'true' ? :'relationships/show-prod-tags' : :'relationships/show'
15+
haml page, {locals: {relationships: view_model, title: "Pacts"}, layout: :'layouts/main'}
1616
end
1717

1818
end

lib/pact_broker/ui/view_models/relationship.rb

+19-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,18 @@ def provider_name
2121
@relationship.provider_name
2222
end
2323

24+
def consumer_version_number
25+
@relationship.consumer_version_number
26+
end
27+
28+
def provider_version_number
29+
@relationship.provider_version_number
30+
end
31+
32+
def tag_names
33+
@relationship.tag_names.any? ? " (#{@relationship.tag_names.join(', ')}) ": ""
34+
end
35+
2436
def consumer_group_url
2537
Helpers::URLHelper.group_url consumer_name
2638
end
@@ -29,7 +41,7 @@ def provider_group_url
2941
Helpers::URLHelper.group_url provider_name
3042
end
3143

32-
def latest_pact_url
44+
def pact_url
3345
"#{pactigration_base_url('', @relationship)}/latest"
3446
end
3547

@@ -38,6 +50,7 @@ def any_webhooks?
3850
end
3951

4052
def webhook_label
53+
return "" unless show_webhook_status?
4154
case @relationship.webhook_status
4255
when :none then "Create"
4356
when :success, :failure then webhook_last_execution_date
@@ -47,6 +60,7 @@ def webhook_label
4760
end
4861

4962
def webhook_status
63+
return "" unless show_webhook_status?
5064
case @relationship.webhook_status
5165
when :success then "success"
5266
when :failure then "danger"
@@ -55,6 +69,10 @@ def webhook_status
5569
end
5670
end
5771

72+
def show_webhook_status?
73+
@relationship.latest?
74+
end
75+
5876
def webhook_last_execution_date
5977
PactBroker::DateHelper.distance_of_time_in_words(@relationship.last_webhook_execution_date, DateTime.now) + " ago"
6078
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
%body
2+
%link{rel: 'stylesheet', href: '/css/bootstrap.min.css'}
3+
%link{rel: 'stylesheet', href: '/stylesheets/relationships.css'}
4+
%script{type: 'text/javascript', src:'/javascripts/jquery-2.1.1.min.js'}
5+
%script{type: 'text/javascript', src:'/javascripts/jquery.tablesorter.min.js'}
6+
%script{type: 'text/javascript', src:'/js/bootstrap.min.js'}
7+
%nav.navbase-default.navbar-right{role: "navigation"}
8+
.container
9+
%ul
10+
%li.navbar-right
11+
%a{href: '/hal-browser/browser.html'}
12+
HAL Browser
13+
.container
14+
%h1.page-header
15+
Pacts
16+
%table.table.table-bordered.table-striped{id: 'relationships'}
17+
%thead
18+
%th.consumer
19+
Consumer
20+
%span.glyphicon.glyphicon-sort.relationships-sort
21+
%th.tag
22+
Version
23+
%span.glyphicon.glyphicon-sort.relationships-sort
24+
%th.pact
25+
%th.provider
26+
Provider
27+
%span.glyphicon.glyphicon-sort.relationships-sort
28+
%th.tag
29+
Version
30+
%span.glyphicon.glyphicon-sort.relationships-sort
31+
%th
32+
Published
33+
%th
34+
Webhook<br>status
35+
%th
36+
Last<br>verified
37+
%tbody
38+
39+
- relationships.each do | relationship |
40+
%tr
41+
%td.consumer
42+
%a{:href => relationship.consumer_group_url}
43+
= relationship.consumer_name
44+
%td
45+
= relationship.consumer_version_number
46+
%span{style: 'color:gray'}
47+
= relationship.tag_names
48+
%td.pact
49+
%a{:href => relationship.pact_url, :title => "View pact"}
50+
%span.pact
51+
%td.provider
52+
%a{:href => relationship.provider_group_url}
53+
= relationship.provider_name
54+
%td
55+
= relationship.provider_version_number
56+
%td
57+
= relationship.publication_date_of_latest_pact.gsub("about ", "")
58+
%td{class: relationship.webhook_status}
59+
- if relationship.show_webhook_status?
60+
%a{:href => relationship.webhook_url}
61+
= relationship.webhook_label
62+
63+
%td{class: relationship.verification_status, title: relationship.verification_tooltip, "data-toggle": "tooltip", "data-placement": "left"}
64+
%div
65+
= relationship.last_verified_date.gsub("about ", "")
66+
- if relationship.warning?
67+
%span.glyphicon.glyphicon-warning-sign{'aria-hidden':true}
68+
%div.relationships-size
69+
= relationships.size_label
70+
71+
:javascript
72+
$(function(){
73+
$("#relationships").tablesorter();
74+
});
75+
76+
$(document).ready(function(){
77+
$("span.pact").load("/images/doc-text.svg");
78+
$('td[data-toggle="tooltip"]').each(function(index, td){
79+
//appended tooltip div screws up table if it's appended after a
80+
//td, so need to append it to a div
81+
$(td).tooltip({container: $(td).first()});
82+
});
83+
});

lib/pact_broker/ui/views/relationships/show.haml

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
%a{:href => relationship.consumer_group_url}
4141
= relationship.consumer_name
4242
%td.pact
43-
%a{:href => relationship.latest_pact_url, :title => "View pact"}
43+
%a{:href => relationship.pact_url, :title => "View pact"}
4444
%span.pact
4545
%td.provider
4646
%a{:href => relationship.provider_group_url}

lib/pact_broker/verifications/repository.rb

+3
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ def find_latest_verifications_for_consumer_version consumer_name, consumer_versi
4343
.all
4444
end
4545

46+
# The most recent verification for the latest revision of the pact
47+
# belonging to the version with the largest consumer_version_order.
48+
4649
def find_latest_verification_for consumer_name, provider_name, tag = nil
4750
query = LatestVerificationsByConsumerVersion
4851
.select_all_qualified

script/seed.rb

+5
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,12 @@ def publish_pact params = {}
5959
.create_verification(provider_version: "1.4.234", success: true, execution_date: DateTime.now - 15)
6060
.revise_pact
6161
.create_consumer_version("1.2.101")
62+
.create_consumer_version_tag('prod')
6263
.publish_pact
64+
.create_verification(provider_version: "9.9.10", success: false, execution_date: DateTime.now - 15)
6365
.create_consumer_version("1.2.102")
6466
.publish_pact(created_at: (Date.today - 7).to_datetime)
67+
.create_verification(provider_version: "9.9.9", success: true, execution_date: DateTime.now - 14)
6568
.create_provider("Animals")
6669
.create_webhook(method: 'GET', url: 'http://localhost:9393/')
6770
.publish_pact(created_at: (Time.now - 140).to_datetime)
@@ -76,6 +79,8 @@ def publish_pact params = {}
7679
.create_provider("The back end")
7780
.create_webhook(method: 'GET', url: 'http://localhost:9393/')
7881
.create_consumer_version("1.2.106")
82+
.create_consumer_version_tag("production")
83+
.create_consumer_version_tag("feat-x")
7984
.publish_pact
8085
.create_consumer("Some other app")
8186
.create_provider("A service")

spec/lib/pact_broker/domain/relationship_spec.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ module Domain
1313
allow(webhook_executions).to receive(:sort).and_return(webhook_executions)
1414
end
1515

16-
subject { Relationship.create(nil, nil, nil, nil, [], webhook_executions) }
16+
subject { Relationship.create(nil, nil, nil, true, nil, [], webhook_executions) }
1717

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

0 commit comments

Comments
 (0)