2014-03-18 13:27:03 -04:00
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: services
|
|
|
|
#
|
2015-03-04 17:14:00 -05:00
|
|
|
# id :integer not null, primary key
|
|
|
|
# type :string(255)
|
|
|
|
# title :string(255)
|
|
|
|
# project_id :integer
|
|
|
|
# created_at :datetime
|
|
|
|
# updated_at :datetime
|
|
|
|
# active :boolean default(FALSE), not null
|
|
|
|
# properties :text
|
|
|
|
# template :boolean default(FALSE)
|
2015-02-19 00:02:57 -05:00
|
|
|
# push_events :boolean default(TRUE)
|
|
|
|
# issues_events :boolean default(TRUE)
|
|
|
|
# merge_requests_events :boolean default(TRUE)
|
|
|
|
# tag_push_events :boolean default(TRUE)
|
2015-03-05 13:38:23 -05:00
|
|
|
# note_events :boolean default(TRUE), not null
|
2014-03-18 13:27:03 -04:00
|
|
|
#
|
|
|
|
|
|
|
|
class SlackService < Service
|
2014-12-28 15:08:33 -05:00
|
|
|
prop_accessor :webhook, :username, :channel
|
2014-10-03 10:24:11 -04:00
|
|
|
validates :webhook, presence: true, if: :activated?
|
2014-03-18 13:27:03 -04:00
|
|
|
|
|
|
|
def title
|
|
|
|
'Slack'
|
|
|
|
end
|
|
|
|
|
|
|
|
def description
|
|
|
|
'A team communication tool for the 21st century'
|
|
|
|
end
|
|
|
|
|
|
|
|
def to_param
|
|
|
|
'slack'
|
|
|
|
end
|
|
|
|
|
|
|
|
def fields
|
|
|
|
[
|
2014-12-28 15:08:33 -05:00
|
|
|
{ type: 'text', name: 'webhook',
|
|
|
|
placeholder: 'https://hooks.slack.com/services/...' },
|
|
|
|
{ type: 'text', name: 'username', placeholder: 'username' },
|
|
|
|
{ type: 'text', name: 'channel', placeholder: '#channel' }
|
2014-03-18 13:27:03 -04:00
|
|
|
]
|
|
|
|
end
|
|
|
|
|
2015-02-28 11:33:18 -05:00
|
|
|
def supported_events
|
2015-03-06 11:31:49 -05:00
|
|
|
%w(push issue merge_request note tag_push)
|
2015-02-28 11:33:18 -05:00
|
|
|
end
|
|
|
|
|
2015-02-19 00:02:57 -05:00
|
|
|
def execute(data)
|
2015-02-28 11:33:18 -05:00
|
|
|
return unless supported_events.include?(data[:object_kind])
|
2014-10-23 14:47:28 -04:00
|
|
|
return unless webhook.present?
|
|
|
|
|
2015-02-19 00:02:57 -05:00
|
|
|
object_kind = data[:object_kind]
|
|
|
|
|
|
|
|
data = data.merge(
|
2014-03-18 13:27:03 -04:00
|
|
|
project_url: project_url,
|
|
|
|
project_name: project_name
|
2015-02-19 00:02:57 -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.
|
|
|
|
|
2015-02-20 08:49:26 -05:00
|
|
|
message = \
|
|
|
|
case object_kind
|
2015-03-06 11:31:49 -05:00
|
|
|
when "push", "tag_push"
|
2015-02-20 08:49:26 -05:00
|
|
|
PushMessage.new(data)
|
|
|
|
when "issue"
|
|
|
|
IssueMessage.new(data) unless is_update?(data)
|
|
|
|
when "merge_request"
|
|
|
|
MergeMessage.new(data) unless is_update?(data)
|
2015-03-05 13:38:23 -05:00
|
|
|
when "note"
|
|
|
|
NoteMessage.new(data)
|
2015-02-20 08:49:26 -05:00
|
|
|
end
|
2014-03-18 13:27:03 -04:00
|
|
|
|
2014-12-28 15:08:33 -05:00
|
|
|
opt = {}
|
|
|
|
opt[:channel] = channel if channel
|
|
|
|
opt[:username] = username if username
|
|
|
|
|
2015-02-19 00:02:57 -05:00
|
|
|
if message
|
|
|
|
notifier = Slack::Notifier.new(webhook, opt)
|
|
|
|
notifier.ping(message.pretext, attachments: message.attachments)
|
|
|
|
end
|
2014-03-18 13:27:03 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def project_name
|
|
|
|
project.name_with_namespace.gsub(/\s/, '')
|
|
|
|
end
|
|
|
|
|
|
|
|
def project_url
|
|
|
|
project.web_url
|
|
|
|
end
|
2015-02-19 00:02:57 -05:00
|
|
|
|
|
|
|
def is_update?(data)
|
|
|
|
data[:object_attributes][:action] == 'update'
|
|
|
|
end
|
2014-03-18 13:27:03 -04:00
|
|
|
end
|
2015-02-20 08:49:26 -05:00
|
|
|
|
|
|
|
require "slack_service/issue_message"
|
|
|
|
require "slack_service/push_message"
|
2015-02-20 10:05:40 -05:00
|
|
|
require "slack_service/merge_message"
|
2015-03-05 13:38:23 -05:00
|
|
|
require "slack_service/note_message"
|