Skip to content

Commit

Permalink
feat(create-webhook): support creating webhooks for contract_publishe…
Browse files Browse the repository at this point in the history
…d, provider_verification_succeeded and provider_verification_failed events, and allow description to be set
  • Loading branch information
bethesque committed Feb 13, 2020
1 parent 6e3d30f commit b76f9c4
Show file tree
Hide file tree
Showing 7 changed files with 216 additions and 30 deletions.
75 changes: 74 additions & 1 deletion doc/pacts/markdown/Pact Broker Client - Pact Broker.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@

* [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 create a webhook with every possible event type](#a_request_to_create_a_webhook_with_every_possible_event_type_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

* [A request to get the Pricing Service](#a_request_to_get_the_Pricing_Service_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 @@ -734,6 +736,7 @@ Upon receiving **a request to create a global webhook with a JSON body** from Pa
"Accept": "application/hal+json"
},
"body": {
"description": "a webhook",
"events": [
{
"name": "contract_content_changed"
Expand Down Expand Up @@ -783,6 +786,7 @@ Given **'Condor' does not exist in the pact-broker**, upon receiving **a request
"Accept": "application/hal+json"
},
"body": {
"description": "a webhook",
"events": [
{
"name": "contract_content_changed"
Expand Down Expand Up @@ -818,12 +822,13 @@ Given **the 'Pricing Service' and 'Condor' already exist in the pact-broker**, u
```json
{
"method": "put",
"path": "/webhooks/9999",
"path": "/webhooks/696c5f93-1b7f-44bc-8d03-59440fcaa9a0",
"headers": {
"Content-Type": "application/json",
"Accept": "application/hal+json"
},
"body": {
"description": "a webhook",
"events": [
{
"name": "contract_content_changed"
Expand Down Expand Up @@ -879,6 +884,7 @@ Given **the 'Pricing Service' and 'Condor' already exist in the pact-broker**, u
"Accept": "application/hal+json"
},
"body": {
"description": "a webhook",
"events": [
{
"name": "contract_content_changed"
Expand Down Expand Up @@ -931,6 +937,7 @@ Given **the 'Pricing Service' and 'Condor' already exist in the pact-broker**, u
"Accept": "application/hal+json"
},
"body": {
"description": "a webhook",
"events": [
{
"name": "contract_content_changed"
Expand Down Expand Up @@ -980,6 +987,7 @@ Upon receiving **a request to create a webhook with a JSON body for a consumer t
"Accept": "application/hal+json"
},
"body": {
"description": "a webhook",
"events": [
{
"name": "contract_content_changed"
Expand Down Expand Up @@ -1031,6 +1039,7 @@ Given **the 'Pricing Service' and 'Condor' already exist in the pact-broker**, u
"Accept": "application/hal+json"
},
"body": {
"description": "a webhook",
"events": [
{
"name": "contract_content_changed"
Expand Down Expand Up @@ -1083,6 +1092,7 @@ Given **the 'Pricing Service' and 'Condor' already exist in the pact-broker**, u
"Accept": "application/hal+json"
},
"body": {
"description": "a webhook",
"events": [
{
"name": "contract_content_changed"
Expand Down Expand Up @@ -1119,6 +1129,68 @@ Pact Broker will respond with:
}
}
```
<a name="a_request_to_create_a_webhook_with_every_possible_event_type_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 every possible event type** from Pact Broker Client, with
```json
{
"method": "post",
"path": "/webhooks/provider/Pricing%20Service/consumer/Condor",
"headers": {
"Content-Type": "application/json",
"Accept": "application/hal+json"
},
"body": {
"description": "a webhook",
"events": [
{
"name": "contract_content_changed"
},
{
"name": "contract_published"
},
{
"name": "provider_verification_published"
},
{
"name": "provider_verification_succeeded"
},
{
"name": "provider_verification_failed"
}
],
"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_get_the_Pricing_Service_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 get the Pricing Service** from Pact Broker Client, with
```json
Expand Down Expand Up @@ -1739,6 +1811,7 @@ Given **the 'Pricing Service' and 'Condor' already exist in the pact-broker**, u
"Accept": "application/hal+json"
},
"body": {
"description": "a webhook",
"events": [
{
"name": "contract_content_changed"
Expand Down
42 changes: 25 additions & 17 deletions lib/pact_broker/client/cli/broker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -194,11 +194,21 @@ def pact_broker_client_options
client_options
end

def run_webhook_commands webhook_url
require 'pact_broker/client/webhooks/create'
def parse_webhook_events
events = []
events << 'contract_content_changed' if options.contract_content_changed
events << 'contract_published' if options.contract_published
events << 'provider_verification_published' if options.provider_verification_published
events << 'provider_verification_succeeded' if options.provider_verification_succeeded
events << 'provider_verification_failed' if options.provider_verification_failed
events
end

if !(options.contract_content_changed || options.provider_verification_published)
raise PactBroker::Client::Error.new("You must select at least one of --contract-content-changed or --provider-verification-published")
def parse_webhook_options(webhook_url)
events = parse_webhook_events

if events.size == 0
raise WebhookCreationError.new("You must specify at least one of --contract-content-changed, --contract-published, --provider-verification-published, --provider-verification-succeeded or --provider-verification-failed")
end

username = options.user ? options.user.split(":", 2).first : nil
Expand All @@ -212,15 +222,11 @@ def run_webhook_commands webhook_url
begin
body = File.read(filepath)
rescue StandardError => e
raise PactBroker::Client::Error.new("Couldn't read data from file \"#{filepath}\" due to #{e.class} #{e.message}")
raise WebhookCreationError.new("Couldn't read data from file \"#{filepath}\" due to #{e.class} #{e.message}")
end
end

events = []
events << 'contract_content_changed' if options.contract_content_changed
events << 'provider_verification_published' if options.provider_verification_published

params = {
{
uuid: options.uuid,
http_method: options.request,
url: webhook_url,
Expand All @@ -233,13 +239,15 @@ def run_webhook_commands webhook_url
events: events
}

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

def run_webhook_commands webhook_url
require 'pact_broker/client/webhooks/create'
result = PactBroker::Client::Webhooks::Create.call(parse_webhook_options(webhook_url), 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
end
Expand Down
4 changes: 4 additions & 0 deletions lib/pact_broker/client/cli/custom_thor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,12 @@ def self.shared_options_for_webhook_commands
method_option :broker_username, desc: "Pact Broker basic auth username"
method_option :broker_password, aliases: "-p", desc: "Pact Broker basic auth password"
method_option :broker_token, aliases: "-k", desc: "Pact Broker bearer token"
method_option :description, desc: "The description of the webhook"
method_option :contract_content_changed, type: :boolean, desc: "Trigger this webhook when the pact content changes"
method_option :contract_published, type: :boolean, desc: "Trigger this webhook when a pact is published"
method_option :provider_verification_published, type: :boolean, desc: "Trigger this webhook when a provider verification result is published"
method_option :provider_verification_failed, type: :boolean, desc: "Trigger this webhook when a failed provider verification result is published"
method_option :provider_verification_succeeded, type: :boolean, desc: "Trigger this webhook when a successful provider verification result is published"
method_option :verbose, aliases: "-v", type: :boolean, default: false, required: false, desc: "Verbose output. Default: false"
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/pact_broker/client/webhooks/create.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def request_body
username: params.username,
password: params.password
}
}
}.tap { |req| req[:description] = params.description if params.description }
end

def request_body_with_optional_consumer_and_provider
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ module CLI
end

it "raises an error" do
expect { subject }.to raise_error PactBroker::Client::Error, /You must select at least one/
expect { subject }.to raise_error WebhookCreationError, /You must specify at least one/
end
end

Expand Down Expand Up @@ -159,8 +159,8 @@ module CLI

let(:data) { "@doesnotexist.json" }

it "raises a PactBroker::Client::Error" do
expect { subject }.to raise_error PactBroker::Client::Error, /Couldn't read data from file/
it "raises a WebhookCreationError" do
expect { subject }.to raise_error WebhookCreationError, /Couldn't read data from file/
end
end

Expand Down
Loading

0 comments on commit b76f9c4

Please sign in to comment.