Skip to content

Commit e9b632a

Browse files
committed
feat(badge markdown): show badge in HTML pact and display markdown when clicked
1 parent 0a9bc8c commit e9b632a

File tree

8 files changed

+51
-12
lines changed

8 files changed

+51
-12
lines changed

lib/pact_broker/api/pact_broker_urls.rb

+4
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,10 @@ def webhooks_for_pact_url consumer, provider, base_url
121121
"#{base_url}/webhooks/provider/#{url_encode(provider.name)}/consumer/#{url_encode(consumer.name)}"
122122
end
123123

124+
def badge_url_for_latest_pact pact, base_url = ''
125+
"#{latest_pact_url(base_url, pact)}/badge.svg"
126+
end
127+
124128
def hal_browser_url target_url
125129
"/hal-browser/browser.html#" + target_url
126130
end

lib/pact_broker/api/renderers/html_pact_renderer.rb

+36-3
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,14 @@ class NotAPactError < StandardError; end
1414

1515
include PactBroker::Logging
1616

17-
def self.call pact
18-
new(pact).call
17+
def self.call pact, options = {}
18+
new(pact, options).call
1919
end
2020

21-
def initialize pact
21+
def initialize pact, options = {}
2222
@json_content = pact.json_content
2323
@pact = pact
24+
@options = options
2425
end
2526

2627
def call
@@ -40,12 +41,16 @@ def head
4041
<link rel='stylesheet' type='text/css' href='/stylesheets/pact.css'>
4142
<link rel='stylesheet' type='text/css' href='/stylesheets/github-json.css'>
4243
<script src='/javascripts/highlight.pack.js'></script>
44+
<script src='/javascripts/jquery-2.1.1.min.js'></script>
45+
<script src='/js/bootstrap.min.js'></script>
46+
<script src='/javascripts/pact.js'></script>
4347
<script>hljs.initHighlightingOnLoad();</script>"
4448
end
4549

4650
def pact_metadata
4751
"<div class='pact-metadata'>
4852
<ul>
53+
#{badge_li}
4954
<li>
5055
<span class='name'>#{@pact.consumer.name} version:</span>
5156
<span class='value'>#{@pact.consumer_version_number}#{tags}</span>
@@ -64,6 +69,26 @@ def pact_metadata
6469
</div>"
6570
end
6671

72+
def badge_li
73+
if PactBroker.configuration.enable_badge_resources
74+
"<li>
75+
<img src='#{badge_url}' class='badge'/>
76+
</li>
77+
<li class='badge-markdown' style='display:none'>
78+
<textarea rows='3' cols='100' >#{badge_markdown}</textarea>
79+
</li>
80+
"
81+
end
82+
end
83+
84+
def badge_markdown
85+
"[![#{@pact.consumer.name}/#{@pact.provider.name} Pact Status](#{badge_url})](#{latest_pact_url})"
86+
end
87+
88+
def base_url
89+
@options[:base_url] || ''
90+
end
91+
6792
def title
6893
"Pact between #{@pact.consumer.name} and #{@pact.provider.name}"
6994
end
@@ -80,6 +105,14 @@ def pact_url
80105
PactBroker::Api::PactBrokerUrls.pact_url '', @pact
81106
end
82107

108+
def latest_pact_url
109+
PactBroker::Api::PactBrokerUrls.latest_pact_url base_url, @pact
110+
end
111+
112+
def badge_url
113+
PactBroker::Api::PactBrokerUrls.badge_url_for_latest_pact @pact, base_url
114+
end
115+
83116
def tags
84117
if @pact.consumer_version_tag_names.any?
85118
" (#{@pact.consumer_version_tag_names.join(", ")})"

lib/pact_broker/api/resources/badge.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ def content_types_provided
1717
end
1818

1919
def resource_exists?
20-
PactBroker.configuration.enable_badge_resources
20+
true
2121
end
2222

2323
def is_authorized?(authorization_header)
24-
true
24+
PactBroker.configuration.enable_badge_resources
2525
end
2626

2727
def forbidden?

lib/pact_broker/api/resources/latest_pact.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def to_json
2727
end
2828

2929
def to_html
30-
PactBroker.configuration.html_pact_renderer.call(pact)
30+
PactBroker.configuration.html_pact_renderer.call(pact, { base_url: base_url })
3131
end
3232

3333
def pact

lib/pact_broker/api/resources/pact.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ def to_json
7979
end
8080

8181
def to_html
82-
PactBroker.configuration.html_pact_renderer.call(pact)
82+
PactBroker.configuration.html_pact_renderer.call(pact, { base_url: base_url })
8383
end
8484

8585
def delete_resource

lib/pact_broker/configuration.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,9 @@ def self.default_configuration
5252
end
5353

5454
def self.default_html_pact_render
55-
lambda { |pact|
55+
lambda { |pact, options|
5656
require 'pact_broker/api/renderers/html_pact_renderer'
57-
PactBroker::Api::Renderers::HtmlPactRenderer.call pact
57+
PactBroker::Api::Renderers::HtmlPactRenderer.call pact, options
5858
}
5959
end
6060

spec/lib/pact_broker/api/renderers/html_pact_renderer_spec.rb

+2-1
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,13 @@ module Renderers
2121
let(:json_content) { load_fixture('renderer_pact.json') }
2222
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)}
2323
let(:pact_url) { '/pact/url' }
24+
let(:options) { { base_url: 'http://base' } }
2425

2526
before do
2627
allow(PactBroker::Api::PactBrokerUrls).to receive(:pact_url).with('', pact).and_return(pact_url)
2728
end
2829

29-
subject { HtmlPactRenderer.call pact }
30+
subject { HtmlPactRenderer.call pact, options }
3031

3132
describe ".call" do
3233
it "renders the pact as HTML" do

spec/lib/pact_broker/configuration_spec.rb

+3-2
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,11 @@ module PactBroker
1010
describe ".html_pact_renderer" do
1111

1212
let(:pact) { double('pact') }
13+
let(:options) { double('options') }
1314

1415
it "calls the inbuilt HtmlPactRenderer" do
15-
expect(PactBroker::Api::Renderers::HtmlPactRenderer).to receive(:call).with(pact)
16-
PactBroker.configuration.html_pact_renderer.call pact
16+
expect(PactBroker::Api::Renderers::HtmlPactRenderer).to receive(:call).with(pact, options)
17+
PactBroker.configuration.html_pact_renderer.call pact, options
1718
end
1819

1920
end

0 commit comments

Comments
 (0)