summaryrefslogtreecommitdiff
path: root/app/models/concerns/triggerable_hooks.rb
blob: c52baa0524c678112aa50dced6acd46a54ee0d0a (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
42
43
44
45
46
47
48
49
# frozen_string_literal: true

module TriggerableHooks
  AVAILABLE_TRIGGERS = {
    repository_update_hooks:  :repository_update_events,
    push_hooks:               :push_events,
    tag_push_hooks:           :tag_push_events,
    issue_hooks:              :issues_events,
    confidential_note_hooks:  :confidential_note_events,
    confidential_issue_hooks: :confidential_issues_events,
    note_hooks:               :note_events,
    merge_request_hooks:      :merge_requests_events,
    job_hooks:                :job_events,
    pipeline_hooks:           :pipeline_events,
    wiki_page_hooks:          :wiki_page_events
  }.freeze

  extend ActiveSupport::Concern

  class_methods do
    attr_reader :triggerable_hooks

    attr_reader :triggers

    def hooks_for(trigger)
      callable_scopes = triggers.keys + [:all]
      return none unless callable_scopes.include?(trigger)

      public_send(trigger) # rubocop:disable GitlabSecurity/PublicSend
    end

    def select_active(hooks_scope, data)
      select do |hook|
        ActiveHookFilter.new(hook).matches?(hooks_scope, data)
      end
    end

    private

    def triggerable_hooks(hooks)
      triggers = AVAILABLE_TRIGGERS.slice(*hooks)
      @triggers = triggers

      triggers.each do |trigger, event|
        scope trigger, -> { where(event => true) }
      end
    end
  end
end