class PactBroker::Webhooks::Webhook

Public Class Methods

from_domain(webhook, consumer, provider) click to toggle source
# File lib/pact_broker/webhooks/webhook.rb, line 88
def self.from_domain webhook, consumer, provider
  new(
    properties_hash_from_domain(webhook).merge(uuid: webhook.uuid)
  ).tap do | db_webhook |
    db_webhook.consumer_id = consumer.id if consumer
    db_webhook.provider_id = provider.id if provider
  end
end
not_plain_text_password(password) click to toggle source
# File lib/pact_broker/webhooks/webhook.rb, line 97
def self.not_plain_text_password password
  password.nil? ? nil : Base64.strict_encode64(password)
end
properties_hash_from_domain(webhook) click to toggle source
# File lib/pact_broker/webhooks/webhook.rb, line 149
def self.properties_hash_from_domain webhook
  is_json_request_body = !(String === webhook.request.body || webhook.request.body.nil?) # Can't rely on people to set content type
  {
    description: webhook.description,
    method: webhook.request.method,
    url: webhook.request.url,
    username: webhook.request.username,
    password: not_plain_text_password(webhook.request.password),
    enabled: webhook.enabled.nil? ? true : webhook.enabled,
    body: (is_json_request_body ? webhook.request.body.to_json : webhook.request.body),
    is_json_request_body: is_json_request_body,
    headers: webhook.request.headers,
    consumer_label: webhook.consumer&.label,
    provider_label: webhook.provider&.label
  }
end

Public Instance Methods

delete() click to toggle source

Keep the triggered webhooks after the webhook has been deleted

Calls superclass method
# File lib/pact_broker/webhooks/webhook.rb, line 21
def delete
  require "pact_broker/webhooks/triggered_webhook"
  TriggeredWebhook.where(webhook: self).update(webhook_id: nil)
  super
end
enabled() click to toggle source
# File lib/pact_broker/webhooks/webhook.rb, line 53
def enabled
  where(enabled: true)
end
find_by_consumer_and_or_provider(consumer, provider) click to toggle source
# File lib/pact_broker/webhooks/webhook.rb, line 32
def find_by_consumer_and_or_provider consumer, provider

  where(
    Sequel.|(
      { consumer_id: consumer.id, provider_id: provider.id },
      { consumer_id: nil, provider_id: provider.id, consumer_label: nil },
      { consumer_id: consumer.id, provider_id: nil, provider_label: nil },
      { consumer_id: nil, provider_id: nil, consumer_label: nil, provider_label: nil },
      *labels_criteria_for_consumer_or_provider(consumer, provider)
    )
  )
end
find_by_consumer_and_provider(consumer, provider) click to toggle source
# File lib/pact_broker/webhooks/webhook.rb, line 45
def find_by_consumer_and_provider consumer, provider
  criteria = {
    consumer_id: (consumer ? consumer.id : nil),
    provider_id: (provider ? provider.id : nil)
  }
  where(criteria)
end
for_event_name(event_name) click to toggle source
# File lib/pact_broker/webhooks/webhook.rb, line 27
def for_event_name(event_name)
  join(:webhook_events, { webhook_id: :id })
    .where(Sequel[:webhook_events][:name] => event_name)
end
is_for?(integration) click to toggle source
# File lib/pact_broker/webhooks/webhook.rb, line 130
def is_for? integration
  (
    consumer_id == integration.consumer_id ||
    match_label?(:consumer, integration) ||
    match_all?(:consumer)
  ) && (
    provider_id == integration.provider_id ||
    match_label?(:provider, integration) ||
    match_all?(:provider)
  )
end
labels_criteria_for_consumer_or_provider(consumer, provider) click to toggle source
# File lib/pact_broker/webhooks/webhook.rb, line 59
def labels_criteria_for_consumer_or_provider(consumer, provider)
  consumer_labels = consumer.labels.map(&:name)
  provider_labels = provider.labels.map(&:name)

  [].then do |criteria|
    next criteria if consumer_labels.empty?
    criteria + [
      { consumer_label: consumer_labels, provider_label: nil, provider_id: nil },
      { consumer_label: consumer_labels, provider_label: nil, provider_id: provider.id }
    ]
  end.then do |criteria|
    next criteria if provider_labels.empty?
    criteria + [
      { provider_label: provider_labels, consumer_label: nil, consumer_id: nil },
      { provider_label: provider_labels, consumer_label: nil, consumer_id: consumer.id }
    ]
  end.then do |criteria|
    next criteria if consumer_labels.empty? || provider_labels.empty?
    criteria + [
      { consumer_label: consumer_labels, provider_label: provider_labels }
    ]
  end
end
match_all?(name) click to toggle source
# File lib/pact_broker/webhooks/webhook.rb, line 178
def match_all?(name)
  public_send(:"#{name}_id").nil? && public_send(:"#{name}_label").nil?
end
match_label?(name, integration) click to toggle source
# File lib/pact_broker/webhooks/webhook.rb, line 182
def match_label?(name, integration)
  label = public_send(:"#{name}_label")
  public_send(:"#{name}_id").nil? && integration.public_send(name).label?(label)
end
parsed_body() click to toggle source
# File lib/pact_broker/webhooks/webhook.rb, line 122
def parsed_body
  if body && is_json_request_body
    JSON.parse(body)
  else
    body
  end
end
plain_text_password() click to toggle source
# File lib/pact_broker/webhooks/webhook.rb, line 118
def plain_text_password
  password.nil? ? nil : Base64.strict_decode64(password)
end
request_attributes() click to toggle source
# File lib/pact_broker/webhooks/webhook.rb, line 114
def request_attributes
  values.merge(headers: headers, body: parsed_body, password: plain_text_password, uuid: uuid)
end
to_domain() click to toggle source
# File lib/pact_broker/webhooks/webhook.rb, line 101
def to_domain
  Domain::Webhook.new(
    uuid: uuid,
    description: description,
    consumer: webhook_consumer,
    provider: webhook_provider,
    events: events,
    request: Webhooks::WebhookRequestTemplate.new(request_attributes),
    enabled: enabled,
    created_at: created_at,
    updated_at: updated_at)
end
update_from_domain(webhook) click to toggle source
# File lib/pact_broker/webhooks/webhook.rb, line 84
def update_from_domain webhook
  set(self.class.properties_hash_from_domain(webhook))
end
webhook_consumer() click to toggle source
# File lib/pact_broker/webhooks/webhook.rb, line 166
def webhook_consumer
  return if consumer.nil? && consumer_label.nil?

  Domain::WebhookPacticipant.new(name: consumer&.name, label: consumer_label)
end
webhook_provider() click to toggle source
# File lib/pact_broker/webhooks/webhook.rb, line 172
def webhook_provider
  return if provider.nil? && provider_label.nil?

  Domain::WebhookPacticipant.new(name: provider&.name, label: provider_label)
end