Skip to content

Commit

Permalink
feat: add record-deployment
Browse files Browse the repository at this point in the history
  • Loading branch information
bethesque committed Feb 27, 2021
1 parent 1ac64eb commit 6e5e208
Show file tree
Hide file tree
Showing 12 changed files with 877 additions and 7 deletions.
180 changes: 180 additions & 0 deletions doc/pacts/markdown/Pact Broker Client - Pact Broker.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

#### Requests from Pact Broker Client to Pact Broker

* [A request for a pacticipant version](#a_request_for_a_pacticipant_version_given_version_5556b8149bf8bac76bc30f50a8a2dd4c22c85f30_of_pacticipant_Foo_exists_with_2_environments_that_aren't_test_available_for_deployment) given version 5556b8149bf8bac76bc30f50a8a2dd4c22c85f30 of pacticipant Foo exists with 2 environments that aren't test available for deployment

* [A request for a pacticipant version](#a_request_for_a_pacticipant_version_given_version_5556b8149bf8bac76bc30f50a8a2dd4c22c85f30_of_pacticipant_Foo_exists_with_a_test_environment_available_for_deployment) given version 5556b8149bf8bac76bc30f50a8a2dd4c22c85f30 of pacticipant Foo exists with a test environment available for deployment

* [A request for a pacticipant version](#a_request_for_a_pacticipant_version_given_version_5556b8149bf8bac76bc30f50a8a2dd4c22c85f30_of_pacticipant_Foo_does_not_exist) given version 5556b8149bf8bac76bc30f50a8a2dd4c22c85f30 of pacticipant Foo does not exist

* [A request for the compatibility matrix for a pacticipant that does not exist](#a_request_for_the_compatibility_matrix_for_a_pacticipant_that_does_not_exist)

* [A request for the compatibility matrix for all versions of Foo and Bar](#a_request_for_the_compatibility_matrix_for_all_versions_of_Foo_and_Bar_given_the_pact_for_Foo_version_1.2.3_and_1.2.4_has_been_verified_by_Bar_version_4.5.6) given the pact for Foo version 1.2.3 and 1.2.4 has been verified by Bar version 4.5.6
Expand All @@ -20,6 +26,8 @@

* [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 environments](#a_request_for_the_environments_given_an_environment_with_name_test_exists) given an environment with name test exists

* [A request for the index resource](#a_request_for_the_index_resource)

* [A request for the index resource](#a_request_for_the_index_resource_given_the_pacticipant_relations_are_present) given the pacticipant relations are present
Expand All @@ -28,6 +36,8 @@

* [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 index resource](#a_request_for_the_index_resource_given_the_pb:pacticipant-version_and_pb:environments_relations_exist_in_the_index_resource) given the pb:pacticipant-version and pb:environments relations exist in the index resource

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

* [A request for the index resource with the webhook relation](#a_request_for_the_index_resource_with_the_webhook_relation)
Expand Down Expand Up @@ -82,6 +92,8 @@

* [A request to publish a pact with method put](#a_request_to_publish_a_pact_with_method_put_given_the_'Pricing_Service'_and_'Condor'_already_exist_in_the_pact-broker,_and_Condor_already_has_a_pact_published_for_version_1.3.0) given the 'Pricing Service' and 'Condor' already exist in the pact-broker, and Condor already has a pact published for version 1.3.0

* [A request to record a deployment](#a_request_to_record_a_deployment_given_version_5556b8149bf8bac76bc30f50a8a2dd4c22c85f30_of_pacticipant_Foo_exists_with_a_test_environment_available_for_deployment) given version 5556b8149bf8bac76bc30f50a8a2dd4c22c85f30 of pacticipant Foo exists with a test environment available for deployment

* [A request to register the repository URL of a pacticipant](#a_request_to_register_the_repository_URL_of_a_pacticipant_given_the_'Pricing_Service'_already_exists_in_the_pact-broker) given the 'Pricing Service' already exists in the pact-broker

* [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
Expand Down Expand Up @@ -114,6 +126,87 @@

#### Interactions

<a name="a_request_for_a_pacticipant_version_given_version_5556b8149bf8bac76bc30f50a8a2dd4c22c85f30_of_pacticipant_Foo_exists_with_2_environments_that_aren&#39;t_test_available_for_deployment"></a>
Given **version 5556b8149bf8bac76bc30f50a8a2dd4c22c85f30 of pacticipant Foo exists with 2 environments that aren't test available for deployment**, upon receiving **a request for a pacticipant version** from Pact Broker Client, with
```json
{
"method": "GET",
"path": "/HAL-REL-PLACEHOLDER-PB-PACTICIPANT-VERSION-Foo-5556b8149bf8bac76bc30f50a8a2dd4c22c85f30",
"headers": {
"Accept": "application/hal+json"
}
}
```
Pact Broker will respond with:
```json
{
"status": 200,
"headers": {
"Content-Type": "application/hal+json;charset=utf-8"
},
"body": {
"_links": {
"pb:record-deployment": [
{
"name": "prod",
"href": "href"
},
{
"name": "dev",
"href": "href"
}
]
}
}
}
```
<a name="a_request_for_a_pacticipant_version_given_version_5556b8149bf8bac76bc30f50a8a2dd4c22c85f30_of_pacticipant_Foo_exists_with_a_test_environment_available_for_deployment"></a>
Given **version 5556b8149bf8bac76bc30f50a8a2dd4c22c85f30 of pacticipant Foo exists with a test environment available for deployment**, upon receiving **a request for a pacticipant version** from Pact Broker Client, with
```json
{
"method": "GET",
"path": "/HAL-REL-PLACEHOLDER-PB-PACTICIPANT-VERSION-Foo-5556b8149bf8bac76bc30f50a8a2dd4c22c85f30",
"headers": {
"Accept": "application/hal+json"
}
}
```
Pact Broker will respond with:
```json
{
"status": 200,
"headers": {
"Content-Type": "application/hal+json;charset=utf-8"
},
"body": {
"_links": {
"pb:record-deployment": [
{
"name": "test",
"href": "http://localhost:1234/HAL-REL-PLACEHOLDER-PB-RECORD-DEPLOYMENT-FOO-5556B8149BF8BAC76BC30F50A8A2DD4C22C85F30-TEST"
}
]
}
}
}
```
<a name="a_request_for_a_pacticipant_version_given_version_5556b8149bf8bac76bc30f50a8a2dd4c22c85f30_of_pacticipant_Foo_does_not_exist"></a>
Given **version 5556b8149bf8bac76bc30f50a8a2dd4c22c85f30 of pacticipant Foo does not exist**, upon receiving **a request for a pacticipant version** from Pact Broker Client, with
```json
{
"method": "GET",
"path": "/HAL-REL-PLACEHOLDER-PB-PACTICIPANT-VERSION-Foo-5556b8149bf8bac76bc30f50a8a2dd4c22c85f30",
"headers": {
"Accept": "application/hal+json"
}
}
```
Pact Broker will respond with:
```json
{
"status": 404
}
```
<a name="a_request_for_the_compatibility_matrix_for_a_pacticipant_that_does_not_exist"></a>
Upon receiving **a request for the compatibility matrix for a pacticipant that does not exist** from Pact Broker Client, with
```json
Expand Down Expand Up @@ -535,6 +628,36 @@ Pact Broker will respond with:
}
}
```
<a name="a_request_for_the_environments_given_an_environment_with_name_test_exists"></a>
Given **an environment with name test exists**, upon receiving **a request for the environments** from Pact Broker Client, with
```json
{
"method": "GET",
"path": "/HAL-REL-PLACEHOLDER-PB-ENVIRONMENTS",
"headers": {
"Accept": "application/hal+json"
}
}
```
Pact Broker will respond with:
```json
{
"status": 200,
"headers": {
"Content-Type": "application/hal+json;charset=utf-8"
},
"body": {
"_links": {
"pb:environments": [
{
"name": "test",
"href": "href"
}
]
}
}
}
```
<a name="a_request_for_the_index_resource"></a>
Upon receiving **a request for the index resource** from Pact Broker Client, with
```json
Expand Down Expand Up @@ -652,6 +775,36 @@ Pact Broker will respond with:
}
}
```
<a name="a_request_for_the_index_resource_given_the_pb:pacticipant-version_and_pb:environments_relations_exist_in_the_index_resource"></a>
Given **the pb:pacticipant-version and pb:environments relations exist in the index resource**, upon receiving **a request for the index resource** from Pact Broker Client, with
```json
{
"method": "GET",
"path": "/",
"headers": {
"Accept": "application/hal+json"
}
}
```
Pact Broker will respond with:
```json
{
"status": 200,
"headers": {
"Content-Type": "application/hal+json;charset=utf-8"
},
"body": {
"_links": {
"pb:pacticipant-version": {
"href": "http://localhost:1234/HAL-REL-PLACEHOLDER-PB-PACTICIPANT-VERSION-{pacticipant}-{version}"
},
"pb:environments": {
"href": "http://localhost:1234/HAL-REL-PLACEHOLDER-PB-ENVIRONMENTS"
}
}
}
}
```
<a name="a_request_for_the_index_resource_given_the_pb:pacticipant-version_relation_exists_in_the_index_resource"></a>
Given **the pb:pacticipant-version relation exists in the index resource**, upon receiving **a request for the index resource** from Pact Broker Client, with
```json
Expand Down Expand Up @@ -1775,6 +1928,33 @@ Pact Broker will respond with:
}
}
```
<a name="a_request_to_record_a_deployment_given_version_5556b8149bf8bac76bc30f50a8a2dd4c22c85f30_of_pacticipant_Foo_exists_with_a_test_environment_available_for_deployment"></a>
Given **version 5556b8149bf8bac76bc30f50a8a2dd4c22c85f30 of pacticipant Foo exists with a test environment available for deployment**, upon receiving **a request to record a deployment** from Pact Broker Client, with
```json
{
"method": "POST",
"path": "/HAL-REL-PLACEHOLDER-PB-RECORD-DEPLOYMENT-FOO-5556B8149BF8BAC76BC30F50A8A2DD4C22C85F30-TEST",
"headers": {
"Content-Type": "application/json",
"Accept": "application/hal+json"
},
"body": {
"replacedPreviousDeployedVersion": true
}
}
```
Pact Broker will respond with:
```json
{
"status": 201,
"headers": {
"Content-Type": "application/hal+json;charset=utf-8"
},
"body": {
"replacedPreviousDeployedVersion": true
}
}
```
<a name="a_request_to_register_the_repository_URL_of_a_pacticipant_given_the_&#39;Pricing_Service&#39;_already_exists_in_the_pact-broker"></a>
Given **the 'Pricing Service' already exists in the pact-broker**, upon receiving **a request to register the repository URL of a pacticipant** from Pact Broker Client, with
```json
Expand Down
27 changes: 27 additions & 0 deletions lib/pact_broker/client/cli/broker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,33 @@ def list_latest_pact_versions(*required_but_ignored)
exit(1) unless result.success
end

ignored_and_hidden_potential_options_from_environment_variables
desc "record-deployment", "Record deployment of a pacticipant version to an environment"
method_option :pacticipant, required: true, aliases: "-a", desc: "The name of the pacticipant that was deployed."
method_option :version, required: true, aliases: "-e", desc: "The pacticipant version number that was deployed."
method_option :environment, required: true, desc: "The name of the environment that the pacticipant version was deployed to."
method_option :replaced_previous_deployed_version, type: :boolean, default: true, required: false, desc: "Whether or not this deployment replaced the previous deployed version. If it did, the previous deployed version of this pacticipant will be marked as undeployed in the Pact Broker."
method_option :output, aliases: "-o", desc: "json or text", default: 'text'
shared_authentication_options

def record_deployment
require 'pact_broker/client/versions/record_deployment'
params = {
pacticipant_name: options.pacticipant,
version_number: options.version,
environment_name: options.environment,
replaced_previous_deployed_version: options.replaced_previous_deployed_version,
output: options.output
}
result = PactBroker::Client::Versions::RecordDeployment.call(
params,
options.broker_base_url,
pact_broker_client_options
)
$stdout.puts result.message
exit(1) unless result.success
end

ignored_and_hidden_potential_options_from_environment_variables
desc 'version', "Show the pact_broker-client gem version"
def version
Expand Down
30 changes: 27 additions & 3 deletions lib/pact_broker/client/hal/entity.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require 'erb'
require 'delegate'
require 'pact_broker/client/hal/link'
require 'pact_broker/client/hal/links'

module PactBroker
module Client
Expand Down Expand Up @@ -51,10 +52,27 @@ def _link(key, fallback_key = nil)
end
end

def _links(key)
if @links[key] && @links[key].is_a?(Array)
link_collection = @links[key].collect do | hash |
Link.new(hash, @client)
end
Links.new(@href, key, link_collection)
elsif @links[key].is_a?(Hash)
Links.new(@href, key, [Link.new(@links[key], @client)])
else
nil
end
end

def _link!(key)
_link(key) or raise RelationNotFoundError.new("Could not find relation '#{key}' in resource at #{@href}")
end

def _links!(key)
_links(key) or raise RelationNotFoundError.new("Could not find relation '#{key}' in resource at #{@href}")
end

def success?
true
end
Expand Down Expand Up @@ -85,7 +103,7 @@ def respond_to_missing?(method_name, include_private = false)
@data.key?(method_name) || @links.key?(method_name)
end

def assert_success!
def assert_success!(_ignored = nil)
self
end
end
Expand All @@ -107,8 +125,14 @@ def success?
false
end

def assert_success!
raise ErrorResponseReturned.new("Error retrieving #{@href} status=#{response ? response.code: nil} #{response ? response.raw_body : ''}")
def assert_success!(messages = {})
default_message = "Error retrieving #{@href} status=#{response ? response.status: nil} #{response ? response.raw_body : ''}".strip
message = if response && messages[response.status]
(messages[response.status] || "") + " (#{default_message})"
else
default_message
end
raise ErrorResponseReturned.new(message)
end
end
end
Expand Down
4 changes: 4 additions & 0 deletions lib/pact_broker/client/hal/http_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ def body
end
end

def headers
__getobj__().to_hash
end

def raw_body
__getobj__().body
end
Expand Down
32 changes: 32 additions & 0 deletions lib/pact_broker/client/hal/links.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
require 'uri'
require 'delegate'

module PactBroker
module Client
module Hal
class Links
def initialize(href, key, links)
@href = href
@key = key
@links = links
end

def find(name, not_found_message = nil)
link = links.find{ | link | link.name == name }
if link
link
else
names = links.collect(&:name).compact.uniq
message = not_found_message || "Could not find relation '#{key}' with name '#{name}' in resource at #{href}."
available_options = names.any? ? names.join(", ") : "<none found>"
raise RelationNotFoundError.new(message.chomp(".") + ". Available options: #{available_options}")
end
end

private

attr_reader :links, :key, :href
end
end
end
end
11 changes: 11 additions & 0 deletions lib/pact_broker/client/hal_client_methods.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'pact_broker/client/hal'
require 'pact_broker/client/retry'

module PactBroker
module Client
Expand All @@ -10,6 +11,16 @@ def create_index_entry_point(pact_broker_base_url, pact_broker_client_options)
def create_http_client(pact_broker_client_options)
PactBroker::Client::Hal::HttpClient.new(pact_broker_client_options.merge(pact_broker_client_options[:basic_auth] || {}))
end

def index_entry_point
@index_entry_point ||= create_index_entry_point(pact_broker_base_url, pact_broker_client_options)
end

def index_resource
@index_resource ||= Retry.while_error do
index_entry_point.get!
end
end
end
end
end
Loading

0 comments on commit 6e5e208

Please sign in to comment.