2018-08-10 02:45:01 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2013-05-23 14:10:32 -04:00
|
|
|
class HipchatService < Service
|
2016-10-04 11:27:40 -04:00
|
|
|
include ActionView::Helpers::SanitizeHelper
|
|
|
|
|
2014-07-29 04:04:19 -04:00
|
|
|
MAX_COMMITS = 3
|
2016-10-19 15:51:15 -04:00
|
|
|
HIPCHAT_ALLOWED_TAGS = %w[
|
|
|
|
a b i strong em br img pre code
|
|
|
|
table th tr td caption colgroup col thead tbody tfoot
|
|
|
|
ul ol li dl dt dd
|
2017-02-21 18:32:18 -05:00
|
|
|
].freeze
|
2014-07-29 04:04:19 -04:00
|
|
|
|
2016-12-05 09:40:53 -05:00
|
|
|
prop_accessor :token, :room, :server, :color, :api_version
|
2017-03-17 19:06:11 -04:00
|
|
|
boolean_accessor :notify_only_broken_pipelines, :notify
|
2013-05-23 14:10:32 -04:00
|
|
|
validates :token, presence: true, if: :activated?
|
|
|
|
|
2015-12-11 07:28:40 -05:00
|
|
|
def initialize_properties
|
|
|
|
if properties.nil?
|
|
|
|
self.properties = {}
|
2017-03-17 19:06:11 -04:00
|
|
|
self.notify_only_broken_pipelines = true
|
2015-12-11 07:28:40 -05:00
|
|
|
end
|
|
|
|
end
|
2015-12-10 08:08:09 -05:00
|
|
|
|
2013-05-23 14:10:32 -04:00
|
|
|
def title
|
2014-11-09 00:12:14 -05:00
|
|
|
'HipChat'
|
2013-05-23 14:10:32 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def description
|
2013-11-20 07:05:42 -05:00
|
|
|
'Private group chat and IM'
|
2013-05-23 14:10:32 -04:00
|
|
|
end
|
|
|
|
|
2016-12-27 07:44:24 -05:00
|
|
|
def self.to_param
|
2013-05-23 14:10:32 -04:00
|
|
|
'hipchat'
|
|
|
|
end
|
|
|
|
|
|
|
|
def fields
|
|
|
|
[
|
2017-05-22 06:07:12 -04:00
|
|
|
{ type: 'text', name: 'token', placeholder: 'Room token', required: true },
|
2015-01-15 17:35:32 -05:00
|
|
|
{ type: 'text', name: 'room', placeholder: 'Room name or ID' },
|
2015-04-26 14:56:13 -04:00
|
|
|
{ type: 'checkbox', name: 'notify' },
|
2017-02-22 12:46:57 -05:00
|
|
|
{ type: 'select', name: 'color', choices: %w(yellow red green purple gray random) },
|
2015-01-23 19:29:02 -05:00
|
|
|
{ type: 'text', name: 'api_version',
|
|
|
|
placeholder: 'Leave blank for default (v2)' },
|
2014-11-09 00:04:31 -05:00
|
|
|
{ type: 'text', name: 'server',
|
2015-12-07 07:23:23 -05:00
|
|
|
placeholder: 'Leave blank for default. https://hipchat.example.com' },
|
2017-05-03 07:22:03 -04:00
|
|
|
{ type: 'checkbox', name: 'notify_only_broken_pipelines' }
|
2013-05-23 14:10:32 -04:00
|
|
|
]
|
|
|
|
end
|
|
|
|
|
2016-12-27 07:44:24 -05:00
|
|
|
def self.supported_events
|
2018-04-03 07:00:33 -04:00
|
|
|
%w(push issue confidential_issue merge_request note confidential_note tag_push pipeline)
|
2015-02-28 11:33:18 -05:00
|
|
|
end
|
|
|
|
|
2015-02-20 08:49:26 -05:00
|
|
|
def execute(data)
|
2015-02-28 11:33:18 -05:00
|
|
|
return unless supported_events.include?(data[:object_kind])
|
2017-11-14 04:02:39 -05:00
|
|
|
|
2015-03-30 18:54:30 -04:00
|
|
|
message = create_message(data)
|
|
|
|
return unless message.present?
|
2017-11-14 04:02:39 -05:00
|
|
|
|
2017-08-10 12:39:26 -04:00
|
|
|
gate[room].send('GitLab', message, message_options(data)) # rubocop:disable GitlabSecurity/PublicSend
|
2013-05-23 14:10:32 -04:00
|
|
|
end
|
|
|
|
|
2015-08-12 03:40:54 -04:00
|
|
|
def test(data)
|
|
|
|
begin
|
|
|
|
result = execute(data)
|
|
|
|
rescue StandardError => error
|
|
|
|
return { success: false, result: error }
|
|
|
|
end
|
|
|
|
|
|
|
|
{ success: true, result: result }
|
|
|
|
end
|
|
|
|
|
2013-05-23 14:10:32 -04:00
|
|
|
private
|
|
|
|
|
|
|
|
def gate
|
2015-05-27 00:51:31 -04:00
|
|
|
options = { api_version: api_version.present? ? api_version : 'v2' }
|
2014-12-29 15:20:22 -05:00
|
|
|
options[:server_url] = server unless server.blank?
|
2014-11-09 00:12:14 -05:00
|
|
|
@gate ||= HipChat::Client.new(token, options)
|
2013-05-23 14:10:32 -04:00
|
|
|
end
|
|
|
|
|
2016-07-26 05:55:00 -04:00
|
|
|
def message_options(data = nil)
|
2016-12-05 09:40:53 -05:00
|
|
|
{ notify: notify.present? && Gitlab::Utils.to_boolean(notify), color: message_color(data) }
|
2015-04-26 14:56:13 -04:00
|
|
|
end
|
|
|
|
|
2015-03-05 04:54:28 -05:00
|
|
|
def create_message(data)
|
|
|
|
object_kind = data[:object_kind]
|
|
|
|
|
2015-10-03 01:56:37 -04:00
|
|
|
case object_kind
|
|
|
|
when "push", "tag_push"
|
|
|
|
create_push_message(data)
|
|
|
|
when "issue"
|
2017-08-24 12:43:47 -04:00
|
|
|
create_issue_message(data) unless update?(data)
|
2015-10-03 01:56:37 -04:00
|
|
|
when "merge_request"
|
2017-08-24 12:43:47 -04:00
|
|
|
create_merge_request_message(data) unless update?(data)
|
2015-10-03 01:56:37 -04:00
|
|
|
when "note"
|
|
|
|
create_note_message(data)
|
2017-03-17 19:06:11 -04:00
|
|
|
when "pipeline"
|
|
|
|
create_pipeline_message(data) if should_pipeline_be_notified?(data)
|
2015-10-03 01:56:37 -04:00
|
|
|
end
|
2015-03-05 04:54:28 -05:00
|
|
|
end
|
|
|
|
|
2016-10-04 07:38:31 -04:00
|
|
|
def render_line(text)
|
2016-10-04 11:23:16 -04:00
|
|
|
markdown(text.lines.first.chomp, pipeline: :single_line) if text
|
2016-10-04 07:38:31 -04:00
|
|
|
end
|
|
|
|
|
2015-03-05 04:54:28 -05:00
|
|
|
def create_push_message(push)
|
2015-03-10 06:51:36 -04:00
|
|
|
ref_type = Gitlab::Git.tag_ref?(push[:ref]) ? 'tag' : 'branch'
|
|
|
|
ref = Gitlab::Git.ref_name(push[:ref])
|
2015-03-06 11:31:49 -05:00
|
|
|
|
2013-05-23 14:10:32 -04:00
|
|
|
before = push[:before]
|
|
|
|
after = push[:after]
|
|
|
|
|
2018-08-10 02:45:01 -04:00
|
|
|
message = []
|
2013-05-23 14:10:32 -04:00
|
|
|
message << "#{push[:user_name]} "
|
2018-01-11 11:34:01 -05:00
|
|
|
|
2015-03-10 06:51:36 -04:00
|
|
|
if Gitlab::Git.blank_ref?(before)
|
2015-03-06 11:31:49 -05:00
|
|
|
message << "pushed new #{ref_type} <a href=\""\
|
2016-01-12 04:32:25 -05:00
|
|
|
"#{project_url}/commits/#{CGI.escape(ref)}\">#{ref}</a>"\
|
2015-03-06 11:31:49 -05:00
|
|
|
" to #{project_link}\n"
|
2015-03-10 06:51:36 -04:00
|
|
|
elsif Gitlab::Git.blank_ref?(after)
|
2015-03-06 11:31:49 -05:00
|
|
|
message << "removed #{ref_type} <b>#{ref}</b> from <a href=\"#{project.web_url}\">#{project_name}</a> \n"
|
2013-05-23 14:10:32 -04:00
|
|
|
else
|
2015-03-06 11:31:49 -05:00
|
|
|
message << "pushed to #{ref_type} <a href=\""\
|
2016-01-12 04:32:25 -05:00
|
|
|
"#{project.web_url}/commits/#{CGI.escape(ref)}\">#{ref}</a> "
|
2018-03-05 09:15:26 -05:00
|
|
|
message << "of <a href=\"#{project.web_url}\">#{project.full_name.gsub!(/\s/, '')}</a> "
|
2013-05-23 14:10:32 -04:00
|
|
|
message << "(<a href=\"#{project.web_url}/compare/#{before}...#{after}\">Compare changes</a>)"
|
2014-07-29 04:04:19 -04:00
|
|
|
|
|
|
|
push[:commits].take(MAX_COMMITS).each do |commit|
|
2016-10-04 07:38:31 -04:00
|
|
|
message << "<br /> - #{render_line(commit[:message])} (<a href=\"#{commit[:url]}\">#{commit[:id][0..5]}</a>)"
|
2014-07-29 04:04:19 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
if push[:commits].count > MAX_COMMITS
|
|
|
|
message << "<br />... #{push[:commits].count - MAX_COMMITS} more commits"
|
2013-05-23 14:10:32 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-08-10 02:45:01 -04:00
|
|
|
message.join
|
2013-05-23 14:10:32 -04:00
|
|
|
end
|
2015-03-05 04:54:28 -05:00
|
|
|
|
2016-10-04 11:25:45 -04:00
|
|
|
def markdown(text, options = {})
|
2016-10-12 02:51:02 -04:00
|
|
|
return "" unless text
|
2016-10-04 11:25:45 -04:00
|
|
|
|
2016-10-12 02:51:02 -04:00
|
|
|
context = {
|
|
|
|
project: project,
|
|
|
|
pipeline: :email
|
|
|
|
}
|
2016-10-04 11:25:45 -04:00
|
|
|
|
2016-10-12 02:51:02 -04:00
|
|
|
Banzai.render(text, context)
|
2016-10-04 11:23:16 -04:00
|
|
|
|
2016-10-12 02:51:02 -04:00
|
|
|
context.merge!(options)
|
|
|
|
|
2018-09-19 15:58:43 -04:00
|
|
|
html = Banzai.render_and_post_process(text, context)
|
2016-10-19 15:51:15 -04:00
|
|
|
sanitized_html = sanitize(html, tags: HIPCHAT_ALLOWED_TAGS, attributes: %w[href title alt])
|
2016-10-12 02:51:02 -04:00
|
|
|
|
2016-10-19 15:51:15 -04:00
|
|
|
sanitized_html.truncate(200, separator: ' ', omission: '...')
|
2016-10-04 11:23:16 -04:00
|
|
|
end
|
|
|
|
|
2015-03-05 04:54:28 -05:00
|
|
|
def create_issue_message(data)
|
2015-03-06 11:31:49 -05:00
|
|
|
user_name = data[:user][:name]
|
2015-03-05 04:54:28 -05:00
|
|
|
|
|
|
|
obj_attr = data[:object_attributes]
|
|
|
|
obj_attr = HashWithIndifferentAccess.new(obj_attr)
|
2016-10-04 07:38:31 -04:00
|
|
|
title = render_line(obj_attr[:title])
|
2015-03-05 04:54:28 -05:00
|
|
|
state = obj_attr[:state]
|
|
|
|
issue_iid = obj_attr[:iid]
|
|
|
|
issue_url = obj_attr[:url]
|
|
|
|
description = obj_attr[:description]
|
|
|
|
|
2015-03-06 11:31:49 -05:00
|
|
|
issue_link = "<a href=\"#{issue_url}\">issue ##{issue_iid}</a>"
|
2015-03-05 04:54:28 -05:00
|
|
|
|
2018-08-10 02:45:01 -04:00
|
|
|
message = ["#{user_name} #{state} #{issue_link} in #{project_link}: <b>#{title}</b>"]
|
2016-10-19 15:51:15 -04:00
|
|
|
message << "<pre>#{markdown(description)}</pre>"
|
2015-03-05 04:54:28 -05:00
|
|
|
|
2018-08-10 02:45:01 -04:00
|
|
|
message.join
|
2015-03-05 04:54:28 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def create_merge_request_message(data)
|
2015-03-06 11:31:49 -05:00
|
|
|
user_name = data[:user][:name]
|
2015-03-05 04:54:28 -05:00
|
|
|
|
|
|
|
obj_attr = data[:object_attributes]
|
|
|
|
obj_attr = HashWithIndifferentAccess.new(obj_attr)
|
|
|
|
merge_request_id = obj_attr[:iid]
|
|
|
|
state = obj_attr[:state]
|
|
|
|
description = obj_attr[:description]
|
2016-10-04 07:38:31 -04:00
|
|
|
title = render_line(obj_attr[:title])
|
2015-03-05 04:54:28 -05:00
|
|
|
|
|
|
|
merge_request_url = "#{project_url}/merge_requests/#{merge_request_id}"
|
2016-04-19 02:32:02 -04:00
|
|
|
merge_request_link = "<a href=\"#{merge_request_url}\">merge request !#{merge_request_id}</a>"
|
2018-08-10 02:45:01 -04:00
|
|
|
message = ["#{user_name} #{state} #{merge_request_link} in " \
|
|
|
|
"#{project_link}: <b>#{title}</b>"]
|
2015-03-05 04:54:28 -05:00
|
|
|
|
2016-10-19 15:51:15 -04:00
|
|
|
message << "<pre>#{markdown(description)}</pre>"
|
2018-08-10 02:45:01 -04:00
|
|
|
message.join
|
2015-03-05 13:38:23 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def format_title(title)
|
2016-10-04 07:38:31 -04:00
|
|
|
"<b>#{render_line(title)}</b>"
|
2015-03-05 13:38:23 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def create_note_message(data)
|
|
|
|
data = HashWithIndifferentAccess.new(data)
|
2015-03-06 11:31:49 -05:00
|
|
|
user_name = data[:user][:name]
|
2015-03-05 13:38:23 -05:00
|
|
|
|
|
|
|
obj_attr = HashWithIndifferentAccess.new(data[:object_attributes])
|
|
|
|
note = obj_attr[:note]
|
|
|
|
note_url = obj_attr[:url]
|
|
|
|
noteable_type = obj_attr[:noteable_type]
|
2016-10-04 11:23:16 -04:00
|
|
|
commit_id = nil
|
2015-03-05 13:38:23 -05:00
|
|
|
|
|
|
|
case noteable_type
|
|
|
|
when "Commit"
|
|
|
|
commit_attr = HashWithIndifferentAccess.new(data[:commit])
|
2016-10-04 11:23:16 -04:00
|
|
|
commit_id = commit_attr[:id]
|
|
|
|
subject_desc = commit_id
|
2015-03-05 13:38:23 -05:00
|
|
|
subject_desc = Commit.truncate_sha(subject_desc)
|
|
|
|
subject_type = "commit"
|
|
|
|
title = format_title(commit_attr[:message])
|
|
|
|
when "Issue"
|
|
|
|
subj_attr = HashWithIndifferentAccess.new(data[:issue])
|
|
|
|
subject_id = subj_attr[:iid]
|
|
|
|
subject_desc = "##{subject_id}"
|
|
|
|
subject_type = "issue"
|
|
|
|
title = format_title(subj_attr[:title])
|
|
|
|
when "MergeRequest"
|
|
|
|
subj_attr = HashWithIndifferentAccess.new(data[:merge_request])
|
|
|
|
subject_id = subj_attr[:iid]
|
2016-04-19 02:32:02 -04:00
|
|
|
subject_desc = "!#{subject_id}"
|
2015-03-05 13:38:23 -05:00
|
|
|
subject_type = "merge request"
|
|
|
|
title = format_title(subj_attr[:title])
|
|
|
|
when "Snippet"
|
|
|
|
subj_attr = HashWithIndifferentAccess.new(data[:snippet])
|
|
|
|
subject_id = subj_attr[:id]
|
|
|
|
subject_desc = "##{subject_id}"
|
|
|
|
subject_type = "snippet"
|
|
|
|
title = format_title(subj_attr[:title])
|
|
|
|
end
|
|
|
|
|
|
|
|
subject_html = "<a href=\"#{note_url}\">#{subject_type} #{subject_desc}</a>"
|
2018-08-10 02:45:01 -04:00
|
|
|
message = ["#{user_name} commented on #{subject_html} in #{project_link}: "]
|
2015-03-05 13:38:23 -05:00
|
|
|
message << title
|
|
|
|
|
2016-10-19 15:51:15 -04:00
|
|
|
message << "<pre>#{markdown(note, ref: commit_id)}</pre>"
|
2018-08-10 02:45:01 -04:00
|
|
|
message.join
|
2015-03-05 04:54:28 -05:00
|
|
|
end
|
|
|
|
|
2017-03-17 19:06:11 -04:00
|
|
|
def create_pipeline_message(data)
|
|
|
|
pipeline_attributes = data[:object_attributes]
|
|
|
|
pipeline_id = pipeline_attributes[:id]
|
|
|
|
ref_type = pipeline_attributes[:tag] ? 'tag' : 'branch'
|
|
|
|
ref = pipeline_attributes[:ref]
|
|
|
|
user_name = (data[:user] && data[:user][:name]) || 'API'
|
|
|
|
status = pipeline_attributes[:status]
|
|
|
|
duration = pipeline_attributes[:duration]
|
2015-12-07 07:23:23 -05:00
|
|
|
|
2016-01-12 04:32:25 -05:00
|
|
|
branch_link = "<a href=\"#{project_url}/commits/#{CGI.escape(ref)}\">#{ref}</a>"
|
2017-03-17 19:06:11 -04:00
|
|
|
pipeline_url = "<a href=\"#{project_url}/pipelines/#{pipeline_id}\">##{pipeline_id}</a>"
|
2015-12-07 07:23:23 -05:00
|
|
|
|
2017-03-17 19:06:11 -04:00
|
|
|
"#{project_link}: Pipeline #{pipeline_url} of #{branch_link} #{ref_type} by #{user_name} #{humanized_status(status)} in #{duration} second(s)"
|
2015-12-07 07:23:23 -05:00
|
|
|
end
|
|
|
|
|
2016-07-27 05:29:05 -04:00
|
|
|
def message_color(data)
|
2017-03-17 19:06:11 -04:00
|
|
|
pipeline_status_color(data) || color || 'yellow'
|
2016-07-27 05:29:05 -04:00
|
|
|
end
|
|
|
|
|
2017-03-17 19:06:11 -04:00
|
|
|
def pipeline_status_color(data)
|
|
|
|
return unless data && data[:object_kind] == 'pipeline'
|
2016-07-26 05:55:00 -04:00
|
|
|
|
2017-03-17 19:06:11 -04:00
|
|
|
case data[:object_attributes][:status]
|
2016-07-26 05:55:00 -04:00
|
|
|
when 'success'
|
2016-01-06 11:01:48 -05:00
|
|
|
'green'
|
|
|
|
else
|
|
|
|
'red'
|
2016-07-26 05:55:00 -04:00
|
|
|
end
|
2016-01-06 11:01:48 -05:00
|
|
|
end
|
|
|
|
|
2015-03-05 04:54:28 -05:00
|
|
|
def project_name
|
2018-03-05 09:15:26 -05:00
|
|
|
project.full_name.gsub(/\s/, '')
|
2015-03-05 04:54:28 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def project_url
|
|
|
|
project.web_url
|
|
|
|
end
|
|
|
|
|
|
|
|
def project_link
|
|
|
|
"<a href=\"#{project_url}\">#{project_name}</a>"
|
|
|
|
end
|
|
|
|
|
2017-08-24 12:43:47 -04:00
|
|
|
def update?(data)
|
2015-03-05 04:54:28 -05:00
|
|
|
data[:object_attributes][:action] == 'update'
|
|
|
|
end
|
2015-12-07 07:23:23 -05:00
|
|
|
|
|
|
|
def humanized_status(status)
|
|
|
|
case status
|
|
|
|
when 'success'
|
|
|
|
'passed'
|
|
|
|
else
|
|
|
|
status
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-03-17 19:06:11 -04:00
|
|
|
def should_pipeline_be_notified?(data)
|
|
|
|
case data[:object_attributes][:status]
|
2015-12-07 07:23:23 -05:00
|
|
|
when 'success'
|
2017-03-17 19:06:11 -04:00
|
|
|
!notify_only_broken_pipelines?
|
2015-12-07 07:23:23 -05:00
|
|
|
when 'failed'
|
|
|
|
true
|
|
|
|
else
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end
|
2013-06-19 08:40:33 -04:00
|
|
|
end
|