|
|
|
@ -33,11 +33,11 @@ class Webhook < ApplicationRecord
|
|
|
|
|
validates :secret, presence: true, length: { minimum: 12 }
|
|
|
|
|
validates :events, presence: true
|
|
|
|
|
|
|
|
|
|
validate :validate_events
|
|
|
|
|
validate :events_validation_error, if: :invalid_events?
|
|
|
|
|
validate :validate_permissions
|
|
|
|
|
validate :validate_template
|
|
|
|
|
|
|
|
|
|
before_validation :strip_events
|
|
|
|
|
normalizes :events, with: ->(events) { events.filter_map { |event| event.strip.presence } }
|
|
|
|
|
before_validation :generate_secret
|
|
|
|
|
|
|
|
|
|
def rotate_secret!
|
|
|
|
@ -69,8 +69,12 @@ class Webhook < ApplicationRecord
|
|
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
|
|
def validate_events
|
|
|
|
|
errors.add(:events, :invalid) if events.any? { |e| EVENTS.exclude?(e) }
|
|
|
|
|
def events_validation_error
|
|
|
|
|
errors.add(:events, :invalid)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def invalid_events?
|
|
|
|
|
events.blank? || events.difference(EVENTS).any?
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def validate_permissions
|
|
|
|
@ -88,10 +92,6 @@ class Webhook < ApplicationRecord
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def strip_events
|
|
|
|
|
self.events = events.filter_map { |str| str.strip.presence } if events.present?
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def generate_secret
|
|
|
|
|
self.secret = SecureRandom.hex(20) if secret.blank?
|
|
|
|
|
end
|
|
|
|
|