Welcome to mirror list, hosted at ThFree Co, Russian Federation.

base_service.rb « test_hooks « services « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 4abd2c44b2f0d312dc9ee54c4a6100d448716dce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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
      trigger_data_method = "#{trigger}_data"

      if !self.respond_to?(trigger_data_method, true) ||
          !hook.class::TRIGGERS.value?(trigger.to_sym)

        return error('Testing not available for this hook')
      end

      error_message = catch(:validation_error) do
        sample_data = self.__send__(trigger_data_method) # rubocop:disable GitlabSecurity/PublicSend

        return hook.execute(sample_data, trigger)
      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