<commit_before>require 'core_ext/active_record/none_scope'
require 'travis/services/base'

# v2 builds.all
#   build => commit, request, matrix.id

module Travis
  module Services
    class FindBuilds < Base
      register :find_builds

      scope_access!

      def run
        preload(result)
      end

      private

        def result
          @result ||= params[:ids] ? by_ids : by_params
        end

        def by_ids
          scope(:build).where(:id => params[:ids])
        end

        def by_params
          # params now include the :access_token, which makes the `recent` case
          # not match. not sure why that is.
          params = params.to_h.reject { |key, _| key == :access_token }.symbolize_keys

          if repo
            # TODO :after_number seems like a bizarre api why not just pass an id? pagination style?
            builds = repo.builds
            builds = builds.by_event_type(params[:event_type]) if params[:event_type]
            if params[:number]
              builds.where(:number => params[:number].to_s)
            else
              builds.older_than(params[:after_number])
            end
          elsif params[:running]
            scope(:build).running.limit(25)
          elsif params.nil? || params == {}
            scope(:build).recent
          else
            scope(:build).none
          end
        end

        def preload(builds)
          builds.includes(:commit, :matrix)
        end

        def repo
          @repo ||= run_service(:find_repo, params)
        end
    end
  end
end<commit_msg>fix failing find_builds service<commit_after>require 'core_ext/active_record/none_scope'
require 'travis/services/base'

# v2 builds.all
#   build => commit, request, matrix.id

module Travis
  module Services
    class FindBuilds < Base
      register :find_builds

      scope_access!

      def run
        preload(result)
      end

      private

        def result
          @result ||= params[:ids] ? by_ids : by_params
        end

        def by_ids
          scope(:build).where(:id => params[:ids])
        end

        def by_params
          if repo
            # TODO :after_number seems like a bizarre api why not just pass an id? pagination style?
            builds = repo.builds
            builds = builds.by_event_type(params[:event_type]) if params[:event_type]
            if params[:number]
              builds.where(:number => params[:number].to_s)
            else
              builds.older_than(params[:after_number])
            end
          elsif params[:running]
            scope(:build).running.limit(25)
          elsif params.nil? || params == {} || params.keys.map(&:to_s) == ['access_token']
            scope(:build).recent
          else
            scope(:build).none
          end
        end

        def preload(builds)
          builds.includes(:commit, :matrix)
        end

        def repo
          @repo ||= run_service(:find_repo, params)
        end
    end
  end
end
{'fraction_non_alphanumeric': 0.09310113864701942, 'fraction_numerical': 0.0020093770931011385, 'mean_word_length': 1.804694835680751, 'pattern_counts': {'":': 0, '<': 5, '<?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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3378923', 'n_tokens_mistral': 843, 'n_tokens_neox': 828, 'n_words': 215}