Skip to content

Commit

Permalink
feat(describe-version): allow retrieving latest pacticipant version
Browse files Browse the repository at this point in the history
  • Loading branch information
bethesque committed Jan 25, 2018
1 parent 7d3f3e1 commit 7679f61
Show file tree
Hide file tree
Showing 9 changed files with 398 additions and 9 deletions.
106 changes: 106 additions & 0 deletions doc/pacts/markdown/Pact Broker Client - Pact Broker.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@

* [A request for the compatibility matrix where only the version of Foo is specified](#a_request_for_the_compatibility_matrix_where_only_the_version_of_Foo_is_specified_given_the_pact_for_Foo_version_1.2.3_has_been_verified_by_Bar_version_4.5.6_and_version_5.6.7) given the pact for Foo version 1.2.3 has been verified by Bar version 4.5.6 and version 5.6.7

* [A request for the index resource](#a_request_for_the_index_resource_given_the_pb:latest-tagged-version_relation_exists_in_the_index_resource) given the pb:latest-tagged-version relation exists in the index resource

* [A request for the index resource](#a_request_for_the_index_resource_given_the_pb:latest-version_relation_exists_in_the_index_resource) given the pb:latest-version relation exists in the index resource

* [A request for the list of the latest pacts from all consumers for the Pricing Service'](#a_request_for_the_list_of_the_latest_pacts_from_all_consumers_for_the_Pricing_Service'_given_a_latest_pact_between_Condor_and_the_Pricing_Service_exists) given a latest pact between Condor and the Pricing Service exists

* [A request for the list of the latest prod pacts from all consumers for the Pricing Service'](#a_request_for_the_list_of_the_latest_prod_pacts_from_all_consumers_for_the_Pricing_Service'_given_tagged_as_prod_pact_between_Condor_and_the_Pricing_Service_exists) given tagged as prod pact between Condor and the Pricing Service exists
Expand Down Expand Up @@ -50,10 +54,14 @@

* [A request to register the repository URL of a pacticipant](#a_request_to_register_the_repository_URL_of_a_pacticipant_given_the_'Pricing_Service'_does_not_exist_in_the_pact-broker) given the 'Pricing Service' does not exist in the pact-broker

* [A request to retrieve the latest 'production' version of Condor](#a_request_to_retrieve_the_latest_'production'_version_of_Condor_given_'Condor'_exists_in_the_pact-broker_with_the_latest_tagged_'production'_version_1.2.3) given 'Condor' exists in the pact-broker with the latest tagged 'production' version 1.2.3

* [A request to retrieve the latest pact between Condor and the Pricing Service](#a_request_to_retrieve_the_latest_pact_between_Condor_and_the_Pricing_Service_given_a_pact_between_Condor_and_the_Pricing_Service_exists) given a pact between Condor and the Pricing Service exists

* [A request to retrieve the latest pact between Condor and the Pricing Service](#a_request_to_retrieve_the_latest_pact_between_Condor_and_the_Pricing_Service_given_no_pact_between_Condor_and_the_Pricing_Service_exists) given no pact between Condor and the Pricing Service exists

* [A request to retrieve the latest version of Condor](#a_request_to_retrieve_the_latest_version_of_Condor_given_'Condor'_exists_in_the_pact-broker_with_the_latest_version_1.2.3) given 'Condor' exists in the pact-broker with the latest version 1.2.3

* [A request to retrieve the pact between the production verison of Condor and the Pricing Service](#a_request_to_retrieve_the_pact_between_the_production_verison_of_Condor_and_the_Pricing_Service_given_a_pact_between_Condor_and_the_Pricing_Service_exists_for_the_production_version_of_Condor) given a pact between Condor and the Pricing Service exists for the production version of Condor

* [A request to tag the production version of Condor](#a_request_to_tag_the_production_version_of_Condor_given_'Condor'_exists_in_the_pact-broker_with_version_1.3.0,_tagged_with_'prod') given 'Condor' exists in the pact-broker with version 1.3.0, tagged with 'prod'
Expand Down Expand Up @@ -418,6 +426,54 @@ Pact Broker will respond with:
}
}
```
<a name="a_request_for_the_index_resource_given_the_pb:latest-tagged-version_relation_exists_in_the_index_resource"></a>
Given **the pb:latest-tagged-version relation exists in the index resource**, upon receiving **a request for the index resource** from Pact Broker Client, with
```json
{
"method": "get",
"path": "/",
"headers": {
"Accept": "application/json, application/hal+json"
}
}
```
Pact Broker will respond with:
```json
{
"status": 200,
"body": {
"_links": {
"pb:latest-tagged-version": {
"href": "http://localhost:1234/HAL-REL-PLACEHOLDER-INDEX-PB-LATEST-TAGGED-VERSION-{pacticipant}-{tag}"
}
}
}
}
```
<a name="a_request_for_the_index_resource_given_the_pb:latest-version_relation_exists_in_the_index_resource"></a>
Given **the pb:latest-version relation exists in the index resource**, upon receiving **a request for the index resource** from Pact Broker Client, with
```json
{
"method": "get",
"path": "/",
"headers": {
"Accept": "application/json, application/hal+json"
}
}
```
Pact Broker will respond with:
```json
{
"status": 200,
"body": {
"_links": {
"pb:latest-version": {
"href": "http://localhost:1234/HAL-REL-PLACEHOLDER-INDEX-PB-LATEST-VERSION-{pacticipant}"
}
}
}
}
```
<a name="a_request_for_the_list_of_the_latest_pacts_from_all_consumers_for_the_Pricing_Service&#39;_given_a_latest_pact_between_Condor_and_the_Pricing_Service_exists"></a>
Given **a latest pact between Condor and the Pricing Service exists**, upon receiving **a request for the list of the latest pacts from all consumers for the Pricing Service'** from Pact Broker Client, with
```json
Expand Down Expand Up @@ -923,6 +979,31 @@ Pact Broker will respond with:
"status": 201
}
```
<a name="a_request_to_retrieve_the_latest_&#39;production&#39;_version_of_Condor_given_&#39;Condor&#39;_exists_in_the_pact-broker_with_the_latest_tagged_&#39;production&#39;_version_1.2.3"></a>
Given **'Condor' exists in the pact-broker with the latest tagged 'production' version 1.2.3**, upon receiving **a request to retrieve the latest 'production' version of Condor** from Pact Broker Client, with
```json
{
"method": "get",
"path": "/HAL-REL-PLACEHOLDER-INDEX-PB-LATEST-TAGGED-VERSION-Condor-production",
"headers": {
"Accept": "application/json, application/hal+json"
}
}
```
Pact Broker will respond with:
```json
{
"status": 200,
"body": {
"number": "1.2.3",
"_links": {
"self": {
"href": "http://localhost:1234/some-url"
}
}
}
}
```
<a name="a_request_to_retrieve_the_latest_pact_between_Condor_and_the_Pricing_Service_given_a_pact_between_Condor_and_the_Pricing_Service_exists"></a>
Given **a pact between Condor and the Pricing Service exists**, upon receiving **a request to retrieve the latest pact between Condor and the Pricing Service** from Pact Broker Client, with
```json
Expand Down Expand Up @@ -966,6 +1047,31 @@ Pact Broker will respond with:
"status": 404
}
```
<a name="a_request_to_retrieve_the_latest_version_of_Condor_given_&#39;Condor&#39;_exists_in_the_pact-broker_with_the_latest_version_1.2.3"></a>
Given **'Condor' exists in the pact-broker with the latest version 1.2.3**, upon receiving **a request to retrieve the latest version of Condor** from Pact Broker Client, with
```json
{
"method": "get",
"path": "/HAL-REL-PLACEHOLDER-INDEX-PB-LATEST-VERSION-Condor",
"headers": {
"Accept": "application/json, application/hal+json"
}
}
```
Pact Broker will respond with:
```json
{
"status": 200,
"body": {
"number": "1.2.3",
"_links": {
"self": {
"href": "http://localhost:1234/some-url"
}
}
}
}
```
<a name="a_request_to_retrieve_the_pact_between_the_production_verison_of_Condor_and_the_Pricing_Service_given_a_pact_between_Condor_and_the_Pricing_Service_exists_for_the_production_version_of_Condor"></a>
Given **a pact between Condor and the Pricing Service exists for the production version of Condor**, upon receiving **a request to retrieve the pact between the production verison of Condor and the Pricing Service** from Pact Broker Client, with
```json
Expand Down
17 changes: 16 additions & 1 deletion lib/pact_broker/client/base_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def default_put_headers

def handle_response response
if response.success?
yield
yield response
elsif response.code == 404
nil
elsif response.code == 401
Expand Down Expand Up @@ -98,6 +98,21 @@ def get url, *args
self.class.get(url, *args)
end

def url_for_relation relation_name, params
handle_response(get("/", headers: default_get_headers)) do | response |
relation = (JSON.parse(response.body)['_links'] || {})[relation_name]
if relation
url = relation['href']
params.each do | (key, value) |
url = url.gsub("{#{key}}", value)
end
url
else
raise PactBroker::Client::RelationNotFound.new("Could not find relation #{relation_name} in index resource. Try upgrading your Pact Broker as the feature you require may not exist in your version.")
end
end
end

def verbose?
@verbose
end
Expand Down
2 changes: 1 addition & 1 deletion lib/pact_broker/client/cli/broker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def describe_version
pacticipant: options.pacticipant,
version: options.version,
latest: latest,
tag: options.latest.is_a?(String) ? options.latest : nil
tag: options.latest != "latest" ? options.latest : nil
}
opts = {
output: options.output
Expand Down
2 changes: 2 additions & 0 deletions lib/pact_broker/client/error.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
module PactBroker
module Client
class Error < StandardError; end

class RelationNotFound < Error; end
end
end
14 changes: 8 additions & 6 deletions lib/pact_broker/client/versions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@ def find options
end
end

# TODO this is not a valid URL!
def latest options
query = options[:tag] ? {tag: options[:tag]} : {}
response = self.class.get("#{versions_base_url(options)}/latest", query: query, headers: default_get_headers)

handle_response(response) do
string_keys_to_symbols(response.to_hash)
puts options
url = if options[:tag]
url_for_relation('pb:latest-tagged-version', pacticipant: options.fetch(:pacticipant), tag: options.fetch(:tag))
else
url_for_relation('pb:latest-version', pacticipant: options.fetch(:pacticipant))
end
handle_response(get(url, headers: default_get_headers)) do | response |
JSON.parse(response.body, symbolize_names: true)
end
end

Expand Down
6 changes: 5 additions & 1 deletion lib/pact_broker/client/versions/describe.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@ def call
else
pact_broker_client.pacticipants.versions.latest(params)
end
Result.new(true, format_version(version_hash))
if version_hash
Result.new(true, format_version(version_hash))
else
Result.new(false, "Pacticipant version not found")
end
end

private
Expand Down
28 changes: 28 additions & 0 deletions spec/lib/pact_broker/client/base_client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ module PactBroker
module Client
describe BaseClient do
describe '#initialize' do
subject { BaseClient.new(base_url: base_url) }

let(:base_url) { 'http://pact_broker_base_url'}
let(:username) { 'pact_repo_username'}
let(:password) { 'pact_repo_password'}
Expand Down Expand Up @@ -45,6 +47,32 @@ module Client
subject
end
end

describe "url_for_relation" do
let(:index_body) do
{
_links: {
:'pb:foo-bar' => {
href: "http://foo-bar/{name}"
}
}
}.to_json
end

let!(:request) do
stub_request(:get, "http://pact_broker_base_url/").to_return(status: 200, body: index_body, headers: { "Content-Type" => "application/json" } )
end

it "retrieves a relation URL from the index" do
expect(subject.url_for_relation('pb:foo-bar', name: 'wiffle')).to eq 'http://foo-bar/wiffle'
end

context "when the relation is not found" do
it "raises an error" do
expect { subject.url_for_relation('pb:not-found', name: 'wiffle') }.to raise_error PactBroker::Client::RelationNotFound, /Could not find relation pb:not-found in index resource/
end
end
end
end
end
end
Expand Down
118 changes: 118 additions & 0 deletions spec/pacts/pact_broker_client-pact_broker.json
Original file line number Diff line number Diff line change
Expand Up @@ -1090,6 +1090,124 @@
}
}
}
},
{
"description": "a request for the index resource",
"providerState": "the pb:latest-version relation exists in the index resource",
"request": {
"method": "get",
"path": "/",
"headers": {
"Accept": "application/json, application/hal+json"
}
},
"response": {
"status": 200,
"headers": {
},
"body": {
"_links": {
"pb:latest-version": {
"href": "http://localhost:1234/HAL-REL-PLACEHOLDER-INDEX-PB-LATEST-VERSION-{pacticipant}"
}
}
},
"matchingRules": {
"$.body._links.pb:latest-version.href": {
"match": "regex",
"regex": "http:\\/\\/.*{pacticipant}"
}
}
}
},
{
"description": "a request to retrieve the latest version of Condor",
"providerState": "'Condor' exists in the pact-broker with the latest version 1.2.3",
"request": {
"method": "get",
"path": "/HAL-REL-PLACEHOLDER-INDEX-PB-LATEST-VERSION-Condor",
"headers": {
"Accept": "application/json, application/hal+json"
}
},
"response": {
"status": 200,
"headers": {
},
"body": {
"number": "1.2.3",
"_links": {
"self": {
"href": "http://localhost:1234/some-url"
}
}
},
"matchingRules": {
"$.body._links.self.href": {
"match": "regex",
"regex": "http:\\/\\/.*"
}
}
}
},
{
"description": "a request for the index resource",
"providerState": "the pb:latest-tagged-version relation exists in the index resource",
"request": {
"method": "get",
"path": "/",
"headers": {
"Accept": "application/json, application/hal+json"
}
},
"response": {
"status": 200,
"headers": {
},
"body": {
"_links": {
"pb:latest-tagged-version": {
"href": "http://localhost:1234/HAL-REL-PLACEHOLDER-INDEX-PB-LATEST-TAGGED-VERSION-{pacticipant}-{tag}"
}
}
},
"matchingRules": {
"$.body._links.pb:latest-tagged-version.href": {
"match": "regex",
"regex": "http:\\/\\/.*{pacticipant}.*{tag}"
}
}
}
},
{
"description": "a request to retrieve the latest 'production' version of Condor",
"providerState": "'Condor' exists in the pact-broker with the latest tagged 'production' version 1.2.3",
"request": {
"method": "get",
"path": "/HAL-REL-PLACEHOLDER-INDEX-PB-LATEST-TAGGED-VERSION-Condor-production",
"headers": {
"Accept": "application/json, application/hal+json"
}
},
"response": {
"status": 200,
"headers": {
},
"body": {
"number": "1.2.3",
"_links": {
"self": {
"href": "http://localhost:1234/some-url"
}
}
},
"matchingRules": {
"$.body._links.self.href": {
"match": "regex",
"regex": "http:\\/\\/.*"
}
}
}
}
],
"metadata": {
Expand Down
Loading

0 comments on commit 7679f61

Please sign in to comment.