Class: ChatNotificationService

Inherits:
Service show all
Includes:
ChatMessage, NotificationBranchSelection
Defined in:
app/models/project_services/chat_notification_service.rb

Overview

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

Constant Summary collapse

SUPPORTED_EVENTS =
%w[
  push issue confidential_issue merge_request note confidential_note
  tag_push pipeline wiki_page deployment
].freeze
EVENT_CHANNEL =
proc { |event| "#{event}_channel" }

Constants inherited from Service

Service::DEV_SERVICE_NAMES, Service::SERVICE_NAMES

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 Service

#activated?, #api_field_names, #async_execute, available_services_names, available_services_types, boolean_accessor, build_from_integration, #can_test?, #category, #configurable_event_actions, #configurable_events, default_integration, #description, dev_services_names, #editable?, event_description, #event_names, event_names, find_or_create_templates, find_or_initialize_all, find_or_initialize_integration, #help, instance_exists_for?, #issue_tracker?, #json_fields, #operating?, prop_accessor, #reset_updated_properties, services_names, services_types, #show_active_box?, supported_event_actions, #supported_events, #supports_data_fields?, #test, #title, #to_data_fields_hash, #to_param, to_param, #to_service_hash, #updated_properties

Methods included from ProjectServicesLoggable

#build_message, #log_error, #log_info, #logger

Methods inherited from ApplicationRecord

at_most, id_in, id_not_in, iid_in, pluck_primary_key, primary_key_in, safe_ensure_unique, safe_find_or_create_by, safe_find_or_create_by!, underscore, without_order

Class Method Details

.supported_eventsObject


52
53
54
# File 'app/models/project_services/chat_notification_service.rb', line 52

def self.supported_events
  SUPPORTED_EVENTS
end

Instance Method Details

#confidential_issue_channelObject


44
45
46
# File 'app/models/project_services/chat_notification_service.rb', line 44

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

#confidential_note_channelObject


48
49
50
# File 'app/models/project_services/chat_notification_service.rb', line 48

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

#default_channel_placeholderObject

Raises:

  • (NotImplementedError)

111
112
113
# File 'app/models/project_services/chat_notification_service.rb', line 111

def default_channel_placeholder
  raise NotImplementedError
end

#default_fieldsObject


60
61
62
63
64
65
66
67
# File 'app/models/project_services/chat_notification_service.rb', line 60

def default_fields
  [
    { type: 'text', name: 'webhook', placeholder: "e.g. #{webhook_placeholder}", required: true }.freeze,
    { type: 'text', name: 'username', placeholder: 'e.g. GitLab' }.freeze,
    { type: 'checkbox', name: 'notify_only_broken_pipelines' }.freeze,
    { type: 'select', name: 'branches_to_be_notified', choices: branch_choices }.freeze
  ].freeze
end

#event_channel_namesObject


99
100
101
# File 'app/models/project_services/chat_notification_service.rb', line 99

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

#event_field(event) ⇒ Object


103
104
105
# File 'app/models/project_services/chat_notification_service.rb', line 103

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

#execute(data) ⇒ Object


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
# File 'app/models/project_services/chat_notification_service.rb', line 69

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

  object_kind = data[:object_kind]

  data = custom_data(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

  return false unless notify(message, opts)

  true
end

#fieldsObject


56
57
58
# File 'app/models/project_services/chat_notification_service.rb', line 56

def fields
  default_fields + build_event_channels
end

#global_fieldsObject


107
108
109
# File 'app/models/project_services/chat_notification_service.rb', line 107

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

#initialize_propertiesObject


27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'app/models/project_services/chat_notification_service.rb', line 27

def initialize_properties
  if properties.nil?
    self.properties = {}
    self.notify_only_broken_pipelines = true
    self.branches_to_be_notified = "default"
  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 hasn't specified one already. When users edit the service and
    # selects a value for this new property, it will override everything.

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