hipchat 1.0.1 → 1.1.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: 9cdd550f65e0e012c7111c61836744d879ddbf90
4
- data.tar.gz: ee465ca047742841fc8d640a182480e22df302e0
3
+ metadata.gz: b2ec52d652412c7540c45f243508e99f5a155ec1
4
+ data.tar.gz: 0082b930a02ab0a0fac1d46d7181a7cd64202634
5
5
  SHA512:
6
- metadata.gz: a75f6ad13c8ea975899a5307e465d210332d645d809e63fef60e60110074309d25abc873b4b5d574fcf1873f77dab38cf2127b3cf2508e67c57d544f9a3ee8ff
7
- data.tar.gz: 06150b37bfdc38a204b90eb6eb4d5f25ab4eb4d43008799a8af6aebe602e170a16ec79e8ac6f67d4ede652ae0298d6de5455c0f2ab73f2f391071e3979a950cf
6
+ metadata.gz: 0470720c384fce9a0403b3df9fc930b98cd1047d496b783a62cb0fbbec8999bdd4998db5f76153f366450ffc91e2ec48f2a44cfaeb8181c9ebcf47d11dbc9fac
7
+ data.tar.gz: 82098108cb71cffb51993058336b39072da7154ae8a579e7077365dc98a1dc6c22671a29f817f0fed68f8d5dd1bf60056ec44924a4b016a94ff6febb926e874e
@@ -0,0 +1 @@
1
+ hipchat-rb
@@ -0,0 +1 @@
1
+ 2.1.0
@@ -3,3 +3,4 @@ language: ruby
3
3
  rvm:
4
4
  - 1.9.3
5
5
  - 2.0.0
6
+ - 2.1.0
@@ -40,6 +40,9 @@ client['my room'].history()
40
40
  # Get history for a date in time with a particular timezone (default is latest 75 messages, timezone default is 'UTC')
41
41
  client['my room'].history(:date => '2010-11-19', :timezone => 'PST')
42
42
 
43
+ # Create a new room, V1 requires owner_user_id (see https://www.hipchat.com/docs/api/method/rooms/create)
44
+ client.create_room("Name", :owner_user_id => 'user_id')
45
+
43
46
  h2. "API v2":https://www.hipchat.com/docs/apiv2
44
47
 
45
48
  bc.. client = HipChat::Client.new(api_token, :api_version => 'v2')
@@ -67,6 +70,18 @@ client['my room'].history()
67
70
  # Get history for a date in time with a particular timezone (default is latest 75 messages, timezone default is 'UTC')
68
71
  client['my room'].history(:date => '2010-11-19', :timezone => 'PST')
69
72
 
73
+ # Create a new room (see https://www.hipchat.com/docs/apiv2/method/create_room)
74
+ client.create_room("Name", options = {})
75
+
76
+ # Get room data (see https://www.hipchat.com/docs/apiv2/method/get_room)
77
+ client['my room'].get_room
78
+
79
+ # Invite user to room (see https://www.hipchat.com/docs/apiv2/method/invite_user)
80
+ client['my room'].invite("USER_ID_OR_NAME", options = {})
81
+
82
+ # Sends a user a private message. Valid value for user are user id or email address
83
+ client.user('foo@bar.org').send('I can send private messages')
84
+
70
85
  h2. Capistrano
71
86
 
72
87
  *APIv1 ONLY, use APIv1 Key*
@@ -96,6 +111,15 @@ To determine the user that is currently running the deploy, the capistrano tasks
96
111
  # The git user.name var.
97
112
  # The $USER environment variable.
98
113
 
114
+ h3. Commit log notification (only for Capistrano 2)
115
+
116
+ Send commit log with deploy notification. We currently support git and svn.
117
+
118
+ bc.. set :hipchat_commit_log, true
119
+ # Optional
120
+ set :hipchat_commit_log_format, ":time :user\n:message\n"
121
+ set :hipchat_commit_log_time_format, "%Y/%m/%d %H:%M:%S"
122
+
99
123
  h2. Rails 3 Rake Task
100
124
 
101
125
  Send a message using a rake task:
@@ -138,7 +162,7 @@ h2. Chef Handler
138
162
  Report on Chef runs. Within a Recipe:
139
163
 
140
164
  bc.. include_recipe 'chef_handler'
141
-
165
+
142
166
  gem_package 'hipchat'
143
167
 
144
168
  chef_handler 'HipChat::NotifyRoom' do
@@ -1,211 +1,9 @@
1
- require 'httparty'
2
- require 'ostruct'
3
-
4
1
  require 'hipchat/railtie' if defined?(Rails::Railtie)
5
2
  require "hipchat/version"
6
- require 'hipchat/api_version'
7
3
 
8
4
  module HipChat
9
- class UnknownRoom < StandardError; end
10
- class Unauthorized < StandardError; end
11
- class UnknownResponseCode < StandardError; end
12
- class UsernameTooLong < StandardError; end
13
-
14
- class Client
15
- include HTTParty
16
-
17
- format :json
18
-
19
- def initialize(token, options={})
20
- @token = token
21
- @api_version = options[:api_version]
22
- @api = HipChat::ApiVersion::Client.new(@api_version)
23
- self.class.base_uri(@api.base_uri)
24
- http_proxy = options[:http_proxy] || ENV['http_proxy']
25
- setup_proxy(http_proxy) if http_proxy
26
- end
27
-
28
- def rooms
29
- @rooms ||= _rooms
30
- end
31
-
32
- def [](name)
33
- Room.new(@token, :room_id => name, :api_version => @api_version)
34
- end
35
-
36
- private
37
- def setup_proxy(proxy_url)
38
- proxy_url = URI.parse(proxy_url)
39
-
40
- self.class.http_proxy(proxy_url.host, proxy_url.port,
41
- proxy_url.user, proxy_url.password)
42
- HipChat::Room.http_proxy(proxy_url.host, proxy_url.port,
43
- proxy_url.user, proxy_url.password)
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
62
- end
63
-
64
- class Room < OpenStruct
65
- include HTTParty
66
-
67
- format :json
68
-
69
- def initialize(token, params)
70
- @token = token
71
- @api = HipChat::ApiVersion::Room.new(params[:room_id],
72
- params.delete(:api_version))
73
- self.class.base_uri(@api.base_uri)
74
- super(params)
75
- end
76
-
77
- # Send a message to this room.
78
- #
79
- # Usage:
80
- #
81
- # # Default
82
- # send 'nickname', 'some message'
83
- #
84
- # # Notify users and color the message red
85
- # send 'nickname', 'some message', :notify => true, :color => 'red'
86
- #
87
- # # Notify users (deprecated)
88
- # send 'nickname', 'some message', true
89
- #
90
- # Options:
91
- #
92
- # +color+:: "yellow", "red", "green", "purple", or "random"
93
- # (default "yellow")
94
- # +notify+:: true or false
95
- # (default false)
96
- def send(from, message, options_or_notify = {})
97
- if from.length > 15
98
- raise UsernameTooLong, "Username #{from} is `#{from.length} characters long. Limit is 15'"
99
- end
100
- options = if options_or_notify == true or options_or_notify == false
101
- warn "DEPRECATED: Specify notify flag as an option (e.g., :notify => true)"
102
- { :notify => options_or_notify }
103
- else
104
- options_or_notify || {}
105
- end
106
-
107
- options = { :color => 'yellow', :notify => false }.merge options
108
-
109
- response = self.class.post(@api.send_config[:url],
110
- :query => { :auth_token => @token },
111
- :body => {
112
- :room_id => room_id,
113
- :from => from,
114
- :message => message,
115
- :message_format => options[:message_format] || 'html',
116
- :color => options[:color],
117
- :notify => @api.bool_val(options[:notify])
118
- }.send(@api.send_config[:body_format]),
119
- :headers => @api.headers
120
- )
121
-
122
- case response.code
123
- when 200, 204; true
124
- when 404
125
- raise UnknownRoom, "Unknown room: `#{room_id}'"
126
- when 401
127
- raise Unauthorized, "Access denied to room `#{room_id}'"
128
- else
129
- raise UnknownResponseCode, "Unexpected #{response.code} for room `#{room_id}'"
130
- end
131
- end
132
-
133
- # Change this room's topic
134
- #
135
- # Usage:
136
- #
137
- # # Default
138
- # topic 'my awesome topic'
139
- #
140
- # Options:
141
- #
142
- # +from+:: the name of the person changing the topic
143
- # (default "API")
144
- def topic(new_topic, options = {})
145
-
146
- options = { :from => 'API' }.merge options
147
-
148
- response = self.class.send(@api.topic_config[:method], @api.topic_config[:url],
149
- :query => { :auth_token => @token },
150
- :body => {
151
- :room_id => room_id,
152
- :from => options[:from],
153
- :topic => new_topic
154
- }.send(@api.topic_config[:body_format]),
155
- :headers => @api.headers
156
- )
157
-
158
- case response.code
159
- when 204,200; true
160
- when 404
161
- raise UnknownRoom, "Unknown room: `#{room_id}'"
162
- when 401
163
- raise Unauthorized, "Access denied to room `#{room_id}'"
164
- else
165
- raise UnknownResponseCode, "Unexpected #{response.code} for room `#{room_id}'"
166
- end
167
- end
168
-
169
- # Pull this room's history
170
- #
171
- # Usage
172
- #
173
- # # Default
174
- #
175
- #
176
- # Options
177
- #
178
- # +date+:: Whether to return a specific day (YYYY-MM-DD format) or recent
179
- # (default "recent")
180
- # +timezone+:: Your timezone. Supported timezones are at: https://www.hipchat.com/docs/api/timezones
181
- # (default "UTC")
182
- # +format+:: Format to retrieve the history in. Valid options are JSON and XML
183
- # (default "JSON")
184
- def history(options = {})
185
-
186
- options = { :date => 'recent', :timezone => 'UTC', :format => 'JSON' }.merge options
187
-
188
- response = self.class.get(@api.history_config[:url],
189
- :query => {
190
- :room_id => room_id,
191
- :date => options[:date],
192
- :timezone => options[:timezone],
193
- :format => options[:format],
194
- :auth_token => @token,
195
- },
196
- :headers => @api.headers
197
- )
198
-
199
- case response.code
200
- when 200
201
- response.body
202
- when 404
203
- raise UnknownRoom, "Unknown room: `#{room_id}'"
204
- when 401
205
- raise Unauthorized, "Access denied to room `#{room_id}'"
206
- else
207
- raise UnknownResponseCode, "Unexpected #{response.code} for room `#{room_id}'"
208
- end
209
- end
210
- end
5
+ require 'hipchat/errors'
6
+ require 'hipchat/room'
7
+ require 'hipchat/client'
8
+ require 'hipchat/user'
211
9
  end
@@ -1,16 +1,26 @@
1
+ require 'uri'
2
+
1
3
  module HipChat
2
4
  class ApiVersion
3
5
 
4
- class Client
6
+ def bool_val(opt)
7
+ if version.eql?('v1')
8
+ opt ? 1 : 0
9
+ else
10
+ opt
11
+ end
12
+ end
13
+
14
+ class Client < ApiVersion
5
15
 
6
16
  def initialize(version = 'v1')
7
17
  @version = !version.nil? ? version : 'v1'
8
18
  if @version.eql?('v1')
9
- @base_uri = "https://api.hipchat.com/v1/rooms"
19
+ @base_uri = "https://api.hipchat.com/v1"
10
20
  @headers = {'Accept' => 'application/json',
11
21
  'Content-Type' => 'application/x-www-form-urlencoded'}
12
22
  else
13
- @base_uri = "https://api.hipchat.com/v2/room"
23
+ @base_uri = "https://api.hipchat.com/v2"
14
24
  @headers = {'Accept' => 'application/json',
15
25
  'Content-Type' => 'application/json'}
16
26
  end
@@ -21,7 +31,7 @@ module HipChat
21
31
  def rooms_config
22
32
  {
23
33
  'v1' => {
24
- :url => '/list',
34
+ :url => '/rooms/list',
25
35
  :data_key => 'rooms'
26
36
  },
27
37
  'v2' => {
@@ -30,9 +40,30 @@ module HipChat
30
40
  }
31
41
  }[version]
32
42
  end
43
+
44
+
45
+ def create_room_config
46
+ {
47
+ 'v1' => {
48
+ :url => '/rooms/create',
49
+ :body_format => :to_hash
50
+ },
51
+ 'v2' => {
52
+ :url => '/room',
53
+ :body_format => :to_json
54
+ }
55
+ }[version]
56
+ end
57
+
58
+ def users_config
59
+ {
60
+ :url => '/user',
61
+ :data_key => 'items'
62
+ }
63
+ end
33
64
  end
34
65
 
35
- class Room
66
+ class Room < ApiVersion
36
67
 
37
68
  def initialize(room_id, version = 'v1')
38
69
  @room_id = room_id
@@ -50,6 +81,23 @@ module HipChat
50
81
 
51
82
  attr_reader :version, :base_uri, :room_id, :headers
52
83
 
84
+ def get_room_config
85
+ {
86
+ 'v2' => {
87
+ :url => URI::escape("/#{room_id}")
88
+ }
89
+ }[version]
90
+ end
91
+
92
+ def invite_config
93
+ {
94
+ 'v2' => {
95
+ :url => URI::escape("/#{room_id}/invite"),
96
+ :body_format => :to_json
97
+ }
98
+ }[version]
99
+ end
100
+
53
101
  def send_config
54
102
  {
55
103
  'v1' => {
@@ -57,7 +105,7 @@ module HipChat
57
105
  :body_format => :to_hash
58
106
  },
59
107
  'v2' => {
60
- :url => "/#{room_id}/notification",
108
+ :url => URI::escape("/#{room_id}/notification"),
61
109
  :body_format => :to_json
62
110
  }
63
111
  }[version]
@@ -71,7 +119,7 @@ module HipChat
71
119
  :body_format => :to_hash
72
120
  },
73
121
  'v2' => {
74
- :url => "/#{room_id}/topic",
122
+ :url => URI::escape("/#{room_id}/topic"),
75
123
  :method => :put,
76
124
  :body_format => :to_json
77
125
  }
@@ -84,19 +132,37 @@ module HipChat
84
132
  :url => '/history'
85
133
  },
86
134
  'v2' => {
87
- :url => "/#{room_id}/history"
135
+ :url => URI::escape("/#{room_id}/history")
88
136
  }
89
137
  }[version]
90
138
  end
139
+ end
91
140
 
92
- def bool_val(opt)
93
- if version.eql?('v1')
94
- opt ? 1 : 0
95
- else
96
- opt
97
- end
141
+ class User
142
+
143
+ def initialize(user_id, version)
144
+ @user_id = user_id
145
+ raise InvalidApiVersion, "user API calls invalid for API v1" if ! version.eql?('v2')
146
+ @base_uri = "https://api.hipchat.com/v2/user"
147
+ @headers = {'Accept' => 'application/json',
148
+ 'Content-Type' => 'application/json'}
98
149
  end
99
150
 
151
+ attr_reader :version, :base_uri, :user_id, :headers
152
+
153
+ def send_config
154
+ {
155
+ :url => URI::escape("/#{user_id}/message"),
156
+ :body_format => :to_json
157
+ }
158
+ end
159
+
160
+ def view_config
161
+ {
162
+ :url => URI::escape("/#{user_id}"),
163
+ :body_format => :to_json
164
+ }
165
+ end
100
166
  end
101
167
  end
102
168
  end
@@ -39,6 +39,12 @@ Capistrano::Configuration.instance(:must_exist).load do
39
39
  environment_string = "#{stage} (#{env})"
40
40
  end
41
41
 
42
+ if fetch(:hipchat_commit_log, false)
43
+ logs = commit_logs
44
+ unless logs.empty?
45
+ send(logs.join("\n"), send_options)
46
+ end
47
+ end
42
48
  send("#{human} finished deploying #{deployment_name} to #{environment_string}#{fetch(:hipchat_with_migrations, '')}.", send_options)
43
49
  end
44
50
  end
@@ -123,9 +129,43 @@ Capistrano::Configuration.instance(:must_exist).load do
123
129
  end
124
130
  end
125
131
 
132
+ def commit_logs
133
+ from = (previous_revision rescue nil)
134
+ to = (latest_revision rescue nil)
135
+
136
+ log_hashes = []
137
+ case scm.to_s
138
+ when 'git'
139
+ logs = run_locally(source.local.scm(:log, "--no-merges --pretty=format:'%H$$%at$$%an$$%s' #{from}..#{to}"))
140
+ logs.split(/\n/).each do |log|
141
+ ll = log.split(/\$\$/)
142
+ log_hashes << {revision: ll[0], time: Time.at(ll[1].to_i), user: ll[2], message: ll[3]}
143
+ end
144
+ when 'svn'
145
+ logs = run_locally(source.local.scm(:log, "--non-interactive -r #{from}:#{to}"))
146
+ logs.scan(/^[-]+$\n\s*(?<revision>[^\|]+)\s+\|\s+(?<user>[^\|]+)\s+\|\s+(?<time>[^\|]+)\s+.*\n+^\s*(?<message>.*)\s*$\n/) do |m|
147
+ h = Regexp.last_match
148
+ log_hashes << {revision: h[:revision], time: Time.parse(h[:time]), user: h[:user], message: h[:message]}
149
+ end
150
+ else
151
+ puts "We haven't supported this scm yet."
152
+ return []
153
+ end
154
+
155
+ format = fetch(:hipchat_commit_log_format, ":time :user\n:message\n")
156
+ time_format = fetch(:hipchat_commit_log_time_format, "%Y/%m/%d %H:%M:%S")
157
+
158
+ log_hashes.map do |log_hash|
159
+ log_hash[:time] &&= log_hash[:time].localtime.strftime(time_format)
160
+ log_hash.inject(format) do |l, (k, v)|
161
+ l.gsub(/:#{k}/, v.to_s)
162
+ end
163
+ end
164
+ end
165
+
126
166
  before "deploy", "hipchat:trigger_notification"
127
167
  before "deploy:migrations", "hipchat:trigger_notification", "hipchat:configure_for_migrations"
128
168
  before "deploy:update_code", "hipchat:notify_deploy_started"
129
169
  after "deploy", "hipchat:notify_deploy_finished"
130
170
  after "deploy:migrations", "hipchat:notify_deploy_finished"
131
- end
171
+ end