module PactBroker::Webhooks::Service

Public Instance Methods

create(uuid, webhook, consumer, provider) click to toggle source
# File lib/pact_broker/webhooks/service.rb, line 64
def create uuid, webhook, consumer, provider
  webhook_repository.create uuid, webhook, consumer, provider
end
delete_by_uuid(uuid) click to toggle source
# File lib/pact_broker/webhooks/service.rb, line 75
def delete_by_uuid uuid
  webhook_repository.delete_by_uuid uuid
end
errors(webhook, uuid = nil) click to toggle source
# File lib/pact_broker/webhooks/service.rb, line 52
def errors webhook, uuid = nil
  contract = PactBroker::Api::Contracts::WebhookContract.new(webhook)
  contract.validate(webhook.attributes)
  messages = contract.errors.messages

  if uuid && !valid_uuid_format?(uuid)
    messages["uuid"] = [message("errors.validation.invalid_webhook_uuid")]
  end

  OpenStruct.new(messages: messages, empty?: messages.empty?, any?: messages.any?)
end
next_uuid() click to toggle source
# File lib/pact_broker/webhooks/service.rb, line 48
def next_uuid
  SecureRandom.urlsafe_base64
end
parameters() click to toggle source
# File lib/pact_broker/webhooks/service.rb, line 91
def parameters
  PactAndVerificationParameters::ALL.collect do | parameter |
    OpenStruct.new(
      name: parameter,
      description: message("messages.webhooks.parameters.#{parameter}")
    )
  end
end
update_by_uuid(uuid, params) click to toggle source
# File lib/pact_broker/webhooks/service.rb, line 68
def update_by_uuid uuid, params
  webhook = webhook_repository.find_by_uuid(uuid)
  maintain_redacted_params(webhook, params)
  PactBroker::Api::Decorators::WebhookDecorator.new(webhook).from_hash(params)
  webhook_repository.update_by_uuid uuid, webhook
end
valid_uuid_format?(uuid) click to toggle source

Not actually a UUID. Ah well.

# File lib/pact_broker/webhooks/service.rb, line 44
def valid_uuid_format?(uuid)
  !!(uuid =~ /^[A-Za-z0-9_\-]{16,}$/)
end

Private Instance Methods

maintain_redacted_params(webhook, params) click to toggle source

Dirty hack to maintain existing password or Authorization header if it is submitted with value **** This is required because the password and Authorization header is **** out in the API response for security purposes, so it would need to be re-entered with every response. TODO implement proper 'secrets' management.

# File lib/pact_broker/webhooks/service.rb, line 106
def maintain_redacted_params(webhook, params)
  if webhook.request.password && password_key_does_not_exist_or_is_starred?(params)
    params["request"]["password"] = webhook.request.password
  end

  new_headers = params["request"]["headers"] ||= {}
  existing_headers = webhook.request.headers
  starred_new_headers = new_headers.select { |_key, value| value =~ /^\**$/ }
  starred_new_headers.each do | (key, _value) |
    new_headers[key] = existing_headers[key]
  end
  params["request"]["headers"] = new_headers
  params
end
password_key_does_not_exist_or_is_starred?(params) click to toggle source
# File lib/pact_broker/webhooks/service.rb, line 121
def password_key_does_not_exist_or_is_starred?(params)
  !params["request"].key?("password") || params.dig("request","password") =~ /^\**$/
end