hipchat_service.rb 8.9 KB
Newer Older
1 2
# frozen_string_literal: true

R
Ronald van Eede 已提交
3
class HipchatService < Service
D
David Eisner 已提交
4 5
  include ActionView::Helpers::SanitizeHelper

S
Stefan Kanev 已提交
6
  MAX_COMMITS = 3
7 8 9 10
  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
D
Douwe Maan 已提交
11
  ].freeze
S
Stefan Kanev 已提交
12

R
Robert Schilling 已提交
13
  prop_accessor :token, :room, :server, :color, :api_version
K
Kamil Trzciński 已提交
14
  boolean_accessor :notify_only_broken_pipelines, :notify
R
Ronald van Eede 已提交
15 16
  validates :token, presence: true, if: :activated?

K
Kamil Trzcinski 已提交
17 18 19
  def initialize_properties
    if properties.nil?
      self.properties = {}
K
Kamil Trzciński 已提交
20
      self.notify_only_broken_pipelines = true
K
Kamil Trzcinski 已提交
21 22
    end
  end
23

R
Ronald van Eede 已提交
24
  def title
25
    'HipChat'
R
Ronald van Eede 已提交
26 27 28
  end

  def description
29
    'Private group chat and IM'
R
Ronald van Eede 已提交
30 31
  end

32
  def self.to_param
R
Ronald van Eede 已提交
33 34 35 36 37
    'hipchat'
  end

  def fields
    [
38
      { type: 'text', name: 'token',     placeholder: 'Room token', required: true },
39
      { type: 'text', name: 'room',      placeholder: 'Room name or ID' },
40
      { type: 'checkbox', name: 'notify' },
D
Douwe Maan 已提交
41
      { type: 'select', name: 'color', choices: %w(yellow red green purple gray random) },
42 43
      { type: 'text', name: 'api_version',
        placeholder: 'Leave blank for default (v2)' },
44
      { type: 'text', name: 'server',
45
        placeholder: 'Leave blank for default. https://hipchat.example.com' },
46
      { type: 'checkbox', name: 'notify_only_broken_pipelines' }
R
Ronald van Eede 已提交
47 48 49
    ]
  end

50
  def self.supported_events
51
    %w(push issue confidential_issue merge_request note confidential_note tag_push pipeline)
52 53
  end

D
Douwe Maan 已提交
54
  def execute(data)
55
    return unless supported_events.include?(data[:object_kind])
56

57 58
    message = create_message(data)
    return unless message.present?
59

60
    gate[room].send('GitLab', message, message_options(data)) # rubocop:disable GitlabSecurity/PublicSend
R
Ronald van Eede 已提交
61 62
  end

63 64 65 66 67 68 69 70 71 72
  def test(data)
    begin
      result = execute(data)
    rescue StandardError => error
      return { success: false, result: error }
    end

    { success: true, result: result }
  end

R
Ronald van Eede 已提交
73 74 75
  private

  def gate
76
    options = { api_version: api_version.present? ? api_version : 'v2' }
D
Drew Blessing 已提交
77
    options[:server_url] = server unless server.blank?
78
    @gate ||= HipChat::Client.new(token, options)
R
Ronald van Eede 已提交
79 80
  end

81
  def message_options(data = nil)
R
Robert Schilling 已提交
82
    { notify: notify.present? && Gitlab::Utils.to_boolean(notify), color: message_color(data) }
83 84
  end

85 86 87
  def create_message(data)
    object_kind = data[:object_kind]

G
Guilherme Garnier 已提交
88 89 90 91
    case object_kind
    when "push", "tag_push"
      create_push_message(data)
    when "issue"
92
      create_issue_message(data) unless update?(data)
G
Guilherme Garnier 已提交
93
    when "merge_request"
94
      create_merge_request_message(data) unless update?(data)
G
Guilherme Garnier 已提交
95 96
    when "note"
      create_note_message(data)
K
Kamil Trzciński 已提交
97 98
    when "pipeline"
      create_pipeline_message(data) if should_pipeline_be_notified?(data)
G
Guilherme Garnier 已提交
99
    end
100 101
  end

102
  def render_line(text)
103
    markdown(text.lines.first.chomp, pipeline: :single_line) if text
104 105
  end

106
  def create_push_message(push)
107 108
    ref_type = Gitlab::Git.tag_ref?(push[:ref]) ? 'tag' : 'branch'
    ref = Gitlab::Git.ref_name(push[:ref])
109

R
Ronald van Eede 已提交
110 111 112
    before = push[:before]
    after = push[:after]

113
    message = []
R
Ronald van Eede 已提交
114
    message << "#{push[:user_name]} "
115

116
    if Gitlab::Git.blank_ref?(before)
117
      message << "pushed new #{ref_type} <a href=\""\
118
                 "#{project_url}/commits/#{CGI.escape(ref)}\">#{ref}</a>"\
119
                 " to #{project_link}\n"
120
    elsif Gitlab::Git.blank_ref?(after)
121
      message << "removed #{ref_type} <b>#{ref}</b> from <a href=\"#{project.web_url}\">#{project_name}</a> \n"
R
Ronald van Eede 已提交
122
    else
123
      message << "pushed to #{ref_type} <a href=\""\
124
                  "#{project.web_url}/commits/#{CGI.escape(ref)}\">#{ref}</a> "
125
      message << "of <a href=\"#{project.web_url}\">#{project.full_name.gsub!(/\s/, '')}</a> "
R
Ronald van Eede 已提交
126
      message << "(<a href=\"#{project.web_url}/compare/#{before}...#{after}\">Compare changes</a>)"
S
Stefan Kanev 已提交
127 128

      push[:commits].take(MAX_COMMITS).each do |commit|
129
        message << "<br /> - #{render_line(commit[:message])} (<a href=\"#{commit[:url]}\">#{commit[:id][0..5]}</a>)"
S
Stefan Kanev 已提交
130 131 132 133
      end

      if push[:commits].count > MAX_COMMITS
        message << "<br />... #{push[:commits].count - MAX_COMMITS} more commits"
R
Ronald van Eede 已提交
134 135 136
      end
    end

137
    message.join
R
Ronald van Eede 已提交
138
  end
139

140
  def markdown(text, options = {})
141
    return "" unless text
142

143 144 145 146
    context = {
      project: project,
      pipeline: :email
    }
147

148
    Banzai.render(text, context)
149

150 151
    context.merge!(options)

152 153
    html = Banzai.post_process(Banzai.render(text, context), context)
    sanitized_html = sanitize(html, tags: HIPCHAT_ALLOWED_TAGS, attributes: %w[href title alt])
154

155
    sanitized_html.truncate(200, separator: ' ', omission: '...')
156 157
  end

158
  def create_issue_message(data)
159
    user_name = data[:user][:name]
160 161 162

    obj_attr = data[:object_attributes]
    obj_attr = HashWithIndifferentAccess.new(obj_attr)
163
    title = render_line(obj_attr[:title])
164 165 166 167 168
    state = obj_attr[:state]
    issue_iid = obj_attr[:iid]
    issue_url = obj_attr[:url]
    description = obj_attr[:description]

169
    issue_link = "<a href=\"#{issue_url}\">issue ##{issue_iid}</a>"
170

171
    message = ["#{user_name} #{state} #{issue_link} in #{project_link}: <b>#{title}</b>"]
172
    message << "<pre>#{markdown(description)}</pre>"
173

174
    message.join
175 176 177
  end

  def create_merge_request_message(data)
178
    user_name = data[:user][:name]
179 180 181 182 183 184

    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]
185
    title = render_line(obj_attr[:title])
186 187

    merge_request_url = "#{project_url}/merge_requests/#{merge_request_id}"
188
    merge_request_link = "<a href=\"#{merge_request_url}\">merge request !#{merge_request_id}</a>"
189 190
    message = ["#{user_name} #{state} #{merge_request_link} in " \
      "#{project_link}: <b>#{title}</b>"]
191

192
    message << "<pre>#{markdown(description)}</pre>"
193
    message.join
194 195 196
  end

  def format_title(title)
197
    "<b>#{render_line(title)}</b>"
198 199 200 201
  end

  def create_note_message(data)
    data = HashWithIndifferentAccess.new(data)
202
    user_name = data[:user][:name]
203 204 205 206 207

    obj_attr = HashWithIndifferentAccess.new(data[:object_attributes])
    note = obj_attr[:note]
    note_url = obj_attr[:url]
    noteable_type = obj_attr[:noteable_type]
208
    commit_id = nil
209 210 211 212

    case noteable_type
    when "Commit"
      commit_attr = HashWithIndifferentAccess.new(data[:commit])
213 214
      commit_id = commit_attr[:id]
      subject_desc = commit_id
215 216 217 218 219 220 221 222 223 224 225 226
      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]
227
      subject_desc = "!#{subject_id}"
228 229 230 231 232 233 234 235 236 237 238
      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>"
239
    message = ["#{user_name} commented on #{subject_html} in #{project_link}: "]
240 241
    message << title

242
    message << "<pre>#{markdown(note, ref: commit_id)}</pre>"
243
    message.join
244 245
  end

K
Kamil Trzciński 已提交
246 247 248 249 250 251 252 253
  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]
254

255
    branch_link = "<a href=\"#{project_url}/commits/#{CGI.escape(ref)}\">#{ref}</a>"
K
Kamil Trzciński 已提交
256
    pipeline_url = "<a href=\"#{project_url}/pipelines/#{pipeline_id}\">##{pipeline_id}</a>"
257

K
Kamil Trzciński 已提交
258
    "#{project_link}: Pipeline #{pipeline_url} of #{branch_link} #{ref_type} by #{user_name} #{humanized_status(status)} in #{duration} second(s)"
259 260
  end

261
  def message_color(data)
K
Kamil Trzciński 已提交
262
    pipeline_status_color(data) || color || 'yellow'
263 264
  end

K
Kamil Trzciński 已提交
265 266
  def pipeline_status_color(data)
    return unless data && data[:object_kind] == 'pipeline'
267

K
Kamil Trzciński 已提交
268
    case data[:object_attributes][:status]
269
    when 'success'
270 271 272
      'green'
    else
      'red'
273
    end
274 275
  end

276
  def project_name
277
    project.full_name.gsub(/\s/, '')
278 279 280 281 282 283 284 285 286 287
  end

  def project_url
    project.web_url
  end

  def project_link
    "<a href=\"#{project_url}\">#{project_name}</a>"
  end

288
  def update?(data)
289 290
    data[:object_attributes][:action] == 'update'
  end
291 292 293 294 295 296 297 298 299 300

  def humanized_status(status)
    case status
    when 'success'
      'passed'
    else
      status
    end
  end

K
Kamil Trzciński 已提交
301 302
  def should_pipeline_be_notified?(data)
    case data[:object_attributes][:status]
303
    when 'success'
K
Kamil Trzciński 已提交
304
      !notify_only_broken_pipelines?
305 306 307 308 309 310
    when 'failed'
      true
    else
      false
    end
  end
D
Dmitriy Zaporozhets 已提交
311
end