hipchat_service.rb
1.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
if defined? HipChat
class NotificationServices::HipchatService < NotificationService
LABEL = 'hipchat'
FIELDS += [
[:service, {
placeholder: "'v1' (admin API token) or 'v2' (account API token)",
label: "HipChat API version"
}],
[:service_url, {
placeholder: "Optional, leave empty for HipChat.com",
label: "Custom HipChat Server URL"
}],
[:api_token, {
placeholder: "API token",
label: "API token"
}],
[:room_id, {
placeholder: "Room name",
label: "Room name"
}]
]
MANDATORY_FIELDS = [:service, :api_token, :room_id]
API_VERSIONS = %w(v1 v2)
def check_params
FIELDS.each do |field, hash|
if MANDATORY_FIELDS.include?(field) && self[field].blank?
errors.add field, "You must specify #{hash[:label]}"
end
end
unless API_VERSIONS.include?(self[:service])
errors.add :service, "API version must be #{API_VERSIONS.join(' or ')}"
end
end
def url
"https://www.hipchat.com/sign_in"
end
def create_notification(problem)
url = app_problem_url problem.app, problem
message = <<-MSG.strip_heredoc
<strong>#{ERB::Util.html_escape problem.app.name}</strong> error in <strong>#{ERB::Util.html_escape problem.environment}</strong> at <strong>#{ERB::Util.html_escape problem.where}</strong> (<a href="#{url}">details</a>)<br>
#{ERB::Util.html_escape problem.message.to_s.truncate(100)}<br>
Times occurred: #{problem.notices_count}
MSG
options = { api_version: self[:service] }
options[:server_url] = self[:service_url] if service_url.present?
client = HipChat::Client.new(api_token, options)
client[room_id].send('Errbit', message, color: 'red', notify: true)
end
end
end