bender-bot 0.3.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. checksums.yaml +4 -4
  2. data/VERSION +1 -1
  3. data/lib/bender/bot.rb +38 -20
  4. data/lib/bender/main.rb +69 -16
  5. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 50e516ad8b6f747483302fc79ab811a8cccf2368
4
- data.tar.gz: bd931995c92e814c96bbcea6f305534c2941cede
3
+ metadata.gz: 39a23282811b3e535eed1c40660ab4d3038c2759
4
+ data.tar.gz: 1c4d67b9740cfde6671ea41697cc17e745ca50c7
5
5
  SHA512:
6
- metadata.gz: b43003dffdf9a21bb11877116e287e04b80a84618a3d2743b7a83a030025d7c0303d6842a5d478c048c49680e9a0955576de7497ae0275e08baa26540701abe5
7
- data.tar.gz: 23de30730b6a02d26c1e71603966bb6811ef7ef4a7037c978f4dc69e75e6d3afe4abccafdd8d4eae6c9afd0d40eae674d3a47f470e52a8cffc56d14e1d9c07e9
6
+ metadata.gz: 4d9e6af21a110702f2f33ab2c872562657569e152042e5bc4d9fc88b0d5eda00bead8ddc636f0af6be23f3001909ab593b4437eb4e069800b7a3fd1acece899e
7
+ data.tar.gz: 733651b2f64a53e739ae94bb9a4623dbc420ded8f92350b8564a118c27bb8e12f6dc32bfc6bafb71ad6f3fe1617c0b42b1462b41833e06baac385443a0c1645f
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.0
1
+ 0.4.0
data/lib/bender/bot.rb CHANGED
@@ -221,38 +221,50 @@ class BenderBot
221
221
 
222
222
  # /inc resolve NUM - Resolve an incident
223
223
  when /^\s*\/inc\s+resolve\s+(\d+)\s*$/
224
- incident = select_incident $1
225
- if incident
226
- reply_html *resolve_incident(incident)
224
+ unless allowed?
225
+ reply_html "You're not allowed to do that!", :red
227
226
  else
228
- reply_html 'Sorry, no such incident!', :red
227
+ incident = select_incident $1
228
+ if incident
229
+ reply_html *resolve_incident(incident)
230
+ else
231
+ reply_html 'Sorry, no such incident!', :red
232
+ end
229
233
  end
230
234
 
231
235
  # /inc close NUM - Close an incident
232
236
  when /^\s*\/inc\s+close\s+(\d+)\s*$/
233
- incident = select_incident $1
234
- if incident
235
- reply_html *close_incident(incident)
237
+ unless allowed?
238
+ reply_html "You're not allowed to do that!", :red
236
239
  else
237
- reply_html 'Sorry, no such incident!', :red
240
+ incident = select_incident $1
241
+ if incident
242
+ reply_html *close_incident(incident)
243
+ else
244
+ reply_html 'Sorry, no such incident!', :red
245
+ end
238
246
  end
239
247
 
240
248
  # /inc open SEVERITY SUMMARY - File a new incident
241
249
  when /^\s*\/inc\s+open\s+(severity|sev|s|p)?(\d+)\s+(.*?)\s*$/i
242
- user = user_where name: sender
243
- data = {
244
- fields: {
245
- project: { key: options.jira_project },
246
- issuetype: { name: options.jira_type },
247
- reporter: { name: user[:nick] },
248
- summary: $3,
249
- SHOW_FIELDS.key('Severity') => {
250
- id: SEVERITIES[$2.to_i]
250
+ unless allowed?
251
+ reply_html "You're not allowed to do that!", :red
252
+ else
253
+ user = user_where name: sender
254
+ data = {
255
+ fields: {
256
+ project: { key: options.jira_project },
257
+ issuetype: { name: options.jira_type },
258
+ reporter: { name: user[:nick] },
259
+ summary: $3,
260
+ SHOW_FIELDS.key('Severity') => {
261
+ id: SEVERITIES[$2.to_i]
262
+ }
251
263
  }
252
264
  }
253
- }
254
265
 
255
- reply_html *file_incident(data)
266
+ reply_html *file_incident(data)
267
+ end
256
268
 
257
269
 
258
270
  # /inc comment [INCIDENT_NUMBER] [COMMENT_TEXT]
@@ -432,4 +444,10 @@ private
432
444
  end
433
445
  end
434
446
 
435
- end
447
+
448
+ def allowed?
449
+ user = user_where name: @sender
450
+ store['group'].include? user[:name]
451
+ end
452
+
453
+ end
data/lib/bender/main.rb CHANGED
@@ -111,6 +111,11 @@ module Bender
111
111
  aliases: %w[ -J ],
112
112
  desc: 'Set JIRA project',
113
113
  required: true
114
+ option :jira_group, \
115
+ type: :string,
116
+ aliases: %w[ -G ],
117
+ desc: 'Set JIRA group for write mode',
118
+ required: true
114
119
  option :jira_type, \
115
120
  type: :string,
116
121
  aliases: %w[ -T ],
@@ -126,9 +131,20 @@ module Bender
126
131
  aliases: %w[ -S ],
127
132
  desc: 'Set JIRA issue refresh rate',
128
133
  default: 5
134
+ option :group_refresh, \
135
+ type: :numeric,
136
+ aliases: %w[ -T ],
137
+ desc: 'Set JIRA group refresh rate',
138
+ default: 60
139
+ option :order, \
140
+ type: :boolean,
141
+ aliases: %w[ -O ],
142
+ desc: 'Reverse order of HipChat API loading (hack)',
143
+ default: false
129
144
  include_common_options
130
145
  def start
131
146
  bot = start_bot
147
+ periodically_refresh_group bot
132
148
  periodically_refresh_users bot
133
149
  periodically_refresh_incidents bot
134
150
  serve_web bot
@@ -168,7 +184,7 @@ module Bender
168
184
  @room_name ||= bot.store['primary_room_name'] || new_room['name']
169
185
  @room_topic ||= bot.store['primary_room_topic'] || new_room['topic']
170
186
 
171
- @open = false unless defined? @open
187
+ @open = 0 unless defined? @open
172
188
 
173
189
  log.info \
174
190
  primary_room_name: bot.store['primary_room_name'],
@@ -186,42 +202,52 @@ module Bender
186
202
  room.update_room(new_room)
187
203
  end
188
204
 
189
- @open = false
205
+ @open = 0
190
206
 
191
207
  else
192
- unless @open
208
+ if @open.zero?
193
209
  @room_name = new_room['name']
194
210
  @room_topic = new_room['topic']
195
- bot.store['primary_room_name'] = @room_name
196
- bot.store['primary_room_topic'] = @room_topic
211
+ end
212
+
213
+ unless @open == open_incidents.size
197
214
  new_room['name'] = 'DANGER WILL ROBINSON'
198
215
  new_room['topic'] = '%d open incicents!' % open_incidents.size
199
216
  room.update_room(new_room)
200
217
  end
201
218
 
202
- @open = true
219
+ @open = open_incidents.size
203
220
  end
221
+
222
+ bot.store['primary_room_name'] = @room_name
223
+ bot.store['primary_room_topic'] = @room_topic
204
224
  end
205
225
 
206
226
 
207
227
  def periodically_refresh_incidents bot
208
228
  Thread.new do
209
- hipchat_v2 = HipChat::Client.new \
210
- options.hipchat_v2_token, api_version: 'v2'
211
-
212
- hipchat_v1 = HipChat::Client.new \
213
- options.hipchat_token, api_version: 'v1'
229
+ if options.order
230
+ hipchat_v1 = HipChat::Client.new \
231
+ options.hipchat_token, api_version: 'v1'
232
+ hipchat_v2 = HipChat::Client.new \
233
+ options.hipchat_v2_token, api_version: 'v2'
234
+ else
235
+ hipchat_v2 = HipChat::Client.new \
236
+ options.hipchat_v2_token, api_version: 'v2'
237
+ hipchat_v1 = HipChat::Client.new \
238
+ options.hipchat_token, api_version: 'v1'
239
+ end
214
240
 
215
241
  room_id = options.primary_room_id
216
242
 
217
243
  loop do
218
244
  is = refresh_incidents bot
219
- begin
245
+ # begin
220
246
  set_room_name_and_topic room_id, is, hipchat_v2, bot
221
247
  sleep options.issue_refresh
222
- rescue NoMethodError
223
- sleep 1
224
- end
248
+ # rescue NoMethodError
249
+ # sleep 1
250
+ # end
225
251
  end
226
252
  end
227
253
  end
@@ -245,7 +271,8 @@ module Bender
245
271
  Thread.new do
246
272
  loop do
247
273
  resp = http.request req
248
- users = JSON.parse(resp.body).inject({}) do |h, user|
274
+ data = JSON.parse(resp.body)
275
+ users = data.inject({}) do |h, user|
249
276
  h[user['key']] = {
250
277
  nick: user['key'],
251
278
  name: user['displayName'],
@@ -261,6 +288,32 @@ module Bender
261
288
  end
262
289
 
263
290
 
291
+ def periodically_refresh_group bot
292
+ req_path = '/rest/api/2/group'
293
+ req_params = QueryParams.encode \
294
+ groupname: options.jira_group,
295
+ expand: 'users'
296
+
297
+ uri = URI(options.jira_site + req_path + '?' + req_params)
298
+ http = Net::HTTP.new uri.hostname, uri.port
299
+
300
+ req = Net::HTTP::Get.new uri
301
+ req.basic_auth options.jira_user, options.jira_pass
302
+ req['Content-Type'] = 'application/json'
303
+ req['Accept'] = 'application/json'
304
+
305
+ Thread.new do
306
+ loop do
307
+ resp = http.request req
308
+ data = JSON.parse(resp.body)
309
+ user_names = data['users']['items'].map { |u| u['displayName'] }
310
+ bot.store['group'] = user_names
311
+ sleep options.group_refresh
312
+ end
313
+ end
314
+ end
315
+
316
+
264
317
  def serve_web bot
265
318
  Web.set :environment, options.environment
266
319
  Web.set :port, options.port
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bender-bot
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sean Clemmer