Merge tag 'v4.2.0-beta2'

This commit is contained in:
bgme 2023-08-22 03:51:20 +08:00
commit 9e38d55101
3010 changed files with 81215 additions and 55173 deletions

View file

@ -11,13 +11,18 @@
# enabled :boolean default(TRUE), not null
# created_at :datetime not null
# updated_at :datetime not null
# template :text
#
class Webhook < ApplicationRecord
EVENTS = %w(
account.approved
account.created
account.updated
report.created
report.updated
status.created
status.updated
).freeze
attr_writer :current_account
@ -30,6 +35,7 @@ class Webhook < ApplicationRecord
validate :validate_events
validate :validate_permissions
validate :validate_template
before_validation :strip_events
before_validation :generate_secret
@ -54,15 +60,32 @@ class Webhook < ApplicationRecord
case event
when 'account.approved', 'account.created', 'account.updated'
:manage_users
when 'report.created'
when 'report.created', 'report.updated'
:manage_reports
when 'status.created', 'status.updated'
:view_devops
end
end
private
def validate_events
errors.add(:events, :invalid) if events.any? { |e| !EVENTS.include?(e) }
errors.add(:events, :invalid) if events.any? { |e| EVENTS.exclude?(e) }
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
return if template.blank?
begin
parser = Webhooks::PayloadRenderer::TemplateParser.new
parser.parse(template)
rescue Parslet::ParseFailed
errors.add(:template, :invalid)
end
end
def validate_permissions
@ -70,7 +93,7 @@ class Webhook < ApplicationRecord
end
def strip_events
self.events = events.map { |str| str.strip.presence }.compact if events.present?
self.events = events.filter_map { |str| str.strip.presence } if events.present?
end
def generate_secret