<commit_before>require 'json'
require 'open-uri'
require 'slack/post'
require 'sucker_punch'

class CodeshipCheckerJob
  include SuckerPunch::Job

  def perform(settings, git_commit)
    @settings = settings
    @git_commit = git_commit

    attempted_build_finds = 0
    testing_notified = false
    waiting_notified = false
    loop do
      build = builds.find {|b| b['commit_id'] == @git_commit}
      unless build
        return if attempted_build_finds > (settings.codeship['attempted_build_finds'] || 5).to_i
        attempted_build_finds += 1
        sleep 3
        next
      end

      started_monitoring = Time.now.to_i
      if build['status'] == 'testing'
        notify_slack(build) unless testing_notified
        testing_notified = true
        sleep 10
      elsif build['status'] == 'waiting'
        notify_slack(build) unless waiting_notified
        waiting_notified = true
        sleep 10
      elsif ['success', 'error'].include?(build['status'])
        return notify_slack(build)
      end

      if Time.now.to_i - started_monitoring > (settings.codeship['wait_timeout'] || 1500).to_i
        return notify_slack({}, 'Wait timeout of 1500 seconds exceeded')
      end
    end
  end

  private

  def builds
    JSON.parse(open("https://codeship.com/api/v1/projects/#{@settings.codeship['project_id']}.json?api_key=#{@settings.codeship['api_key']}").read)['builds']
  end

  def build_message(build)
    build_url = "https://codeship.com/projects/#{@settings.codeship['project_id']}/builds/#{build['id']}"
    status_text = case build['status']
                  when 'testing'
                    'is pending'
                  when 'success'
                    'succeeded'
                  when 'error'
                    'FAILED'
                  when 'stopped'
                    'was stopped'
                  when 'waiting'
                    'is waiting to start'
                  when 'infrastructure_failure'
                    'FAILED due to a Codeship error'
                  when 'ignored'
                    'was ignored because the account is over the monthly build limit'
                  when 'blocked'
                    'was blocked because of excessive resource consumption'
                  else
                    'did something weird...'
                  end
    "<#{build_url}|#{build['branch']} build>#{build['github_username'] ? " by #{build['github_username']}" : ''} #{status_text}"
  end<commit_msg>Extract status_text to its own method.<commit_after>require 'json'
require 'open-uri'
require 'slack/post'
require 'sucker_punch'

class CodeshipCheckerJob
  include SuckerPunch::Job

  def perform(settings, git_commit)
    @settings = settings
    @git_commit = git_commit

    attempted_build_finds = 0
    testing_notified = false
    waiting_notified = false
    loop do
      build = builds.find {|b| b['commit_id'] == @git_commit}
      unless build
        return if attempted_build_finds > (settings.codeship['attempted_build_finds'] || 5).to_i
        attempted_build_finds += 1
        sleep 3
        next
      end

      started_monitoring = Time.now.to_i
      if build['status'] == 'testing'
        notify_slack(build) unless testing_notified
        testing_notified = true
        sleep 10
      elsif build['status'] == 'waiting'
        notify_slack(build) unless waiting_notified
        waiting_notified = true
        sleep 10
      elsif ['success', 'error'].include?(build['status'])
        return notify_slack(build)
      end

      if Time.now.to_i - started_monitoring > (settings.codeship['wait_timeout'] || 1500).to_i
        return notify_slack({}, 'Wait timeout of 1500 seconds exceeded')
      end
    end
  end

  private

  def builds
    JSON.parse(open("https://codeship.com/api/v1/projects/#{@settings.codeship['project_id']}.json?api_key=#{@settings.codeship['api_key']}").read)['builds']
  end

  def status_text(status)
    case status
    when 'testing'
      'is pending'
    when 'success'
      'succeeded'
    when 'error'
      'FAILED'
    when 'stopped'
      'was stopped'
    when 'waiting'
      'is waiting to start'
    when 'infrastructure_failure'
      'FAILED due to a Codeship error'
    when 'ignored'
      'was ignored because the account is over the monthly build limit'
    when 'blocked'
      'was blocked because of excessive resource consumption'
    else
      'did something weird...'
    end
  end

  def build_message(build)
    build_url = "https://codeship.com/projects/#{@settings.codeship['project_id']}/builds/#{build['id']}"
    username = build['github_username'] ? " by #{build['github_username']}" : ''
    "<#{build_url}|#{build['branch']} build>#{username} #{status_text(build['status'])}"
  end
{'fraction_non_alphanumeric': 0.11221052631578947, 'fraction_numerical': 0.007157894736842105, 'mean_word_length': 2.4831378299120233, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2333904', 'n_tokens_mistral': 1444, 'n_tokens_neox': 1394, 'n_words': 332}