summaryrefslogtreecommitdiff
path: root/app/models/integrations/base_chat_notification.rb
blob: d5b6357cb6696007f44bfbff6303fe9fde16e6cd (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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
# frozen_string_literal: true

# Base class for Chat notifications services
# This class is not meant to be used directly, but only to inherit from.

module Integrations
  class BaseChatNotification < Integration
    include ChatMessage
    include NotificationBranchSelection

    SUPPORTED_EVENTS = %w[
      push issue confidential_issue merge_request note confidential_note
      tag_push pipeline wiki_page deployment
    ].freeze

    SUPPORTED_EVENTS_FOR_LABEL_FILTER = %w[issue confidential_issue merge_request note confidential_note].freeze

    EVENT_CHANNEL = proc { |event| "#{event}_channel" }

    LABEL_NOTIFICATION_BEHAVIOURS = [
      MATCH_ANY_LABEL = 'match_any',
      MATCH_ALL_LABELS = 'match_all'
    ].freeze

    default_value_for :category, 'chat'

    prop_accessor :webhook, :username, :channel, :branches_to_be_notified, :labels_to_be_notified, :labels_to_be_notified_behavior

    # Custom serialized properties initialization
    prop_accessor(*SUPPORTED_EVENTS.map { |event| EVENT_CHANNEL[event] })

    boolean_accessor :notify_only_broken_pipelines, :notify_only_default_branch

    validates :webhook, presence: true, public_url: true, if: :activated?
    validates :labels_to_be_notified_behavior, inclusion: { in: LABEL_NOTIFICATION_BEHAVIOURS }, allow_blank: true

    def initialize_properties
      if properties.nil?
        self.properties = {}
        self.notify_only_broken_pipelines = true
        self.branches_to_be_notified = "default"
        self.labels_to_be_notified_behavior = MATCH_ANY_LABEL
      elsif !self.notify_only_default_branch.nil?
        # In older versions, there was only a boolean property named
        # `notify_only_default_branch`. Now we have a string property named
        # `branches_to_be_notified`. Instead of doing a background migration, we
        # opted to set a value for the new property based on the old one, if
        # users haven't specified one already. When users edit the service and
        # select a value for this new property, it will override everything.

        self.branches_to_be_notified ||= notify_only_default_branch? ? "default" : "all"
      end
    end

    def confidential_issue_channel
      properties['confidential_issue_channel'].presence || properties['issue_channel']
    end

    def confidential_note_channel
      properties['confidential_note_channel'].presence || properties['note_channel']
    end

    def self.supported_events
      SUPPORTED_EVENTS
    end

    def fields
      default_fields + build_event_channels
    end

    def default_fields
      [
        { type: 'text', name: 'webhook', placeholder: "#{webhook_placeholder}", required: true }.freeze,
        { type: 'text', name: 'username', placeholder: 'GitLab-integration' }.freeze,
        { type: 'checkbox', name: 'notify_only_broken_pipelines', help: 'Do not send notifications for successful pipelines.' }.freeze,
        {
          type: 'select',
          name: 'branches_to_be_notified',
          title: s_('Integrations|Branches for which notifications are to be sent'),
          choices: branch_choices
        }.freeze,
        {
          type: 'text',
          name: 'labels_to_be_notified',
          placeholder: '~backend,~frontend',
          help: 'Send notifications for issue, merge request, and comment events with the listed labels only. Leave blank to receive notifications for all events.'
        }.freeze,
        {
          type: 'select',
          name: 'labels_to_be_notified_behavior',
          choices: [
            ['Match any of the labels', MATCH_ANY_LABEL],
            ['Match all of the labels', MATCH_ALL_LABELS]
          ]
        }.freeze
      ].freeze
    end

    def execute(data)
      return unless supported_events.include?(data[:object_kind])

      return unless webhook.present?

      object_kind = data[:object_kind]

      data = custom_data(data)

      return unless notify_label?(data)

      # WebHook events often have an 'update' event that follows a 'open' or
      # 'close' action. Ignore update events for now to prevent duplicate
      # messages from arriving.

      message = get_message(object_kind, data)

      return false unless message

      event_type = data[:event_type] || object_kind

      channel_names = get_channel_field(event_type).presence || channel.presence
      channels = channel_names&.split(',')&.map(&:strip)

      opts = {}
      opts[:channel] = channels if channels.present?
      opts[:username] = username if username

      if notify(message, opts)
        log_usage(event_type, user_id_from_hook_data(data))
        return true
      end

      false
    end

    def event_channel_names
      supported_events.map { |event| event_channel_name(event) }
    end

    def event_field(event)
      fields.find { |field| field[:name] == event_channel_name(event) }
    end

    def global_fields
      fields.reject { |field| field[:name].end_with?('channel') }
    end

    def default_channel_placeholder
      raise NotImplementedError
    end

    private

    def log_usage(_, _)
      # Implement in child class
    end

    def labels_to_be_notified_list
      return [] if labels_to_be_notified.nil?

      labels_to_be_notified.delete('~').split(',').map(&:strip)
    end

    def notify_label?(data)
      return true unless SUPPORTED_EVENTS_FOR_LABEL_FILTER.include?(data[:object_kind]) && labels_to_be_notified.present?

      labels = data[:labels] || data.dig(:issue, :labels) || data.dig(:merge_request, :labels) || data.dig(:object_attributes, :labels)

      return false if labels.blank?

      matching_labels = labels_to_be_notified_list & labels.pluck(:title)

      if labels_to_be_notified_behavior == MATCH_ALL_LABELS
        labels_to_be_notified_list.difference(matching_labels).empty?
      else
        matching_labels.any?
      end
    end

    def user_id_from_hook_data(data)
      data.dig(:user, :id) || data[:user_id]
    end

    # every notifier must implement this independently
    def notify(message, opts)
      raise NotImplementedError
    end

    def custom_data(data)
      data.merge(project_url: project_url, project_name: project_name).with_indifferent_access
    end

    def get_message(object_kind, data)
      case object_kind
      when "push", "tag_push"
        Integrations::ChatMessage::PushMessage.new(data) if notify_for_ref?(data)
      when "issue"
        Integrations::ChatMessage::IssueMessage.new(data) unless update?(data)
      when "merge_request"
        Integrations::ChatMessage::MergeMessage.new(data) unless update?(data)
      when "note"
        Integrations::ChatMessage::NoteMessage.new(data)
      when "pipeline"
        Integrations::ChatMessage::PipelineMessage.new(data) if should_pipeline_be_notified?(data)
      when "wiki_page"
        Integrations::ChatMessage::WikiPageMessage.new(data)
      when "deployment"
        Integrations::ChatMessage::DeploymentMessage.new(data) if notify_for_ref?(data)
      end
    end

    def get_channel_field(event)
      field_name = event_channel_name(event)
      self.public_send(field_name) # rubocop:disable GitlabSecurity/PublicSend
    end

    def build_event_channels
      supported_events.reduce([]) do |channels, event|
        channels << { type: 'text', name: event_channel_name(event), placeholder: default_channel_placeholder }
      end
    end

    def event_channel_name(event)
      EVENT_CHANNEL[event]
    end

    def project_name
      project.full_name
    end

    def project_url
      project.web_url
    end

    def update?(data)
      data[:object_attributes][:action] == 'update'
    end

    def should_pipeline_be_notified?(data)
      notify_for_ref?(data) && notify_for_pipeline?(data)
    end

    def notify_for_ref?(data)
      return true if data[:object_kind] == 'tag_push'

      ref = data[:ref] || data.dig(:object_attributes, :ref)
      return true if ref.blank? # No need to check protected branches when there is no ref
      return true if Gitlab::Git.tag_ref?(ref) # Skip protected branch check because it doesn't support tags

      notify_for_branch?(data)
    end

    def notify_for_pipeline?(data)
      case data[:object_attributes][:status]
      when 'success'
        !notify_only_broken_pipelines?
      when 'failed'
        true
      else
        false
      end
    end
  end
end

Integrations::BaseChatNotification.prepend_mod_with('Integrations::BaseChatNotification')