Skip to content

Commit dc48049

Browse files
authored
feat: support non root context (pact-foundation#344)
1 parent 0954ac0 commit dc48049

File tree

27 files changed

+125
-100
lines changed

27 files changed

+125
-100
lines changed

lib/pact_broker/api/pact_broker_urls.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -284,8 +284,8 @@ def group_url(pacticipant_name, base_url = '')
284284
"#{base_url}/groups/#{pacticipant_name}"
285285
end
286286

287-
def hal_browser_url target_url
288-
"/hal-browser/browser.html#" + target_url
287+
def hal_browser_url target_url, base_url = ''
288+
"#{base_url}/hal-browser/browser.html#" + target_url
289289
end
290290

291291
def url_encode param

lib/pact_broker/api/renderers/html_pact_renderer.rb

+14-14
Original file line numberDiff line numberDiff line change
@@ -37,18 +37,18 @@ def call
3737

3838
def head
3939
"<title>#{title}</title>
40-
<link rel='stylesheet' type='text/css' href='/stylesheets/github.css'>
41-
<link rel='stylesheet' type='text/css' href='/stylesheets/pact.css'>
42-
<link rel='stylesheet' type='text/css' href='/stylesheets/github-json.css'>
43-
<link rel='stylesheet' type='text/css' href='/css/bootstrap.min.css'>
44-
<link rel='stylesheet' type='text/css' href='/stylesheets/material-menu.css'>
45-
<link rel='stylesheet' type='text/css' href='/stylesheets/jquery-confirm.min.css'>
46-
<script src='/javascripts/highlight.pack.js'></script>
47-
<script src='/javascripts/jquery-3.3.1.min.js'></script>
48-
<script src='/js/bootstrap.min.js'></script>
49-
<script src='/javascripts/material-menu.js'></script>
50-
<script src='/javascripts/pact.js'></script>
51-
<script src='/javascripts/jquery-confirm.min.js'></script>
40+
<link rel='stylesheet' type='text/css' href='#{base_url}/stylesheets/github.css'>
41+
<link rel='stylesheet' type='text/css' href='#{base_url}/stylesheets/pact.css'>
42+
<link rel='stylesheet' type='text/css' href='#{base_url}/stylesheets/github-json.css'>
43+
<link rel='stylesheet' type='text/css' href='#{base_url}/css/bootstrap.min.css'>
44+
<link rel='stylesheet' type='text/css' href='#{base_url}/stylesheets/material-menu.css'>
45+
<link rel='stylesheet' type='text/css' href='#{base_url}/stylesheets/jquery-confirm.min.css'>
46+
<script src='#{base_url}/javascripts/highlight.pack.js'></script>
47+
<script src='#{base_url}/javascripts/jquery-3.3.1.min.js'></script>
48+
<script src='#{base_url}/js/bootstrap.min.js'></script>
49+
<script src='#{base_url}/javascripts/material-menu.js'></script>
50+
<script src='#{base_url}/javascripts/pact.js'></script>
51+
<script src='#{base_url}/javascripts/jquery-confirm.min.js'></script>
5252
<script>hljs.initHighlightingOnLoad();</script>"
5353
end
5454

@@ -72,7 +72,7 @@ def pact_metadata
7272
<a href=\"#{matrix_url}\">View Matrix</a>
7373
</li>
7474
<li>
75-
<a href=\"/\">Home</a>
75+
<a href=\"#{base_url}\">Home</a>
7676
</li>
7777
<li>
7878
<span data-consumer-name=\"#{@pact.consumer.name}\"
@@ -129,7 +129,7 @@ def published_date_in_words
129129
end
130130

131131
def json_url
132-
PactBroker::Api::PactBrokerUrls.hal_browser_url pact_url
132+
PactBroker::Api::PactBrokerUrls.hal_browser_url pact_url, base_url
133133
end
134134

135135
def pact_url

lib/pact_broker/api/resources/pact.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ def pact_params
100100

101101
def set_post_deletion_response
102102
latest_pact = pact_service.find_latest_pact(pact_params)
103-
response_body = { "_links" => {} }
103+
response_body = { "_links" => { index: { href: base_url } } }
104104
if latest_pact
105105
response_body["_links"]["pb:latest-pact-version"] = {
106106
href: latest_pact_url(base_url, latest_pact),

lib/pact_broker/doc/controllers/app.rb

+7-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def resource_exists? rel_name, context = nil
3333
get ":rel_name" do
3434
rel_name = params[:rel_name]
3535
context = params[:context]
36-
view_params = {:layout_engine => :haml, layout: :'layouts/main'}
36+
view_params = {:layout_engine => :haml, layout: :'layouts/main', locals: { base_url: base_url }}
3737
if resource_exists? rel_name, context
3838
markdown view_name_for(rel_name, context).to_sym, view_params, {}
3939
elsif resource_exists? rel_name
@@ -42,6 +42,12 @@ def resource_exists? rel_name, context = nil
4242
markdown :not_found, view_params, {}
4343
end
4444
end
45+
46+
private
47+
48+
def base_url
49+
PactBroker.configuration.base_url || request.base_url
50+
end
4551
end
4652
end
4753
end
+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
%html
22
%head
3-
%link{rel: 'stylesheet', href: '/css/bootstrap.min.css'}
3+
%link{rel: 'stylesheet', href: "#{base_url}/css/bootstrap.min.css"}
44
%body{style: 'margin:20px'}
55
= yield

lib/pact_broker/ui/app.rb

+1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ def initialize
4747
end
4848

4949
map "/" do
50+
use PathInfoFixer
5051
run PactBroker::UI::Controllers::Index
5152
end
5253
}

lib/pact_broker/ui/controllers/clusters.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ def initialize pacticipants, relationships
1818
end
1919

2020
get "/" do
21-
view_model = ViewDomain::IndexItems.new(pacticipant_service.find_index_items)
22-
haml 'clusters/show', locals: {relationships: view_model}
21+
view_model = ViewDomain::IndexItems.new(pacticipant_service.find_index_items, base_url: base_url)
22+
haml 'clusters/show', locals: {relationships: view_model, base_url: base_url}
2323
end
2424

2525
end

lib/pact_broker/ui/controllers/groups.rb

+3-2
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@ class Groups < Base
1313
pacticipant = pacticipant_service.find_pacticipant_by_name(params[:name])
1414
erb :'groups/show.html', {
1515
locals: {
16-
csv_path: "/groups/#{params[:name]}.csv",
16+
csv_path: "#{base_url}/groups/#{params[:name]}.csv",
1717
pacticipant_name: params[:name],
18-
repository_url: pacticipant&.repository_url
18+
repository_url: pacticipant&.repository_url,
19+
base_url: base_url
1920
}
2021
}, {
2122
layout: 'layouts/main'

lib/pact_broker/ui/controllers/index.rb

+3-2
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class Index < Base
2525

2626
# TODO remove this code when verified
2727
options[:optimised] = true unless params[:optimised] == 'false'
28-
index_items = ViewDomain::IndexItems.new(index_service.find_index_items(options))
28+
index_items = ViewDomain::IndexItems.new(index_service.find_index_items(options), base_url: base_url)
2929

3030
page = tags ? :'index/show-with-tags' : :'index/show'
3131
locals = {
@@ -34,7 +34,8 @@ class Index < Base
3434
page_number: page_number,
3535
page_size: page_size,
3636
pagination_record_count: index_items.pagination_record_count,
37-
current_page_size: index_items.size
37+
current_page_size: index_items.size,
38+
base_url: base_url
3839
}
3940

4041
haml page, {locals: locals, layout: :'layouts/main'}

lib/pact_broker/ui/controllers/matrix.rb

+5-3
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ class Matrix < Base
2121
lines: [],
2222
title: "The Matrix",
2323
selectors: create_selector_objects(selectors),
24-
options: create_options_model(options)
24+
options: create_options_model(options),
25+
base_url: base_url
2526
}
2627
begin
2728
if params[:q]
@@ -48,15 +49,16 @@ class Matrix < Base
4849
selectors = [ PactBroker::Matrix::UnresolvedSelector.new(pacticipant_name: params[:consumer_name]), PactBroker::Matrix::UnresolvedSelector.new(pacticipant_name: params[:provider_name]) ]
4950
options = {latestby: 'cvpv', limit: 100}
5051
lines = matrix_service.find(selectors, options)
51-
lines = PactBroker::UI::ViewDomain::MatrixLines.new(lines)
52+
lines = PactBroker::UI::ViewDomain::MatrixLines.new(lines, base_url: base_url)
5253
locals = {
5354
lines: lines,
5455
title: "The Matrix",
5556
consumer_name: params[:consumer_name],
5657
provider_name: params[:provider_name],
5758
selectors: create_selector_objects(selectors),
5859
options: create_options_model(options),
59-
badge_url: nil
60+
badge_url: nil,
61+
base_url: base_url
6062
}
6163
haml :'matrix/show', {locals: locals, layout: :'layouts/main'}
6264
end

lib/pact_broker/ui/helpers/url_helper.rb

+4-4
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ module URLHelper
77

88
extend self
99

10-
def group_url pacticipant_name
11-
"/groups/#{ERB::Util.url_encode(pacticipant_name)}"
10+
def group_url pacticipant_name, base_url = ''
11+
"#{base_url}/groups/#{ERB::Util.url_encode(pacticipant_name)}"
1212
end
1313

14-
def matrix_url consumer_name, provider_name
15-
"/matrix/provider/#{ERB::Util.url_encode(provider_name)}/consumer/#{ERB::Util.url_encode(consumer_name)}"
14+
def matrix_url consumer_name, provider_name, base_url = ''
15+
"#{base_url}/matrix/provider/#{ERB::Util.url_encode(provider_name)}/consumer/#{ERB::Util.url_encode(consumer_name)}"
1616
end
1717
end
1818
end

lib/pact_broker/ui/view_models/index_item.rb

+16-11
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@ class IndexItem
1111

1212
include PactBroker::Api::PactBrokerUrls
1313

14-
def initialize relationship
14+
def initialize relationship, options = {}
1515
@relationship = relationship
16+
@options = options
1617
end
1718

1819
def consumer_name
@@ -48,35 +49,35 @@ def provider_version_latest_tag_names
4849
end
4950

5051
def consumer_group_url
51-
Helpers::URLHelper.group_url consumer_name
52+
Helpers::URLHelper.group_url(consumer_name, base_url)
5253
end
5354

5455
def provider_group_url
55-
Helpers::URLHelper.group_url provider_name
56+
Helpers::URLHelper.group_url(provider_name, base_url)
5657
end
5758

5859
def latest_pact_url
59-
"#{pactigration_base_url('', @relationship)}/latest"
60+
"#{pactigration_base_url(base_url, @relationship)}/latest"
6061
end
6162

6263
def pact_url
63-
PactBroker::Api::PactBrokerUrls.pact_url('', @relationship)
64+
PactBroker::Api::PactBrokerUrls.pact_url(base_url, @relationship)
6465
end
6566

6667
def pact_matrix_url
67-
Helpers::URLHelper.matrix_url consumer_name, provider_name
68+
Helpers::URLHelper.matrix_url(consumer_name, provider_name, base_url)
6869
end
6970

7071
def any_webhooks?
7172
@relationship.any_webhooks?
7273
end
7374

7475
def pact_versions_url
75-
PactBroker::Api::PactBrokerUrls.pact_versions_url(consumer_name, provider_name)
76+
PactBroker::Api::PactBrokerUrls.pact_versions_url(consumer_name, provider_name, base_url)
7677
end
7778

7879
def integration_url
79-
PactBroker::Api::PactBrokerUrls.integration_url(consumer_name, provider_name)
80+
PactBroker::Api::PactBrokerUrls.integration_url(consumer_name, provider_name, base_url)
8081
end
8182

8283
def webhook_label
@@ -114,11 +115,11 @@ def webhook_last_execution_date
114115
def webhook_url
115116
url = case @relationship.webhook_status
116117
when :none
117-
PactBroker::Api::PactBrokerUrls.webhooks_for_consumer_and_provider_url @relationship.latest_pact.consumer, @relationship.latest_pact.provider
118+
PactBroker::Api::PactBrokerUrls.webhooks_for_consumer_and_provider_url @relationship.latest_pact.consumer, @relationship.latest_pact.provider, base_url
118119
else
119-
PactBroker::Api::PactBrokerUrls.webhooks_status_url @relationship.latest_pact.consumer, @relationship.latest_pact.provider
120+
PactBroker::Api::PactBrokerUrls.webhooks_status_url @relationship.latest_pact.consumer, @relationship.latest_pact.provider, base_url
120121
end
121-
"/hal-browser/browser.html##{url}"
122+
PactBroker::Api::PactBrokerUrls.hal_browser_url(url, base_url)
122123
end
123124

124125
def last_verified_date
@@ -181,6 +182,10 @@ def short_version_number version_number
181182
version_number
182183
end
183184
end
185+
186+
def base_url
187+
@options[:base_url]
188+
end
184189
end
185190
end
186191
end

lib/pact_broker/ui/view_models/index_items.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ class IndexItems
77

88
attr_reader :pagination_record_count
99

10-
def initialize index_items
10+
def initialize index_items, options = {}
1111
# Why are we sorting twice!?
12-
@index_items = index_items.collect{ |index_item| IndexItem.new(index_item) }.sort
12+
@index_items = index_items.collect{ |index_item| IndexItem.new(index_item, options) }.sort
1313
# until the feature flag is turned on
1414
@pagination_record_count = index_items.size
1515
@pagination_record_count = index_items.pagination_record_count if index_items.respond_to?(:pagination_record_count)

lib/pact_broker/ui/view_models/matrix_line.rb

+12-7
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@ class MatrixLine
1212
include PactBroker::Api::PactBrokerUrls
1313
include PactBroker::Messages
1414

15-
def initialize line
15+
def initialize line, options = {}
1616
@line = line
17+
@options = options
1718
@overwritten = false # true if the pact was revised and this revision is no longer the latest
1819
end
1920

@@ -22,15 +23,15 @@ def provider_name
2223
end
2324

2425
def provider_name_url
25-
hal_browser_url(pacticipant_url_from_params(pacticipant_name: provider_name))
26+
hal_browser_url(pacticipant_url_from_params({ pacticipant_name: provider_name }, base_url), base_url)
2627
end
2728

2829
def consumer_name
2930
@line.consumer_name
3031
end
3132

3233
def consumer_name_url
33-
hal_browser_url(pacticipant_url_from_params(pacticipant_name: consumer_name))
34+
hal_browser_url(pacticipant_url_from_params({ pacticipant_name: consumer_name }, base_url), base_url)
3435
end
3536

3637
def pact_version_sha
@@ -68,7 +69,7 @@ def display_consumer_version_number
6869

6970
def consumer_version_number_url
7071
params = { pacticipant_name: consumer_name, version_number: consumer_version_number }
71-
hal_browser_url(version_url_from_params(params))
72+
hal_browser_url(version_url_from_params(params, base_url), base_url)
7273
end
7374

7475
def consumer_version_order
@@ -85,7 +86,7 @@ def display_provider_version_number
8586

8687
def provider_version_number_url
8788
params = { pacticipant_name: provider_name, version_number: provider_version_number }
88-
hal_browser_url(version_url_from_params(params))
89+
hal_browser_url(version_url_from_params(params, base_url), base_url)
8990
end
9091

9192
def provider_version_order
@@ -142,15 +143,15 @@ def verification_status
142143
end
143144

144145
def verification_status_url
145-
hal_browser_url(verification_url(self))
146+
hal_browser_url(verification_url(self, base_url), base_url)
146147
end
147148

148149
def pact_publication_date
149150
relative_date(@line.pact_created_at)
150151
end
151152

152153
def pact_publication_date_url
153-
pact_url('', @line)
154+
pact_url(base_url, @line)
154155
end
155156

156157
def relative_date date
@@ -182,6 +183,10 @@ def pre_verified_message
182183
message("messages.matrix.pre_verified")
183184
end
184185
end
186+
187+
def base_url
188+
@options[:base_url]
189+
end
185190
end
186191
end
187192
end

lib/pact_broker/ui/view_models/matrix_lines.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ module UI
55
module ViewDomain
66
class MatrixLines < Array
77

8-
def initialize rows
8+
def initialize rows, options = {}
99
lines = rows.collect do | row |
10-
PactBroker::UI::ViewDomain::MatrixLine.new(row)
10+
PactBroker::UI::ViewDomain::MatrixLine.new(row, options)
1111
end
1212
super(lines.sort)
1313
end

lib/pact_broker/ui/views/groups/show.html.erb

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
<head>
66
<meta charset="utf-8">
77
<title>Network Graph</title>
8-
<link rel='shortcut icon' href='/favicon.ico' type='image/x-icon'/>
8+
<link rel='shortcut icon' href='<%= base_url %>/favicon.ico' type='image/x-icon'/>
99
<meta name="viewport" content="width=device-width, initial-scale=1.0">
1010
<meta name="description" content="">
1111
<meta name="author" content="Duncan Alexander">
12-
<script type="text/javascript" src="/javascripts/d3.v3.js.pagespeed.ce.dFNRrGTALe.js"></script>
12+
<script type="text/javascript" src="<%= base_url %>/javascripts/d3.v3.js.pagespeed.ce.dFNRrGTALe.js"></script>
1313
<style>
1414
body{
1515
font-family:"Helvetica Neue",Arial,sans-serif;
@@ -105,7 +105,7 @@ var relationshipPath = function(relationship, nodeLocations, pacticipants) {
105105

106106
var latestPactUrl = function (consumerName, providerName) {
107107
//TODO send this with the relationship data
108-
return '/pacts/provider/' + providerName + '/consumer/' + consumerName + '/latest';
108+
return '<%= base_url %>/pacts/provider/' + providerName + '/consumer/' + consumerName + '/latest';
109109
};
110110

111111
var relationshipData = function(pacticipant, relationships) {

0 commit comments

Comments
 (0)