Skip to content

Commit fe85b0b

Browse files
committed
feat: allow HAL docs to specify a context so that two relations with the same name in different resources don't clash
1 parent 3eaa79c commit fe85b0b

30 files changed

+72
-31
lines changed

lib/pact_broker/api/decorators/pact_collection_decorator.rb

+8
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,14 @@ def create_representable_pact pact
4444
}
4545
end
4646
end
47+
48+
curies do | options |
49+
[{
50+
name: :pb,
51+
href: options[:base_url] + '/doc/{rel}?context=pacts',
52+
templated: true
53+
}]
54+
end
4755
end
4856
end
4957
end

lib/pact_broker/api/decorators/pact_decorator.rb

+1-2
Original file line numberDiff line numberDiff line change
@@ -160,11 +160,10 @@ def to_hash(options = {})
160160
curies do | options |
161161
[{
162162
name: :pb,
163-
href: options[:base_url] + '/doc/{rel}',
163+
href: options[:base_url] + '/doc/{rel}?context=pact',
164164
templated: true
165165
}]
166166
end
167-
168167
end
169168
end
170169
end

lib/pact_broker/api/decorators/version_decorator.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class VersionDecorator < BaseDecorator
4646
curies do | options |
4747
[{
4848
name: :pb,
49-
href: options.fetch(:base_url) + '/doc/{rel}',
49+
href: options.fetch(:base_url) + '/doc/{rel}?context=version',
5050
templated: true
5151
}]
5252
end

lib/pact_broker/api/resources/index.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ def to_json
8989
'curies' =>
9090
[{
9191
name: 'pb',
92-
href: base_url + '/doc/{rel}',
92+
href: base_url + '/doc/{rel}?context=index',
9393
templated: true
9494
}]
9595
}

lib/pact_broker/doc/controllers/app.rb

+12-8
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,28 @@ class App < Padrino::Application
1717
}.freeze
1818

1919
helpers do
20-
def view_name_for rel_name
21-
MAPPINGS[rel_name] || rel_name
20+
def view_name_for rel_name, context = nil
21+
view_name = MAPPINGS[rel_name] || rel_name
22+
context ? "#{context}/#{view_name}" : view_name
2223
end
2324

24-
def resource_exists? rel_name
25-
File.exist? File.join(self.class.root, 'views', "#{view_name_for(rel_name)}.markdown")
25+
def resource_exists? rel_name, context = nil
26+
File.exist? File.join(self.class.root, 'views', "#{view_name_for(rel_name, context)}.markdown")
2627
end
2728
end
2829

2930
get ":rel_name" do
3031
rel_name = params[:rel_name]
31-
if resource_exists? rel_name
32-
markdown view_name_for(rel_name).to_sym, {:layout_engine => :haml, layout: :'layouts/main'}, {}
32+
context = params[:context]
33+
view_params = {:layout_engine => :haml, layout: :'layouts/main'}
34+
if resource_exists? rel_name, context
35+
markdown view_name_for(rel_name, context).to_sym, view_params, {}
36+
elsif resource_exists? rel_name
37+
markdown view_name_for(rel_name).to_sym, view_params, {}
3338
else
34-
response.status = 404
39+
markdown :not_found, view_params, {}
3540
end
3641
end
37-
3842
end
3943
end
4044
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Consumer version
2+
3+
Allowed method: `GET`, `PUT`, `DELETE`
4+
5+
The pacticipant (application) version of the consumer that this resource belongs to. Deleting this resource will delete all child resources.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
No documentation exists for this relation yet.
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Diff with previous distinct version
22

3-
Allowed methods: GET
3+
Allowed methods: `GET`
44

55
This resource displays the difference between the current pact, and the previous "distinct" version. Given that the pact file does not change every time it is published, the previous distinct version is the most recent version published before the current one where the content is actually different.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Diff between two pact versions
2+
3+
Allowed methods: `GET`
4+
5+
This resource displays the difference between the current pact version, and the specified pact version. Note that the pact version in the URL is the pact version content sha, not the consumer version number.

lib/pact_broker/doc/views/publish-verification-results.markdown lib/pact_broker/doc/views/pact/publish-verification-results.markdown

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
# Publish pact verification result
22

3-
Allowed methods: POST
3+
Allowed methods: `POST`
44

55
Use a `POST` request to the `pb:publish-verification-results` link (`$['_links']['pb:publish-verification-results']['href']`) in the pact resource to publish the result (either success or failure) of a pact verification. The body of the request must include the success (true or false) and the provider application version that the pact was verified against. It may also include the build URL to facilitate debugging when failures occur.
6-
6+
77
{
88
"success": true,
99
"providerApplicationVersion": "4.5.6",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Tag Production Version
2+
3+
Allowed methods: `PUT`
4+
5+
Add the "prod" tag to this consumer version. This allows you to retrieve the latest version production version using the following URL.
6+
7+
http://pact-broker/pacts/provider/{provider}/consumer/{consumer}/latest/prod
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Add Tag to Consumer Version
2+
3+
Allowed methods: `PUT`
4+
5+
Add a tag with the specified name to this consumer version.

lib/pact_broker/doc/views/provider.markdown

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22

33
Allowed methods: GET, PATCH, DELETE
44

5-
The application that initiates the HTTP request.
5+
The application that responds to the HTTP request.
66

77
See [Pacticipant](/doc/pacticipants) for more information.

lib/pact_broker/doc/views/tag-prod-version.markdown

-7
This file was deleted.

lib/pact_broker/doc/views/tag-version.markdown

-4
This file was deleted.

spec/lib/pact_broker/doc/controllers/app_spec.rb

+21-3
Original file line numberDiff line numberDiff line change
@@ -31,19 +31,37 @@ module Controllers
3131
context "when the resource does not exist" do
3232
subject { get "/blah" }
3333

34-
it "returns a 404 status" do
34+
it "returns a 200 status, because otherwise, the Rack cascade will make it return a 404 from the webmachine API" do
3535
subject
36-
expect(last_response.status).to eq 404
36+
expect(last_response.status).to eq 200
3737
end
3838

3939
it "returns a html content type" do
4040
subject
4141
expect(last_response.headers['Content-Type']).to eq "text/html;charset=utf-8"
4242
end
4343

44+
it "returns a custom error page" do
45+
subject
46+
expect(last_response.body).to include "No documentation exists"
47+
end
4448
end
45-
end
4649

50+
context "when the resource has a context and there is a folder with a matching name" do
51+
subject { get "/diff?context=pact" }
52+
53+
it "returns documentation in a folder of the matching name" do
54+
subject
55+
expect(last_response.status).to eq 200
56+
expect(last_response.body).to include "Diff"
57+
end
58+
59+
it "returns a html content type" do
60+
subject
61+
expect(last_response.headers['Content-Type']).to eq "text/html;charset=utf-8"
62+
end
63+
end
64+
end
4765
end
4866
end
4967
end

spec/lib/pact_broker/doc/coverage_spec.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
relations_without_docs = {}
2222

2323
relations_that_should_have_docs.each do | relation |
24-
path = "/docs/#{relation.split(":", 2).last}"
24+
path = "/docs/#{relation.split(":", 2).last}?context=index"
2525
get path, {}, { 'HTTP_ACCEPT' => 'text/html' }
2626
if last_response.status != 200
2727
relations_without_docs[relation] = last_response.status

0 commit comments

Comments
 (0)