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

118 lines
2.8 KiB
Ruby
Raw Normal View History

2014-09-02 18:07:02 +05:30
class SlackService < Service
2015-04-26 12:48:37 +05:30
prop_accessor :webhook, :username, :channel
2015-12-23 02:04:40 +05:30
boolean_accessor :notify_only_broken_builds
2016-06-02 11:05:42 +05:30
validates :webhook, presence: true, url: true, if: :activated?
2014-09-02 18:07:02 +05:30
2015-12-23 02:04:40 +05:30
def initialize_properties
if properties.nil?
self.properties = {}
self.notify_only_broken_builds = true
end
end
2014-09-02 18:07:02 +05:30
def title
'Slack'
end
def description
'A team communication tool for the 21st century'
end
def to_param
'slack'
end
2015-09-25 12:07:36 +05:30
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-09-02 18:07:02 +05:30
def fields
[
2015-04-26 12:48:37 +05:30
{ type: 'text', name: 'webhook',
placeholder: 'https://hooks.slack.com/services/...' },
{ type: 'text', name: 'username', placeholder: 'username' },
2015-12-23 02:04:40 +05:30
{ type: 'text', name: 'channel', placeholder: '#channel' },
{ type: 'checkbox', name: 'notify_only_broken_builds' },
2014-09-02 18:07:02 +05:30
]
end
2015-04-26 12:48:37 +05:30
def supported_events
2016-06-02 11:05:42 +05:30
%w(push issue merge_request note tag_push build wiki_page)
2015-04-26 12:48: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 = data.merge(
2014-09-02 18:07:02 +05:30
project_url: project_url,
project_name: project_name
2015-04-26 12:48:37 +05:30
)
# 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.
2014-09-02 18:07:02 +05:30
2015-04-26 12:48:37 +05:30
message = \
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)
2015-12-23 02:04:40 +05:30
when "build"
BuildMessage.new(data) if should_build_be_notified?(data)
2016-06-02 11:05:42 +05:30
when "wiki_page"
WikiPageMessage.new(data)
2015-04-26 12:48:37 +05:30
end
opt = {}
opt[:channel] = channel if channel
opt[:username] = username if username
if message
notifier = Slack::Notifier.new(webhook, opt)
2015-12-23 02:04:40 +05:30
notifier.ping(message.pretext, attachments: message.attachments, fallback: message.fallback)
2015-04-26 12:48:37 +05:30
end
2014-09-02 18:07:02 +05:30
end
private
def project_name
project.name_with_namespace.gsub(/\s/, '')
end
def project_url
project.web_url
end
2015-04-26 12:48:37 +05:30
def is_update?(data)
data[:object_attributes][:action] == 'update'
end
2015-12-23 02:04:40 +05:30
def should_build_be_notified?(data)
case data[:commit][:status]
when 'success'
!notify_only_broken_builds?
when 'failed'
true
else
false
end
end
2014-09-02 18:07:02 +05:30
end
2015-04-26 12:48:37 +05:30
require "slack_service/issue_message"
require "slack_service/push_message"
require "slack_service/merge_message"
require "slack_service/note_message"
2015-12-23 02:04:40 +05:30
require "slack_service/build_message"
2016-06-02 11:05:42 +05:30
require "slack_service/wiki_page_message"