Add configuration variable for webhooks, disable by default [SCI-6031]

This commit is contained in:
Oleksii Kriuchykhin 2021-08-26 14:08:15 +02:00
parent 295d27f0d7
commit d67f7691a1
5 changed files with 14 additions and 2 deletions

View file

@ -74,7 +74,8 @@ class Activity < ApplicationRecord
breadcrumbs: {}
)
after_create ->(activity) { Activities::DispatchWebhooksJob.perform_later(activity) }
after_create ->(activity) { Activities::DispatchWebhooksJob.perform_later(activity) },
if: -> { Rails.application.config.x.webhooks_enabled }
def self.activity_types_list
activity_list = type_ofs.map do |key, value|

View file

@ -6,12 +6,19 @@ class Webhook < ApplicationRecord
belongs_to :activity_filter
validates :http_method, presence: true
validates :url, presence: true
validate :enabled?
validate :valid_url
scope :active, -> { where(active: true) }
private
def enabled?
unless Rails.application.config.x.webhooks_enabled
errors.add(:configuration, I18n.t('activerecord.errors.models.webhook.attributes.configuration.disabled'))
end
end
def valid_url
unless /\A#{URI::DEFAULT_PARSER.make_regexp(%w(http https))}\z/.match?(url)
errors.add(:url, I18n.t('activerecord.errors.models.webhook.attributes.url.not_valid'))

View file

@ -14,7 +14,7 @@ module Organization
end
can :create_acitivity_filters do
true
Rails.application.config.x.webhooks_enabled
end
end
end

View file

@ -40,6 +40,8 @@ module Scinote
# Max uploaded file size in MB
config.x.file_max_size_mb = (ENV['FILE_MAX_SIZE_MB'] || 50).to_i
config.x.webhooks_enabled = ENV['ENABLE_WEBHOOKS'] == 'true'
# Logging
config.log_formatter = proc do |severity, datetime, progname, msg|
"[#{datetime}] #{severity}: #{msg}\n"

View file

@ -148,6 +148,8 @@ en:
per_column_limit: "Too many items in the column"
webhook:
attributes:
configuration:
disabled: 'Webhooks are disabled'
url:
not_valid: 'Not valid URL'