2018-11-20 20:47:30 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
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) },
|
2021-04-29 21:17:54 +05:30
|
|
|
{ type: 'text', name: 'api_version', title: _('API version'),
|
2015-09-11 14:41:01 +05:30
|
|
|
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
|
2018-04-05 14:03:07 +05:30
|
|
|
%w(push issue confidential_issue merge_request note confidential_note tag_push pipeline)
|
2015-04-26 12:48:37 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def execute(data)
|
2021-04-01 16:36:13 +05:30
|
|
|
# We removed the hipchat gem due to https://gitlab.com/gitlab-org/gitlab/-/issues/325851#note_537143149
|
|
|
|
# HipChat is unusable anyway, so do nothing in this method
|
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
|
|
|
|
|
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
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
def render_line(text)
|
|
|
|
markdown(text.lines.first.chomp, pipeline: :single_line) if text
|
|
|
|
end
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
html = Banzai.render_and_post_process(text, context)
|
2017-08-17 22:00:37 +05:30
|
|
|
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 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
|
|
|
|
|
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
|
2018-03-27 19:54:05 +05:30
|
|
|
project.full_name.gsub(/\s/, '')
|
2015-04-26 12:48:37 +05:30
|
|
|
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
|