diff --git a/lib/pact_broker/api/pact_broker_urls.rb b/lib/pact_broker/api/pact_broker_urls.rb index 6beb80d51..8114f4482 100644 --- a/lib/pact_broker/api/pact_broker_urls.rb +++ b/lib/pact_broker/api/pact_broker_urls.rb @@ -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 }], diff --git a/lib/pact_broker/api/resources/badge.rb b/lib/pact_broker/api/resources/badge.rb index 98da28c00..9e5e1f7b4 100644 --- a/lib/pact_broker/api/resources/badge.rb +++ b/lib/pact_broker/api/resources/badge.rb @@ -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 @@ -72,6 +72,10 @@ def comment verification_number = latest_verification ? latest_verification.number : "?" "\n" end + + def tags + {} + end end end end diff --git a/lib/pact_broker/api/resources/matrix_badge.rb b/lib/pact_broker/api/resources/matrix_badge.rb index daa78e78d..ceb82c8a1 100644 --- a/lib/pact_broker/api/resources/matrix_badge.rb +++ b/lib/pact_broker/api/resources/matrix_badge.rb @@ -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( diff --git a/lib/pact_broker/badges/service.rb b/lib/pact_broker/badges/service.rb index a9b3da790..88866ddaf 100644 --- a/lib/pact_broker/badges/service.rb +++ b/lib/pact_broker/badges/service.rb @@ -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) @@ -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 @@ -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 diff --git a/lib/pact_broker/matrix/unresolved_selector.rb b/lib/pact_broker/matrix/unresolved_selector.rb index 03fb7ea3a..6715feac7 100644 --- a/lib/pact_broker/matrix/unresolved_selector.rb +++ b/lib/pact_broker/matrix/unresolved_selector.rb @@ -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 diff --git a/lib/pact_broker/ui/controllers/base_controller.rb b/lib/pact_broker/ui/controllers/base_controller.rb index 860d089a2..192bedaa4 100644 --- a/lib/pact_broker/ui/controllers/base_controller.rb +++ b/lib/pact_broker/ui/controllers/base_controller.rb @@ -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 diff --git a/lib/pact_broker/ui/controllers/matrix.rb b/lib/pact_broker/ui/controllers/matrix.rb index 5ea70b312..65a8b35c9 100644 --- a/lib/pact_broker/ui/controllers/matrix.rb +++ b/lib/pact_broker/ui/controllers/matrix.rb @@ -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 @@ -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 @@ -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 @@ -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 diff --git a/lib/pact_broker/ui/views/matrix/show.haml b/lib/pact_broker/ui/views/matrix/show.haml index 3fcb1fead..4dcb760f1 100644 --- a/lib/pact_broker/ui/views/matrix/show.haml +++ b/lib/pact_broker/ui/views/matrix/show.haml @@ -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 diff --git a/public/stylesheets/matrix.css b/public/stylesheets/matrix.css index ec62969ff..abe94fe88 100644 --- a/public/stylesheets/matrix.css +++ b/public/stylesheets/matrix.css @@ -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; +} diff --git a/spec/integration/ui/matrix_spec.rb b/spec/integration/ui/matrix_spec.rb index 36bd73616..896de2e8c 100644 --- a/spec/integration/ui/matrix_spec.rb +++ b/spec/integration/ui/matrix_spec.rb @@ -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") } @@ -27,4 +26,12 @@ expect(subject.body.scan('