lib/bender/main.rb in bender-bot-0.3.0 vs lib/bender/main.rb in bender-bot-0.4.0

- old
+ new

@@ -109,10 +109,15 @@ option :jira_project, \ type: :string, aliases: %w[ -J ], desc: 'Set JIRA project', required: true + option :jira_group, \ + type: :string, + aliases: %w[ -G ], + desc: 'Set JIRA group for write mode', + required: true option :jira_type, \ type: :string, aliases: %w[ -T ], desc: 'Set JIRA issue type', required: true @@ -124,13 +129,24 @@ option :issue_refresh, \ type: :numeric, aliases: %w[ -S ], desc: 'Set JIRA issue refresh rate', default: 5 + option :group_refresh, \ + type: :numeric, + aliases: %w[ -T ], + desc: 'Set JIRA group refresh rate', + default: 60 + option :order, \ + type: :boolean, + aliases: %w[ -O ], + desc: 'Reverse order of HipChat API loading (hack)', + default: false include_common_options def start bot = start_bot + periodically_refresh_group bot periodically_refresh_users bot periodically_refresh_incidents bot serve_web bot end @@ -166,11 +182,11 @@ end @room_name ||= bot.store['primary_room_name'] || new_room['name'] @room_topic ||= bot.store['primary_room_topic'] || new_room['topic'] - @open = false unless defined? @open + @open = 0 unless defined? @open log.info \ primary_room_name: bot.store['primary_room_name'], primary_room_topic: bot.store['primary_room_topic'], new_room_name: new_room['name'], @@ -184,46 +200,56 @@ new_room['name'] = @room_name new_room['topic'] = @room_topic room.update_room(new_room) end - @open = false + @open = 0 else - unless @open + if @open.zero? @room_name = new_room['name'] @room_topic = new_room['topic'] - bot.store['primary_room_name'] = @room_name - bot.store['primary_room_topic'] = @room_topic + end + + unless @open == open_incidents.size new_room['name'] = 'DANGER WILL ROBINSON' new_room['topic'] = '%d open incicents!' % open_incidents.size room.update_room(new_room) end - @open = true + @open = open_incidents.size end + + bot.store['primary_room_name'] = @room_name + bot.store['primary_room_topic'] = @room_topic end def periodically_refresh_incidents bot Thread.new do - hipchat_v2 = HipChat::Client.new \ - options.hipchat_v2_token, api_version: 'v2' + if options.order + hipchat_v1 = HipChat::Client.new \ + options.hipchat_token, api_version: 'v1' + hipchat_v2 = HipChat::Client.new \ + options.hipchat_v2_token, api_version: 'v2' + else + hipchat_v2 = HipChat::Client.new \ + options.hipchat_v2_token, api_version: 'v2' + hipchat_v1 = HipChat::Client.new \ + options.hipchat_token, api_version: 'v1' + end - hipchat_v1 = HipChat::Client.new \ - options.hipchat_token, api_version: 'v1' - room_id = options.primary_room_id loop do is = refresh_incidents bot - begin + # begin set_room_name_and_topic room_id, is, hipchat_v2, bot sleep options.issue_refresh - rescue NoMethodError - sleep 1 - end + # rescue NoMethodError + # sleep 1 + # end end end end @@ -243,20 +269,47 @@ req['Accept'] = 'application/json' Thread.new do loop do resp = http.request req - users = JSON.parse(resp.body).inject({}) do |h, user| + data = JSON.parse(resp.body) + users = data.inject({}) do |h, user| h[user['key']] = { nick: user['key'], name: user['displayName'], email: user['emailAddress'] } ; h end bot.store['users'] = users sleep options.user_refresh + end + end + end + + + def periodically_refresh_group bot + req_path = '/rest/api/2/group' + req_params = QueryParams.encode \ + groupname: options.jira_group, + expand: 'users' + + uri = URI(options.jira_site + req_path + '?' + req_params) + http = Net::HTTP.new uri.hostname, uri.port + + req = Net::HTTP::Get.new uri + req.basic_auth options.jira_user, options.jira_pass + req['Content-Type'] = 'application/json' + req['Accept'] = 'application/json' + + Thread.new do + loop do + resp = http.request req + data = JSON.parse(resp.body) + user_names = data['users']['items'].map { |u| u['displayName'] } + bot.store['group'] = user_names + sleep options.group_refresh end end end