hipchat_service.rb 2.67 KB
Newer Older
1
2
3
4
# == Schema Information
#
# Table name: services
#
Valery Sizov's avatar
Valery Sizov committed
5
6
7
#  id         :integer          not null, primary key
#  type       :string(255)
#  title      :string(255)
8
#  project_id :integer
Valery Sizov's avatar
Valery Sizov committed
9
10
11
12
#  created_at :datetime
#  updated_at :datetime
#  active     :boolean          default(FALSE), not null
#  properties :text
13
#  template   :boolean          default(FALSE)
14
15
16
17
#  push_events           :boolean
#  issues_events         :boolean
#  merge_requests_events :boolean
#  tag_push_events       :boolean
18
19
20
#

class HipchatService < Service
21
22
  MAX_COMMITS = 3

23
  prop_accessor :token, :room, :server
24
25
26
  validates :token, presence: true, if: :activated?

  def title
27
    'HipChat'
28
29
30
  end

  def description
31
    'Private group chat and IM'
32
33
34
35
36
37
38
39
  end

  def to_param
    'hipchat'
  end

  def fields
    [
40
41
      { type: 'text', name: 'token',     placeholder: 'Room token' },
      { type: 'text', name: 'room',      placeholder: 'Room name or ID' },
42
      { type: 'text', name: 'server',
Drew Blessing's avatar
Drew Blessing committed
43
        placeholder: 'Leave blank for default. https://hipchat.example.com' }
44
45
46
47
    ]
  end

  def execute(push_data)
48
49
50
    object_kind = push_data.fetch(:object_kind)
    return unless object_kind == "push"

51
    gate[room].send('GitLab', create_message(push_data))
52
53
54
55
56
  end

  private

  def gate
57
    options = { api_version: 'v2' }
Drew Blessing's avatar
Drew Blessing committed
58
    options[:server_url] = server unless server.blank?
59
    @gate ||= HipChat::Client.new(token, options)
60
61
62
63
64
65
66
67
68
  end

  def create_message(push)
    ref = push[:ref].gsub("refs/heads/", "")
    before = push[:before]
    after = push[:after]

    message = ""
    message << "#{push[:user_name]} "
69
    if before.include?('000000')
70
71
72
73
      message << "pushed new branch <a href=\""\
                 "#{project.web_url}/commits/#{URI.escape(ref)}\">#{ref}</a>"\
                 " to <a href=\"#{project.web_url}\">"\
                 "#{project.name_with_namespace.gsub!(/\s/, "")}</a>\n"
74
    elsif after.include?('000000')
75
76
      message << "removed branch #{ref} from <a href=\"#{project.web_url}\">#{project.name_with_namespace.gsub!(/\s/,'')}</a> \n"
    else
77
78
      message << "pushed to branch <a href=\""\
                  "#{project.web_url}/commits/#{URI.escape(ref)}\">#{ref}</a> "
79
80
      message << "of <a href=\"#{project.web_url}\">#{project.name_with_namespace.gsub!(/\s/,'')}</a> "
      message << "(<a href=\"#{project.web_url}/compare/#{before}...#{after}\">Compare changes</a>)"
81
82
83
84
85
86
87

      push[:commits].take(MAX_COMMITS).each do |commit|
        message << "<br /> - #{commit[:message].lines.first} (<a href=\"#{commit[:url]}\">#{commit[:id][0..5]}</a>)"
      end

      if push[:commits].count > MAX_COMMITS
        message << "<br />... #{push[:commits].count - MAX_COMMITS} more commits"
88
89
90
91
92
      end
    end

    message
  end
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
93
end