|
|
@ -24,6 +24,8 @@ class Webhook < ApplicationRecord
|
|
|
|
status.updated
|
|
|
|
status.updated
|
|
|
|
).freeze
|
|
|
|
).freeze
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
attr_writer :current_account
|
|
|
|
|
|
|
|
|
|
|
|
scope :enabled, -> { where(enabled: true) }
|
|
|
|
scope :enabled, -> { where(enabled: true) }
|
|
|
|
|
|
|
|
|
|
|
|
validates :url, presence: true, url: true
|
|
|
|
validates :url, presence: true, url: true
|
|
|
@ -31,6 +33,7 @@ class Webhook < ApplicationRecord
|
|
|
|
validates :events, presence: true
|
|
|
|
validates :events, presence: true
|
|
|
|
|
|
|
|
|
|
|
|
validate :validate_events
|
|
|
|
validate :validate_events
|
|
|
|
|
|
|
|
validate :validate_permissions
|
|
|
|
validate :validate_template
|
|
|
|
validate :validate_template
|
|
|
|
|
|
|
|
|
|
|
|
before_validation :strip_events
|
|
|
|
before_validation :strip_events
|
|
|
@ -48,12 +51,31 @@ class Webhook < ApplicationRecord
|
|
|
|
update!(enabled: false)
|
|
|
|
update!(enabled: false)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
|
private
|
|
|
|
private
|
|
|
|
|
|
|
|
|
|
|
|
def validate_events
|
|
|
|
def validate_events
|
|
|
|
errors.add(:events, :invalid) if events.any? { |e| EVENTS.exclude?(e) }
|
|
|
|
errors.add(:events, :invalid) if events.any? { |e| EVENTS.exclude?(e) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def validate_permissions
|
|
|
|
|
|
|
|
errors.add(:events, :invalid_permissions) if defined?(@current_account) && required_permissions.any? { |permission| !@current_account.user_role.can?(permission) }
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def validate_template
|
|
|
|
def validate_template
|
|
|
|
return if template.blank?
|
|
|
|
return if template.blank?
|
|
|
|
|
|
|
|
|
|
|
|