pipelines_finder.rb 3.3 KB
Newer Older
1 2
# frozen_string_literal: true

K
Kamil Trzcinski 已提交
3
class PipelinesFinder
B
Bob Van Landuyt 已提交
4
  attr_reader :project, :pipelines, :params, :current_user
K
Kamil Trzcinski 已提交
5

6 7
  ALLOWED_INDEXED_COLUMNS = %w[id status ref user_id].freeze

B
Bob Van Landuyt 已提交
8
  def initialize(project, current_user, params = {})
K
Kamil Trzcinski 已提交
9
    @project = project
B
Bob Van Landuyt 已提交
10
    @current_user = current_user
11
    @pipelines = project.pipelines
12
    @params = params
K
Kamil Trzcinski 已提交
13 14
  end

15
  def execute
B
Bob Van Landuyt 已提交
16 17 18 19
    unless Ability.allowed?(current_user, :read_pipeline, project)
      return Ci::Pipeline.none
    end

20 21 22 23
    items = pipelines
    items = by_scope(items)
    items = by_status(items)
    items = by_ref(items)
J
James Ramsay 已提交
24
    items = by_sha(items)
S
Shinya Maeda 已提交
25
    items = by_name(items)
S
Shinya Maeda 已提交
26 27
    items = by_username(items)
    items = by_yaml_errors(items)
28
    sort_items(items)
K
Kamil Trzcinski 已提交
29 30 31 32
  end

  private

33
  # rubocop: disable CodeReuse/ActiveRecord
34
  def ids_for_ref(refs)
K
Kamil Trzcinski 已提交
35 36
    pipelines.where(ref: refs).group(:ref).select('max(id)')
  end
37
  # rubocop: enable CodeReuse/ActiveRecord
K
Kamil Trzcinski 已提交
38

39
  # rubocop: disable CodeReuse/ActiveRecord
40
  def from_ids(ids)
K
Kamil Trzcinski 已提交
41 42
    pipelines.unscoped.where(id: ids)
  end
43
  # rubocop: enable CodeReuse/ActiveRecord
K
Kamil Trzcinski 已提交
44 45

  def branches
46
    project.repository.branch_names
K
Kamil Trzcinski 已提交
47 48 49
  end

  def tags
50
    project.repository.tag_names
K
Kamil Trzcinski 已提交
51
  end
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69

  def by_scope(items)
    case params[:scope]
    when 'running'
      items.running
    when 'pending'
      items.pending
    when 'finished'
      items.finished
    when 'branches'
      from_ids(ids_for_ref(branches))
    when 'tags'
      from_ids(ids_for_ref(tags))
    else
      items
    end
  end

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

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

78
  # rubocop: disable CodeReuse/ActiveRecord
79 80 81 82 83 84 85
  def by_ref(items)
    if params[:ref].present?
      items.where(ref: params[:ref])
    else
      items
    end
  end
86
  # rubocop: enable CodeReuse/ActiveRecord
87

88
  # rubocop: disable CodeReuse/ActiveRecord
J
James Ramsay 已提交
89 90 91 92 93 94 95
  def by_sha(items)
    if params[:sha].present?
      items.where(sha: params[:sha])
    else
      items
    end
  end
96
  # rubocop: enable CodeReuse/ActiveRecord
J
James Ramsay 已提交
97

98
  # rubocop: disable CodeReuse/ActiveRecord
S
Shinya Maeda 已提交
99 100
  def by_name(items)
    if params[:name].present?
S
Shinya Maeda 已提交
101
      items.joins(:user).where(users: { name: params[:name] })
S
Shinya Maeda 已提交
102 103 104 105
    else
      items
    end
  end
106
  # rubocop: enable CodeReuse/ActiveRecord
S
Shinya Maeda 已提交
107

108
  # rubocop: disable CodeReuse/ActiveRecord
S
Shinya Maeda 已提交
109 110
  def by_username(items)
    if params[:username].present?
S
Shinya Maeda 已提交
111
      items.joins(:user).where(users: { username: params[:username] })
112 113 114 115
    else
      items
    end
  end
116
  # rubocop: enable CodeReuse/ActiveRecord
S
Shinya Maeda 已提交
117

118
  # rubocop: disable CodeReuse/ActiveRecord
S
Shinya Maeda 已提交
119
  def by_yaml_errors(items)
S
Shinya Maeda 已提交
120 121 122 123 124
    case Gitlab::Utils.to_boolean(params[:yaml_errors])
    when true
      items.where("yaml_errors IS NOT NULL")
    when false
      items.where("yaml_errors IS NULL")
125 126 127 128
    else
      items
    end
  end
129
  # rubocop: enable CodeReuse/ActiveRecord
130

131
  # rubocop: disable CodeReuse/ActiveRecord
132
  def sort_items(items)
133
    order_by = if ALLOWED_INDEXED_COLUMNS.include?(params[:order_by])
134 135 136 137
                 params[:order_by]
               else
                 :id
               end
S
Shinya Maeda 已提交
138

139 140 141 142 143
    sort = if params[:sort] =~ /\A(ASC|DESC)\z/i
             params[:sort]
           else
             :desc
           end
S
Shinya Maeda 已提交
144

145
    items.order(order_by => sort)
146
  end
147
  # rubocop: enable CodeReuse/ActiveRecord
K
Kamil Trzcinski 已提交
148
end