Skip to content

Commit

Permalink
feat: allow webhooks with optional consumer and provider to be create…
Browse files Browse the repository at this point in the history
…d using the create-webhooks CLI
  • Loading branch information
bethesque committed Jun 21, 2018
1 parent 19a6498 commit c6763b0
Show file tree
Hide file tree
Showing 12 changed files with 838 additions and 45 deletions.
241 changes: 241 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,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 index resource](#a_request_for_the_index_resource)

* [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
Expand All @@ -32,10 +34,18 @@

* [A request retrieve a pact for a specific version](#a_request_retrieve_a_pact_for_a_specific_version_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 create a global webhook with a JSON body](#a_request_to_create_a_global_webhook_with_a_JSON_body)

* [A request to create a webhook for a consumer and provider](#a_request_to_create_a_webhook_for_a_consumer_and_provider_given_'Condor'_does_not_exist_in_the_pact-broker) given 'Condor' does not exist in the pact-broker

* [A request to create a webhook with a JSON body for a consumer](#a_request_to_create_a_webhook_with_a_JSON_body_for_a_consumer_given_the_'Pricing_Service'_and_'Condor'_already_exist_in_the_pact-broker) given the 'Pricing Service' and 'Condor' already exist in the pact-broker

* [A request to create a webhook with a JSON body for a consumer and provider](#a_request_to_create_a_webhook_with_a_JSON_body_for_a_consumer_and_provider_given_the_'Pricing_Service'_and_'Condor'_already_exist_in_the_pact-broker) given the 'Pricing Service' and 'Condor' already exist in the pact-broker

* [A request to create a webhook with a JSON body for a consumer that does not exist](#a_request_to_create_a_webhook_with_a_JSON_body_for_a_consumer_that_does_not_exist)

* [A request to create a webhook with a JSON body for a provider](#a_request_to_create_a_webhook_with_a_JSON_body_for_a_provider_given_the_'Pricing_Service'_and_'Condor'_already_exist_in_the_pact-broker) given the 'Pricing Service' and 'Condor' already exist in the pact-broker

* [A request to create a webhook with a non-JSON body for a consumer and provider](#a_request_to_create_a_webhook_with_a_non-JSON_body_for_a_consumer_and_provider_given_the_'Pricing_Service'_and_'Condor'_already_exist_in_the_pact-broker) given the 'Pricing Service' and 'Condor' already exist in the pact-broker

* [A request to get the Pricing Service](#a_request_to_get_the_Pricing_Service_given_the_'Pricing_Service'_already_exists_in_the_pact-broker) given the 'Pricing Service' already exists in the pact-broker
Expand Down Expand Up @@ -461,6 +471,33 @@ Pact Broker will respond with:
}
}
```
<a name="a_request_for_the_index_resource"></a>
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:webhooks": {
"href": "http://localhost:1234/HAL-REL-PLACEHOLDER-PB-WEBHOOKS"
}
}
}
}
```
<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
Expand Down Expand Up @@ -648,6 +685,55 @@ Pact Broker will respond with:
}
}
```
<a name="a_request_to_create_a_global_webhook_with_a_JSON_body"></a>
Upon receiving **a request to create a global webhook with a JSON body** from Pact Broker Client, with
```json
{
"method": "post",
"path": "/HAL-REL-PLACEHOLDER-PB-WEBHOOKS",
"headers": {
"Content-Type": "application/json",
"Accept": "application/hal+json"
},
"body": {
"events": [
{
"name": "contract_content_changed"
}
],
"request": {
"url": "https://webhook",
"method": "POST",
"headers": {
"Foo": "bar",
"Bar": "foo"
},
"body": {
"some": "body"
},
"username": "username",
"password": "password"
}
}
}
```
Pact Broker will respond with:
```json
{
"status": 201,
"headers": {
"Content-Type": "application/hal+json;charset=utf-8"
},
"body": {
"_links": {
"self": {
"href": "http://localhost:1234/some-url",
"title": "A title"
}
}
}
}
```
<a name="a_request_to_create_a_webhook_for_a_consumer_and_provider_given_&#39;Condor&#39;_does_not_exist_in_the_pact-broker"></a>
Given **'Condor' does not exist in the pact-broker**, upon receiving **a request to create a webhook for a consumer and provider** from Pact Broker Client, with
```json
Expand Down Expand Up @@ -689,6 +775,58 @@ Pact Broker will respond with:
}
}
```
<a name="a_request_to_create_a_webhook_with_a_JSON_body_for_a_consumer_given_the_&#39;Pricing_Service&#39;_and_&#39;Condor&#39;_already_exist_in_the_pact-broker"></a>
Given **the 'Pricing Service' and 'Condor' already exist in the pact-broker**, upon receiving **a request to create a webhook with a JSON body for a consumer** from Pact Broker Client, with
```json
{
"method": "post",
"path": "/HAL-REL-PLACEHOLDER-PB-WEBHOOKS",
"headers": {
"Content-Type": "application/json",
"Accept": "application/hal+json"
},
"body": {
"events": [
{
"name": "contract_content_changed"
}
],
"request": {
"url": "https://webhook",
"method": "POST",
"headers": {
"Foo": "bar",
"Bar": "foo"
},
"body": {
"some": "body"
},
"username": "username",
"password": "password"
},
"consumer": {
"name": "Condor"
}
}
}
```
Pact Broker will respond with:
```json
{
"status": 201,
"headers": {
"Content-Type": "application/hal+json;charset=utf-8"
},
"body": {
"_links": {
"self": {
"href": "http://localhost:1234/some-url",
"title": "A title"
}
}
}
}
```
<a name="a_request_to_create_a_webhook_with_a_JSON_body_for_a_consumer_and_provider_given_the_&#39;Pricing_Service&#39;_and_&#39;Condor&#39;_already_exist_in_the_pact-broker"></a>
Given **the 'Pricing Service' and 'Condor' already exist in the pact-broker**, upon receiving **a request to create a webhook with a JSON body for a consumer and provider** from Pact Broker Client, with
```json
Expand Down Expand Up @@ -738,6 +876,109 @@ Pact Broker will respond with:
}
}
```
<a name="a_request_to_create_a_webhook_with_a_JSON_body_for_a_consumer_that_does_not_exist"></a>
Upon receiving **a request to create a webhook with a JSON body for a consumer that does not exist** from Pact Broker Client, with
```json
{
"method": "post",
"path": "/HAL-REL-PLACEHOLDER-PB-WEBHOOKS",
"headers": {
"Content-Type": "application/json",
"Accept": "application/hal+json"
},
"body": {
"events": [
{
"name": "contract_content_changed"
}
],
"request": {
"url": "https://webhook",
"method": "POST",
"headers": {
"Foo": "bar",
"Bar": "foo"
},
"body": {
"some": "body"
},
"username": "username",
"password": "password"
},
"consumer": {
"name": "Condor"
}
}
}
```
Pact Broker will respond with:
```json
{
"status": 400,
"headers": {
"Content-Type": "application/hal+json;charset=utf-8"
},
"body": {
"errors": {
"consumer.name": [
"Some error"
]
}
}
}
```
<a name="a_request_to_create_a_webhook_with_a_JSON_body_for_a_provider_given_the_&#39;Pricing_Service&#39;_and_&#39;Condor&#39;_already_exist_in_the_pact-broker"></a>
Given **the 'Pricing Service' and 'Condor' already exist in the pact-broker**, upon receiving **a request to create a webhook with a JSON body for a provider** from Pact Broker Client, with
```json
{
"method": "post",
"path": "/HAL-REL-PLACEHOLDER-PB-WEBHOOKS",
"headers": {
"Content-Type": "application/json",
"Accept": "application/hal+json"
},
"body": {
"events": [
{
"name": "contract_content_changed"
}
],
"request": {
"url": "https://webhook",
"method": "POST",
"headers": {
"Foo": "bar",
"Bar": "foo"
},
"body": {
"some": "body"
},
"username": "username",
"password": "password"
},
"provider": {
"name": "Pricing Service"
}
}
}
```
Pact Broker will respond with:
```json
{
"status": 201,
"headers": {
"Content-Type": "application/hal+json;charset=utf-8"
},
"body": {
"_links": {
"self": {
"href": "http://localhost:1234/some-url",
"title": "A title"
}
}
}
}
```
<a name="a_request_to_create_a_webhook_with_a_non-JSON_body_for_a_consumer_and_provider_given_the_&#39;Pricing_Service&#39;_and_&#39;Condor&#39;_already_exist_in_the_pact-broker"></a>
Given **the 'Pricing Service' and 'Condor' already exist in the pact-broker**, upon receiving **a request to create a webhook with a non-JSON body for a consumer and provider** from Pact Broker Client, with
```json
Expand Down
16 changes: 11 additions & 5 deletions lib/pact_broker/client/cli/broker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ module Client
module CLI
# Thor::Error will have its backtrace hidden
class PactPublicationError < ::Thor::Error; end
class WebhookCreationError < ::Thor::Error; end

class Broker < CustomThor
desc 'can-i-deploy', ''
Expand Down Expand Up @@ -103,8 +104,8 @@ def describe_version
method_option :header, aliases: "-H", type: :array, desc: "Header"
method_option :data, aliases: "-d", desc: "Data"
method_option :user, aliases: "-u", desc: "Basic auth username and password eg. username:password"
method_option :consumer, desc: "Consumer name", required: true
method_option :provider, desc: "Provider name", required: true
method_option :consumer, desc: "Consumer name"
method_option :provider, desc: "Provider name"
method_option :broker_base_url, required: true, aliases: "-b", desc: "The base URL of the Pact Broker"
method_option :broker_username, aliases: "-u", desc: "Pact Broker basic auth username"
method_option :broker_password, aliases: "-p", desc: "Pact Broker basic auth password"
Expand Down Expand Up @@ -151,9 +152,14 @@ def create_webhook webhook_url
provider: options.provider,
events: events
}
result = PactBroker::Client::Webhooks::Create.call(params, options.broker_base_url, pact_broker_client_options)
$stdout.puts result.message
exit(1) unless result.success

begin
result = PactBroker::Client::Webhooks::Create.call(params, options.broker_base_url, pact_broker_client_options)
$stdout.puts result.message
exit(1) unless result.success
rescue PactBroker::Client::Error => e
raise WebhookCreationError, "#{e.class} - #{e.message}"
end
end

desc 'version', "Show the pact_broker-client gem version"
Expand Down
1 change: 1 addition & 0 deletions lib/pact_broker/client/hal.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require 'pact_broker/client/hal/link'
require 'pact_broker/client/hal/entry_point'
require 'pact_broker/client/hal/http_client'
require 'pact_broker/client/hal/entity'
13 changes: 13 additions & 0 deletions lib/pact_broker/client/hal/entry_point.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
require 'pact_broker/client/hal/link'

module PactBroker
module Client
module Hal
class EntryPoint < Link
def initialize(url, http_client)
super({ "href" => url }, http_client)
end
end
end
end
end
14 changes: 11 additions & 3 deletions lib/pact_broker/client/hal/http_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def post href, body = nil, headers = {}
end

def create_request uri, http_method, body = nil, headers = {}
request = Net::HTTP.const_get(http_method).new(uri.to_s)
request = Net::HTTP.const_get(http_method).new(uri.request_uri)
request['Content-Type'] = "application/json" if ['Post', 'Put', 'Patch'].include?(http_method)
request['Accept'] = "application/hal+json"
headers.each do | key, value |
Expand All @@ -43,10 +43,10 @@ def create_request uri, http_method, body = nil, headers = {}
end

def perform_request request, uri
options = {:use_ssl => uri.scheme == 'https'}
response = Retry.until_true do
http = Net::HTTP.new(uri.host, uri.port, :ENV, options)
http = Net::HTTP.new(uri.host, uri.port, :ENV)
http.set_debug_output($stderr) if verbose
http.use_ssl = (uri.scheme == 'https')
http.start do |http|
http.request request
end
Expand All @@ -64,6 +64,14 @@ def body
end
end

def raw_body
__getobj__().body
end

def status
code.to_i
end

def success?
__getobj__().code.start_with?("2")
end
Expand Down
Loading

0 comments on commit c6763b0

Please sign in to comment.