Skip to content

Commit

Permalink
feat(create-or-update-webhook): show helpful error message when attem…
Browse files Browse the repository at this point in the history
…pting to create a webhook with a UUID on a version of the Pact Broker that doesn't support it
  • Loading branch information
bethesque committed Feb 13, 2020
1 parent a274bd7 commit 6e3d30f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 12 deletions.
21 changes: 15 additions & 6 deletions lib/pact_broker/client/webhooks/create.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ module PactBroker
module Client
module Webhooks
class Create

WEBHOOKS_WITH_OPTIONAL_PACTICICPANTS_NOT_SUPPORTED = "This version of the Pact Broker requires that both consumer and provider are specified for a webhook. Please upgrade your broker to >= 2.22.0 to create a webhook with optional consumer and provider."
CREATING_WEBHOOK_WITH_UUID_NOT_SUPPORTED = "This version of the Pact Broker does not support creating webhooks with a specified UUID. Please upgrade your broker to >= 2.49.0 or use the create-webhook command."

attr_reader :params, :pact_broker_base_url, :basic_auth_options, :verbose

Expand Down Expand Up @@ -41,14 +41,19 @@ def create_webhook_with_consumer_and_provider
end

def create_webhook_with_optional_consumer_and_provider
index_entity = index_link.get!
if params.uuid
webhook_entity = index_link.get!._link("pb:webhook").expand(uuid: params.uuid).put(request_body_with_optional_consumer_and_provider)
if index_entity.can?("pb:webhook")
webhook_entity = index_entity._link("pb:webhook").expand(uuid: params.uuid).put(request_body_with_optional_consumer_and_provider)
else
return error_result(CREATING_WEBHOOK_WITH_UUID_NOT_SUPPORTED)
end
else
webhook_entity = index_link.get!._link("pb:webhooks").post(request_body_with_optional_consumer_and_provider)
webhook_entity = index_entity._link("pb:webhooks").post(request_body_with_optional_consumer_and_provider)
end

if webhook_entity.response.status == 405
raise PactBroker::Client::Error.new(WEBHOOKS_WITH_OPTIONAL_PACTICICPANTS_NOT_SUPPORTED)
return error_result(WEBHOOKS_WITH_OPTIONAL_PACTICICPANTS_NOT_SUPPORTED)
end

handle_response(webhook_entity)
Expand Down Expand Up @@ -91,15 +96,19 @@ def handle_response(webhook_entity)
if webhook_entity.success?
success_result(webhook_entity)
else
error_result(webhook_entity)
http_error_result(webhook_entity)
end
end

def success_result(webhook_entity)
CommandResult.new(true, "Webhook #{webhook_entity._link('self').title_or_name.inspect} created")
end

def error_result(webhook_entity)
def error_result(message)
CommandResult.new(false, message)
end

def http_error_result(webhook_entity)
CommandResult.new(false, "Error creating webhook. response status=#{webhook_entity.response.status} body=#{webhook_entity.response.raw_body}")
end

Expand Down
22 changes: 16 additions & 6 deletions spec/lib/pact_broker/client/webhooks/create_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,6 @@ module Webhooks
stub_request(:get, "http://broker").with(headers: { "Authorization" => /.*/}).to_return(status: 200, body: index_body, headers: { "Content-Type" => "application/hal+json" } )
end

let!(:webhook_request) do
stub_request(:post, "http://broker/webhooks").to_return(status: 405)
end

let(:params) do
{
http_method: "POST",
Expand All @@ -44,8 +40,22 @@ module Webhooks
subject { Create.call(params, "http://broker", pact_broker_client_options) }

context "when a 405 is returned from the webhook creation request" do
it "raises an error with a message to upgrade the Pact Broker" do
expect { subject }.to raise_error PactBroker::Client::Error, /This version of the Pact Broker/
let!(:webhook_request) do
stub_request(:post, "http://broker/webhooks").to_return(status: 405)
end

it "returns a result with success=false and a message" do
expect(subject.success).to be false
expect(subject.message).to eq Create::WEBHOOKS_WITH_OPTIONAL_PACTICICPANTS_NOT_SUPPORTED
end
end

context "when a UUID is specified and index does not contain a pb:webhook relation" do
subject { Create.call(params.merge(uuid: 'some-uuid'), "http://broker", pact_broker_client_options) }

it "returns a result with success=false and a message" do
expect(subject.success).to be false
expect(subject.message).to eq Create::CREATING_WEBHOOK_WITH_UUID_NOT_SUPPORTED
end
end

Expand Down

0 comments on commit 6e3d30f

Please sign in to comment.