<commit_before>class TravisBuildKiller
  include Sidekiq::Worker
  include Sidetiq::Schedulable
  include MiqToolsServices::SidekiqWorkerMixin
  sidekiq_options :queue => :miq_bot, :retry => false

  recurrence { hourly.minute_of_hour(0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55) }

  def perform
    if !first_unique_worker?
      logger.info "#{self.class} is already running, skipping"
    else
      process_repo
    end
  end

  private

  attr_accessor :repo

  def process_repo
    @repo = Repo.where(:name => "ManageIQ/manageiq").first
    if @repo.nil?
      logger.info "The ManageIQ/manageiq repo has not been defined.  Skipping."
      return
    end

    kill_builds
  end

  def kill_builds
    repo.with_travis_service do |travis|
      builds_to_cancel = travis.builds
        .take_while { |b| b.pending? || b.canceled? }
        .reject(&:canceled?)
        .group_by(&:pull_request_number)
        .flat_map { |_pr_number, builds| builds[1..-1] }

      builds_to_cancel.each do |b|
        if b.pull_request?
          logger.info "Canceling Travis build ##{b.number} for PR ##{b.pull_request_number}"
          b.cancel
        elsif b.running?
          logger.info "Skipping currently running Travis build ##{b.number} for merge commit"
        else
          logger.info "Canceling Travis build ##{b.number} for merge commit"
          b.cancel
        end
      end
    end
  end
end<commit_msg>Fix issue where TravisBuildKiller grouped all non-PRs together.<commit_after>class TravisBuildKiller
  include Sidekiq::Worker
  include Sidetiq::Schedulable
  include MiqToolsServices::SidekiqWorkerMixin
  sidekiq_options :queue => :miq_bot, :retry => false

  recurrence { hourly.minute_of_hour(0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55) }

  def perform
    if !first_unique_worker?
      logger.info "#{self.class} is already running, skipping"
    else
      process_repo
    end
  end

  private

  attr_accessor :repo

  def process_repo
    @repo = Repo.where(:name => "ManageIQ/manageiq").first
    if @repo.nil?
      logger.info "The ManageIQ/manageiq repo has not been defined.  Skipping."
      return
    end

    kill_builds
  end

  def kill_builds
    repo.with_travis_service do |travis|
      builds_to_cancel = travis.builds
        .take_while { |b| b.pending? || b.canceled? }
        .reject(&:canceled?)
        .group_by { |b| b.pull_request_number || b.branch_info }
        .flat_map { |_key, builds| builds[1..-1] }

      builds_to_cancel.each do |b|
        if b.pull_request?
          logger.info "Canceling Travis build ##{b.number} for PR ##{b.pull_request_number}"
          b.cancel
        elsif b.running?
          logger.info "Skipping currently running Travis build ##{b.number} for merge commit"
        else
          logger.info "Canceling Travis build ##{b.number} for merge commit"
          b.cancel
        end
      end
    end
  end
end
{'fraction_non_alphanumeric': 0.09896907216494845, 'fraction_numerical': 0.016494845360824743, 'mean_word_length': 2.620646766169154, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3932642', 'n_tokens_mistral': 1024, 'n_tokens_neox': 967, 'n_words': 251}