2014-03-18 17:27:03 +00:00
|
|
|
class SlackService < Service
|
2014-12-28 20:08:33 +00:00
|
|
|
prop_accessor :webhook, :username, :channel
|
2016-07-29 16:59:54 +00:00
|
|
|
boolean_accessor :notify_only_broken_builds, :notify_only_broken_pipelines
|
2016-04-21 15:13:14 +00:00
|
|
|
validates :webhook, presence: true, url: true, if: :activated?
|
2014-03-18 17:27:03 +00:00
|
|
|
|
2015-12-11 12:28:40 +00:00
|
|
|
def initialize_properties
|
2016-07-06 20:52:00 +00:00
|
|
|
# Custom serialized properties initialization
|
2016-07-12 20:00:49 +00:00
|
|
|
self.supported_events.each { |event| self.class.prop_accessor(event_channel_name(event)) }
|
2016-07-06 20:52:00 +00:00
|
|
|
|
2015-12-11 12:28:40 +00:00
|
|
|
if properties.nil?
|
|
|
|
self.properties = {}
|
|
|
|
self.notify_only_broken_builds = true
|
2016-07-29 16:59:54 +00:00
|
|
|
self.notify_only_broken_pipelines = true
|
2015-12-11 12:28:40 +00:00
|
|
|
end
|
|
|
|
end
|
2015-12-10 13:08:09 +00:00
|
|
|
|
2014-03-18 17:27:03 +00:00
|
|
|
def title
|
|
|
|
'Slack'
|
|
|
|
end
|
|
|
|
|
|
|
|
def description
|
|
|
|
'A team communication tool for the 21st century'
|
|
|
|
end
|
|
|
|
|
|
|
|
def to_param
|
|
|
|
'slack'
|
|
|
|
end
|
|
|
|
|
2015-09-15 18:46:11 +00:00
|
|
|
def help
|
|
|
|
'This service sends notifications to your Slack channel.<br/>
|
|
|
|
To setup this Service you need to create a new <b>"Incoming webhook"</b> in your Slack integration panel,
|
|
|
|
and enter the Webhook URL below.'
|
|
|
|
end
|
|
|
|
|
2014-03-18 17:27:03 +00:00
|
|
|
def fields
|
2016-07-06 20:52:00 +00:00
|
|
|
default_fields =
|
|
|
|
[
|
|
|
|
{ type: 'text', name: 'webhook', placeholder: 'https://hooks.slack.com/services/...' },
|
|
|
|
{ type: 'text', name: 'username', placeholder: 'username' },
|
2016-07-12 20:00:49 +00:00
|
|
|
{ type: 'text', name: 'channel', placeholder: "#general" },
|
2016-07-06 20:52:00 +00:00
|
|
|
{ type: 'checkbox', name: 'notify_only_broken_builds' },
|
2016-07-29 16:59:54 +00:00
|
|
|
{ type: 'checkbox', name: 'notify_only_broken_pipelines' },
|
2016-07-06 20:52:00 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
default_fields + build_event_channels
|
2014-03-18 17:27:03 +00:00
|
|
|
end
|
|
|
|
|
2015-02-28 16:33:18 +00:00
|
|
|
def supported_events
|
2016-09-06 08:23:17 +00:00
|
|
|
%w[push issue confidential_issue merge_request note tag_push
|
|
|
|
build pipeline wiki_page]
|
2015-02-28 16:33:18 +00:00
|
|
|
end
|
|
|
|
|
2015-02-19 05:02:57 +00:00
|
|
|
def execute(data)
|
2015-02-28 16:33:18 +00:00
|
|
|
return unless supported_events.include?(data[:object_kind])
|
2014-10-23 18:47:28 +00:00
|
|
|
return unless webhook.present?
|
|
|
|
|
2015-02-19 05:02:57 +00:00
|
|
|
object_kind = data[:object_kind]
|
|
|
|
|
|
|
|
data = data.merge(
|
2014-03-18 17:27:03 +00:00
|
|
|
project_url: project_url,
|
|
|
|
project_name: project_name
|
2015-02-19 05:02:57 +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.
|
|
|
|
|
2016-07-29 17:18:35 +00:00
|
|
|
message = get_message(object_kind, data)
|
2014-03-18 17:27:03 +00:00
|
|
|
|
2016-08-01 08:06:57 +00:00
|
|
|
if message
|
|
|
|
opt = {}
|
2016-07-06 20:52:00 +00:00
|
|
|
|
2016-08-01 08:06:57 +00:00
|
|
|
event_channel = get_channel_field(object_kind) || channel
|
2016-07-06 20:52:00 +00:00
|
|
|
|
2016-08-01 08:06:57 +00:00
|
|
|
opt[:channel] = event_channel if event_channel
|
|
|
|
opt[:username] = username if username
|
2014-12-28 20:08:33 +00:00
|
|
|
|
2015-02-19 05:02:57 +00:00
|
|
|
notifier = Slack::Notifier.new(webhook, opt)
|
2015-12-07 12:23:23 +00:00
|
|
|
notifier.ping(message.pretext, attachments: message.attachments, fallback: message.fallback)
|
2016-08-01 08:06:57 +00:00
|
|
|
|
|
|
|
true
|
|
|
|
else
|
|
|
|
false
|
2015-02-19 05:02:57 +00:00
|
|
|
end
|
2014-03-18 17:27:03 +00:00
|
|
|
end
|
|
|
|
|
2016-07-06 20:52:00 +00:00
|
|
|
def event_channel_names
|
|
|
|
supported_events.map { |event| event_channel_name(event) }
|
|
|
|
end
|
|
|
|
|
2016-07-13 19:49:47 +00:00
|
|
|
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
|
|
|
|
|
2014-03-18 17:27:03 +00:00
|
|
|
private
|
|
|
|
|
2016-07-29 17:18:35 +00:00
|
|
|
def get_message(object_kind, data)
|
|
|
|
case object_kind
|
|
|
|
when "push", "tag_push"
|
|
|
|
PushMessage.new(data)
|
|
|
|
when "issue"
|
|
|
|
IssueMessage.new(data) unless is_update?(data)
|
|
|
|
when "merge_request"
|
|
|
|
MergeMessage.new(data) unless is_update?(data)
|
|
|
|
when "note"
|
|
|
|
NoteMessage.new(data)
|
|
|
|
when "build"
|
|
|
|
BuildMessage.new(data) if should_build_be_notified?(data)
|
|
|
|
when "pipeline"
|
|
|
|
PipelineMessage.new(data) if should_pipeline_be_notified?(data)
|
|
|
|
when "wiki_page"
|
|
|
|
WikiPageMessage.new(data)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-07-06 20:52:00 +00:00
|
|
|
def get_channel_field(event)
|
|
|
|
field_name = event_channel_name(event)
|
2016-07-12 20:00:49 +00:00
|
|
|
self.public_send(field_name)
|
2016-07-06 20:52:00 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def build_event_channels
|
2016-07-12 20:00:49 +00:00
|
|
|
supported_events.reduce([]) do |channels, event|
|
|
|
|
channels << { type: 'text', name: event_channel_name(event), placeholder: "#general" }
|
2016-07-06 20:52:00 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def event_channel_name(event)
|
|
|
|
"#{event}_channel"
|
|
|
|
end
|
|
|
|
|
2014-03-18 17:27:03 +00:00
|
|
|
def project_name
|
|
|
|
project.name_with_namespace.gsub(/\s/, '')
|
|
|
|
end
|
|
|
|
|
|
|
|
def project_url
|
|
|
|
project.web_url
|
|
|
|
end
|
2015-02-19 05:02:57 +00:00
|
|
|
|
|
|
|
def is_update?(data)
|
|
|
|
data[:object_attributes][:action] == 'update'
|
|
|
|
end
|
2015-12-07 12:23:23 +00:00
|
|
|
|
|
|
|
def should_build_be_notified?(data)
|
|
|
|
case data[:commit][:status]
|
|
|
|
when 'success'
|
|
|
|
!notify_only_broken_builds?
|
|
|
|
when 'failed'
|
|
|
|
true
|
|
|
|
else
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end
|
2016-07-29 16:59:54 +00:00
|
|
|
|
|
|
|
def should_pipeline_be_notified?(data)
|
|
|
|
case data[:object_attributes][:status]
|
|
|
|
when 'success'
|
2016-08-01 08:06:57 +00:00
|
|
|
!notify_only_broken_pipelines?
|
2016-07-29 16:59:54 +00:00
|
|
|
when 'failed'
|
|
|
|
true
|
|
|
|
else
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end
|
2014-03-18 17:27:03 +00:00
|
|
|
end
|
2015-02-20 13:49:26 +00:00
|
|
|
|
|
|
|
require "slack_service/issue_message"
|
|
|
|
require "slack_service/push_message"
|
2015-02-20 15:05:40 +00:00
|
|
|
require "slack_service/merge_message"
|
2015-03-05 18:38:23 +00:00
|
|
|
require "slack_service/note_message"
|
2015-12-07 12:23:23 +00:00
|
|
|
require "slack_service/build_message"
|
2016-07-29 16:59:54 +00:00
|
|
|
require "slack_service/pipeline_message"
|
2016-02-28 07:26:52 +00:00
|
|
|
require "slack_service/wiki_page_message"
|