Skip to content

Commit

Permalink
Add participant label options to webhook create/update commands
Browse files Browse the repository at this point in the history
  • Loading branch information
Ivan Rabotyaga committed Oct 5, 2021
1 parent f5866a3 commit f908268
Show file tree
Hide file tree
Showing 6 changed files with 363 additions and 1 deletion.
112 changes: 112 additions & 0 deletions doc/pacts/markdown/Pact Broker Client - Pact Broker.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,14 @@

* [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 specified by a label](#a_request_to_create_a_webhook_with_a_JSON_body_for_a_consumer_specified_by_a_label_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 JSON body for a provider specified by a label](#a_request_to_create_a_webhook_with_a_JSON_body_for_a_provider_specified_by_a_label_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 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
Expand Down Expand Up @@ -1336,6 +1340,60 @@ Pact Broker will respond with:
}
}
```
<a name="a_request_to_create_a_webhook_with_a_JSON_body_for_a_consumer_specified_by_a_label_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 specified by a label** 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"
},
"username": "username",
"password": "password"
},
"consumer": {
"label": "consumer_label"
}
}
}
```
Pact Broker will respond with:
```json
{
"status": 201,
"headers": {
"Content-Type": "application/hal+json;charset=utf-8"
},
"body": {
"description": "a webhook",
"_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_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
Expand Down Expand Up @@ -1442,6 +1500,60 @@ Pact Broker will respond with:
}
}
```
<a name="a_request_to_create_a_webhook_with_a_JSON_body_for_a_provider_specified_by_a_label_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 specified by a label** 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"
},
"username": "username",
"password": "password"
},
"provider": {
"label": "provider_label"
}
}
}
```
Pact Broker will respond with:
```json
{
"status": 201,
"headers": {
"Content-Type": "application/hal+json;charset=utf-8"
},
"body": {
"description": "a webhook",
"_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
17 changes: 16 additions & 1 deletion lib/pact_broker/client/cli/webhook_commands.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ def self.shared_options_for_webhook_commands
method_option :data, aliases: "-d", desc: "Webhook payload (file or string)"
method_option :user, aliases: "-u", desc: "Webhook basic auth username and password eg. username:password"
method_option :consumer, desc: "Consumer name"
method_option :consumer_label, desc: "Consumer label, mutually exclusive with consumer name"
method_option :provider, desc: "Provider name"
method_option :provider_label, desc: "Provider label, mutually exclusive with provider name"
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"
Expand Down Expand Up @@ -70,6 +72,7 @@ def parse_webhook_events
end

def parse_webhook_options(webhook_url)
validate_mutual_exclusiveness_of_participant_name_and_label_options
events = parse_webhook_events

# TODO update for contract_requiring_verification_published when released
Expand Down Expand Up @@ -102,7 +105,9 @@ def parse_webhook_options(webhook_url)
password: password,
body: body,
consumer: options.consumer,
consumer_label: options.consumer_label,
provider: options.provider,
provider_label: options.provider_label,
events: events,
team_uuid: options.team_uuid
}
Expand All @@ -118,10 +123,20 @@ def run_webhook_commands webhook_url
rescue PactBroker::Client::Error => e
raise WebhookCreationError, "#{e.class} - #{e.message}"
end

def validate_mutual_exclusiveness_of_participant_name_and_label_options
if options.consumer && options.consumer_label
raise WebhookCreationError.new("Consumer name (--consumer) and label (--consumer_label) options are mutually exclusive")
end

if options.provider && options.provider_label
raise WebhookCreationError.new("Provider name (--provider) and label (--provider_label) options are mutually exclusive")
end
end
end
end
end
end
end
end
end
end
4 changes: 4 additions & 0 deletions lib/pact_broker/client/webhooks/create.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,14 @@ def request_body_with_optional_consumer_and_provider

if params.consumer
body[:consumer] = { name: params.consumer }
elsif params.consumer_label
body[:consumer] = { label: params.consumer_label }
end

if params.provider
body[:provider] = { name: params.provider }
elsif params.provider_label
body[:provider] = { label: params.provider_label }
end

if params.team_uuid
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ module CLI
password: "password",
body: "data",
consumer: "consumer",
consumer_label: nil,
provider: "provider",
provider_label: nil,
events: ["contract_content_changed"],
team_uuid: "1234"
}.tap { |it| Pact::Fixture.add_fixture(:create_webhook_params, it) }
Expand Down Expand Up @@ -223,6 +225,59 @@ module CLI
expect { subject }.to raise_error(WebhookCreationError, /foo/)
end
end

end

context "when both consumer name and label options are specified" do
before do
options_hash[:consumer_label] = "consumer_label"
broker.options = OpenStruct.new(options_hash)
end

it "raises a WebhookCreationError" do
expect { subject }.to raise_error(
WebhookCreationError,
"Consumer name (--consumer) and label (--consumer_label) options are mutually exclusive"
)
end
end

context "when both provider name and label options are specified" do
before do
options_hash[:provider_label] = "provider_label"
broker.options = OpenStruct.new(options_hash)
end

it "raises a WebhookCreationError" do
expect { subject }.to raise_error(
WebhookCreationError,
"Provider name (--provider) and label (--provider_label) options are mutually exclusive"
)
end
end

context "when participant labels are specified" do
before do
options_hash.delete(:consumer)
options_hash.delete(:provider)
options_hash.merge!(consumer_label: 'consumer_label', provider_label: 'provider_label')
expected_params.merge!(
consumer: nil,
consumer_label: 'consumer_label',
provider: nil,
provider_label: 'provider_label'
)

broker.options = OpenStruct.new(options_hash)
end

it "calls PactBroker::Client::Webhooks::Create with participant labels in params" do
expect(PactBroker::Client::Webhooks::Create).to receive(:call) do | params, _, _ |
expect(params).to eq expected_params
command_result
end
subject
end
end
end
end
Expand Down
Loading

0 comments on commit f908268

Please sign in to comment.