-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: extract webhook commands from broker.rb
- Loading branch information
Showing
4 changed files
with
123 additions
and
99 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters