Skip to content

Commit cce7cd0

Browse files
committed
feat(badge): include tag names in matrix badge
1 parent e8ec410 commit cce7cd0

File tree

13 files changed

+107
-28
lines changed

13 files changed

+107
-28
lines changed

lib/pact_broker/api/pact_broker_urls.rb

+4
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,10 @@ def matrix_url consumer_name, provider_name, base_url = ''
253253
"/matrix/provider/#{url_encode(provider_name)}/consumer/#{url_encode(consumer_name)}"
254254
end
255255

256+
def matrix_badge_url_for_selectors consumer_selector, provider_selector, base_url = ''
257+
"#{base_url}/matrix/provider/#{url_encode(provider_selector.pacticipant_name)}/latest/#{url_encode(provider_selector.tag)}/consumer/#{url_encode(consumer_selector.pacticipant_name)}/latest/#{url_encode(consumer_selector.tag)}/badge.svg"
258+
end
259+
256260
def matrix_for_pacticipant_version_url(version, base_url = '')
257261
query = {
258262
q: [{ pacticipant: version.pacticipant.name, version: version.number }],

lib/pact_broker/api/resources/badge.rb

+6-2
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,12 @@ def forbidden?
3434

3535
def to_svg
3636
response.headers['Cache-Control'] = 'no-cache'
37-
comment + badge_service.pact_verification_badge(pact, label, initials, pseudo_branch_verification_status)
37+
comment + badge_service.pact_verification_badge(pact, label, initials, pseudo_branch_verification_status, tags)
3838
end
3939

4040
def moved_temporarily?
4141
response.headers['Cache-Control'] = 'no-cache'
42-
badge_service.pact_verification_badge_url(pact, label, initials, pseudo_branch_verification_status)
42+
badge_service.pact_verification_badge_url(pact, label, initials, pseudo_branch_verification_status, tags)
4343
end
4444

4545
private
@@ -72,6 +72,10 @@ def comment
7272
verification_number = latest_verification ? latest_verification.number : "?"
7373
"<!-- #{identifier_from_path[:consumer_name]} version #{consumer_version_number} revision #{pact_revision} #{identifier_from_path[:provider_name]} version #{provider_version_number} number #{verification_number} -->\n"
7474
end
75+
76+
def tags
77+
{}
78+
end
7579
end
7680
end
7781
end

lib/pact_broker/api/resources/matrix_badge.rb

+4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ class MatrixBadge < Badge
77

88
private
99

10+
def tags
11+
{ consumer_tag: identifier_from_path[:tag], provider_tag: identifier_from_path[:provider_tag] }
12+
end
13+
1014
def latest_verification
1115
return nil unless pact
1216
@latest_verification ||= verification_service.find_latest_verification_for_tags(

lib/pact_broker/badges/service.rb

+16-13
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,14 @@ def can_provide_badge_using_redirect?
1919
PactBroker.configuration.badge_provider_mode == :redirect && !!PactBroker.configuration.shields_io_base_url
2020
end
2121

22-
def pact_verification_badge pact, label, initials, pseudo_branch_verification_status
22+
def pact_verification_badge pact, label, initials, pseudo_branch_verification_status, metadata = {}
2323
return static_svg(pact, pseudo_branch_verification_status) unless pact
2424

25-
dynamic_svg(pact, label, initials, pseudo_branch_verification_status) || static_svg(pact, pseudo_branch_verification_status)
25+
dynamic_svg(pact, label, initials, pseudo_branch_verification_status, metadata) || static_svg(pact, pseudo_branch_verification_status)
2626
end
2727

28-
def pact_verification_badge_url(pact, label, initials, pseudo_branch_verification_status)
29-
title = badge_title(pact, label, initials)
28+
def pact_verification_badge_url(pact, label, initials, pseudo_branch_verification_status, metadata = {})
29+
title = badge_title(pact, label, initials, metadata)
3030
status = badge_status(pseudo_branch_verification_status)
3131
color = badge_color(pseudo_branch_verification_status)
3232
build_shield_io_uri(title, status, color)
@@ -38,23 +38,26 @@ def clear_cache
3838

3939
private
4040

41-
def badge_title pact, label, initials
41+
def badge_title pact, label, initials, metadata
4242
return 'pact not found' if pact.nil?
43+
consumer_name = prepare_name(pact.consumer_name, initials, metadata[:consumer_tag])
44+
provider_name = prepare_name(pact.provider_name, initials, metadata[:provider_tag])
4345
title = case (label || '').downcase
44-
when 'consumer' then prepare_name(pact.consumer_name, initials)
45-
when 'provider' then prepare_name(pact.provider_name, initials)
46-
else "#{prepare_name(pact.consumer_name, initials)}%2F#{prepare_name(pact.provider_name, initials)}"
46+
when 'consumer' then consumer_name
47+
when 'provider' then provider_name
48+
else "#{consumer_name}%2F#{provider_name}"
4749
end
4850
"#{title} pact".downcase
4951
end
5052

51-
def prepare_name name, initials
53+
def prepare_name name, initials, tag = nil
54+
tag_suffix = tag ? " (#{tag})" : ''
5255
if initials
5356
parts = split_space_dash_underscore(name)
5457
parts = split_camel_case(name) if parts.size == 1
55-
return parts.collect{ |p| p[0] }.join.downcase if parts.size > 1
58+
return parts.collect{ |p| p[0] }.join.downcase + tag_suffix if parts.size > 1
5659
end
57-
name.downcase
60+
name.downcase + tag_suffix
5861
end
5962

6063
def split_space_dash_underscore name
@@ -86,9 +89,9 @@ def badge_color pseudo_branch_verification_status
8689
end
8790
end
8891

89-
def dynamic_svg pact, label, initials, pseudo_branch_verification_status
92+
def dynamic_svg pact, label, initials, pseudo_branch_verification_status, metadata
9093
return nil unless PactBroker.configuration.shields_io_base_url
91-
uri = pact_verification_badge_url(pact, label, initials, pseudo_branch_verification_status)
94+
uri = pact_verification_badge_url(pact, label, initials, pseudo_branch_verification_status, metadata)
9295
begin
9396
response = do_request(uri)
9497
response.code == '200' ? response.body : nil

lib/pact_broker/matrix/unresolved_selector.rb

+4
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ def max_age= max_age
4848
def max_age
4949
self[:max_age]
5050
end
51+
52+
def latest_for_pacticipant_and_tag?
53+
!!(pacticipant_name && tag && latest)
54+
end
5155
end
5256
end
5357
end

lib/pact_broker/ui/controllers/base_controller.rb

+3
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ class Base < Padrino::Application
1111
set :show_exceptions, ENV['RACK_ENV'] != 'production'
1212
set :dump_errors, false # The padrino logger logs these for us. If this is enabled we get duplicate logging.
1313

14+
def base_url
15+
PactBroker.configuration.base_url || request.base_url
16+
end
1417
end
1518
end
1619
end

lib/pact_broker/ui/controllers/matrix.rb

+15-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
require 'pact_broker/matrix/unresolved_selector'
44
require 'pact_broker/matrix/parse_query'
55
require 'pact_broker/logging'
6+
require 'pact_broker/api/pact_broker_urls'
7+
68
require 'haml'
79

810
module PactBroker
@@ -30,6 +32,7 @@ class Matrix < Base
3032
if errors.empty?
3133
lines = matrix_service.find(selectors, options)
3234
locals[:lines] = PactBroker::UI::ViewDomain::MatrixLines.new(lines)
35+
locals[:badge_url] = matrix_badge_url(selectors, lines)
3336
else
3437
locals[:errors] = errors
3538
end
@@ -52,7 +55,8 @@ class Matrix < Base
5255
consumer_name: params[:consumer_name],
5356
provider_name: params[:provider_name],
5457
selectors: create_selector_objects(selectors),
55-
options: create_options_model(options)
58+
options: create_options_model(options),
59+
badge_url: nil
5660
}
5761
haml :'matrix/show', {locals: locals, layout: :'layouts/main'}
5862
end
@@ -76,6 +80,16 @@ def create_options_model(options)
7680
o.all_rows_checked = o.latestby.nil? ? 'checked' : nil
7781
o
7882
end
83+
84+
def matrix_badge_url(selectors, lines)
85+
if lines.any? && selectors.size == 2 && selectors.all?{ | selector| selector.latest_for_pacticipant_and_tag? }
86+
consumer_selector = selectors.find{ | selector| selector.pacticipant_name == lines.first.consumer_name }
87+
provider_selector = selectors.find{ | selector| selector.pacticipant_name == lines.first.provider_name }
88+
if consumer_selector && provider_selector
89+
PactBroker::Api::PactBrokerUrls.matrix_badge_url_for_selectors(consumer_selector, provider_selector, base_url)
90+
end
91+
end
92+
end
7993
end
8094
end
8195
end

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

+4-1
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,21 @@
1414
Home
1515
%h1.page-header
1616
= title
17+
- if defined?(badge_url) && badge_url
18+
%img{src: badge_url, class: 'pact_badge' }
1719

1820
- if defined?(errors) && errors.any?
1921
- errors.each do | error |
2022
%div.alert.alert-danger
2123
= escape_html(error)
2224

25+
2326
%form{action: '/matrix', onsubmit:'return onSubmit()'}
2427
- selectors.each_with_index do | selector, index |
2528
.selector
2629
%label{for: "pacticipant#{index}"}
2730
Pacticipant name
28-
%input{name: 'q[]pacticipant', id: "pacticipant1#{index}", value: escape_html(selector.pacticipant_name)}
31+
%input{name: 'q[]pacticipant', id: "pacticipant1#{index}", class: 'pacticipant_name', value: escape_html(selector.pacticipant_name)}
2932

3033
.input-group
3134

public/stylesheets/matrix.css

+13
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,16 @@ span.pre-verified-icon {
2828
td.pact-published .tooltip-inner {
2929
max-width: 300px;
3030
}
31+
32+
input.pacticipant_name {
33+
width: 250px;
34+
}
35+
36+
input.tag {
37+
width: 250px;
38+
}
39+
40+
img.pact_badge {
41+
float: right;
42+
margin-top: 15px;
43+
}

spec/integration/ui/matrix_spec.rb

+11-4
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,9 @@
66
let(:params) { {} }
77

88
before do
9-
td.create_pact_with_hierarchy("Foo", "1", "Bar")
10-
.create_consumer_version_tag("prod")
11-
.create_consumer_version("2")
12-
.create_pact
9+
td.create_pact_with_verification("Foo", "1", "Bar", "2")
10+
.create_consumer_version_tag("ctag")
11+
.create_provider_version_tag("ptag")
1312
end
1413

1514
subject { get("/matrix/provider/Bar/consumer/Foo") }
@@ -27,4 +26,12 @@
2726
expect(subject.body.scan('<tr').to_a.count).to be > 1
2827
end
2928
end
29+
30+
describe "with query params, for the latest tagged versions of two pacticipants" do
31+
subject { get("/matrix?q%5B%5Dpacticipant=Foo&q%5B%5Dtag=ctag&q%5B%5Dlatest=true&q%5B%5Dpacticipant=Bar&q%5B%5Dtag=ptag&q%5B%5Dlatest=true&latestby=cvpv&limit=100") }
32+
33+
it "returns a page with a badge" do
34+
expect(subject.body).to include "http://example.org/matrix/provider/Bar/latest/ptag/consumer/Foo/latest/ctag/badge.svg"
35+
end
36+
end
3037
end

spec/lib/pact_broker/api/pact_broker_urls_spec.rb

+8
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,14 @@ module Api
138138

139139
it { is_expected.to eq "http://example.org/matrix?q[][pacticipant]=Foo%2FFoo&q[][version]=2&latestby=cvpv" }
140140
end
141+
142+
describe "matrix_badge_url" do
143+
subject { PactBrokerUrls.matrix_badge_url_for_selectors(consumer_selector, provider_selector, base_url) }
144+
let(:provider_selector) { PactBroker::Matrix::UnresolvedSelector.new(pacticipant_name: provider_name, tag: "meep", latest: true) }
145+
let(:consumer_selector) { PactBroker::Matrix::UnresolvedSelector.new(pacticipant_name: consumer_name, tag: "bar", latest: true) }
146+
147+
it { is_expected.to eq "http://example.org/matrix/provider/Bar%2FBar/latest/meep/consumer/Foo%2FFoo/latest/bar/badge.svg" }
148+
end
141149
end
142150
end
143151
end

spec/lib/pact_broker/api/resources/badge_spec.rb

+4-4
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ module Resources
8383
end
8484

8585
it "creates a badge" do
86-
expect(PactBroker::Badges::Service).to receive(:pact_verification_badge).with(pact, nil, false, :verified)
86+
expect(PactBroker::Badges::Service).to receive(:pact_verification_badge).with(pact, nil, false, :verified, {})
8787
subject
8888
end
8989

@@ -110,7 +110,7 @@ module Resources
110110
end
111111

112112
it "determines the URL of the badge to redirect to" do
113-
expect(PactBroker::Badges::Service).to receive(:pact_verification_badge_url).with(pact, nil, false, :verified)
113+
expect(PactBroker::Badges::Service).to receive(:pact_verification_badge_url).with(pact, nil, false, :verified, {})
114114
subject
115115
end
116116

@@ -125,7 +125,7 @@ module Resources
125125
let(:params) { {label: 'consumer'} }
126126

127127
it "creates a badge with the specified label" do
128-
expect(PactBroker::Badges::Service).to receive(:pact_verification_badge).with(anything, 'consumer', anything, anything)
128+
expect(PactBroker::Badges::Service).to receive(:pact_verification_badge).with(anything, 'consumer', anything, anything, {})
129129
subject
130130
end
131131
end
@@ -134,7 +134,7 @@ module Resources
134134
let(:params) { {initials: 'true'} }
135135

136136
it "creates a badge with initials" do
137-
expect(PactBroker::Badges::Service).to receive(:pact_verification_badge).with(anything, anything, true, anything)
137+
expect(PactBroker::Badges::Service).to receive(:pact_verification_badge).with(anything, anything, true, anything, {})
138138
subject
139139
end
140140
end

spec/lib/pact_broker/badges/service_spec.rb

+15-3
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,11 @@ module Badges
1818
let!(:http_request) do
1919
stub_request(:get, expected_url).to_return(:status => response_status, :body => "svg")
2020
end
21+
let(:tags) { {} }
2122

22-
subject { PactBroker::Badges::Service.pact_verification_badge pact, label, initials, pseudo_branch_verification_status }
23+
subject { PactBroker::Badges::Service.pact_verification_badge(pact, label, initials, pseudo_branch_verification_status, tags) }
2324

24-
let(:pact_verification_badge_url) { PactBroker::Badges::Service.pact_verification_badge_url(pact, label, initials, pseudo_branch_verification_status) }
25+
let(:pact_verification_badge_url) { PactBroker::Badges::Service.pact_verification_badge_url(pact, label, initials, pseudo_branch_verification_status, tags) }
2526

2627
before do
2728
Service.clear_cache
@@ -43,7 +44,6 @@ module Badges
4344
end
4445

4546
describe "#pact_verification_badge" do
46-
4747
it "returns the svg file" do
4848
expect(subject).to eq "svg"
4949
end
@@ -107,6 +107,18 @@ module Badges
107107
expect(pact_verification_badge_url).to eq URI(expected_url)
108108
end
109109
end
110+
111+
context "when the tags are supplied" do
112+
let(:tags) { { consumer_tag: "prod", provider_tag: "master" } }
113+
114+
let(:expected_left_text) { "foo--bar%20(prod)%2fthing__blah%20(master)%20pact" }
115+
116+
it "creates a badge with the consumer and provider names, not initials" do
117+
subject
118+
expect(http_request).to have_been_made
119+
expect(pact_verification_badge_url).to eq URI(expected_url)
120+
end
121+
end
110122
end
111123

112124
context "when label is consumer" do

0 commit comments

Comments
 (0)