diff --git a/lib/pact_broker/api/pact_broker_urls.rb b/lib/pact_broker/api/pact_broker_urls.rb index a8db3cc1b..63a745972 100644 --- a/lib/pact_broker/api/pact_broker_urls.rb +++ b/lib/pact_broker/api/pact_broker_urls.rb @@ -121,6 +121,10 @@ def webhooks_for_pact_url consumer, provider, base_url "#{base_url}/webhooks/provider/#{url_encode(provider.name)}/consumer/#{url_encode(consumer.name)}" end + def badge_url_for_latest_pact pact, base_url = '' + "#{latest_pact_url(base_url, pact)}/badge.svg" + end + def hal_browser_url target_url "/hal-browser/browser.html#" + target_url end diff --git a/lib/pact_broker/api/renderers/html_pact_renderer.rb b/lib/pact_broker/api/renderers/html_pact_renderer.rb index cdf403ca8..cb7b0fb09 100644 --- a/lib/pact_broker/api/renderers/html_pact_renderer.rb +++ b/lib/pact_broker/api/renderers/html_pact_renderer.rb @@ -14,13 +14,14 @@ class NotAPactError < StandardError; end include PactBroker::Logging - def self.call pact - new(pact).call + def self.call pact, options = {} + new(pact, options).call end - def initialize pact + def initialize pact, options = {} @json_content = pact.json_content @pact = pact + @options = options end def call @@ -40,12 +41,16 @@ def head + + + " end def pact_metadata "
" end + def badge_li + if PactBroker.configuration.enable_badge_resources + "
  • + +
  • + + " + end + end + + def badge_markdown + "[![#{@pact.consumer.name}/#{@pact.provider.name} Pact Status](#{badge_url})](#{latest_pact_url})" + end + + def base_url + @options[:base_url] || '' + end + def title "Pact between #{@pact.consumer.name} and #{@pact.provider.name}" end @@ -80,6 +105,14 @@ def pact_url PactBroker::Api::PactBrokerUrls.pact_url '', @pact end + def latest_pact_url + PactBroker::Api::PactBrokerUrls.latest_pact_url base_url, @pact + end + + def badge_url + PactBroker::Api::PactBrokerUrls.badge_url_for_latest_pact @pact, base_url + end + def tags if @pact.consumer_version_tag_names.any? " (#{@pact.consumer_version_tag_names.join(", ")})" diff --git a/lib/pact_broker/api/resources/badge.rb b/lib/pact_broker/api/resources/badge.rb index e7b592b97..e545b6f3a 100644 --- a/lib/pact_broker/api/resources/badge.rb +++ b/lib/pact_broker/api/resources/badge.rb @@ -17,11 +17,11 @@ def content_types_provided end def resource_exists? - PactBroker.configuration.enable_badge_resources + true end def is_authorized?(authorization_header) - true + PactBroker.configuration.enable_badge_resources end def forbidden? diff --git a/lib/pact_broker/api/resources/latest_pact.rb b/lib/pact_broker/api/resources/latest_pact.rb index fa2ba75c2..2bac2a640 100644 --- a/lib/pact_broker/api/resources/latest_pact.rb +++ b/lib/pact_broker/api/resources/latest_pact.rb @@ -27,7 +27,7 @@ def to_json end def to_html - PactBroker.configuration.html_pact_renderer.call(pact) + PactBroker.configuration.html_pact_renderer.call(pact, { base_url: base_url }) end def pact diff --git a/lib/pact_broker/api/resources/pact.rb b/lib/pact_broker/api/resources/pact.rb index 81667c979..22b534399 100644 --- a/lib/pact_broker/api/resources/pact.rb +++ b/lib/pact_broker/api/resources/pact.rb @@ -79,7 +79,7 @@ def to_json end def to_html - PactBroker.configuration.html_pact_renderer.call(pact) + PactBroker.configuration.html_pact_renderer.call(pact, { base_url: base_url }) end def delete_resource diff --git a/lib/pact_broker/configuration.rb b/lib/pact_broker/configuration.rb index a1753007d..75f711418 100644 --- a/lib/pact_broker/configuration.rb +++ b/lib/pact_broker/configuration.rb @@ -52,9 +52,9 @@ def self.default_configuration end def self.default_html_pact_render - lambda { |pact| + lambda { |pact, options| require 'pact_broker/api/renderers/html_pact_renderer' - PactBroker::Api::Renderers::HtmlPactRenderer.call pact + PactBroker::Api::Renderers::HtmlPactRenderer.call pact, options } end diff --git a/spec/lib/pact_broker/api/renderers/html_pact_renderer_spec.rb b/spec/lib/pact_broker/api/renderers/html_pact_renderer_spec.rb index 73116ba9a..6848c1d54 100644 --- a/spec/lib/pact_broker/api/renderers/html_pact_renderer_spec.rb +++ b/spec/lib/pact_broker/api/renderers/html_pact_renderer_spec.rb @@ -21,12 +21,13 @@ module Renderers let(:json_content) { load_fixture('renderer_pact.json') } let(:pact) { double('pact', json_content: json_content, consumer_version_number: '1.2.3', consumer: consumer, provider: provider, consumer_version_tag_names: ['prod', 'master'], created_at: created_at)} let(:pact_url) { '/pact/url' } + let(:options) { { base_url: 'http://base' } } before do allow(PactBroker::Api::PactBrokerUrls).to receive(:pact_url).with('', pact).and_return(pact_url) end - subject { HtmlPactRenderer.call pact } + subject { HtmlPactRenderer.call pact, options } describe ".call" do it "renders the pact as HTML" do diff --git a/spec/lib/pact_broker/configuration_spec.rb b/spec/lib/pact_broker/configuration_spec.rb index 01e4998a1..fb1505f15 100644 --- a/spec/lib/pact_broker/configuration_spec.rb +++ b/spec/lib/pact_broker/configuration_spec.rb @@ -10,10 +10,11 @@ module PactBroker describe ".html_pact_renderer" do let(:pact) { double('pact') } + let(:options) { double('options') } it "calls the inbuilt HtmlPactRenderer" do - expect(PactBroker::Api::Renderers::HtmlPactRenderer).to receive(:call).with(pact) - PactBroker.configuration.html_pact_renderer.call pact + expect(PactBroker::Api::Renderers::HtmlPactRenderer).to receive(:call).with(pact, options) + PactBroker.configuration.html_pact_renderer.call pact, options end end