2022-06-09 19:57:36 +00:00
|
|
|
# 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
|
2023-06-06 08:42:47 +00:00
|
|
|
# template :text
|
2022-06-09 19:57:36 +00:00
|
|
|
#
|
|
|
|
|
|
|
|
class Webhook < ApplicationRecord
|
|
|
|
EVENTS = %w(
|
2023-01-05 12:29:49 +00:00
|
|
|
account.approved
|
2022-06-09 19:57:36 +00:00
|
|
|
account.created
|
2023-03-19 06:47:54 +00:00
|
|
|
account.updated
|
2022-06-09 19:57:36 +00:00
|
|
|
report.created
|
2023-03-19 06:47:54 +00:00
|
|
|
status.created
|
|
|
|
status.updated
|
2022-06-09 19:57:36 +00:00
|
|
|
).freeze
|
|
|
|
|
2023-06-22 12:52:25 +00:00
|
|
|
attr_writer :current_account
|
|
|
|
|
2022-06-09 19:57:36 +00:00
|
|
|
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
|
2023-06-22 12:52:25 +00:00
|
|
|
validate :validate_permissions
|
2023-06-06 08:42:47 +00:00
|
|
|
validate :validate_template
|
2022-06-09 19:57:36 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2023-06-22 12:52:25 +00:00
|
|
|
def required_permissions
|
|
|
|
events.map { |event| Webhook.permission_for_event(event) }
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.permission_for_event(event)
|
|
|
|
case event
|
|
|
|
when 'account.approved', 'account.created', 'account.updated'
|
|
|
|
:manage_users
|
|
|
|
when 'report.created'
|
|
|
|
:manage_reports
|
|
|
|
when 'status.created', 'status.updated'
|
|
|
|
:view_devops
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-06-09 19:57:36 +00:00
|
|
|
private
|
|
|
|
|
|
|
|
def validate_events
|
2023-06-06 08:42:47 +00:00
|
|
|
errors.add(:events, :invalid) if events.any? { |e| EVENTS.exclude?(e) }
|
|
|
|
end
|
|
|
|
|
2023-06-22 12:52:25 +00:00
|
|
|
def validate_permissions
|
|
|
|
errors.add(:events, :invalid_permissions) if defined?(@current_account) && required_permissions.any? { |permission| !@current_account.user_role.can?(permission) }
|
|
|
|
end
|
|
|
|
|
2023-06-06 08:42:47 +00:00
|
|
|
def validate_template
|
|
|
|
return if template.blank?
|
|
|
|
|
|
|
|
begin
|
|
|
|
parser = Webhooks::PayloadRenderer::TemplateParser.new
|
|
|
|
parser.parse(template)
|
|
|
|
rescue Parslet::ParseFailed
|
|
|
|
errors.add(:template, :invalid)
|
|
|
|
end
|
2022-06-09 19:57:36 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def strip_events
|
2023-05-23 08:49:12 +00:00
|
|
|
self.events = events.filter_map { |str| str.strip.presence } if events.present?
|
2022-06-09 19:57:36 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def generate_secret
|
|
|
|
self.secret = SecureRandom.hex(20) if secret.blank?
|
|
|
|
end
|
|
|
|
end
|