Skip to content

Commit

Permalink
feat(badge markdown): show badge in HTML pact and display markdown wh…
Browse files Browse the repository at this point in the history
…en clicked
  • Loading branch information
bethesque committed Sep 15, 2017
1 parent 0a9bc8c commit e9b632a
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 12 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 @@ -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
Expand Down
39 changes: 36 additions & 3 deletions lib/pact_broker/api/renderers/html_pact_renderer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -40,12 +41,16 @@ def head
<link rel='stylesheet' type='text/css' href='/stylesheets/pact.css'>
<link rel='stylesheet' type='text/css' href='/stylesheets/github-json.css'>
<script src='/javascripts/highlight.pack.js'></script>
<script src='/javascripts/jquery-2.1.1.min.js'></script>
<script src='/js/bootstrap.min.js'></script>
<script src='/javascripts/pact.js'></script>
<script>hljs.initHighlightingOnLoad();</script>"
end

def pact_metadata
"<div class='pact-metadata'>
<ul>
#{badge_li}
<li>
<span class='name'>#{@pact.consumer.name} version:</span>
<span class='value'>#{@pact.consumer_version_number}#{tags}</span>
Expand All @@ -64,6 +69,26 @@ def pact_metadata
</div>"
end

def badge_li
if PactBroker.configuration.enable_badge_resources
"<li>
<img src='#{badge_url}' class='badge'/>
</li>
<li class='badge-markdown' style='display:none'>
<textarea rows='3' cols='100' >#{badge_markdown}</textarea>
</li>
"
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
Expand All @@ -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(", ")})"
Expand Down
4 changes: 2 additions & 2 deletions lib/pact_broker/api/resources/badge.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down
2 changes: 1 addition & 1 deletion lib/pact_broker/api/resources/latest_pact.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/pact_broker/api/resources/pact.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions lib/pact_broker/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions spec/lib/pact_broker/configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit e9b632a

Please sign in to comment.