2018-11-18 11:00:15 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
module TestHooks
|
|
|
|
class BaseService
|
|
|
|
attr_accessor :hook, :current_user, :trigger
|
|
|
|
|
|
|
|
def initialize(hook, current_user, trigger)
|
|
|
|
@hook = hook
|
|
|
|
@current_user = current_user
|
|
|
|
@trigger = trigger
|
|
|
|
end
|
|
|
|
|
|
|
|
def execute
|
2018-03-17 18:26:18 +05:30
|
|
|
trigger_key = hook.class.triggers.key(trigger.to_sym)
|
2017-09-10 17:25:29 +05:30
|
|
|
trigger_data_method = "#{trigger}_data"
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
if trigger_key.nil? || !self.respond_to?(trigger_data_method, true)
|
2017-09-10 17:25:29 +05:30
|
|
|
return error('Testing not available for this hook')
|
|
|
|
end
|
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
error_message = catch(:validation_error) do # rubocop:disable Cop/BanCatchThrow
|
2018-03-17 18:26:18 +05:30
|
|
|
sample_data = self.__send__(trigger_data_method) # rubocop:disable GitlabSecurity/PublicSend
|
2017-09-10 17:25:29 +05:30
|
|
|
|
2018-10-15 14:42:47 +05:30
|
|
|
return hook.execute(sample_data, trigger_key) # rubocop:disable Cop/AvoidReturnFromBlocks
|
2017-09-10 17:25:29 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
error(error_message)
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def error(message, http_status = nil)
|
|
|
|
result = {
|
|
|
|
message: message,
|
|
|
|
status: :error
|
|
|
|
}
|
|
|
|
|
|
|
|
result[:http_status] = http_status if http_status
|
|
|
|
result
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|