debian-mirror-gitlab/app/models/project_services/chat_notification_service.rb

186 lines
4.8 KiB
Ruby
Raw Normal View History

2018-11-20 20:47:30 +05:30
# frozen_string_literal: true
2017-08-17 22:00:37 +05:30
# 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
boolean_accessor :notify_only_broken_pipelines, :notify_only_default_branch
2018-11-08 19:23:39 +05:30
validates :webhook, presence: true, public_url: true, if: :activated?
2017-08-17 22:00:37 +05:30
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
self.notify_only_default_branch = true
end
end
2018-04-05 14:03:07 +05:30
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
2017-08-17 22:00:37 +05:30
def self.supported_events
2018-04-05 14:03:07 +05:30
%w[push issue confidential_issue merge_request note confidential_note tag_push
2019-07-31 22:56:46 +05:30
pipeline wiki_page deployment]
2017-08-17 22:00:37 +05:30
end
def fields
default_fields + build_event_channels
end
def default_fields
[
2017-09-10 17:25:29 +05:30
{ type: 'text', name: 'webhook', placeholder: "e.g. #{webhook_placeholder}", required: true },
2017-08-17 22:00:37 +05:30
{ type: 'text', name: 'username', placeholder: 'e.g. GitLab' },
{ type: 'checkbox', name: 'notify_only_broken_pipelines' },
2017-09-10 17:25:29 +05:30
{ type: 'checkbox', name: 'notify_only_default_branch' }
2017-08-17 22:00:37 +05:30
]
end
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
2018-04-05 14:03:07 +05:30
event_type = data[:event_type] || object_kind
channel_name = get_channel_field(event_type).presence || channel
2017-08-17 22:00:37 +05:30
opts = {}
opts[:channel] = channel_name if channel_name
opts[:username] = username if username
return false unless notify(message, opts)
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
def default_channel_placeholder
raise NotImplementedError
end
private
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
def get_message(object_kind, data)
case object_kind
when "push", "tag_push"
2018-03-27 19:54:05 +05:30
ChatMessage::PushMessage.new(data) if notify_for_ref?(data)
2017-08-17 22:00:37 +05:30
when "issue"
2018-03-17 18:26:18 +05:30
ChatMessage::IssueMessage.new(data) unless update?(data)
2017-08-17 22:00:37 +05:30
when "merge_request"
2018-03-17 18:26:18 +05:30
ChatMessage::MergeMessage.new(data) unless update?(data)
2017-08-17 22:00:37 +05:30
when "note"
ChatMessage::NoteMessage.new(data)
when "pipeline"
ChatMessage::PipelineMessage.new(data) if should_pipeline_be_notified?(data)
when "wiki_page"
ChatMessage::WikiPageMessage.new(data)
2019-07-31 22:56:46 +05:30
when "deployment"
ChatMessage::DeploymentMessage.new(data)
2017-08-17 22:00:37 +05:30
end
end
def get_channel_field(event)
field_name = event_channel_name(event)
2018-03-17 18:26:18 +05:30
self.public_send(field_name) # rubocop:disable GitlabSecurity/PublicSend
2017-08-17 22:00:37 +05:30
end
def build_event_channels
supported_events.reduce([]) do |channels, event|
channels << { type: 'text', name: event_channel_name(event), placeholder: default_channel_placeholder }
end
end
def event_channel_name(event)
"#{event}_channel"
end
def project_name
2018-03-27 19:54:05 +05:30
project.full_name.gsub(/\s/, '')
2017-08-17 22:00:37 +05:30
end
def project_url
project.web_url
end
2018-03-17 18:26:18 +05:30
def update?(data)
2017-08-17 22:00:37 +05:30
data[:object_attributes][:action] == 'update'
end
def should_pipeline_be_notified?(data)
notify_for_ref?(data) && notify_for_pipeline?(data)
end
def notify_for_ref?(data)
2018-11-08 19:23:39 +05:30
return true if data[:object_kind] == 'tag_push'
2018-03-27 19:54:05 +05:30
return true if data.dig(:object_attributes, :tag)
2017-08-17 22:00:37 +05:30
return true unless notify_only_default_branch?
2018-03-27 19:54:05 +05:30
ref = if data[:ref]
Gitlab::Git.ref_name(data[:ref])
else
data.dig(:object_attributes, :ref)
end
ref == project.default_branch
2017-08-17 22:00:37 +05:30
end
def notify_for_pipeline?(data)
case data[:object_attributes][:status]
when 'success'
!notify_only_broken_pipelines?
when 'failed'
true
else
false
end
end
end