Skip to content

Commit

Permalink
feat: support specifying team UUID when creating a webhook in Pactflow
Browse files Browse the repository at this point in the history
  • Loading branch information
bethesque committed Jun 24, 2021
1 parent 82cdfa0 commit c4201e1
Show file tree
Hide file tree
Showing 13 changed files with 400 additions and 72 deletions.
98 changes: 51 additions & 47 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,44 +78,44 @@ Usage:
pact-broker can-i-deploy -a, --pacticipant=PACTICIPANT -b, --broker-base-url=BROKER_BASE_URL
Options:
-a, --pacticipant=PACTICIPANT
-a, --pacticipant=PACTICIPANT
# The pacticipant name. Use once for each pacticipant being checked.
-e, [--version=VERSION]
-e, [--version=VERSION]
# The pacticipant version. Must be entered after the --pacticipant that it relates to.
[--ignore=IGNORE]
# The pacticipant name to ignore. Use once for each pacticipant being ignored.
A specific version can be ignored by also specifying a --version after the
[--ignore=IGNORE]
# The pacticipant name to ignore. Use once for each pacticipant being ignored.
A specific version can be ignored by also specifying a --version after the
pacticipant name option.
-l, [--latest=[TAG]]
# Use the latest pacticipant version. Optionally specify a TAG to use the
-l, [--latest=[TAG]]
# Use the latest pacticipant version. Optionally specify a TAG to use the
latest version with the specified tag.
[--to=TAG]
[--to=TAG]
# This is too hard to explain in a short sentence. Look at the examples.
-o, [--output=OUTPUT]
-o, [--output=OUTPUT]
# json or table
# Default: table
[--retry-while-unknown=TIMES]
# The number of times to retry while there is an unknown verification result
[--retry-while-unknown=TIMES]
# The number of times to retry while there is an unknown verification result
(ie. the provider verification is likely still running)
# Default: 0
[--retry-interval=SECONDS]
[--retry-interval=SECONDS]
# The time between retries in seconds. Use in conjuction with --retry-while-unknown
# Default: 10
[--dry-run], [--no-dry-run]
[--dry-run], [--no-dry-run]
# When enabled, always exits process with a success code. Can also be enabled by setting
the environment variable PACT_BROKER_CAN_I_DEPLOY_DRY_RUN=true.
-b, --broker-base-url=BROKER_BASE_URL
-b, --broker-base-url=BROKER_BASE_URL
# The base URL of the Pact Broker
-u, [--broker-username=BROKER_USERNAME]
-u, [--broker-username=BROKER_USERNAME]
# Pact Broker basic auth username
-p, [--broker-password=BROKER_PASSWORD]
-p, [--broker-password=BROKER_PASSWORD]
# Pact Broker basic auth password
-k, [--broker-token=BROKER_TOKEN]
-k, [--broker-token=BROKER_TOKEN]
# Pact Broker bearer token
-v, [--verbose], [--no-verbose]
-v, [--verbose], [--no-verbose]
# Verbose output. Default: false
```

Expand Down Expand Up @@ -235,27 +235,19 @@ Usage:
Options:
-X, --request=METHOD
# HTTP method
# Webhook HTTP method
-H, [--header=one two three]
# Header
# Webhook Header
-d, [--data=DATA]
# Data
# Webhook payload (file or string)
-u, [--user=USER]
# Basic auth username and password eg. username:password
# Webhook basic auth username and password eg. username:password
[--consumer=CONSUMER]
# Consumer name
[--provider=PROVIDER]
# Provider name
-b, --broker-base-url=BROKER_BASE_URL
# The base URL of the Pact Broker
[--broker-username=BROKER_USERNAME]
# Pact Broker basic auth username
-p, [--broker-password=BROKER_PASSWORD]
# Pact Broker basic auth password
-k, [--broker-token=BROKER_TOKEN]
# Pact Broker bearer token
[--description=DESCRIPTION]
# The description of the webhook
# Webhook description
[--contract-content-changed], [--no-contract-content-changed]
# Trigger this webhook when the pact content changes
[--contract-published], [--no-contract-published]
Expand All @@ -266,6 +258,16 @@ Options:
# Trigger this webhook when a failed provider verification result is published
[--provider-verification-succeeded], [--no-provider-verification-succeeded]
# Trigger this webhook when a successful provider verification result is published
[--team-uuid=UUID]
# UUID of the Pactflow team to which the webhook should be assigned (Pactflow only)
-b, --broker-base-url=BROKER_BASE_URL
# The base URL of the Pact Broker
-u, [--broker-username=BROKER_USERNAME]
# Pact Broker basic auth username
-p, [--broker-password=BROKER_PASSWORD]
# Pact Broker basic auth password
-k, [--broker-token=BROKER_TOKEN]
# Pact Broker bearer token
-v, [--verbose], [--no-verbose]
# Verbose output. Default: false
Expand All @@ -283,27 +285,19 @@ Usage:
Options:
-X, --request=METHOD
# HTTP method
# Webhook HTTP method
-H, [--header=one two three]
# Header
# Webhook Header
-d, [--data=DATA]
# Data
# Webhook payload (file or string)
-u, [--user=USER]
# Basic auth username and password eg. username:password
# Webhook basic auth username and password eg. username:password
[--consumer=CONSUMER]
# Consumer name
[--provider=PROVIDER]
# Provider name
-b, --broker-base-url=BROKER_BASE_URL
# The base URL of the Pact Broker
[--broker-username=BROKER_USERNAME]
# Pact Broker basic auth username
-p, [--broker-password=BROKER_PASSWORD]
# Pact Broker basic auth password
-k, [--broker-token=BROKER_TOKEN]
# Pact Broker bearer token
[--description=DESCRIPTION]
# The description of the webhook
# Webhook description
[--contract-content-changed], [--no-contract-content-changed]
# Trigger this webhook when the pact content changes
[--contract-published], [--no-contract-published]
Expand All @@ -314,6 +308,16 @@ Options:
# Trigger this webhook when a failed provider verification result is published
[--provider-verification-succeeded], [--no-provider-verification-succeeded]
# Trigger this webhook when a successful provider verification result is published
[--team-uuid=UUID]
# UUID of the Pactflow team to which the webhook should be assigned (Pactflow only)
-b, --broker-base-url=BROKER_BASE_URL
# The base URL of the Pact Broker
-u, [--broker-username=BROKER_USERNAME]
# Pact Broker basic auth username
-p, [--broker-password=BROKER_PASSWORD]
# Pact Broker basic auth password
-k, [--broker-token=BROKER_TOKEN]
# Pact Broker bearer token
-v, [--verbose], [--no-verbose]
# Verbose output. Default: false
--uuid=UUID
Expand Down
1 change: 0 additions & 1 deletion doc/pacts/markdown/Pact Broker Client - Pact Broker.md
Original file line number Diff line number Diff line change
Expand Up @@ -2264,7 +2264,6 @@ Given **the 'Pricing Service' and 'Condor' already exist in the pact-broker**, u
}
],
"request": {
"url": null,
"method": "POST",
"headers": {
"Foo": "bar",
Expand Down
94 changes: 94 additions & 0 deletions doc/pacts/markdown/Pact Broker Client - Pactflow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
### A pact between Pact Broker Client and Pactflow

#### Requests from Pact Broker Client to Pactflow

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

* [A request to create a webhook for a team](#a_request_to_create_a_webhook_for_a_team_given_a_team_with_UUID_2abbc12a-427d-432a-a521-c870af1739d9_exists) given a team with UUID 2abbc12a-427d-432a-a521-c870af1739d9 exists

#### Interactions

<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"
}
}
```
Pactflow will respond with:
```json
{
"status": 200,
"headers": {
"Content-Type": "application/hal+json;charset=utf-8"
},
"body": {
"_links": {
"pb:webhooks": {
"href": "http://localhost:1235/HAL-REL-PLACEHOLDER-PB-WEBHOOKS"
},
"pb:pacticipants": {
"href": "http://localhost:1235/HAL-REL-PLACEHOLDER-PB-PACTICIPANTS"
},
"pb:pacticipant": {
"href": "http://localhost:1235/HAL-REL-PLACEHOLDER-PB-PACTICIPANT-{pacticipant}"
}
}
}
}
```
<a name="a_request_to_create_a_webhook_for_a_team_given_a_team_with_UUID_2abbc12a-427d-432a-a521-c870af1739d9_exists"></a>
Given **a team with UUID 2abbc12a-427d-432a-a521-c870af1739d9 exists**, upon receiving **a request to create a webhook for a team** from Pact Broker Client, with
```json
{
"method": "post",
"path": "/HAL-REL-PLACEHOLDER-PB-WEBHOOKS",
"headers": {
"Content-Type": "application/json",
"Accept": "application/hal+json"
},
"body": {
"description": "a webhook",
"events": [
{
"name": "contract_content_changed"
}
],
"request": {
"url": "https://webhook",
"method": "POST",
"headers": {
"Foo": "bar",
"Bar": "foo"
},
"body": {
"some": "body"
}
},
"teamUuid": "2abbc12a-427d-432a-a521-c870af1739d9"
}
}
```
Pactflow will respond with:
```json
{
"status": 201,
"headers": {
"Content-Type": "application/hal+json;charset=utf-8"
},
"body": {
"description": "a webhook",
"teamUuid": "2abbc12a-427d-432a-a521-c870af1739d9",
"_links": {
"self": {
"href": "http://localhost:1234/some-url",
"title": "A title"
}
}
}
}
```
1 change: 1 addition & 0 deletions doc/pacts/markdown/README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
### Pacts for Pact Broker Client

* [Pact Broker](Pact%20Broker%20Client%20-%20Pact%20Broker.md)
* [Pactflow](Pact%20Broker%20Client%20-%20Pactflow.md)
6 changes: 4 additions & 2 deletions lib/pact_broker/client/cli/webhook_commands.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@ def self.shared_options_for_webhook_commands
method_option :user, aliases: "-u", desc: "Webhook basic auth username and password eg. username:password"
method_option :consumer, desc: "Consumer name"
method_option :provider, desc: "Provider name"
method_option :description, desc: "Wwebhook description"
method_option :description, desc: "Webhook description"
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 :team_uuid, banner: "UUID", desc: "UUID of the Pactflow team to which the webhook should be assigned (Pactflow only)"
shared_authentication_options
end
end
Expand Down Expand Up @@ -97,7 +98,8 @@ def parse_webhook_options(webhook_url)
body: body,
consumer: options.consumer,
provider: options.provider,
events: events
events: events,
team_uuid: options.team_uuid
}
end

Expand Down
22 changes: 14 additions & 8 deletions lib/pact_broker/client/webhooks/create.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
require 'ostruct'
require 'json'
require 'pact_broker/client/command_result'
require "pact_broker/client/backports"

module PactBroker
module Client
Expand Down Expand Up @@ -61,16 +62,17 @@ def create_webhook_with_optional_consumer_and_provider

def request_body
webhook_request_body = JSON.parse(params.body) rescue params.body
request_params = {
url: params.url,
method: params.http_method,
headers: params.headers,
body: webhook_request_body,
username: params.username,
password: params.password
}.compact
{
events: params.events.collect{ | event | { "name" => event }},
request: {
url: params.url,
method: params.http_method,
headers: params.headers,
body: webhook_request_body,
username: params.username,
password: params.password
}
request: request_params
}.tap { |req| req[:description] = params.description if params.description }
end

Expand All @@ -85,6 +87,10 @@ def request_body_with_optional_consumer_and_provider
body[:provider] = { name: params.provider }
end

if params.team_uuid
body[:teamUuid] = params.team_uuid
end

body
end

Expand Down
12 changes: 12 additions & 0 deletions script/webhook-commands.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/bin/sh

bundle exec bin/pact-broker create-webhook \
'https://api.github.com/repos/foo/bar/statuses/${pactbroker.consumerVersionNumber}' \
-X POST \
-H "Content-Type: application/json" \
-d '{ "state": "${pactbroker.githubVerificationStatus}", "description": "Pact Verification Tests ${pactbroker.providerVersionTags}", "context": "${pactbroker.providerName}", "target_url": "${pactbroker.verificationResultUrl}" }' \
--user username:password \
--description "Publish pact verification status to Github" \
--contract-published \
--provider-verification-published \
--team-uuid 4ac05ed8-9e3b-4159-96c0-ad19e3b93658
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ module CLI
broker_username: "username",
broker_password: "password",
contract_content_changed: true,
verbose: true
verbose: true,
team_uuid: "1234"
}
end

Expand All @@ -49,7 +50,8 @@ module CLI
body: "data",
consumer: "consumer",
provider: "provider",
events: ["contract_content_changed"]
events: ["contract_content_changed"],
team_uuid: "1234"
}.tap { |it| Pact::Fixture.add_fixture(:create_webhook_params, it) }
end

Expand Down
1 change: 0 additions & 1 deletion spec/pacts/pact_broker_client-pact_broker.json
Original file line number Diff line number Diff line change
Expand Up @@ -1846,7 +1846,6 @@
}
],
"request": {
"url": null,
"method": "POST",
"headers": {
"Foo": "bar",
Expand Down
Loading

0 comments on commit c4201e1

Please sign in to comment.