Class: PactBroker::Webhooks::Webhook

Inherits:
Sequel::Model
  • Object
show all
Defined in:
lib/pact_broker/webhooks/webhook.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.from_domain(webhook, consumer, provider) ⇒ Object



88
89
90
91
92
93
94
95
# 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) ⇒ Object



97
98
99
# 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) ⇒ Object



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# 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

Instance Method Details

#deleteObject

Keep the triggered webhooks after the webhook has been deleted



143
144
145
146
147
# File 'lib/pact_broker/webhooks/webhook.rb', line 143

def delete
  require "pact_broker/webhooks/triggered_webhook"
  TriggeredWebhook.where(webhook_id: id).update(webhook_id: nil)
  super
end

#is_for?(integration) ⇒ Boolean

Returns:

  • (Boolean)


130
131
132
133
134
135
136
137
138
139
140
# 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

#match_all?(name) ⇒ Boolean

Returns:

  • (Boolean)


178
179
180
# 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) ⇒ Boolean

Returns:

  • (Boolean)


182
183
184
185
# 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_bodyObject



122
123
124
125
126
127
128
# 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_passwordObject



118
119
120
# File 'lib/pact_broker/webhooks/webhook.rb', line 118

def plain_text_password
  password.nil? ? nil : Base64.strict_decode64(password)
end

#request_attributesObject



114
115
116
# 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_domainObject



101
102
103
104
105
106
107
108
109
110
111
112
# 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) ⇒ Object



84
85
86
# File 'lib/pact_broker/webhooks/webhook.rb', line 84

def update_from_domain webhook
  set(self.class.properties_hash_from_domain(webhook))
end

#webhook_consumerObject



166
167
168
169
170
# 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_providerObject



172
173
174
175
176
# 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