bin/shamebot in shamebot-1.0.2 vs bin/shamebot in shamebot-1.0.3

- old
+ new

@@ -96,93 +96,97 @@ # Part 3. Take requests from Gitlab WebHooks -require 'sinatra' +require 'sinatra/base' -set :port, Opts[:port] -set :environment, Opts[:env] -set :bind, Opts[:bind] -set :raise_errors, true -set :dump_errors, true -set :show_exceptions, true -set :logging, ::Logger::DEBUG if DEBUG +class Shamebot::App < Sinatra::Application + set :port, Opts[:port] + set :environment, Opts[:env] + set :bind, Opts[:bind] + set :raise_errors, true + set :dump_errors, true + set :show_exceptions, true + set :logging, ::Logger::DEBUG if DEBUG -get '/' do - content_type :text - "shamebot #{Shamebot::VERSION}" -end + get '/' do + content_type :text + "shamebot #{Shamebot::VERSION}" + end -post '/' do - now = Time.now - last_half_hour = now - 30 * 60 - request.body.rewind - data = JSON.parse request.body.read + post '/' do + now = Time.now + last_half_hour = now - 30 * 60 + request.body.rewind + data = JSON.parse request.body.read - # Bad commits don't contain JIRA tags... - # And they aren't merge commits - commits = data['commits'].delete_if { |c| c['message'] =~ MERGE_COMMIT } - good_commits, bad_commits = commits.partition { |c| c['message'] =~ JIRA_TAG } + # Bad commits don't contain JIRA tags... + # And they aren't merge commits + commits = data['commits'].delete_if { |c| c['message'] =~ MERGE_COMMIT } + good_commits, bad_commits = commits.partition { |c| c['message'] =~ JIRA_TAG } - # Good commits might contain bogus JIRA tags - bad_commits += good_commits.keep_if do |c| - good_tag = false - c['message'].scan(JIRA_TAG).each do |(project, number)| - tag = '%s-%d' % [project.upcase, number.to_i] - tag_page = `curl --silent http://jira.bluejeansnet.com/browse/#{tag}` rescue '' - next if tag_page =~ /The issue you are trying to view does not exist/i - good_tag = true - break + # Good commits might contain bogus JIRA tags + bad_commits += good_commits.keep_if do |c| + good_tag = false + c['message'].scan(JIRA_TAG).each do |(project, number)| + tag = '%s-%d' % [project.upcase, number.to_i] + tag_page = `curl --silent http://jira.bluejeansnet.com/browse/#{tag}` rescue '' + next if tag_page =~ /The issue you are trying to view does not exist/i + good_tag = true + break + end + !good_tag end - !good_tag - end - # Grab user info from Gitlab - begin - user = Gitlab.user(data['user_id'].to_i) - rescue - $stderr.puts "Warning: Gitlab user for commit author not found" - return - end + # Grab user info from Gitlab + begin + user = Gitlab.user(data['user_id'].to_i) + rescue + $stderr.puts "Warning: Gitlab user for commit author not found" + return + end - # For whatever reason we get a lot of duplicate POSTs, so we need to - # keep track of which commits have already triggered shamings so we - # don't end up repeatedly shaming users for the same mistake. We also - # make sure the commits aren't too old. - new_bad_commits = [] - bad_commits.each do |commit| - unless DEBUG - next if DB.has_key? commit['id'] - commit_time = DateTime.strptime(commit['timestamp']).to_time - next unless commit_time >= last_half_hour + # For whatever reason we get a lot of duplicate POSTs, so we need to + # keep track of which commits have already triggered shamings so we + # don't end up repeatedly shaming users for the same mistake. We also + # make sure the commits aren't too old. + new_bad_commits = [] + bad_commits.each do |commit| + unless DEBUG + next if DB.has_key? commit['id'] + commit_time = DateTime.strptime(commit['timestamp']).to_time + next unless commit_time >= last_half_hour + end + new_bad_commits << commit + DB[commit['id']] = true end - new_bad_commits << commit - DB[commit['id']] = true - end - # Shame the user with a random template - commit_ids = new_bad_commits.map { |c| c['id'] } - commit_urls = new_bad_commits.map { |c| c['url'] } - puts "%s: %s" % [ user.username, commit_ids.inspect ] + # Shame the user with a random template + commit_ids = new_bad_commits.map { |c| c['id'] } + commit_urls = new_bad_commits.map { |c| c['url'] } + puts "%s: %s" % [ user.username, commit_ids.inspect ] - hipchat_users = [] - 0.upto(5) do |i| - hipchat_users += HTTParty.get("https://api.hipchat.com/v2/user", :query => { 'start-index' => 100 * i, :start_index => 100 * i, 'max-results' => 100, :max_results => 1000, :auth_token => CONFIG['hipchat']['auth_token_v2'] })['items'] - end + hipchat_users = [] + 0.upto(5) do |i| + hipchat_users += HTTParty.get("https://api.hipchat.com/v2/user", :query => { 'start-index' => 100 * i, :start_index => 100 * i, 'max-results' => 100, :max_results => 1000, :auth_token => CONFIG['hipchat']['auth_token_v2'] })['items'] + end - hipchat_user = hipchat_users.select { |u| u['name'] =~ /#{user.name}/i } - $stderr.puts hipchat_user + hipchat_user = hipchat_users.select { |u| u['name'] =~ /#{user.name}/i } + $stderr.puts hipchat_user - if hipchat_user.empty? - hipchat_user = user.name - else - hipchat_user = '@' + hipchat_user.first['mention_name'] - end + if hipchat_user.empty? + hipchat_user = user.name + else + hipchat_user = '@' + hipchat_user.first['mention_name'] + end - shame_user(random_template, { - :name => hipchat_user, - :nick => user.username, - :urls => commit_urls.join(' ') - }, ROOM, HIPCHAT, BOTNAME) unless commit_ids.empty? || DEBUG - return commit_ids.inspect + shame_user(random_template, { + :name => hipchat_user, + :nick => user.username, + :urls => commit_urls.join(' ') + }, ROOM, HIPCHAT, BOTNAME) unless commit_ids.empty? || DEBUG + return commit_ids.inspect + end end + +Shamebot::App.run! \ No newline at end of file