Skip to content

Commit aa60a85

Browse files
committed
feat: add endpoints to get latest pacticipant version and latest tagged version
1 parent a7128ef commit aa60a85

File tree

7 files changed

+109
-7
lines changed

7 files changed

+109
-7
lines changed

lib/pact_broker/api.rb

+2
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ module PactBroker
4444
add ['pacticipants', :name], Api::Resources::Pacticipant, {resource_name: "pacticipant"}
4545
add ['pacticipants', :pacticipant_name, 'versions'], Api::Resources::Versions, {resource_name: "pacticipant_versions"}
4646
add ['pacticipants', :pacticipant_name, 'versions', :pacticipant_version_number], Api::Resources::Version, {resource_name: "pacticipant_version"}
47+
add ['pacticipants', :pacticipant_name, 'versions', 'latest', :tag], Api::Resources::Version, {resource_name: "latest_tagged_pacticipant_version"}
48+
add ['pacticipants', :pacticipant_name, 'versions', 'latest'], Api::Resources::Version, {resource_name: "latest_pacticipant_version"}
4749
add ['pacticipants', :pacticipant_name, 'versions', :pacticipant_version_number, 'tags', :tag_name], Api::Resources::Tag, {resource_name: "pacticipant_version_tag"}
4850
add ['pacticipants', :pacticipant_name, 'labels', :label_name], Api::Resources::Label, {resource_name: "pacticipant_label"}
4951

lib/pact_broker/api/resources/index.rb

+12-3
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,20 @@ def to_json
5050
'pb:latest-provider-pacts-with-tag' =>
5151
{
5252
href: base_url + '/pacts/provider/{provider}/latest/{tag}',
53-
title: 'Latest pacts by provider with a specified tag',
53+
title: 'Latest pacts by provider with the specified tag',
5454
templated: true
5555
},
56-
'pb:webhooks' =>
57-
{
56+
'pb:latest-version' => {
57+
href: base_url + '/pacticipants/{pacticipant}/versions/latest',
58+
title: 'Latest pacticipant version',
59+
templated: true
60+
},
61+
'pb:latest-tagged-version' => {
62+
href: base_url + '/pacticipants/{pacticipant}/versions/latest/{tag}',
63+
title: 'Latest pacticipant version with the specified tag',
64+
templated: true
65+
},
66+
'pb:webhooks' => {
5867
href: base_url + '/webhooks',
5968
title: 'Webhooks',
6069
templated: false

lib/pact_broker/api/resources/version.rb

+7-3
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,15 @@ def delete_resource
3232
private
3333

3434
def version
35-
@version ||= version_service.find_by_pacticipant_name_and_number path_info
35+
if path_info[:tag]
36+
@version ||= version_service.find_by_pacticpant_name_and_latest_tag(path_info[:pacticipant_name], path_info[:tag])
37+
elsif path_info[:number]
38+
@version ||= version_service.find_by_pacticipant_name_and_number path_info
39+
else
40+
@version ||= version_service.find_latest_by_pacticpant_name path_info
41+
end
3642
end
37-
3843
end
3944
end
40-
4145
end
4246
end

lib/pact_broker/versions/service.rb

+4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ class Service
77

88
extend PactBroker::Repositories
99

10+
def self.find_latest_by_pacticpant_name params
11+
version_repository.find_latest_by_pacticpant_name params.fetch(:pacticipant_name)
12+
end
13+
1014
def self.find_by_pacticipant_name_and_number params
1115
version_repository.find_by_pacticipant_name_and_number params.fetch(:pacticipant_name), params.fetch(:pacticipant_version_number)
1216
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
class HalRelationProxyApp
2+
3+
# This hash allows us to do a dodgy "generators" implementation
4+
# It means we can use placeholder URLS for the relations in our consumer tests so that
5+
# the consumer does not need to know the actual URLs.
6+
PATH_REPLACEMENTS = {
7+
'/HAL-REL-PLACEHOLDER-INDEX-PB-LATEST-TAGGED-VERSION-Condor-production' =>
8+
'/pacticipants/Condor/versions/latest/production',
9+
'/HAL-REL-PLACEHOLDER-INDEX-PB-LATEST-VERSION-Condor' =>
10+
'/pacticipants/Condor/versions/latest'
11+
}
12+
13+
RESPONSE_BODY_REPLACEMENTS = {
14+
}
15+
16+
def initialize(app)
17+
@app = app
18+
end
19+
20+
def call env
21+
env_with_modified_path = env
22+
PATH_REPLACEMENTS.each do | (find, replace) |
23+
env_with_modified_path['PATH_INFO'] = env_with_modified_path['PATH_INFO'].gsub(find, replace)
24+
end
25+
26+
response = @app.call(env_with_modified_path)
27+
28+
modified_response_body = response.last.join
29+
RESPONSE_BODY_REPLACEMENTS.each do | (find, replace) |
30+
modified_response_body = modified_response_body.gsub(find, replace)
31+
end
32+
33+
[response[0], response[1], [modified_response_body]]
34+
end
35+
36+
end

spec/service_consumers/pact_helper.rb

+21-1
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,32 @@
66
PactBroker::DB.connection = PactBroker::Database.database = DB::PACT_BROKER_DB
77

88
require 'spec/support/database_cleaner'
9-
require 'pact_broker/api'
9+
require 'pact_broker'
10+
require 'pact_broker/app'
1011

12+
require_relative 'hal_relation_proxy_app'
1113
require_relative 'provider_states_for_pact_broker_client'
1214

15+
pact_broker = PactBroker::App.new { |c| c.database_connection = DB::PACT_BROKER_DB }
16+
app_to_verify = HalRelationProxyApp.new(pact_broker)
17+
18+
module Rack
19+
module PactBroker
20+
class DatabaseTransaction
21+
def do_not_rollback? response
22+
# Dodgey hack to stop exceptions raising a Rollback error while verifying
23+
# Otherwise the provider states that deliberately raise exceptions
24+
# end up raising exceptions that break the verification tests
25+
true
26+
end
27+
end
28+
end
29+
end
30+
1331
Pact.service_provider "Pact Broker" do
1432

33+
app { HalRelationProxyApp.new(app_to_verify) }
34+
1535
honours_pact_with "Pact Broker Client" do
1636
pact_uri "https://raw.githubusercontent.com/pact-foundation/pact_broker-client/master/spec/pacts/pact_broker_client-pact_broker.json"
1737
end

spec/service_consumers/provider_states_for_pact_broker_client.rb

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

33
Pact.provider_states_for "Pact Broker Client" do
44

5+
provider_state "the pb:latest-tagged-version relation exists in the index resource" do
6+
no_op
7+
end
8+
9+
provider_state "'Condor' exists in the pact-broker with the latest tagged 'production' version 1.2.3" do
10+
set_up do
11+
TestDataBuilder.new
12+
.create_consumer("Condor")
13+
.create_consumer_version("1.2.3")
14+
.create_consumer_version_tag("production")
15+
.create_consumer_version("2.0.0")
16+
end
17+
end
18+
19+
provider_state "the pb:latest-version relation exists in the index resource" do
20+
no_op
21+
end
22+
23+
provider_state "'Condor' exists in the pact-broker with the latest version 1.2.3" do
24+
set_up do
25+
TestDataBuilder.new
26+
.create_consumer("Condor")
27+
.create_consumer_version("1.0.0")
28+
.create_consumer_version("1.2.3")
29+
end
30+
end
31+
532
provider_state "the pact for Foo Thing version 1.2.3 has been verified by Bar version 4.5.6" do
633
set_up do
734
TestDataBuilder.new

0 commit comments

Comments
 (0)