Module: Integrations::Base::ChatNotification

Extended by:
ActiveSupport::Concern, Gitlab::Utils::Override
Includes:
ChatMessage, NotificationBranchSelection
Included in:
SlackNotification, Discord, HangoutsChat, Matrix, Mattermost, MicrosoftTeams, Pumble, Telegram, UnifyCircuit, WebexTeams
Defined in:
app/models/concerns/integrations/base/chat_notification.rb

Constant Summary collapse

SUPPORTED_EVENTS =
%w[
  push issue confidential_issue merge_request note confidential_note
  tag_push pipeline wiki_page deployment incident
].freeze
GROUP_ONLY_SUPPORTED_EVENTS =
%w[group_mention group_confidential_mention].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
SECRET_MASK =
'************'

Instance Method Summary collapse

Methods included from Gitlab::Utils::Override

extended, extensions, included, method_added, override, prepended, queue_verification, verify!

Methods included from NotificationBranchSelection

#notify_for_branch?

Instance Method Details

#api_field_namesObject



136
137
138
139
140
141
142
# File 'app/models/concerns/integrations/base/chat_notification.rb', line 136

def api_field_names
  if mask_configurable_channels?
    super - event_channel_names
  else
    super
  end
end

#channel_limit_per_eventObject



172
173
174
# File 'app/models/concerns/integrations/base/chat_notification.rb', line 172

def channel_limit_per_event
  10
end

#confidential_issue_channelObject



85
86
87
# File 'app/models/concerns/integrations/base/chat_notification.rb', line 85

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

#confidential_note_channelObject



89
90
91
# File 'app/models/concerns/integrations/base/chat_notification.rb', line 89

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

#configurable_channels?Boolean

With some integrations the webhook is already tied to a specific channel, for others the channels are configurable for each event.

Returns:

  • (Boolean)


158
159
160
# File 'app/models/concerns/integrations/base/chat_notification.rb', line 158

def configurable_channels?
  false
end

#default_channel_placeholderObject

Raises:

  • (NotImplementedError)


148
149
150
# File 'app/models/concerns/integrations/base/chat_notification.rb', line 148

def default_channel_placeholder
  raise NotImplementedError
end

#event_channel_name(event) ⇒ Object



162
163
164
# File 'app/models/concerns/integrations/base/chat_notification.rb', line 162

def event_channel_name(event)
  EVENT_CHANNEL[event]
end

#event_channel_namesObject



129
130
131
132
133
# File 'app/models/concerns/integrations/base/chat_notification.rb', line 129

def event_channel_names
  return [] unless configurable_channels?

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

#event_channel_value(event) ⇒ Object



166
167
168
169
170
# File 'app/models/concerns/integrations/base/chat_notification.rb', line 166

def event_channel_value(event)
  field_name = event_channel_name(event)

  public_send(field_name) # rubocop:disable GitlabSecurity/PublicSend -- Legacy use
end

#execute(data) ⇒ Object



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
# File 'app/models/concerns/integrations/base/chat_notification.rb', line 97

def execute(data)
  object_kind = data[:object_kind]

  return false unless should_execute?(object_kind)

  data = custom_data(data)

  return false 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 = data[:event_type] || object_kind
  channels = channels_for_event(event)

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

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

  false
end

#fieldsObject



93
94
95
# File 'app/models/concerns/integrations/base/chat_notification.rb', line 93

def fields
  self.class.fields + build_event_channels
end

#form_fieldsObject



144
145
146
# File 'app/models/concerns/integrations/base/chat_notification.rb', line 144

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

#initialize_propertiesObject



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'app/models/concerns/integrations/base/chat_notification.rb', line 66

def initialize_properties
  super

  if properties.empty?
    self.notify_only_broken_pipelines = true if respond_to?(:notify_only_broken_pipelines)
    self.branches_to_be_notified = "default"
    self.labels_to_be_notified_behavior = MATCH_ANY_LABEL
  elsif !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 integration and
    # select a value for this new property, it will override everything.

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

#mask_configurable_channels?Boolean

Returns:

  • (Boolean)


176
177
178
# File 'app/models/concerns/integrations/base/chat_notification.rb', line 176

def mask_configurable_channels?
  false
end

#sectionsObject



181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'app/models/concerns/integrations/base/chat_notification.rb', line 181

def sections
  [
    {
      type: ::Integration::SECTION_TYPE_CONNECTION,
      title: s_('Integrations|Connection details'),
      description: help
    },
    {
      type: ::Integration::SECTION_TYPE_TRIGGER,
      title: s_('Integrations|Trigger'),
      description: s_('Integrations|An event will be triggered when one of the following items happen.')
    },
    {
      type: ::Integration::SECTION_TYPE_CONFIGURATION,
      title: s_('Integrations|Notification settings'),
      description: s_('Integrations|Configure the scope of notifications.')
    }
  ]
end

#webhook_helpObject

Raises:

  • (NotImplementedError)


152
153
154
# File 'app/models/concerns/integrations/base/chat_notification.rb', line 152

def webhook_help
  raise NotImplementedError
end