2018-08-10 02:45:01 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-12-06 12:59:03 -05:00
|
|
|
# Base class for Chat notifications services
|
|
|
|
# This class is not meant to be used directly, but only to inherit from.
|
|
|
|
class ChatNotificationService < Service
|
|
|
|
include ChatMessage
|
2019-09-13 14:06:03 -04:00
|
|
|
include NotificationBranchSelection
|
2016-12-06 12:59:03 -05:00
|
|
|
|
2019-09-02 09:25:15 -04:00
|
|
|
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" }
|
|
|
|
|
2016-12-06 12:59:03 -05:00
|
|
|
default_value_for :category, 'chat'
|
|
|
|
|
2019-09-13 14:06:03 -04:00
|
|
|
prop_accessor :webhook, :username, :channel, :branches_to_be_notified
|
2019-09-02 09:25:15 -04:00
|
|
|
|
|
|
|
# Custom serialized properties initialization
|
|
|
|
prop_accessor(*SUPPORTED_EVENTS.map { |event| EVENT_CHANNEL[event] })
|
|
|
|
|
2017-03-22 15:54:17 -04:00
|
|
|
boolean_accessor :notify_only_broken_pipelines, :notify_only_default_branch
|
2016-12-06 12:59:03 -05:00
|
|
|
|
2018-06-01 07:43:53 -04:00
|
|
|
validates :webhook, presence: true, public_url: true, if: :activated?
|
2016-12-06 12:59:03 -05:00
|
|
|
|
|
|
|
def initialize_properties
|
|
|
|
if properties.nil?
|
|
|
|
self.properties = {}
|
|
|
|
self.notify_only_broken_pipelines = true
|
2019-09-13 14:06:03 -04:00
|
|
|
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"
|
2016-12-06 12:59:03 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-04-03 07:00:33 -04:00
|
|
|
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
|
|
|
|
|
2016-12-27 07:44:24 -05:00
|
|
|
def self.supported_events
|
2019-09-02 09:25:15 -04:00
|
|
|
SUPPORTED_EVENTS
|
2016-12-06 12:59:03 -05:00
|
|
|
end
|
|
|
|
|
2017-03-22 15:54:17 -04:00
|
|
|
def fields
|
|
|
|
default_fields + build_event_channels
|
|
|
|
end
|
|
|
|
|
|
|
|
def default_fields
|
|
|
|
[
|
2017-05-22 06:07:12 -04:00
|
|
|
{ type: 'text', name: 'webhook', placeholder: "e.g. #{webhook_placeholder}", required: true },
|
2017-03-22 15:54:17 -04:00
|
|
|
{ type: 'text', name: 'username', placeholder: 'e.g. GitLab' },
|
|
|
|
{ type: 'checkbox', name: 'notify_only_broken_pipelines' },
|
2019-09-13 14:06:03 -04:00
|
|
|
{ type: 'select', name: 'branches_to_be_notified', choices: BRANCH_CHOICES }
|
2017-03-22 15:54:17 -04:00
|
|
|
]
|
|
|
|
end
|
|
|
|
|
2016-12-06 12:59:03 -05:00
|
|
|
def execute(data)
|
|
|
|
return unless supported_events.include?(data[:object_kind])
|
|
|
|
return unless webhook.present?
|
|
|
|
|
|
|
|
object_kind = data[:object_kind]
|
|
|
|
|
2017-04-04 11:10:21 -04:00
|
|
|
data = custom_data(data)
|
2016-12-06 12:59:03 -05:00
|
|
|
|
|
|
|
# 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
|
|
|
|
|
2018-04-03 07:00:33 -04:00
|
|
|
event_type = data[:event_type] || object_kind
|
|
|
|
|
|
|
|
channel_name = get_channel_field(event_type).presence || channel
|
2016-12-06 12:59:03 -05:00
|
|
|
|
2016-12-23 12:19:16 -05:00
|
|
|
opts = {}
|
2016-12-23 12:55:13 -05:00
|
|
|
opts[:channel] = channel_name if channel_name
|
2016-12-23 12:19:16 -05:00
|
|
|
opts[:username] = username if username
|
2016-12-22 13:12:35 -05:00
|
|
|
|
2017-04-04 11:10:21 -04:00
|
|
|
return false unless notify(message, opts)
|
2016-12-06 12:59:03 -05:00
|
|
|
|
|
|
|
true
|
|
|
|
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
|
|
|
|
|
2016-12-22 13:12:35 -05:00
|
|
|
def default_channel_placeholder
|
2016-12-06 12:59:03 -05:00
|
|
|
raise NotImplementedError
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2019-09-27 07:35:37 -04:00
|
|
|
# every notifier must implement this independently
|
2017-04-04 11:10:21 -04:00
|
|
|
def notify(message, opts)
|
2019-09-27 07:35:37 -04:00
|
|
|
raise NotImplementedError
|
2017-04-04 11:10:21 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def custom_data(data)
|
|
|
|
data.merge(project_url: project_url, project_name: project_name)
|
|
|
|
end
|
|
|
|
|
2016-12-06 12:59:03 -05:00
|
|
|
def get_message(object_kind, data)
|
|
|
|
case object_kind
|
|
|
|
when "push", "tag_push"
|
2018-02-23 23:13:35 -05:00
|
|
|
ChatMessage::PushMessage.new(data) if notify_for_ref?(data)
|
2016-12-06 12:59:03 -05:00
|
|
|
when "issue"
|
2017-08-24 12:43:47 -04:00
|
|
|
ChatMessage::IssueMessage.new(data) unless update?(data)
|
2016-12-06 12:59:03 -05:00
|
|
|
when "merge_request"
|
2017-08-24 12:43:47 -04:00
|
|
|
ChatMessage::MergeMessage.new(data) unless update?(data)
|
2016-12-06 12:59:03 -05:00
|
|
|
when "note"
|
2017-01-20 05:31:42 -05:00
|
|
|
ChatMessage::NoteMessage.new(data)
|
2016-12-06 12:59:03 -05:00
|
|
|
when "pipeline"
|
2017-01-20 05:31:42 -05:00
|
|
|
ChatMessage::PipelineMessage.new(data) if should_pipeline_be_notified?(data)
|
2016-12-06 12:59:03 -05:00
|
|
|
when "wiki_page"
|
2017-01-20 05:31:42 -05:00
|
|
|
ChatMessage::WikiPageMessage.new(data)
|
2019-04-26 17:08:41 -04:00
|
|
|
when "deployment"
|
|
|
|
ChatMessage::DeploymentMessage.new(data)
|
2016-12-06 12:59:03 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def get_channel_field(event)
|
|
|
|
field_name = event_channel_name(event)
|
2017-08-10 12:39:26 -04:00
|
|
|
self.public_send(field_name) # rubocop:disable GitlabSecurity/PublicSend
|
2016-12-06 12:59:03 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def build_event_channels
|
|
|
|
supported_events.reduce([]) do |channels, event|
|
2016-12-22 13:12:35 -05:00
|
|
|
channels << { type: 'text', name: event_channel_name(event), placeholder: default_channel_placeholder }
|
2016-12-06 12:59:03 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def event_channel_name(event)
|
2019-09-02 09:25:15 -04:00
|
|
|
EVENT_CHANNEL[event]
|
2016-12-06 12:59:03 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def project_name
|
2018-03-05 09:15:26 -05:00
|
|
|
project.full_name.gsub(/\s/, '')
|
2016-12-06 12:59:03 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def project_url
|
|
|
|
project.web_url
|
|
|
|
end
|
|
|
|
|
2017-08-24 12:43:47 -04:00
|
|
|
def update?(data)
|
2016-12-06 12:59:03 -05:00
|
|
|
data[:object_attributes][:action] == 'update'
|
|
|
|
end
|
|
|
|
|
|
|
|
def should_pipeline_be_notified?(data)
|
2017-03-23 03:16:48 -04:00
|
|
|
notify_for_ref?(data) && notify_for_pipeline?(data)
|
2017-03-22 15:54:17 -04:00
|
|
|
end
|
|
|
|
|
2017-03-23 03:16:48 -04:00
|
|
|
def notify_for_ref?(data)
|
2018-06-14 13:40:35 -04:00
|
|
|
return true if data[:object_kind] == 'tag_push'
|
2018-02-23 23:13:35 -05:00
|
|
|
return true if data.dig(:object_attributes, :tag)
|
2017-03-22 15:54:17 -04:00
|
|
|
|
2019-09-13 14:06:03 -04:00
|
|
|
notify_for_branch?(data)
|
2017-03-22 15:54:17 -04:00
|
|
|
end
|
|
|
|
|
2017-03-23 03:16:48 -04:00
|
|
|
def notify_for_pipeline?(data)
|
2016-12-06 12:59:03 -05:00
|
|
|
case data[:object_attributes][:status]
|
|
|
|
when 'success'
|
|
|
|
!notify_only_broken_pipelines?
|
|
|
|
when 'failed'
|
|
|
|
true
|
|
|
|
else
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|