Skip to content

Commit

Permalink
refactor: extract webhook commands from broker.rb
Browse files Browse the repository at this point in the history
  • Loading branch information
bethesque committed Jun 24, 2021
1 parent 14f5f6c commit 82cdfa0
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 99 deletions.
85 changes: 2 additions & 83 deletions lib/pact_broker/client/cli/broker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
require 'pact_broker/client/cli/environment_commands'
require 'pact_broker/client/cli/deployment_commands'
require 'pact_broker/client/cli/pacticipant_commands'
require 'pact_broker/client/cli/webhook_commands'

module PactBroker
module Client
module CLI
# Thor::Error will have its backtrace hidden
class PactPublicationError < ::Thor::Error; end
class WebhookCreationError < ::Thor::Error; end
class AuthError < ::Thor::Error; end
class VersionCreationError < ::Thor::Error; end

Expand All @@ -21,6 +21,7 @@ class Broker < CustomThor
include PactBroker::Client::CLI::DeploymentCommands
end
include PactBroker::Client::CLI::PacticipantCommands
include PactBroker::Client::CLI::WebhookCommands


desc 'can-i-deploy', ''
Expand Down Expand Up @@ -131,31 +132,7 @@ def describe_version
exit(1) unless result.success
end

shared_options_for_webhook_commands

desc 'create-webhook URL', 'Creates a webhook using the same switches as a curl request.'
long_desc File.read(File.join(File.dirname(__FILE__), 'create_webhook_long_desc.txt'))
def create_webhook webhook_url
run_webhook_commands webhook_url
end

shared_options_for_webhook_commands
method_option :uuid, type: :string, required: true, desc: "Specify the uuid for the webhook"

desc 'create-or-update-webhook URL', 'Creates or updates a webhook with a provided uuid and using the same switches as a curl request.'
long_desc File.read(File.join(File.dirname(__FILE__), 'create_or_update_webhook_long_desc.txt'))
def create_or_update_webhook webhook_url
run_webhook_commands webhook_url
end

desc 'test-webhook', 'Test the execution of a webhook'
method_option :uuid, type: :string, required: true, desc: "Specify the uuid for the webhook"
shared_authentication_options
def test_webhook
require 'pact_broker/client/webhooks/test'
result = PactBroker::Client::Webhooks::Test.call(options, pact_broker_client_options)
$stdout.puts result.message
end

ignored_and_hidden_potential_options_from_environment_variables
desc 'generate-uuid', 'Generate a UUID for use when calling create-or-update-webhook'
Expand Down Expand Up @@ -297,64 +274,6 @@ def pact_broker_client_options

client_options.compact
end

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

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
password = options.user ? options.user.split(":", 2).last : nil

headers = (options.header || []).each_with_object({}) { | header, headers | headers[header.split(":", 2).first.strip] = header.split(":", 2).last.strip }

body = options.data
if body && body.start_with?("@")
filepath = body[1..-1]
begin
body = File.read(filepath)
rescue StandardError => e
raise WebhookCreationError.new("Couldn't read data from file \"#{filepath}\" due to #{e.class} #{e.message}")
end
end

{
uuid: options.uuid,
description: options.description,
http_method: options.request,
url: webhook_url,
headers: headers,
username: username,
password: password,
body: body,
consumer: options.consumer,
provider: options.provider,
events: events
}
end

def run_webhook_commands webhook_url
require 'pact_broker/client/webhooks/create'

validate_credentials
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
end
Expand Down
16 changes: 0 additions & 16 deletions lib/pact_broker/client/cli/custom_thor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -91,22 +91,6 @@ def self.shared_authentication_options
method_option :verbose, aliases: "-v", type: :boolean, default: false, required: false, desc: "Verbose output. Default: false"
end

def self.shared_options_for_webhook_commands
method_option :request, banner: "METHOD", aliases: "-X", desc: "Webhook HTTP method", required: true
method_option :header, aliases: "-H", type: :array, desc: "Webhook Header"
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 :provider, desc: "Provider name"
method_option :description, desc: "Wwebhook 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"
shared_authentication_options
end

def self.verbose_option
method_option :verbose, aliases: "-v", type: :boolean, default: false, required: false, desc: "Verbose output. Default: false"
end
Expand Down
120 changes: 120 additions & 0 deletions lib/pact_broker/client/cli/webhook_commands.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
module PactBroker
module Client
module CLI
# Thor::Error will have its backtrace hidden
class WebhookCreationError < ::Thor::Error; end

module WebhookCommands
def self.included(thor)
thor.class_eval do

no_commands do
def self.shared_options_for_webhook_commands
method_option :request, banner: "METHOD", aliases: "-X", desc: "Webhook HTTP method", required: true
method_option :header, aliases: "-H", type: :array, desc: "Webhook Header"
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 :provider, desc: "Provider name"
method_option :description, desc: "Wwebhook 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"
shared_authentication_options
end
end

shared_options_for_webhook_commands

desc 'create-webhook URL', 'Creates a webhook using the same switches as a curl request.'
long_desc File.read(File.join(File.dirname(__FILE__), 'create_webhook_long_desc.txt'))
def create_webhook webhook_url
run_webhook_commands webhook_url
end

shared_options_for_webhook_commands
method_option :uuid, type: :string, required: true, desc: "Specify the uuid for the webhook"

desc 'create-or-update-webhook URL', 'Creates or updates a webhook with a provided uuid and using the same switches as a curl request.'
long_desc File.read(File.join(File.dirname(__FILE__), 'create_or_update_webhook_long_desc.txt'))
def create_or_update_webhook webhook_url
run_webhook_commands webhook_url
end

desc 'test-webhook', 'Test the execution of a webhook'
method_option :uuid, type: :string, required: true, desc: "Specify the uuid for the webhook"
shared_authentication_options
def test_webhook
require 'pact_broker/client/webhooks/test'
result = PactBroker::Client::Webhooks::Test.call(options, pact_broker_client_options)
$stdout.puts result.message
end

no_commands do

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

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
password = options.user ? options.user.split(":", 2).last : nil

headers = (options.header || []).each_with_object({}) { | header, headers | headers[header.split(":", 2).first.strip] = header.split(":", 2).last.strip }

body = options.data
if body && body.start_with?("@")
filepath = body[1..-1]
begin
body = File.read(filepath)
rescue StandardError => e
raise WebhookCreationError.new("Couldn't read data from file \"#{filepath}\" due to #{e.class} #{e.message}")
end
end

{
uuid: options.uuid,
description: options.description,
http_method: options.request,
url: webhook_url,
headers: headers,
username: username,
password: password,
body: body,
consumer: options.consumer,
provider: options.provider,
events: events
}
end

def run_webhook_commands webhook_url
require 'pact_broker/client/webhooks/create'

validate_credentials
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
end
end
end
end
end
1 change: 1 addition & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
ENV.delete('PACT_BROKER_BASE_URL')
ENV.delete('PACT_BROKER_USERNAME')
ENV.delete('PACT_BROKER_PASSWORD')
ENV.delete('PACT_BROKER_TOKEN')
end

config.after(:all) do
Expand Down

0 comments on commit 82cdfa0

Please sign in to comment.