class PactBroker::Client::Webhooks::Create

Constants

CREATING_WEBHOOK_WITH_UUID_NOT_SUPPORTED
WEBHOOKS_WITH_OPTIONAL_PACTICICPANTS_NOT_SUPPORTED

Attributes

basic_auth_options[R]
http_client[R]
pact_broker_base_url[R]
params[R]
verbose[R]

Public Class Methods

call(params, pact_broker_base_url, pact_broker_client_options) click to toggle source
# File lib/pact_broker/client/webhooks/create.rb, line 21
def self.call(params, pact_broker_base_url, pact_broker_client_options)
  new(params, pact_broker_base_url, pact_broker_client_options).call
end
new(params, pact_broker_base_url, pact_broker_client_options) click to toggle source
# File lib/pact_broker/client/webhooks/create.rb, line 25
def initialize(params, pact_broker_base_url, pact_broker_client_options)
  @params = OpenStruct.new(params)
  @pact_broker_base_url = pact_broker_base_url
  @http_client = PactBroker::Client::Hal::HttpClient.new(pact_broker_client_options.merge(pact_broker_client_options[:basic_auth] || {}))
end

Public Instance Methods

call() click to toggle source
# File lib/pact_broker/client/webhooks/create.rb, line 31
def call
  if params.consumer && params.provider && !params.uuid
    create_webhook_with_consumer_and_provider
  else
    create_webhook_with_optional_consumer_and_provider
  end
end

Private Instance Methods

create_webhook_with_consumer_and_provider() click to toggle source
# File lib/pact_broker/client/webhooks/create.rb, line 43
def create_webhook_with_consumer_and_provider
  webhook_entity = webhook_link.expand(consumer: params.consumer, provider: params.provider).post(request_body)
  handle_response(webhook_entity)
end
create_webhook_with_optional_consumer_and_provider() click to toggle source
# File lib/pact_broker/client/webhooks/create.rb, line 48
def create_webhook_with_optional_consumer_and_provider
  index_entity = index_link.get!
  if params.uuid
    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_entity._link!("pb:webhooks").post(request_body_with_optional_consumer_and_provider)
  end

  if webhook_entity.response.status == 405
    return error_result(WEBHOOKS_WITH_OPTIONAL_PACTICICPANTS_NOT_SUPPORTED)
  end

  handle_response(webhook_entity)
end
error_result(message) click to toggle source
# File lib/pact_broker/client/webhooks/create.rb, line 127
def error_result(message)
  CommandResult.new(false, message)
end
handle_response(webhook_entity) click to toggle source
# File lib/pact_broker/client/webhooks/create.rb, line 109
def handle_response(webhook_entity)
  if webhook_entity.success?
    success_result(webhook_entity)
  else
    http_error_result(webhook_entity)
  end
end
http_error_result(webhook_entity) click to toggle source
# File lib/pact_broker/client/webhooks/create.rb, line 131
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
request_body() click to toggle source
# File lib/pact_broker/client/webhooks/create.rb, line 67
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: request_params
  }.tap { |req| req[:description] = params.description if params.description }
end
request_body_with_optional_consumer_and_provider() click to toggle source
# File lib/pact_broker/client/webhooks/create.rb, line 83
def request_body_with_optional_consumer_and_provider
  body = request_body

  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
    body[:teamUuid] = params.team_uuid
  end

  body
end
success_result(webhook_entity) click to toggle source
# File lib/pact_broker/client/webhooks/create.rb, line 117
def success_result(webhook_entity)
  action = webhook_entity.response.status == 201 ? "created" : "updated"
  name = if webhook_entity.description && webhook_entity.description.size > 0
    webhook_entity.description
  else
    webhook_entity._link('self').title_or_name
  end
  CommandResult.new(true, "Webhook #{name.inspect} #{action}")
end
webhook_for_consumer_and_provider_url() click to toggle source
# File lib/pact_broker/client/webhooks/create.rb, line 105
def webhook_for_consumer_and_provider_url
  "#{pact_broker_base_url.chomp("/")}/webhooks/provider/{provider}/consumer/{consumer}"
end