2017-07-20 11:12:06 -04:00
|
|
|
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
|
2017-09-07 07:29:12 -04:00
|
|
|
trigger_key = hook.class::TRIGGERS.key(trigger.to_sym)
|
2017-07-20 11:12:06 -04:00
|
|
|
trigger_data_method = "#{trigger}_data"
|
|
|
|
|
2017-09-07 07:29:12 -04:00
|
|
|
if trigger_key.nil? || !self.respond_to?(trigger_data_method, true)
|
2017-07-20 11:12:06 -04:00
|
|
|
return error('Testing not available for this hook')
|
|
|
|
end
|
|
|
|
|
|
|
|
error_message = catch(:validation_error) do
|
2017-08-10 12:39:26 -04:00
|
|
|
sample_data = self.__send__(trigger_data_method) # rubocop:disable GitlabSecurity/PublicSend
|
2017-07-20 11:12:06 -04:00
|
|
|
|
2017-09-07 07:29:12 -04:00
|
|
|
return hook.execute(sample_data, trigger_key)
|
2017-07-20 11:12:06 -04:00
|
|
|
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
|