0
0
Fork 0

Add administrative webhooks (#18510)

* Add administrative webhooks

* Fix error when webhook is deleted before delivery worker runs
This commit is contained in:
Eugen Rochko 2022-06-09 21:57:36 +02:00 committed by GitHub
parent 17ba5e1e61
commit a2871cd747
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
33 changed files with 530 additions and 8 deletions

58
app/models/webhook.rb Normal file
View file

@ -0,0 +1,58 @@
# frozen_string_literal: true
# == Schema Information
#
# Table name: webhooks
#
# id :bigint(8) not null, primary key
# url :string not null
# events :string default([]), not null, is an Array
# secret :string default(""), not null
# enabled :boolean default(TRUE), not null
# created_at :datetime not null
# updated_at :datetime not null
#
class Webhook < ApplicationRecord
EVENTS = %w(
account.created
report.created
).freeze
scope :enabled, -> { where(enabled: true) }
validates :url, presence: true, url: true
validates :secret, presence: true, length: { minimum: 12 }
validates :events, presence: true
validate :validate_events
before_validation :strip_events
before_validation :generate_secret
def rotate_secret!
update!(secret: SecureRandom.hex(20))
end
def enable!
update!(enabled: true)
end
def disable!
update!(enabled: false)
end
private
def validate_events
errors.add(:events, :invalid) if events.any? { |e| !EVENTS.include?(e) }
end
def strip_events
self.events = events.map { |str| str.strip.presence }.compact if events.present?
end
def generate_secret
self.secret = SecureRandom.hex(20) if secret.blank?
end
end