hipchat 0.14.0 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a076583ac9843df4a79672f0efb27a3e54855dc7
4
- data.tar.gz: 286a673b9d8f4413a4ef5d5d0b5d3dc933f39859
3
+ metadata.gz: bb8eb196374b9fb0af86c10fd90d72cf2f394918
4
+ data.tar.gz: da191a66ed2c5773b72c4be049f466615f141e26
5
5
  SHA512:
6
- metadata.gz: abd43e5029fc567c3b2168c0280e46e71a138f4f8c68aab21cdbb09b33b95fceec7b67263f4a4443685ebd85337f1f21d0a8f7364489829d1c152cebc4d58ddd
7
- data.tar.gz: 8b53a6201400e0e6362f7f63ffc9efacdbd082a4e8e39390e0a6f39ce83c5513c7e4967be22e2065f91dad15c42a8a469a18b3f87dfa0f4a66849e9df73dceb4
6
+ metadata.gz: ea2874650ffa2ebde7d816fe43fe83cecc1c8c91cef18046009aefc300eac473e375fbef4c888d085a1d9d0bc7f61d88e176db50ed711453efd4b7923756ce4f
7
+ data.tar.gz: ca023bf579d6c705e25d429672fc27230ac6c3c23c2a4e197bd649c5a81951e0835b34a6801bd5b25e06839a82aba3ad344dcd4c4e21bffb1578152daf783717
@@ -2,22 +2,25 @@ h1. HipChat Wrapper
2
2
 
3
3
  A very basic wrapper for the HipChat HTTP API.
4
4
 
5
- h2. Build Status
5
+ h2. CI Status
6
6
 
7
7
  "!https://travis-ci.org/hipchat/hipchat-rb.png!":https://travis-ci.org/hipchat/hipchat-rb
8
+ !https://coveralls.io/repos/hipchat/hipchat-rb/badge.png(Coverage Status)!:https://coveralls.io/r/hipchat/hipchat-rb
8
9
 
9
10
  h2. Requirements
10
11
  * Ruby 1.9.3 or higher
11
- * HipChat Account, signed up "here!":https://hipchat.com/
12
+ * HipChat Account, sign up "here!":https://hipchat.com/
12
13
 
13
- h2. Usage
14
+ h1. Usage
15
+
16
+ h2. "API v1":https://www.hipchat.com/docs/api
14
17
 
15
18
  bc.. client = HipChat::Client.new(api_token)
16
19
  # 'username' is the name for which the message will be presented as from
17
20
  client['my room'].send('username', 'I talk')
18
21
 
19
22
  # Send notifications to users (default false)
20
- client['my room'].send('username', 'I quit!', :notify => true)
23
+ client['my room'].send('username', 'I quit!', :notify => 1)
21
24
 
22
25
  # Color it red. or "yellow", "green", "purple", "random" (default "yellow")
23
26
  client['my room'].send('username', 'Build failed!', :color => 'red')
@@ -37,18 +40,52 @@ client['my room'].history()
37
40
  # Get history for a date in time with a particular timezone (default is latest 75 messages, timezone default is 'UTC')
38
41
  client['my room'].history(:date => '2010-11-19', :timezone => 'PST')
39
42
 
43
+ h2. "API v2":https://www.hipchat.com/docs/apiv2
44
+
45
+ bc.. client = HipChat::Client.new(api_token, :api_version => 'v2')
46
+ # 'username' is the name for which the message will be presented as from
47
+ client['my room'].send('username', 'I talk')
48
+
49
+ # Send notifications to users (default false)
50
+ client['my room'].send('username', 'I quit!', :notify => true)
51
+
52
+ # Color it red. or "yellow", "green", "purple", "random" (default "yellow")
53
+ client['my room'].send('username', 'Build failed!', :color => 'red')
54
+
55
+ # Have your message rendered as text in HipChat (see https://www.hipchat.com/docs/apiv2/method/send_room_notification)
56
+ client['my room'].send('username', '@coworker Build faild!', :message_format => 'text')
57
+
58
+ # Update the topic of a room in HipChat (see https://www.hipchat.com/docs/apiv2/method/set_topic)
59
+ client['my room'].topic('Free Ice Cream in the kitchen')
60
+
61
+ # Change the from field for a topic update (default "API")
62
+ client['my room'].topic('Weekely sales: $10,000', :from => 'Sales Team')
63
+
64
+ # Get history from a room
65
+ client['my room'].history()
66
+
67
+ # Get history for a date in time with a particular timezone (default is latest 75 messages, timezone default is 'UTC')
68
+ client['my room'].history(:date => '2010-11-19', :timezone => 'PST')
40
69
 
41
70
  h2. Capistrano
42
71
 
43
- bc.. require 'hipchat/capistrano'
72
+ *APIv1 ONLY, use APIv1 Key*
73
+
74
+ Capfile
75
+
76
+ bc. require 'hipchat/capistrano'
44
77
 
78
+ deploy.rb
79
+
80
+ bc.. # Required
45
81
  set :hipchat_token, "<your token>"
46
82
  set :hipchat_room_name, "Your room" # If you pass an array such as ["room_a", "room_b"] you can send announcements to multiple rooms.
83
+ # Optional
47
84
  set :hipchat_announce, false # notify users
48
85
  set :hipchat_color, 'yellow' #normal message color
49
86
  set :hipchat_success_color, 'green' #finished deployment message color
50
87
  set :hipchat_failed_color, 'red' #cancelled deployment message color
51
- set :hipchat_message_format, 'text' # Sets the deployment message format, see https://www.hipchat.com/docs/api/method/rooms/message
88
+ set :hipchat_message_format, 'html' # Sets the deployment message format, see https://www.hipchat.com/docs/api/method/rooms/message
52
89
 
53
90
  h3. Who did it?
54
91
 
@@ -61,6 +98,8 @@ To determine the user that is currently running the deploy, the capistrano tasks
61
98
 
62
99
  h2. Rails 3 Rake Task
63
100
 
101
+ *APIv1 ONLY, use APIv1 Key*
102
+
64
103
  Send a message using a rake task:
65
104
 
66
105
  bc. rake hipchat:send["hello world"]
@@ -93,6 +132,23 @@ bc.. on_app_master do
93
132
  run "cd #{release_path} && bundle exec rake hipchat:send MESSAGE='#{message}'"
94
133
  end
95
134
 
135
+ h2. Chef Handler
136
+
137
+ *APIv1 ONLY, use APIv1 Key*
138
+
139
+ Report on Chef runs. Within a Recipe:
140
+
141
+ bc.. include_recipe 'chef_handler'
142
+
143
+ gem_package 'hipchat'
144
+
145
+ chef_handler 'HipChat::NotifyRoom' do
146
+ action :enable
147
+ arguments ['API_KEY', 'HIPCHAT_ROOM']
148
+ source File.join(Gem.all_load_paths.grep(/hipchat/).first,
149
+ 'hipchat', 'chef.rb')
150
+ end
151
+
96
152
  h2. Copyright
97
153
 
98
154
  Copyright (c) 2012 Atlassian. See LICENSE for details.
@@ -28,4 +28,5 @@ Gem::Specification.new do |spec|
28
28
  spec.add_development_dependency "rake"
29
29
  spec.add_development_dependency "webmock"
30
30
  spec.add_development_dependency 'rdoc', '> 2.4.2'
31
+ spec.add_development_dependency 'coveralls'
31
32
  end
@@ -3,6 +3,7 @@ require 'ostruct'
3
3
 
4
4
  require 'hipchat/railtie' if defined?(Rails::Railtie)
5
5
  require "hipchat/version"
6
+ require 'hipchat/api_version'
6
7
 
7
8
  module HipChat
8
9
  class UnknownRoom < StandardError; end
@@ -13,23 +14,23 @@ module HipChat
13
14
  class Client
14
15
  include HTTParty
15
16
 
16
- base_uri 'https://api.hipchat.com/v1/rooms'
17
17
  format :json
18
18
 
19
19
  def initialize(token, options={})
20
20
  @token = token
21
-
21
+ @api_version = options[:api_version]
22
+ @api = HipChat::ApiVersion::Client.new(@api_version)
23
+ self.class.base_uri(@api.base_uri)
22
24
  http_proxy = options[:http_proxy] || ENV['http_proxy']
23
25
  setup_proxy(http_proxy) if http_proxy
24
26
  end
25
27
 
26
28
  def rooms
27
- @rooms ||= self.class.get("/list", :query => {:auth_token => @token})['rooms'].
28
- map { |r| Room.new(@token, r) }
29
+ @rooms ||= _rooms
29
30
  end
30
31
 
31
32
  def [](name)
32
- Room.new(@token, :room_id => name)
33
+ Room.new(@token, :room_id => name, :api_version => @api_version)
33
34
  end
34
35
 
35
36
  private
@@ -41,16 +42,35 @@ module HipChat
41
42
  HipChat::Room.http_proxy(proxy_url.host, proxy_url.port,
42
43
  proxy_url.user, proxy_url.password)
43
44
  end
45
+
46
+ def _rooms
47
+ response = self.class.get(@api.rooms_config[:url],
48
+ :query => {
49
+ :auth_token => @token
50
+ },
51
+ :headers => @api.headers
52
+ )
53
+ case response.code
54
+ when 200
55
+ response[@api.rooms_config[:data_key]].map do |r|
56
+ Room.new(@token, r.merge(:api_version => @api_version))
57
+ end
58
+ else
59
+ raise UnknownResponseCode, "Unexpected #{response.code} for room `#{room_id}'"
60
+ end
61
+ end
44
62
  end
45
63
 
46
64
  class Room < OpenStruct
47
65
  include HTTParty
48
66
 
49
- base_uri 'https://api.hipchat.com/v1/rooms'
67
+ format :json
50
68
 
51
69
  def initialize(token, params)
52
70
  @token = token
53
-
71
+ @api = HipChat::ApiVersion::Room.new(params[:room_id],
72
+ params.delete(:api_version))
73
+ self.class.base_uri(@api.base_uri)
54
74
  super(params)
55
75
  end
56
76
 
@@ -86,7 +106,7 @@ module HipChat
86
106
 
87
107
  options = { :color => 'yellow', :notify => false }.merge options
88
108
 
89
- response = self.class.post('/message',
109
+ response = self.class.post(@api.send_config[:url],
90
110
  :query => { :auth_token => @token },
91
111
  :body => {
92
112
  :room_id => room_id,
@@ -94,12 +114,13 @@ module HipChat
94
114
  :message => message,
95
115
  :message_format => options[:message_format] || 'html',
96
116
  :color => options[:color],
97
- :notify => options[:notify] ? 1 : 0
98
- }
117
+ :notify => @api.bool_val(options[:notify])
118
+ }.send(@api.send_config[:body_format]),
119
+ :headers => @api.headers
99
120
  )
100
121
 
101
122
  case response.code
102
- when 200; true
123
+ when 200, 204; true
103
124
  when 404
104
125
  raise UnknownRoom, "Unknown room: `#{room_id}'"
105
126
  when 401
@@ -124,17 +145,18 @@ module HipChat
124
145
 
125
146
  options = { :from => 'API' }.merge options
126
147
 
127
- response = self.class.post('/topic',
148
+ response = self.class.send(@api.topic_config[:method], @api.topic_config[:url],
128
149
  :query => { :auth_token => @token },
129
150
  :body => {
130
151
  :room_id => room_id,
131
152
  :from => options[:from],
132
153
  :topic => new_topic
133
- }
154
+ }.send(@api.topic_config[:body_format]),
155
+ :headers => @api.headers
134
156
  )
135
157
 
136
158
  case response.code
137
- when 200; true
159
+ when 204,200; true
138
160
  when 404
139
161
  raise UnknownRoom, "Unknown room: `#{room_id}'"
140
162
  when 401
@@ -163,14 +185,15 @@ module HipChat
163
185
 
164
186
  options = { :date => 'recent', :timezone => 'UTC', :format => 'JSON' }.merge options
165
187
 
166
- response = self.class.get('/history',
188
+ response = self.class.get(@api.history_config[:url],
167
189
  :query => {
168
190
  :room_id => room_id,
169
191
  :date => options[:date],
170
192
  :timezone => options[:timezone],
171
193
  :format => options[:format],
172
194
  :auth_token => @token,
173
- }
195
+ },
196
+ :headers => @api.headers
174
197
  )
175
198
 
176
199
  case response.code
@@ -0,0 +1,102 @@
1
+ module HipChat
2
+ class ApiVersion
3
+
4
+ class Client
5
+
6
+ def initialize(version = 'v1')
7
+ @version = !version.nil? ? version : 'v1'
8
+ if @version.eql?('v1')
9
+ @base_uri = "https://api.hipchat.com/v1/rooms"
10
+ @headers = {'Accept' => 'application/json',
11
+ 'Content-Type' => 'application/x-www-form-urlencoded'}
12
+ else
13
+ @base_uri = "https://api.hipchat.com/v2/room"
14
+ @headers = {'Accept' => 'application/json',
15
+ 'Content-Type' => 'application/json'}
16
+ end
17
+ end
18
+
19
+ attr_reader :version, :base_uri, :headers
20
+
21
+ def rooms_config
22
+ {
23
+ 'v1' => {
24
+ :url => '/list',
25
+ :data_key => 'rooms'
26
+ },
27
+ 'v2' => {
28
+ :url => '/room',
29
+ :data_key => 'items'
30
+ }
31
+ }[version]
32
+ end
33
+ end
34
+
35
+ class Room
36
+
37
+ def initialize(room_id, version = 'v1')
38
+ @room_id = room_id
39
+ @version = !version.nil? ? version : 'v1'
40
+ if @version.eql?('v1')
41
+ @base_uri = "https://api.hipchat.com/v1/rooms"
42
+ @headers = {'Accept' => 'application/json',
43
+ 'Content-Type' => 'application/x-www-form-urlencoded'}
44
+ else
45
+ @base_uri = "https://api.hipchat.com/v2/room"
46
+ @headers = {'Accept' => 'application/json',
47
+ 'Content-Type' => 'application/json'}
48
+ end
49
+ end
50
+
51
+ attr_reader :version, :base_uri, :room_id, :headers
52
+
53
+ def send_config
54
+ {
55
+ 'v1' => {
56
+ :url => "/message",
57
+ :body_format => :to_hash
58
+ },
59
+ 'v2' => {
60
+ :url => "/#{room_id}/notification",
61
+ :body_format => :to_json
62
+ }
63
+ }[version]
64
+ end
65
+
66
+ def topic_config
67
+ {
68
+ 'v1' => {
69
+ :url => '/topic',
70
+ :method => :post,
71
+ :body_format => :to_hash
72
+ },
73
+ 'v2' => {
74
+ :url => "/#{room_id}/topic",
75
+ :method => :put,
76
+ :body_format => :to_json
77
+ }
78
+ }[version]
79
+ end
80
+
81
+ def history_config
82
+ {
83
+ 'v1' => {
84
+ :url => '/history'
85
+ },
86
+ 'v2' => {
87
+ :url => "/#{room_id}/history"
88
+ }
89
+ }[version]
90
+ end
91
+
92
+ def bool_val(opt)
93
+ if version.eql?('v1')
94
+ opt ? 1 : 0
95
+ else
96
+ opt
97
+ end
98
+ end
99
+
100
+ end
101
+ end
102
+ end
@@ -1,131 +1,5 @@
1
- require 'hipchat'
2
-
3
- Capistrano::Configuration.instance(:must_exist).load do
4
- set :hipchat_send_notification, false
5
- set :hipchat_with_migrations, ''
6
-
7
- namespace :hipchat do
8
- task :trigger_notification do
9
- set :hipchat_send_notification, true if !dry_run
10
- end
11
-
12
- task :configure_for_migrations do
13
- set :hipchat_with_migrations, ' (with migrations)'
14
- end
15
-
16
- task :notify_deploy_started do
17
- if hipchat_send_notification
18
-
19
- environment_string = env
20
- if self.respond_to?(:stage)
21
- environment_string = "#{stage} (#{env})"
22
- end
23
-
24
- on_rollback do
25
- send_options.merge!(:color => failed_message_color)
26
- send("#{human} cancelled deployment of #{deployment_name} to #{environment_string}.", send_options)
27
- end
28
-
29
- send("#{human} is deploying #{deployment_name} to #{environment_string}#{fetch(:hipchat_with_migrations, '')}.", send_options)
30
- end
31
- end
32
-
33
- task :notify_deploy_finished do
34
- if hipchat_send_notification
35
- send_options.merge!(:color => success_message_color)
36
-
37
- environment_string = env
38
- if self.respond_to?(:stage)
39
- environment_string = "#{stage} (#{env})"
40
- end
41
-
42
- send("#{human} finished deploying #{deployment_name} to #{environment_string}#{fetch(:hipchat_with_migrations, '')}.", send_options)
43
- end
44
- end
45
-
46
- def send_options
47
- return @send_options if defined?(@send_options)
48
- @send_options = message_format ? {:message_format => message_format } : {}
49
- @send_options.merge!(:notify => message_notification)
50
- @send_options.merge!(:color => message_color)
51
- @send_options
52
- end
53
-
54
- def send(message, options)
55
- set :hipchat_client, HipChat::Client.new(hipchat_token) if fetch(:hipchat_client, nil).nil?
56
-
57
- if hipchat_room_name.is_a?(String)
58
- rooms = [hipchat_room_name]
59
- elsif hipchat_room_name.is_a?(Symbol)
60
- rooms = [hipchat_room_name.to_s]
61
- else
62
- rooms = hipchat_room_name
63
- end
64
-
65
- rooms.each { |room|
66
- begin
67
- hipchat_client[room].send(deploy_user, message, options)
68
- rescue => e
69
- puts e.message
70
- puts e.backtrace
71
- end
72
- }
73
- end
74
-
75
- def deployment_name
76
- if fetch(:branch, nil)
77
- name = "#{application}/#{branch}"
78
- name += " (revision #{real_revision[0..7]})" if real_revision
79
- name
80
- else
81
- application
82
- end
83
- end
84
-
85
- def message_color
86
- fetch(:hipchat_color, nil)
87
- end
88
-
89
- def success_message_color
90
- fetch(:hipchat_success_color, "green")
91
- end
92
-
93
- def failed_message_color
94
- fetch(:hipchat_failed_color, "red")
95
- end
96
-
97
- def message_notification
98
- fetch(:hipchat_announce, false)
99
- end
100
-
101
- def message_format
102
- fetch(:hipchat_message_format, "html")
103
- end
104
-
105
- def deploy_user
106
- fetch(:hipchat_deploy_user, "Deploy")
107
- end
108
-
109
- def human
110
- ENV['HIPCHAT_USER'] ||
111
- fetch(:hipchat_human,
112
- if (u = %x{git config user.name}.strip) != ""
113
- u
114
- elsif (u = ENV['USER']) != ""
115
- u
116
- else
117
- "Someone"
118
- end)
119
- end
120
-
121
- def env
122
- fetch(:hipchat_env, fetch(:rack_env, fetch(:rails_env, "production")))
123
- end
124
- end
125
-
126
- before "deploy", "hipchat:trigger_notification"
127
- before "deploy:migrations", "hipchat:trigger_notification", "hipchat:configure_for_migrations"
128
- before "deploy:update_code", "hipchat:notify_deploy_started"
129
- after "deploy", "hipchat:notify_deploy_finished"
130
- after "deploy:migrations", "hipchat:notify_deploy_finished"
131
- end
1
+ if defined?(Capistrano::VERSION) && Gem::Version.new(Capistrano::VERSION).release >= Gem::Version.new('3.0.0')
2
+ load File.expand_path('capistrano/tasks/hipchat.rake', File.dirname(__FILE__))
3
+ else
4
+ require_relative 'capistrano2'
5
+ end
@@ -0,0 +1,111 @@
1
+ require 'hipchat'
2
+
3
+ namespace :hipchat do
4
+
5
+ task :notify_deploy_started do
6
+ send_message("#{human} is deploying #{deployment_name} to #{environment_name}.", send_options)
7
+ end
8
+
9
+ task :notify_deploy_finished do
10
+ send_options.merge!(:color => success_message_color)
11
+ send_message("#{human} finished deploying #{deployment_name} to #{environment_name}.", send_options)
12
+ end
13
+
14
+ task :notify_deploy_reverted do
15
+ send_options.merge!(:color => failed_message_color)
16
+ send_message("#{human} cancelled deployment of #{deployment_name} to #{environment_name}.", send_options)
17
+ end
18
+
19
+ def send_options
20
+ return @send_options if defined?(@send_options)
21
+ @send_options = message_format ? {:message_format => message_format } : {}
22
+ @send_options.merge!(:notify => message_notification)
23
+ @send_options.merge!(:color => message_color)
24
+ @send_options
25
+ end
26
+
27
+ def send_message(message, options)
28
+ hipchat_token = fetch(:hipchat_token)
29
+ hipchat_room_name = fetch(:hipchat_room_name)
30
+
31
+ hipchat_client = fetch(:hipchat_client, HipChat::Client.new(hipchat_token))
32
+
33
+ if hipchat_room_name.is_a?(String)
34
+ rooms = [hipchat_room_name]
35
+ elsif hipchat_room_name.is_a?(Symbol)
36
+ rooms = [hipchat_room_name.to_s]
37
+ else
38
+ rooms = hipchat_room_name
39
+ end
40
+
41
+ rooms.each { |room|
42
+ begin
43
+ hipchat_client[room].send(deploy_user, message, options)
44
+ rescue => e
45
+ puts e.message
46
+ puts e.backtrace
47
+ end
48
+ }
49
+ end
50
+
51
+ def deployment_name
52
+ if fetch(:branch, nil)
53
+ application = fetch(:application)
54
+ branch = fetch(:branch)
55
+ real_revision = fetch(:real_revision)
56
+
57
+ name = "#{application}/#{branch}"
58
+ name += " (revision #{real_revision[0..7]})" if real_revision
59
+ name
60
+ else
61
+ application
62
+ end
63
+ end
64
+
65
+ def message_color
66
+ fetch(:hipchat_color)
67
+ end
68
+
69
+ def success_message_color
70
+ fetch(:hipchat_success_color, 'green')
71
+ end
72
+
73
+ def failed_message_color
74
+ fetch(:hipchat_failed_color, 'red')
75
+ end
76
+
77
+ def message_notification
78
+ fetch(:hipchat_announce, false)
79
+ end
80
+
81
+ def message_format
82
+ fetch(:hipchat_message_format, 'html')
83
+ end
84
+
85
+ def deploy_user
86
+ fetch(:hipchat_deploy_user, 'Deploy')
87
+ end
88
+
89
+ def human
90
+ user = ENV['HIPCHAT_USER'] || fetch(:hipchat_human)
91
+ user = user || if (u = %x{git config user.name}.strip) != ''
92
+ u
93
+ elsif (u = ENV['USER']) != ''
94
+ u
95
+ else
96
+ 'Someone'
97
+ end
98
+ user
99
+ end
100
+
101
+ def environment_name
102
+ fetch(:hipchat_env, fetch(:rack_env, fetch(:rails_env, fetch(:stage))))
103
+ end
104
+
105
+ before 'deploy:starting', :notify_deploy_started
106
+ after 'deploy:finished', :notify_deploy_finished
107
+ if Rake::Task.task_defined? 'deploy:failed'
108
+ after 'deploy:failed', :notify_deploy_reverted
109
+ end
110
+
111
+ end
@@ -0,0 +1,131 @@
1
+ require 'hipchat'
2
+
3
+ Capistrano::Configuration.instance(:must_exist).load do
4
+ set :hipchat_send_notification, false
5
+ set :hipchat_with_migrations, ''
6
+
7
+ namespace :hipchat do
8
+ task :trigger_notification do
9
+ set :hipchat_send_notification, true if !dry_run
10
+ end
11
+
12
+ task :configure_for_migrations do
13
+ set :hipchat_with_migrations, ' (with migrations)'
14
+ end
15
+
16
+ task :notify_deploy_started do
17
+ if hipchat_send_notification
18
+
19
+ environment_string = env
20
+ if self.respond_to?(:stage)
21
+ environment_string = "#{stage} (#{env})"
22
+ end
23
+
24
+ on_rollback do
25
+ send_options.merge!(:color => failed_message_color)
26
+ send("#{human} cancelled deployment of #{deployment_name} to #{environment_string}.", send_options)
27
+ end
28
+
29
+ send("#{human} is deploying #{deployment_name} to #{environment_string}#{fetch(:hipchat_with_migrations, '')}.", send_options)
30
+ end
31
+ end
32
+
33
+ task :notify_deploy_finished do
34
+ if hipchat_send_notification
35
+ send_options.merge!(:color => success_message_color)
36
+
37
+ environment_string = env
38
+ if self.respond_to?(:stage)
39
+ environment_string = "#{stage} (#{env})"
40
+ end
41
+
42
+ send("#{human} finished deploying #{deployment_name} to #{environment_string}#{fetch(:hipchat_with_migrations, '')}.", send_options)
43
+ end
44
+ end
45
+
46
+ def send_options
47
+ return @send_options if defined?(@send_options)
48
+ @send_options = message_format ? {:message_format => message_format } : {}
49
+ @send_options.merge!(:notify => message_notification)
50
+ @send_options.merge!(:color => message_color)
51
+ @send_options
52
+ end
53
+
54
+ def send(message, options)
55
+ set :hipchat_client, HipChat::Client.new(hipchat_token) if fetch(:hipchat_client, nil).nil?
56
+
57
+ if hipchat_room_name.is_a?(String)
58
+ rooms = [hipchat_room_name]
59
+ elsif hipchat_room_name.is_a?(Symbol)
60
+ rooms = [hipchat_room_name.to_s]
61
+ else
62
+ rooms = hipchat_room_name
63
+ end
64
+
65
+ rooms.each { |room|
66
+ begin
67
+ hipchat_client[room].send(deploy_user, message, options)
68
+ rescue => e
69
+ puts e.message
70
+ puts e.backtrace
71
+ end
72
+ }
73
+ end
74
+
75
+ def deployment_name
76
+ if fetch(:branch, nil)
77
+ name = "#{application}/#{branch}"
78
+ name += " (revision #{real_revision[0..7]})" if real_revision
79
+ name
80
+ else
81
+ application
82
+ end
83
+ end
84
+
85
+ def message_color
86
+ fetch(:hipchat_color, nil)
87
+ end
88
+
89
+ def success_message_color
90
+ fetch(:hipchat_success_color, "green")
91
+ end
92
+
93
+ def failed_message_color
94
+ fetch(:hipchat_failed_color, "red")
95
+ end
96
+
97
+ def message_notification
98
+ fetch(:hipchat_announce, false)
99
+ end
100
+
101
+ def message_format
102
+ fetch(:hipchat_message_format, "html")
103
+ end
104
+
105
+ def deploy_user
106
+ fetch(:hipchat_deploy_user, "Deploy")
107
+ end
108
+
109
+ def human
110
+ ENV['HIPCHAT_USER'] ||
111
+ fetch(:hipchat_human,
112
+ if (u = %x{git config user.name}.strip) != ""
113
+ u
114
+ elsif (u = ENV['USER']) != ""
115
+ u
116
+ else
117
+ "Someone"
118
+ end)
119
+ end
120
+
121
+ def env
122
+ fetch(:hipchat_env, fetch(:rack_env, fetch(:rails_env, "production")))
123
+ end
124
+ end
125
+
126
+ before "deploy", "hipchat:trigger_notification"
127
+ before "deploy:migrations", "hipchat:trigger_notification", "hipchat:configure_for_migrations"
128
+ before "deploy:update_code", "hipchat:notify_deploy_started"
129
+ after "deploy", "hipchat:notify_deploy_finished"
130
+ after "deploy:migrations", "hipchat:notify_deploy_finished"
131
+ end
@@ -12,7 +12,8 @@ namespace :hipchat do
12
12
  :user => ENV['HIPCHAT_USER'],
13
13
  :notify => ENV['NOTIFY'],
14
14
  :room => ENV['ROOM'],
15
- :token => ENV['TOKEN']
15
+ :token => ENV['TOKEN'],
16
+ :api_version => ENV['API_VERSION']
16
17
  }.reject { |k, v| v.blank? }
17
18
 
18
19
  system_options = {
@@ -37,7 +38,7 @@ namespace :hipchat do
37
38
  exit
38
39
  end
39
40
 
40
- client = HipChat::Client.new(options[:token])
41
+ client = HipChat::Client.new(options[:token], options)
41
42
 
42
43
  client[options[:room]].send(options[:user], options[:message], :notify => options[:notify])
43
44
  end
@@ -1,3 +1,3 @@
1
1
  module HipChat
2
- VERSION = "0.14.0"
2
+ VERSION = "1.0.0"
3
3
  end
@@ -1,47 +1,10 @@
1
1
  require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+ require File.expand_path(File.dirname(__FILE__) + '/shared_hipchat')
2
3
 
3
4
  describe HipChat do
4
- subject { HipChat::Client.new("blah") }
5
-
6
- let(:room) { subject["Hipchat"] }
7
-
8
- # Helper for mocking room message post requests
9
- def mock_successful_send(from, message, options={})
10
- options = {:color => 'yellow', :notify => 0, :message_format => 'html'}.merge(options)
11
- mock(HipChat::Room).post("/message",
12
- :query => {:auth_token => "blah"},
13
- :body => {:room_id => "Hipchat",
14
- :from => "Dude",
15
- :message => "Hello world",
16
- :message_format => options[:message_format],
17
- :color => options[:color],
18
- :notify => options[:notify]}) {
19
- OpenStruct.new(:code => 200)
20
- }
21
- end
22
-
23
- def mock_successful_topic_change(topic, options={})
24
- options = {:from => 'API'}.merge(options)
25
- mock(HipChat::Room).post("/topic",
26
- :query => {:auth_token => "blah"},
27
- :body => {:room_id => "Hipchat",
28
- :from => options[:from],
29
- :topic => "Nice topic" } ) {
30
- OpenStruct.new(:code => 200)
31
- }
32
- end
33
-
34
- def mock_successful_history(options={})
35
- options = { :date => 'recent', :timezone => 'UTC', :format => 'JSON' }.merge(options)
36
- canned_response = File.new File.expand_path(File.dirname(__FILE__) + '/example/history.json')
37
- stub_request(:get, "https://api.hipchat.com/v1/rooms/history").with(:query => {:auth_token => "blah",
38
- :room_id => "Hipchat",
39
- :date => options[:date],
40
- :timezone => options[:timezone],
41
- :format => options[:format]}).to_return(canned_response)
42
- end
43
5
 
44
- describe "#history" do
6
+ describe "#history (API V1)" do
7
+ include_context "HipChatV1"
45
8
  it "is successful without custom options" do
46
9
  mock_successful_history()
47
10
 
@@ -79,7 +42,8 @@ describe HipChat do
79
42
  end
80
43
  end
81
44
 
82
- describe "#topic" do
45
+ describe "#topic (API V1)" do
46
+ include_context "HipChatV1"
83
47
  it "is successful without custom options" do
84
48
  mock_successful_topic_change("Nice topic")
85
49
 
@@ -118,7 +82,8 @@ describe HipChat do
118
82
  end
119
83
  end
120
84
 
121
- describe "sends a message to a room" do
85
+ describe "sends a message to a room (API V1)" do
86
+ include_context "HipChatV1"
122
87
  it "successfully without custom options" do
123
88
  mock_successful_send 'Dude', 'Hello world'
124
89
 
@@ -128,6 +93,141 @@ describe HipChat do
128
93
  it "successfully with notifications on as option" do
129
94
  mock_successful_send 'Dude', 'Hello world', :notify => 1
130
95
 
96
+ room.send("Dude", "Hello world", :notify => 1).should be_true
97
+ end
98
+
99
+ it "successfully with custom color" do
100
+ mock_successful_send 'Dude', 'Hello world', :color => 'red'
101
+
102
+ room.send("Dude", "Hello world", :color => 'red').should be_true
103
+ end
104
+
105
+ it "successfully with text message_format" do
106
+ mock_successful_send 'Dude', 'Hello world', :message_format => 'text'
107
+
108
+ room.send("Dude", "Hello world", :message_format => 'text').should be_true
109
+ end
110
+
111
+ it "but fails when the room doesn't exist" do
112
+ mock(HipChat::Room).post(anything, anything) {
113
+ OpenStruct.new(:code => 404)
114
+ }
115
+
116
+ lambda { room.send "", "" }.should raise_error(HipChat::UnknownRoom)
117
+ end
118
+
119
+ it "but fails when we're not allowed to do so" do
120
+ mock(HipChat::Room).post(anything, anything) {
121
+ OpenStruct.new(:code => 401)
122
+ }
123
+
124
+ lambda { room.send "", "" }.should raise_error(HipChat::Unauthorized)
125
+ end
126
+
127
+ it "but fails if the username is more than 15 chars" do
128
+ lambda { room.send "a very long username here", "a message" }.should raise_error(HipChat::UsernameTooLong)
129
+ end
130
+
131
+ it "but fails if we get an unknown response code" do
132
+ mock(HipChat::Room).post(anything, anything) {
133
+ OpenStruct.new(:code => 403)
134
+ }
135
+
136
+ lambda { room.send "", "" }.
137
+ should raise_error(HipChat::UnknownResponseCode)
138
+ end
139
+ end
140
+
141
+ describe "#history (API v2)" do
142
+ include_context "HipChatV2"
143
+ it "is successful without custom options" do
144
+ mock_successful_history()
145
+
146
+ room.history().should be_true
147
+ end
148
+
149
+ it "is successful with custom options" do
150
+ mock_successful_history(:timezone => 'America/Los_Angeles', :date => '2010-11-19')
151
+ room.history(:timezone => 'America/Los_Angeles', :date => '2010-11-19').should be_true
152
+ end
153
+
154
+ it "fails when the room doen't exist" do
155
+ mock(HipChat::Room).get(anything, anything) {
156
+ OpenStruct.new(:code => 404)
157
+ }
158
+
159
+ lambda { room.history }.should raise_error(HipChat::UnknownRoom)
160
+ end
161
+
162
+ it "fails when we're not allowed to do so" do
163
+ mock(HipChat::Room).get(anything, anything) {
164
+ OpenStruct.new(:code => 401)
165
+ }
166
+
167
+ lambda { room.history }.should raise_error(HipChat::Unauthorized)
168
+ end
169
+
170
+ it "fails if we get an unknown response code" do
171
+ mock(HipChat::Room).get(anything, anything) {
172
+ OpenStruct.new(:code => 403)
173
+ }
174
+
175
+ lambda { room.history }.
176
+ should raise_error(HipChat::UnknownResponseCode)
177
+ end
178
+ end
179
+
180
+ describe "#topic (API v2)" do
181
+ include_context "HipChatV2"
182
+ it "is successful without custom options" do
183
+ mock_successful_topic_change("Nice topic")
184
+
185
+ room.topic("Nice topic").should be_true
186
+ end
187
+
188
+ it "is successful with a custom from" do
189
+ mock_successful_topic_change("Nice topic", :from => "Me")
190
+
191
+ room.topic("Nice topic", :from => "Me").should be_true
192
+ end
193
+
194
+ it "fails when the room doesn't exist" do
195
+ mock(HipChat::Room).put(anything, anything) {
196
+ OpenStruct.new(:code => 404)
197
+ }
198
+
199
+ lambda { room.topic "" }.should raise_error(HipChat::UnknownRoom)
200
+ end
201
+
202
+ it "fails when we're not allowed to do so" do
203
+ mock(HipChat::Room).put(anything, anything) {
204
+ OpenStruct.new(:code => 401)
205
+ }
206
+
207
+ lambda { room.topic "" }.should raise_error(HipChat::Unauthorized)
208
+ end
209
+
210
+ it "fails if we get an unknown response code" do
211
+ mock(HipChat::Room).put(anything, anything) {
212
+ OpenStruct.new(:code => 403)
213
+ }
214
+
215
+ lambda { room.topic "" }.
216
+ should raise_error(HipChat::UnknownResponseCode)
217
+ end
218
+ end
219
+
220
+ describe "sends a message to a room (API V2)" do
221
+ include_context "HipChatV2"
222
+ it "successfully without custom options" do
223
+ mock_successful_send 'Dude', 'Hello world'
224
+
225
+ room.send("Dude", "Hello world").should be_true
226
+ end
227
+
228
+ it "successfully with notifications on as option" do
229
+ mock_successful_send 'Dude', 'Hello world', :notify => true
230
+
131
231
  room.send("Dude", "Hello world", :notify => true).should be_true
132
232
  end
133
233
 
@@ -0,0 +1,82 @@
1
+ shared_context "HipChatV1" do
2
+ before { @api_version = 'v1'}
3
+ # Helper for mocking room message post requests
4
+ def mock_successful_send(from, message, options={})
5
+ options = {:color => 'yellow', :notify => 0, :message_format => 'html'}.merge(options)
6
+ stub_request(:post, "https://api.hipchat.com/v1/rooms/message").with(
7
+ :query => {:auth_token => "blah"},
8
+ :body => {:room_id => "Hipchat",
9
+ :from => "Dude",
10
+ :message => "Hello world",
11
+ :message_format => options[:message_format],
12
+ :color => options[:color],
13
+ :notify => "#{options[:notify]}"},
14
+ :headers => {'Accept' => 'application/json',
15
+ 'Content-Type' => 'application/x-www-form-urlencoded'}).to_return(:status => 200, :body => "", :headers => {})
16
+
17
+ end
18
+
19
+ def mock_successful_topic_change(topic, options={})
20
+ options = {:from => 'API'}.merge(options)
21
+ stub_request(:post, "https://api.hipchat.com/v1/rooms/topic").with(
22
+ :query => {:auth_token => "blah"},
23
+ :body => {:room_id => "Hipchat",
24
+ :from => options[:from],
25
+ :topic => "Nice topic" },
26
+ :headers => {'Accept' => 'application/json',
27
+ 'Content-Type' => 'application/x-www-form-urlencoded'}).to_return(:status => 200, :body => "", :headers => {})
28
+ end
29
+
30
+ def mock_successful_history(options={})
31
+ options = { :date => 'recent', :timezone => 'UTC', :format => 'JSON' }.merge(options)
32
+ canned_response = File.new File.expand_path(File.dirname(__FILE__) + '/example/history.json')
33
+ stub_request(:get, "https://api.hipchat.com/v1/rooms/history").with(:query => {:auth_token => "blah",
34
+ :room_id => "Hipchat",
35
+ :date => options[:date],
36
+ :timezone => options[:timezone],
37
+ :format => options[:format]}).to_return(canned_response)
38
+ end
39
+ subject { HipChat::Client.new("blah", :api_version => @api_version) }
40
+ let(:room) { subject["Hipchat"] }
41
+ end
42
+
43
+ shared_context "HipChatV2" do
44
+ before { @api_version = 'v2'}
45
+ # Helper for mocking room message post requests
46
+ def mock_successful_send(from, message, options={})
47
+ options = {:color => 'yellow', :notify => false, :message_format => 'html'}.merge(options)
48
+ stub_request(:post, "https://api.hipchat.com/v2/room/Hipchat/notification").with(
49
+ :query => {:auth_token => "blah"},
50
+ :body => {:room_id => "Hipchat",
51
+ :from => "Dude",
52
+ :message => "Hello world",
53
+ :message_format => options[:message_format],
54
+ :color => options[:color],
55
+ :notify => options[:notify]}.to_json,
56
+ :headers => {'Accept' => 'application/json',
57
+ 'Content-Type' => 'application/json'}).to_return(:status => 200, :body => "", :headers => {})
58
+ end
59
+
60
+ def mock_successful_topic_change(topic, options={})
61
+ options = {:from => 'API'}.merge(options)
62
+ stub_request(:put, "https://api.hipchat.com/v2/room/Hipchat/topic").with(
63
+ :query => {:auth_token => "blah"},
64
+ :body => {:room_id => "Hipchat",
65
+ :from => options[:from],
66
+ :topic => "Nice topic" }.to_json,
67
+ :headers => {'Accept' => 'application/json',
68
+ 'Content-Type' => 'application/json'}).to_return(:status => 200, :body => "", :headers => {})
69
+ end
70
+
71
+ def mock_successful_history(options={})
72
+ options = { :date => 'recent', :timezone => 'UTC', :format => 'JSON' }.merge(options)
73
+ canned_response = File.new File.expand_path(File.dirname(__FILE__) + '/example/history.json')
74
+ stub_request(:get, "https://api.hipchat.com/v2/room/Hipchat/history").with(:query => {:auth_token => "blah",
75
+ :room_id => "Hipchat",
76
+ :date => options[:date],
77
+ :timezone => options[:timezone],
78
+ :format => options[:format]}).to_return(canned_response)
79
+ end
80
+ subject { HipChat::Client.new("blah", :api_version => @api_version) }
81
+ let(:room) { subject["Hipchat"] }
82
+ end
@@ -4,6 +4,9 @@ require 'hipchat'
4
4
  require 'rspec'
5
5
  require 'rspec/autorun'
6
6
  require 'webmock/rspec'
7
+ require 'coveralls'
8
+
9
+ Coveralls.wear!
7
10
 
8
11
  RSpec.configure do |config|
9
12
  config.mock_with :rr
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hipchat
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.14.0
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - HipChat/Atlassian
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-11-24 00:00:00.000000000 Z
11
+ date: 2013-12-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: httparty
@@ -108,6 +108,20 @@ dependencies:
108
108
  - - ">"
109
109
  - !ruby/object:Gem::Version
110
110
  version: 2.4.2
111
+ - !ruby/object:Gem::Dependency
112
+ name: coveralls
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
111
125
  description: Ruby library to interact with HipChat
112
126
  email:
113
127
  - support@hipchat.com
@@ -124,13 +138,17 @@ files:
124
138
  - Rakefile
125
139
  - hipchat.gemspec
126
140
  - lib/hipchat.rb
141
+ - lib/hipchat/api_version.rb
127
142
  - lib/hipchat/capistrano.rb
143
+ - lib/hipchat/capistrano/tasks/hipchat.rake
144
+ - lib/hipchat/capistrano2.rb
128
145
  - lib/hipchat/chef.rb
129
146
  - lib/hipchat/rails3_tasks.rb
130
147
  - lib/hipchat/railtie.rb
131
148
  - lib/hipchat/version.rb
132
149
  - spec/example/history.json
133
150
  - spec/hipchat_spec.rb
151
+ - spec/shared_hipchat.rb
134
152
  - spec/spec.opts
135
153
  - spec/spec_helper.rb
136
154
  homepage: https://github.com/hipchat/hipchat-rb
@@ -160,6 +178,7 @@ summary: Ruby library to interact with HipChat
160
178
  test_files:
161
179
  - spec/example/history.json
162
180
  - spec/hipchat_spec.rb
181
+ - spec/shared_hipchat.rb
163
182
  - spec/spec.opts
164
183
  - spec/spec_helper.rb
165
184
  has_rdoc: