Skip to content

Commit

Permalink
Merge pull request #893 from bdewater/hipchat_api_v2
Browse files Browse the repository at this point in the history
Support HipChat v2 API
  • Loading branch information
stevecrozz committed May 27, 2015
2 parents 4f26840 + 9c2d03c commit 05cda19
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 7 deletions.
6 changes: 4 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -130,16 +130,17 @@ GEM
tilt
hashie (2.0.5)
hike (1.2.3)
hipchat (0.12.0)
hipchat (1.5.1)
httparty
mimemagic
hoi (0.0.6)
httparty (> 0.6.0)
json (> 1.4.0)
hoptoad_notifier (2.4.11)
activesupport
builder
htmlentities (4.3.3)
httparty (0.13.3)
httparty (0.13.5)
json (~> 1.8)
multi_xml (>= 0.5.2)
httpauth (0.2.0)
Expand Down Expand Up @@ -168,6 +169,7 @@ GEM
railties
method_source (0.8.2)
mime-types (1.25.1)
mimemagic (0.3.0)
mini_portile (0.6.2)
minitest (5.5.1)
mongoid (4.0.2)
Expand Down
27 changes: 23 additions & 4 deletions app/models/notification_services/hipchat_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,34 @@
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"
:placeholder => "API token",
:label => "API token"
}],
[:room_id, {
:placeholder => "Room name",
:label => "Room name"
}],
]
Mandatory_fields = [:service, :api_token, :room_id]
API_versions = ['v1', 'v2']

def check_params
if Fields.any? { |f, _| self[f].blank? }
errors.add :base, 'You must specify your Hipchat API token and Room ID'
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

Expand All @@ -29,7 +45,10 @@ def create_notification(problem)
&nbsp;&nbsp;Times occurred: #{problem.notices_count}
MSG

client = HipChat::Client.new(api_token)
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
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class AddV1ToHipchatNotificationServices < Mongoid::Migration
def self.up
App.all.each do |app|
ns = app.notification_service
if ns.is_a?(NotificationServices::HipchatService) && ns.service.blank?
app.notification_service.update_attribute(:service, 'v1')
end
end
end

def self.down
end
end
6 changes: 5 additions & 1 deletion spec/fabricators/notification_service_fabricator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
service_url { sequence :word }
end

%w(campfire flowdock hipchat hoiio hubot pushover webhook).each do |t|
Fabricator :hipchat_notification_service, :from => :notification_service, :class_name => "NotificationService::HipchatService" do
service { 'v2' }
end

%w(campfire flowdock hoiio hubot pushover webhook).each do |t|
Fabricator "#{t}_notification_service".to_sym, :from => :notification_service, :class_name => "NotificationService::#{t.camelcase}Service"
end
28 changes: 28 additions & 0 deletions spec/models/notification_service/hipchat_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,34 @@
allow_any_instance_of(HipChat::Client).to receive(:[]).and_return(room)
end

describe '#check_params' do
context 'empty field check' do
%w(service api_token room_id).each do |field|
it "'doesn\'t allow #{field} to be empty'" do
service[field.to_sym] = ''
service.check_params
expect(service.errors).to include(field.to_sym)
end
end
end

context 'API version field check' do
%w(v1 v2).each do |version|
it "allows #{version}" do
service[:service] = version
service.check_params
expect(service.errors).to_not include(:service)
end
end

it 'doesn\t allow an unknown version' do
service[:service] = 'vFOO'
service.check_params
expect(service.errors).to include(:service)
end
end
end

it 'sends message' do
expect(room).to receive(:send)
service.create_notification(problem)
Expand Down

0 comments on commit 05cda19

Please sign in to comment.