Class: Integrations::BaseChatNotification

Inherits:
Integration show all
Includes:
ChatMessage, NotificationBranchSelection
Defined in:
app/models/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
].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

Constants inherited from Integration

Integration::BASE_CLASSES, Integration::DEV_INTEGRATION_NAMES, Integration::INTEGRATION_NAMES, Integration::PROJECT_SPECIFIC_INTEGRATION_NAMES, Integration::SECTION_TYPE_CONNECTION, Integration::UnknownType

Constants inherited from ApplicationRecord

ApplicationRecord::MAX_PLUCK

Instance Attribute Summary

Attributes included from Importable

#imported, #importing

Class Method Summary collapse

Instance Method Summary collapse

Methods included from NotificationBranchSelection

#branch_choices, #notify_for_branch?

Methods inherited from Integration

#activate_disabled_reason, #activated?, #api_field_names, #async_execute, #attributes, available_integration_names, available_integration_types, boolean_accessor, build_from_integration, #category, #configurable_events, create_from_active_default_integrations, default_integration, #default_test_event, default_test_event, #description, dev_integration_names, #dup, #editable?, event_description, event_names, #event_names, field, fields, find_or_initialize_all_non_project_specific, find_or_initialize_non_project_specific_integration, #group_level?, #help, #inheritable?, inherited_descendants_from_self_or_ancestors_from, instance_exists_for?, #instance_level?, integration_name_to_model, integration_name_to_type, integration_names, #json_fields, #operating?, #parent, #project_level?, project_specific_integration_names, prop_accessor, #properties=, #reencrypt_properties, #reset_updated_properties, #secret_fields, #sections, #show_active_box?, #supported_events, #supports_data_fields?, #test, #testable?, #title, #to_data_fields_hash, #to_integration_hash, to_param, #to_param, #updated_properties

Methods included from Gitlab::Utils::Override

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

Methods included from ResetSecretFields

#exposing_secrets_fields

Methods included from Loggable

#build_message, #log_error, #log_exception, #log_info, #logger

Methods inherited from ApplicationRecord

cached_column_list, #create_or_load_association, declarative_enum, default_select_columns, id_in, id_not_in, iid_in, pluck_primary_key, primary_key_in, #readable_by?, safe_ensure_unique, safe_find_or_create_by, safe_find_or_create_by!, #to_ability_name, underscore, where_exists, where_not_exists, with_fast_read_statement_timeout, without_order

Methods included from SensitiveSerializableHash

#serializable_hash

Class Method Details

.supported_eventsObject


64
65
66
# File 'app/models/integrations/base_chat_notification.rb', line 64

def self.supported_events
  SUPPORTED_EVENTS
end

Instance Method Details

#confidential_issue_channelObject


56
57
58
# File 'app/models/integrations/base_chat_notification.rb', line 56

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

#confidential_note_channelObject


60
61
62
# File 'app/models/integrations/base_chat_notification.rb', line 60

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

#default_channel_placeholderObject

Raises:

  • (NotImplementedError)

148
149
150
# File 'app/models/integrations/base_chat_notification.rb', line 148

def default_channel_placeholder
  raise NotImplementedError
end

#default_fieldsObject


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
# File 'app/models/integrations/base_chat_notification.rb', line 72

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

#event_channel_namesObject


136
137
138
# File 'app/models/integrations/base_chat_notification.rb', line 136

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

#event_field(event) ⇒ Object


140
141
142
# File 'app/models/integrations/base_chat_notification.rb', line 140

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

#execute(data) ⇒ Object


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
# File 'app/models/integrations/base_chat_notification.rb', line 100

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

#fieldsObject


68
69
70
# File 'app/models/integrations/base_chat_notification.rb', line 68

def fields
  default_fields + build_event_channels
end

#global_fieldsObject


144
145
146
# File 'app/models/integrations/base_chat_notification.rb', line 144

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

#initialize_propertiesObject


37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'app/models/integrations/base_chat_notification.rb', line 37

def initialize_properties
  super

  if properties.empty?
    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

#webhook_placeholderObject

Raises:

  • (NotImplementedError)

152
153
154
# File 'app/models/integrations/base_chat_notification.rb', line 152

def webhook_placeholder
  raise NotImplementedError
end