Welcome to mirror list, hosted at ThFree Co, Russian Federation.

runner_jobs_finder.rb « ci « finders « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: ffcdb407e7e6a583ebe3276c6be9cd93a08dfe03 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# frozen_string_literal: true

module Ci
  class RunnerJobsFinder
    attr_reader :runner, :params

    ALLOWED_INDEXED_COLUMNS = %w[id].freeze

    def initialize(runner, params = {})
      @runner = runner
      @params = params
    end

    def execute
      items = @runner.builds
      items = by_status(items)
      sort_items(items)
    end

    private

    # rubocop: disable CodeReuse/ActiveRecord
    def by_status(items)
      return items unless HasStatus::AVAILABLE_STATUSES.include?(params[:status])

      items.where(status: params[:status])
    end
    # rubocop: enable CodeReuse/ActiveRecord

    # rubocop: disable CodeReuse/ActiveRecord
    def sort_items(items)
      return items unless ALLOWED_INDEXED_COLUMNS.include?(params[:order_by])

      order_by = params[:order_by]
      sort = if /\A(ASC|DESC)\z/i.match?(params[:sort])
               params[:sort]
             else
               :desc
             end

      items.order(order_by => sort)
    end
    # rubocop: enable CodeReuse/ActiveRecord
  end
end