Skip to content

Commit

Permalink
feat(badge): include tag names in matrix badge
Browse files Browse the repository at this point in the history
  • Loading branch information
bethesque committed May 1, 2020
1 parent e8ec410 commit cce7cd0
Show file tree
Hide file tree
Showing 13 changed files with 107 additions and 28 deletions.
4 changes: 4 additions & 0 deletions lib/pact_broker/api/pact_broker_urls.rb
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,10 @@ def matrix_url consumer_name, provider_name, base_url = ''
"/matrix/provider/#{url_encode(provider_name)}/consumer/#{url_encode(consumer_name)}"
end

def matrix_badge_url_for_selectors consumer_selector, provider_selector, base_url = ''
"#{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"
end

def matrix_for_pacticipant_version_url(version, base_url = '')
query = {
q: [{ pacticipant: version.pacticipant.name, version: version.number }],
Expand Down
8 changes: 6 additions & 2 deletions lib/pact_broker/api/resources/badge.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ def forbidden?

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

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

private
Expand Down Expand Up @@ -72,6 +72,10 @@ def comment
verification_number = latest_verification ? latest_verification.number : "?"
"<!-- #{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"
end

def tags
{}
end
end
end
end
Expand Down
4 changes: 4 additions & 0 deletions lib/pact_broker/api/resources/matrix_badge.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ class MatrixBadge < Badge

private

def tags
{ consumer_tag: identifier_from_path[:tag], provider_tag: identifier_from_path[:provider_tag] }
end

def latest_verification
return nil unless pact
@latest_verification ||= verification_service.find_latest_verification_for_tags(
Expand Down
29 changes: 16 additions & 13 deletions lib/pact_broker/badges/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ def can_provide_badge_using_redirect?
PactBroker.configuration.badge_provider_mode == :redirect && !!PactBroker.configuration.shields_io_base_url
end

def pact_verification_badge pact, label, initials, pseudo_branch_verification_status
def pact_verification_badge pact, label, initials, pseudo_branch_verification_status, metadata = {}
return static_svg(pact, pseudo_branch_verification_status) unless pact

dynamic_svg(pact, label, initials, pseudo_branch_verification_status) || static_svg(pact, pseudo_branch_verification_status)
dynamic_svg(pact, label, initials, pseudo_branch_verification_status, metadata) || static_svg(pact, pseudo_branch_verification_status)
end

def pact_verification_badge_url(pact, label, initials, pseudo_branch_verification_status)
title = badge_title(pact, label, initials)
def pact_verification_badge_url(pact, label, initials, pseudo_branch_verification_status, metadata = {})
title = badge_title(pact, label, initials, metadata)
status = badge_status(pseudo_branch_verification_status)
color = badge_color(pseudo_branch_verification_status)
build_shield_io_uri(title, status, color)
Expand All @@ -38,23 +38,26 @@ def clear_cache

private

def badge_title pact, label, initials
def badge_title pact, label, initials, metadata
return 'pact not found' if pact.nil?
consumer_name = prepare_name(pact.consumer_name, initials, metadata[:consumer_tag])
provider_name = prepare_name(pact.provider_name, initials, metadata[:provider_tag])
title = case (label || '').downcase
when 'consumer' then prepare_name(pact.consumer_name, initials)
when 'provider' then prepare_name(pact.provider_name, initials)
else "#{prepare_name(pact.consumer_name, initials)}%2F#{prepare_name(pact.provider_name, initials)}"
when 'consumer' then consumer_name
when 'provider' then provider_name
else "#{consumer_name}%2F#{provider_name}"
end
"#{title} pact".downcase
end

def prepare_name name, initials
def prepare_name name, initials, tag = nil
tag_suffix = tag ? " (#{tag})" : ''
if initials
parts = split_space_dash_underscore(name)
parts = split_camel_case(name) if parts.size == 1
return parts.collect{ |p| p[0] }.join.downcase if parts.size > 1
return parts.collect{ |p| p[0] }.join.downcase + tag_suffix if parts.size > 1
end
name.downcase
name.downcase + tag_suffix
end

def split_space_dash_underscore name
Expand Down Expand Up @@ -86,9 +89,9 @@ def badge_color pseudo_branch_verification_status
end
end

def dynamic_svg pact, label, initials, pseudo_branch_verification_status
def dynamic_svg pact, label, initials, pseudo_branch_verification_status, metadata
return nil unless PactBroker.configuration.shields_io_base_url
uri = pact_verification_badge_url(pact, label, initials, pseudo_branch_verification_status)
uri = pact_verification_badge_url(pact, label, initials, pseudo_branch_verification_status, metadata)
begin
response = do_request(uri)
response.code == '200' ? response.body : nil
Expand Down
4 changes: 4 additions & 0 deletions lib/pact_broker/matrix/unresolved_selector.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ def max_age= max_age
def max_age
self[:max_age]
end

def latest_for_pacticipant_and_tag?
!!(pacticipant_name && tag && latest)
end
end
end
end
3 changes: 3 additions & 0 deletions lib/pact_broker/ui/controllers/base_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ class Base < Padrino::Application
set :show_exceptions, ENV['RACK_ENV'] != 'production'
set :dump_errors, false # The padrino logger logs these for us. If this is enabled we get duplicate logging.

def base_url
PactBroker.configuration.base_url || request.base_url
end
end
end
end
Expand Down
16 changes: 15 additions & 1 deletion lib/pact_broker/ui/controllers/matrix.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
require 'pact_broker/matrix/unresolved_selector'
require 'pact_broker/matrix/parse_query'
require 'pact_broker/logging'
require 'pact_broker/api/pact_broker_urls'

require 'haml'

module PactBroker
Expand Down Expand Up @@ -30,6 +32,7 @@ class Matrix < Base
if errors.empty?
lines = matrix_service.find(selectors, options)
locals[:lines] = PactBroker::UI::ViewDomain::MatrixLines.new(lines)
locals[:badge_url] = matrix_badge_url(selectors, lines)
else
locals[:errors] = errors
end
Expand All @@ -52,7 +55,8 @@ class Matrix < Base
consumer_name: params[:consumer_name],
provider_name: params[:provider_name],
selectors: create_selector_objects(selectors),
options: create_options_model(options)
options: create_options_model(options),
badge_url: nil
}
haml :'matrix/show', {locals: locals, layout: :'layouts/main'}
end
Expand All @@ -76,6 +80,16 @@ def create_options_model(options)
o.all_rows_checked = o.latestby.nil? ? 'checked' : nil
o
end

def matrix_badge_url(selectors, lines)
if lines.any? && selectors.size == 2 && selectors.all?{ | selector| selector.latest_for_pacticipant_and_tag? }
consumer_selector = selectors.find{ | selector| selector.pacticipant_name == lines.first.consumer_name }
provider_selector = selectors.find{ | selector| selector.pacticipant_name == lines.first.provider_name }
if consumer_selector && provider_selector
PactBroker::Api::PactBrokerUrls.matrix_badge_url_for_selectors(consumer_selector, provider_selector, base_url)
end
end
end
end
end
end
Expand Down
5 changes: 4 additions & 1 deletion lib/pact_broker/ui/views/matrix/show.haml
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,21 @@
Home
%h1.page-header
= title
- if defined?(badge_url) && badge_url
%img{src: badge_url, class: 'pact_badge' }

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


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

.input-group

Expand Down
13 changes: 13 additions & 0 deletions public/stylesheets/matrix.css
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,16 @@ span.pre-verified-icon {
td.pact-published .tooltip-inner {
max-width: 300px;
}

input.pacticipant_name {
width: 250px;
}

input.tag {
width: 250px;
}

img.pact_badge {
float: right;
margin-top: 15px;
}
15 changes: 11 additions & 4 deletions spec/integration/ui/matrix_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@
let(:params) { {} }

before do
td.create_pact_with_hierarchy("Foo", "1", "Bar")
.create_consumer_version_tag("prod")
.create_consumer_version("2")
.create_pact
td.create_pact_with_verification("Foo", "1", "Bar", "2")
.create_consumer_version_tag("ctag")
.create_provider_version_tag("ptag")
end

subject { get("/matrix/provider/Bar/consumer/Foo") }
Expand All @@ -27,4 +26,12 @@
expect(subject.body.scan('<tr').to_a.count).to be > 1
end
end

describe "with query params, for the latest tagged versions of two pacticipants" do
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") }

it "returns a page with a badge" do
expect(subject.body).to include "http://example.org/matrix/provider/Bar/latest/ptag/consumer/Foo/latest/ctag/badge.svg"
end
end
end
8 changes: 8 additions & 0 deletions spec/lib/pact_broker/api/pact_broker_urls_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,14 @@ module Api

it { is_expected.to eq "http://example.org/matrix?q[][pacticipant]=Foo%2FFoo&q[][version]=2&latestby=cvpv" }
end

describe "matrix_badge_url" do
subject { PactBrokerUrls.matrix_badge_url_for_selectors(consumer_selector, provider_selector, base_url) }
let(:provider_selector) { PactBroker::Matrix::UnresolvedSelector.new(pacticipant_name: provider_name, tag: "meep", latest: true) }
let(:consumer_selector) { PactBroker::Matrix::UnresolvedSelector.new(pacticipant_name: consumer_name, tag: "bar", latest: true) }

it { is_expected.to eq "http://example.org/matrix/provider/Bar%2FBar/latest/meep/consumer/Foo%2FFoo/latest/bar/badge.svg" }
end
end
end
end
8 changes: 4 additions & 4 deletions spec/lib/pact_broker/api/resources/badge_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ module Resources
end

it "creates a badge" do
expect(PactBroker::Badges::Service).to receive(:pact_verification_badge).with(pact, nil, false, :verified)
expect(PactBroker::Badges::Service).to receive(:pact_verification_badge).with(pact, nil, false, :verified, {})
subject
end

Expand All @@ -110,7 +110,7 @@ module Resources
end

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

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

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

it "creates a badge with initials" do
expect(PactBroker::Badges::Service).to receive(:pact_verification_badge).with(anything, anything, true, anything)
expect(PactBroker::Badges::Service).to receive(:pact_verification_badge).with(anything, anything, true, anything, {})
subject
end
end
Expand Down
18 changes: 15 additions & 3 deletions spec/lib/pact_broker/badges/service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@ module Badges
let!(:http_request) do
stub_request(:get, expected_url).to_return(:status => response_status, :body => "svg")
end
let(:tags) { {} }

subject { PactBroker::Badges::Service.pact_verification_badge pact, label, initials, pseudo_branch_verification_status }
subject { PactBroker::Badges::Service.pact_verification_badge(pact, label, initials, pseudo_branch_verification_status, tags) }

let(:pact_verification_badge_url) { PactBroker::Badges::Service.pact_verification_badge_url(pact, label, initials, pseudo_branch_verification_status) }
let(:pact_verification_badge_url) { PactBroker::Badges::Service.pact_verification_badge_url(pact, label, initials, pseudo_branch_verification_status, tags) }

before do
Service.clear_cache
Expand All @@ -43,7 +44,6 @@ module Badges
end

describe "#pact_verification_badge" do

it "returns the svg file" do
expect(subject).to eq "svg"
end
Expand Down Expand Up @@ -107,6 +107,18 @@ module Badges
expect(pact_verification_badge_url).to eq URI(expected_url)
end
end

context "when the tags are supplied" do
let(:tags) { { consumer_tag: "prod", provider_tag: "master" } }

let(:expected_left_text) { "foo--bar%20(prod)%2fthing__blah%20(master)%20pact" }

it "creates a badge with the consumer and provider names, not initials" do
subject
expect(http_request).to have_been_made
expect(pact_verification_badge_url).to eq URI(expected_url)
end
end
end

context "when label is consumer" do
Expand Down

0 comments on commit cce7cd0

Please sign in to comment.