Commit 9c2d03caa7c9aaf856aef04e6d1813fea3a019aa
1 parent
4f268405
Exists in
master
and in
1 other branch
Allow usage of the HipChat v2 API
Showing
5 changed files
with
73 additions
and
7 deletions
Show diff stats
Gemfile.lock
@@ -130,8 +130,9 @@ GEM | @@ -130,8 +130,9 @@ GEM | ||
130 | tilt | 130 | tilt |
131 | hashie (2.0.5) | 131 | hashie (2.0.5) |
132 | hike (1.2.3) | 132 | hike (1.2.3) |
133 | - hipchat (0.12.0) | 133 | + hipchat (1.5.1) |
134 | httparty | 134 | httparty |
135 | + mimemagic | ||
135 | hoi (0.0.6) | 136 | hoi (0.0.6) |
136 | httparty (> 0.6.0) | 137 | httparty (> 0.6.0) |
137 | json (> 1.4.0) | 138 | json (> 1.4.0) |
@@ -139,7 +140,7 @@ GEM | @@ -139,7 +140,7 @@ GEM | ||
139 | activesupport | 140 | activesupport |
140 | builder | 141 | builder |
141 | htmlentities (4.3.3) | 142 | htmlentities (4.3.3) |
142 | - httparty (0.13.3) | 143 | + httparty (0.13.5) |
143 | json (~> 1.8) | 144 | json (~> 1.8) |
144 | multi_xml (>= 0.5.2) | 145 | multi_xml (>= 0.5.2) |
145 | httpauth (0.2.0) | 146 | httpauth (0.2.0) |
@@ -168,6 +169,7 @@ GEM | @@ -168,6 +169,7 @@ GEM | ||
168 | railties | 169 | railties |
169 | method_source (0.8.2) | 170 | method_source (0.8.2) |
170 | mime-types (1.25.1) | 171 | mime-types (1.25.1) |
172 | + mimemagic (0.3.0) | ||
171 | mini_portile (0.6.2) | 173 | mini_portile (0.6.2) |
172 | minitest (5.5.1) | 174 | minitest (5.5.1) |
173 | mongoid (4.0.2) | 175 | mongoid (4.0.2) |
app/models/notification_services/hipchat_service.rb
@@ -2,18 +2,34 @@ if defined? HipChat | @@ -2,18 +2,34 @@ if defined? HipChat | ||
2 | class NotificationServices::HipchatService < NotificationService | 2 | class NotificationServices::HipchatService < NotificationService |
3 | Label = 'hipchat' | 3 | Label = 'hipchat' |
4 | Fields += [ | 4 | Fields += [ |
5 | + [:service, { | ||
6 | + :placeholder => "'v1' (admin API token) or 'v2' (account API token)", | ||
7 | + :label => "HipChat API version" | ||
8 | + }], | ||
9 | + [:service_url, { | ||
10 | + :placeholder => "Optional, leave empty for HipChat.com", | ||
11 | + :label => "Custom HipChat Server URL" | ||
12 | + }], | ||
5 | [:api_token, { | 13 | [:api_token, { |
6 | - :placeholder => "API Token" | 14 | + :placeholder => "API token", |
15 | + :label => "API token" | ||
7 | }], | 16 | }], |
8 | [:room_id, { | 17 | [:room_id, { |
9 | :placeholder => "Room name", | 18 | :placeholder => "Room name", |
10 | :label => "Room name" | 19 | :label => "Room name" |
11 | }], | 20 | }], |
12 | ] | 21 | ] |
22 | + Mandatory_fields = [:service, :api_token, :room_id] | ||
23 | + API_versions = ['v1', 'v2'] | ||
13 | 24 | ||
14 | def check_params | 25 | def check_params |
15 | - if Fields.any? { |f, _| self[f].blank? } | ||
16 | - errors.add :base, 'You must specify your Hipchat API token and Room ID' | 26 | + Fields.each do |field, hash| |
27 | + if Mandatory_fields.include?(field) && self[field].blank? | ||
28 | + errors.add field, "You must specify #{hash[:label]}" | ||
29 | + end | ||
30 | + end | ||
31 | + unless API_versions.include?(self[:service]) | ||
32 | + errors.add :service, "API version must be #{API_versions.join(' or ')}" | ||
17 | end | 33 | end |
18 | end | 34 | end |
19 | 35 | ||
@@ -29,7 +45,10 @@ if defined? HipChat | @@ -29,7 +45,10 @@ if defined? HipChat | ||
29 | Times occurred: #{problem.notices_count} | 45 | Times occurred: #{problem.notices_count} |
30 | MSG | 46 | MSG |
31 | 47 | ||
32 | - client = HipChat::Client.new(api_token) | 48 | + options = { :api_version => self[:service] } |
49 | + options[:server_url] = self[:service_url] if service_url.present? | ||
50 | + | ||
51 | + client = HipChat::Client.new(api_token, options) | ||
33 | client[room_id].send('Errbit', message, :color => 'red', :notify => true) | 52 | client[room_id].send('Errbit', message, :color => 'red', :notify => true) |
34 | end | 53 | end |
35 | end | 54 | end |
db/migrate/20150527202629_add_v1_to_hipchat_notification_services.rb
0 → 100644
@@ -0,0 +1,13 @@ | @@ -0,0 +1,13 @@ | ||
1 | +class AddV1ToHipchatNotificationServices < Mongoid::Migration | ||
2 | + def self.up | ||
3 | + App.all.each do |app| | ||
4 | + ns = app.notification_service | ||
5 | + if ns.is_a?(NotificationServices::HipchatService) && ns.service.blank? | ||
6 | + app.notification_service.update_attribute(:service, 'v1') | ||
7 | + end | ||
8 | + end | ||
9 | + end | ||
10 | + | ||
11 | + def self.down | ||
12 | + end | ||
13 | +end |
spec/fabricators/notification_service_fabricator.rb
@@ -16,6 +16,10 @@ Fabricator :slack_notification_service, :from => :notification_service, :class_n | @@ -16,6 +16,10 @@ Fabricator :slack_notification_service, :from => :notification_service, :class_n | ||
16 | service_url { sequence :word } | 16 | service_url { sequence :word } |
17 | end | 17 | end |
18 | 18 | ||
19 | -%w(campfire flowdock hipchat hoiio hubot pushover webhook).each do |t| | 19 | +Fabricator :hipchat_notification_service, :from => :notification_service, :class_name => "NotificationService::HipchatService" do |
20 | + service { 'v2' } | ||
21 | +end | ||
22 | + | ||
23 | +%w(campfire flowdock hoiio hubot pushover webhook).each do |t| | ||
20 | Fabricator "#{t}_notification_service".to_sym, :from => :notification_service, :class_name => "NotificationService::#{t.camelcase}Service" | 24 | Fabricator "#{t}_notification_service".to_sym, :from => :notification_service, :class_name => "NotificationService::#{t.camelcase}Service" |
21 | end | 25 | end |
spec/models/notification_service/hipchat_service_spec.rb
@@ -7,6 +7,34 @@ describe NotificationServices::HipchatService, type: 'model' do | @@ -7,6 +7,34 @@ describe NotificationServices::HipchatService, type: 'model' do | ||
7 | allow_any_instance_of(HipChat::Client).to receive(:[]).and_return(room) | 7 | allow_any_instance_of(HipChat::Client).to receive(:[]).and_return(room) |
8 | end | 8 | end |
9 | 9 | ||
10 | + describe '#check_params' do | ||
11 | + context 'empty field check' do | ||
12 | + %w(service api_token room_id).each do |field| | ||
13 | + it "'doesn\'t allow #{field} to be empty'" do | ||
14 | + service[field.to_sym] = '' | ||
15 | + service.check_params | ||
16 | + expect(service.errors).to include(field.to_sym) | ||
17 | + end | ||
18 | + end | ||
19 | + end | ||
20 | + | ||
21 | + context 'API version field check' do | ||
22 | + %w(v1 v2).each do |version| | ||
23 | + it "allows #{version}" do | ||
24 | + service[:service] = version | ||
25 | + service.check_params | ||
26 | + expect(service.errors).to_not include(:service) | ||
27 | + end | ||
28 | + end | ||
29 | + | ||
30 | + it 'doesn\t allow an unknown version' do | ||
31 | + service[:service] = 'vFOO' | ||
32 | + service.check_params | ||
33 | + expect(service.errors).to include(:service) | ||
34 | + end | ||
35 | + end | ||
36 | + end | ||
37 | + | ||
10 | it 'sends message' do | 38 | it 'sends message' do |
11 | expect(room).to receive(:send) | 39 | expect(room).to receive(:send) |
12 | service.create_notification(problem) | 40 | service.create_notification(problem) |