Read about our upcoming Code of Conduct on this issue

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

Kamil Trzcinski's avatar
Kamil Trzcinski committed
3
class PipelinesFinder
4
  attr_reader :project, :pipelines, :params, :current_user
Kamil Trzcinski's avatar
Kamil Trzcinski committed
5

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

8
  def initialize(project, current_user, params = {})
Kamil Trzcinski's avatar
Kamil Trzcinski committed
9
    @project = project
10
    @current_user = current_user
11
    @pipelines = project.all_pipelines
12
    @params = params
Kamil Trzcinski's avatar
Kamil Trzcinski committed
13
14
  end

15
  def execute
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)
24
    items = by_sha(items)
25
    items = by_name(items)
Shinya Maeda's avatar
Shinya Maeda committed
26
27
    items = by_username(items)
    items = by_yaml_errors(items)
28
    sort_items(items)
Kamil Trzcinski's avatar
Kamil Trzcinski committed
29
30
31
32
  end

  private

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

39
  # rubocop: disable CodeReuse/ActiveRecord
40
  def from_ids(ids)
Kamil Trzcinski's avatar
Kamil Trzcinski committed
41
42
    pipelines.unscoped.where(id: ids)
  end
43
  # rubocop: enable CodeReuse/ActiveRecord
Kamil Trzcinski's avatar
Kamil Trzcinski committed
44
45

  def branches
46
    project.repository.branch_names
Kamil Trzcinski's avatar
Kamil Trzcinski committed
47
48
49
  end

  def tags
50
    project.repository.tag_names
Kamil Trzcinski's avatar
Kamil Trzcinski committed
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
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
97

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

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

118
  # rubocop: disable CodeReuse/ActiveRecord
Shinya Maeda's avatar
Shinya Maeda committed
119
  def by_yaml_errors(items)
Shinya Maeda's avatar
Shinya Maeda committed
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
Shinya Maeda's avatar
Shinya Maeda committed
138

139
140
141
142
143
    sort = if params[:sort] =~ /\A(ASC|DESC)\z/i
             params[:sort]
           else
             :desc
           end
Shinya Maeda's avatar
Shinya Maeda committed
144

145
    items.order(order_by => sort)
146
  end
147
  # rubocop: enable CodeReuse/ActiveRecord
Kamil Trzcinski's avatar
Kamil Trzcinski committed
148
end