2016-12-06 17:59:03 +00: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
|
|
|
|
|
|
|
|
default_value_for :category, 'chat'
|
|
|
|
|
|
|
|
prop_accessor :webhook, :username, :channel
|
2017-03-22 19:54:17 +00:00
|
|
|
boolean_accessor :notify_only_broken_pipelines, :notify_only_default_branch
|
2016-12-06 17:59:03 +00:00
|
|
|
|
|
|
|
validates :webhook, presence: true, url: true, if: :activated?
|
|
|
|
|
|
|
|
def initialize_properties
|
|
|
|
# Custom serialized properties initialization
|
|
|
|
self.supported_events.each { |event| self.class.prop_accessor(event_channel_name(event)) }
|
|
|
|
|
|
|
|
if properties.nil?
|
|
|
|
self.properties = {}
|
|
|
|
self.notify_only_broken_pipelines = true
|
2017-03-23 07:16:48 +00:00
|
|
|
self.notify_only_default_branch = true
|
2016-12-06 17:59:03 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def can_test?
|
2017-04-18 09:50:04 +00:00
|
|
|
super && valid?
|
2016-12-06 17:59:03 +00:00
|
|
|
end
|
|
|
|
|
2016-12-27 12:44:24 +00:00
|
|
|
def self.supported_events
|
2016-12-06 17:59:03 +00:00
|
|
|
%w[push issue confidential_issue merge_request note tag_push
|
2017-03-17 23:06:11 +00:00
|
|
|
pipeline wiki_page]
|
2016-12-06 17:59:03 +00:00
|
|
|
end
|
|
|
|
|
2017-03-22 19:54:17 +00:00
|
|
|
def fields
|
|
|
|
default_fields + build_event_channels
|
|
|
|
end
|
|
|
|
|
|
|
|
def default_fields
|
|
|
|
[
|
|
|
|
{ type: 'text', name: 'webhook', placeholder: "e.g. #{webhook_placeholder}" },
|
|
|
|
{ type: 'text', name: 'username', placeholder: 'e.g. GitLab' },
|
|
|
|
{ type: 'checkbox', name: 'notify_only_broken_pipelines' },
|
|
|
|
{ type: 'checkbox', name: 'notify_only_default_branch' },
|
|
|
|
]
|
|
|
|
end
|
|
|
|
|
2016-12-06 17:59:03 +00:00
|
|
|
def execute(data)
|
|
|
|
return unless supported_events.include?(data[:object_kind])
|
|
|
|
return unless webhook.present?
|
|
|
|
|
|
|
|
object_kind = data[:object_kind]
|
|
|
|
|
2017-04-04 15:10:21 +00:00
|
|
|
data = custom_data(data)
|
2016-12-06 17:59:03 +00: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
|
|
|
|
|
2016-12-23 17:55:13 +00:00
|
|
|
channel_name = get_channel_field(object_kind).presence || channel
|
2016-12-06 17:59:03 +00:00
|
|
|
|
2016-12-23 17:19:16 +00:00
|
|
|
opts = {}
|
2016-12-23 17:55:13 +00:00
|
|
|
opts[:channel] = channel_name if channel_name
|
2016-12-23 17:19:16 +00:00
|
|
|
opts[:username] = username if username
|
2016-12-22 18:12:35 +00:00
|
|
|
|
2017-04-04 15:10:21 +00:00
|
|
|
return false unless notify(message, opts)
|
2016-12-06 17:59:03 +00: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 18:12:35 +00:00
|
|
|
def default_channel_placeholder
|
2016-12-06 17:59:03 +00:00
|
|
|
raise NotImplementedError
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2017-04-04 15:10:21 +00:00
|
|
|
def notify(message, opts)
|
|
|
|
Slack::Notifier.new(webhook, opts).ping(
|
|
|
|
message.pretext,
|
|
|
|
attachments: message.attachments,
|
|
|
|
fallback: message.fallback
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def custom_data(data)
|
|
|
|
data.merge(project_url: project_url, project_name: project_name)
|
|
|
|
end
|
|
|
|
|
2016-12-06 17:59:03 +00:00
|
|
|
def get_message(object_kind, data)
|
|
|
|
case object_kind
|
|
|
|
when "push", "tag_push"
|
2017-01-20 10:31:42 +00:00
|
|
|
ChatMessage::PushMessage.new(data)
|
2016-12-06 17:59:03 +00:00
|
|
|
when "issue"
|
2017-01-20 10:31:42 +00:00
|
|
|
ChatMessage::IssueMessage.new(data) unless is_update?(data)
|
2016-12-06 17:59:03 +00:00
|
|
|
when "merge_request"
|
2017-01-20 10:31:42 +00:00
|
|
|
ChatMessage::MergeMessage.new(data) unless is_update?(data)
|
2016-12-06 17:59:03 +00:00
|
|
|
when "note"
|
2017-01-20 10:31:42 +00:00
|
|
|
ChatMessage::NoteMessage.new(data)
|
2016-12-06 17:59:03 +00:00
|
|
|
when "pipeline"
|
2017-01-20 10:31:42 +00:00
|
|
|
ChatMessage::PipelineMessage.new(data) if should_pipeline_be_notified?(data)
|
2016-12-06 17:59:03 +00:00
|
|
|
when "wiki_page"
|
2017-01-20 10:31:42 +00:00
|
|
|
ChatMessage::WikiPageMessage.new(data)
|
2016-12-06 17:59:03 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def get_channel_field(event)
|
|
|
|
field_name = event_channel_name(event)
|
|
|
|
self.public_send(field_name)
|
|
|
|
end
|
|
|
|
|
|
|
|
def build_event_channels
|
|
|
|
supported_events.reduce([]) do |channels, event|
|
2016-12-22 18:12:35 +00:00
|
|
|
channels << { type: 'text', name: event_channel_name(event), placeholder: default_channel_placeholder }
|
2016-12-06 17:59:03 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def event_channel_name(event)
|
|
|
|
"#{event}_channel"
|
|
|
|
end
|
|
|
|
|
|
|
|
def project_name
|
|
|
|
project.name_with_namespace.gsub(/\s/, '')
|
|
|
|
end
|
|
|
|
|
|
|
|
def project_url
|
|
|
|
project.web_url
|
|
|
|
end
|
|
|
|
|
|
|
|
def is_update?(data)
|
|
|
|
data[:object_attributes][:action] == 'update'
|
|
|
|
end
|
|
|
|
|
|
|
|
def should_pipeline_be_notified?(data)
|
2017-03-23 07:16:48 +00:00
|
|
|
notify_for_ref?(data) && notify_for_pipeline?(data)
|
2017-03-22 19:54:17 +00:00
|
|
|
end
|
|
|
|
|
2017-03-23 07:16:48 +00:00
|
|
|
def notify_for_ref?(data)
|
|
|
|
return true if data[:object_attributes][:tag]
|
|
|
|
return true unless notify_only_default_branch
|
2017-03-22 19:54:17 +00:00
|
|
|
|
2017-03-23 07:16:48 +00:00
|
|
|
data[:object_attributes][:ref] == project.default_branch
|
2017-03-22 19:54:17 +00:00
|
|
|
end
|
|
|
|
|
2017-03-23 07:16:48 +00:00
|
|
|
def notify_for_pipeline?(data)
|
2016-12-06 17:59:03 +00:00
|
|
|
case data[:object_attributes][:status]
|
|
|
|
when 'success'
|
|
|
|
!notify_only_broken_pipelines?
|
|
|
|
when 'failed'
|
|
|
|
true
|
|
|
|
else
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|