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

312 lines
8.8 KiB
Ruby
Raw Normal View History

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