2018-09-11 15:08:34 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-05-09 19:26:13 -04:00
|
|
|
class PipelinesFinder
|
2018-06-26 12:31:05 -04:00
|
|
|
attr_reader :project, :pipelines, :params, :current_user
|
2016-05-09 19:26:13 -04:00
|
|
|
|
2019-11-22 13:06:00 -05:00
|
|
|
ALLOWED_INDEXED_COLUMNS = %w[id status ref updated_at user_id].freeze
|
2017-03-30 06:30:02 -04:00
|
|
|
|
2018-06-26 12:31:05 -04:00
|
|
|
def initialize(project, current_user, params = {})
|
2016-05-09 19:26:13 -04:00
|
|
|
@project = project
|
2018-06-26 12:31:05 -04:00
|
|
|
@current_user = current_user
|
2018-12-05 09:39:15 -05:00
|
|
|
@pipelines = project.all_pipelines
|
2017-02-21 03:20:50 -05:00
|
|
|
@params = params
|
2016-05-09 19:26:13 -04:00
|
|
|
end
|
|
|
|
|
2017-02-21 03:20:50 -05:00
|
|
|
def execute
|
2018-06-26 12:31:05 -04:00
|
|
|
unless Ability.allowed?(current_user, :read_pipeline, project)
|
|
|
|
return Ci::Pipeline.none
|
|
|
|
end
|
|
|
|
|
2020-01-13 07:08:04 -05:00
|
|
|
items = pipelines.no_child
|
2017-02-21 03:20:50 -05:00
|
|
|
items = by_scope(items)
|
|
|
|
items = by_status(items)
|
|
|
|
items = by_ref(items)
|
2018-04-01 17:16:35 -04:00
|
|
|
items = by_sha(items)
|
2017-03-08 07:05:33 -05:00
|
|
|
items = by_name(items)
|
2017-02-28 07:24:49 -05:00
|
|
|
items = by_username(items)
|
|
|
|
items = by_yaml_errors(items)
|
2019-12-06 13:07:44 -05:00
|
|
|
items = by_updated_at(items)
|
2017-03-17 09:34:38 -04:00
|
|
|
sort_items(items)
|
2016-05-09 19:26:13 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2016-08-29 12:02:08 -04:00
|
|
|
def ids_for_ref(refs)
|
2016-05-09 19:26:13 -04:00
|
|
|
pipelines.where(ref: refs).group(:ref).select('max(id)')
|
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2016-05-09 19:26:13 -04:00
|
|
|
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2016-08-29 12:02:08 -04:00
|
|
|
def from_ids(ids)
|
2020-02-03 10:08:45 -05:00
|
|
|
pipelines.unscoped.where(project_id: project.id, id: ids)
|
2016-05-09 19:26:13 -04:00
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2016-05-09 19:26:13 -04:00
|
|
|
|
|
|
|
def branches
|
2016-06-29 01:10:18 -04:00
|
|
|
project.repository.branch_names
|
2016-05-09 19:26:13 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def tags
|
2016-06-29 01:10:18 -04:00
|
|
|
project.repository.tag_names
|
2016-05-09 19:26:13 -04:00
|
|
|
end
|
2017-02-21 03:20:50 -05:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2017-02-21 03:20:50 -05:00
|
|
|
def by_status(items)
|
2017-03-30 05:59:45 -04:00
|
|
|
return items unless HasStatus::AVAILABLE_STATUSES.include?(params[:status])
|
|
|
|
|
|
|
|
items.where(status: params[:status])
|
2017-02-21 03:20:50 -05:00
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2017-02-21 03:20:50 -05:00
|
|
|
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2017-02-21 03:20:50 -05:00
|
|
|
def by_ref(items)
|
|
|
|
if params[:ref].present?
|
|
|
|
items.where(ref: params[:ref])
|
|
|
|
else
|
|
|
|
items
|
|
|
|
end
|
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2017-02-21 03:20:50 -05:00
|
|
|
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2018-04-01 17:16:35 -04:00
|
|
|
def by_sha(items)
|
|
|
|
if params[:sha].present?
|
|
|
|
items.where(sha: params[:sha])
|
|
|
|
else
|
|
|
|
items
|
|
|
|
end
|
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2018-04-01 17:16:35 -04:00
|
|
|
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2017-03-08 06:24:00 -05:00
|
|
|
def by_name(items)
|
|
|
|
if params[:name].present?
|
2017-03-14 10:47:06 -04:00
|
|
|
items.joins(:user).where(users: { name: params[:name] })
|
2017-03-08 06:24:00 -05:00
|
|
|
else
|
|
|
|
items
|
|
|
|
end
|
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2017-03-08 06:24:00 -05:00
|
|
|
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2017-02-28 07:24:49 -05:00
|
|
|
def by_username(items)
|
|
|
|
if params[:username].present?
|
2017-03-14 10:47:06 -04:00
|
|
|
items.joins(:user).where(users: { username: params[:username] })
|
2017-02-21 03:20:50 -05:00
|
|
|
else
|
|
|
|
items
|
|
|
|
end
|
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2017-02-21 04:19:27 -05:00
|
|
|
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2017-02-28 07:24:49 -05:00
|
|
|
def by_yaml_errors(items)
|
2017-03-07 07:35:15 -05:00
|
|
|
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")
|
2017-02-21 03:20:50 -05:00
|
|
|
else
|
|
|
|
items
|
|
|
|
end
|
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2017-02-21 03:20:50 -05:00
|
|
|
|
2019-12-06 13:07:44 -05:00
|
|
|
def by_updated_at(items)
|
|
|
|
items = items.updated_before(params[:updated_before]) if params[:updated_before].present?
|
|
|
|
items = items.updated_after(params[:updated_after]) if params[:updated_after].present?
|
|
|
|
|
|
|
|
items
|
|
|
|
end
|
|
|
|
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2017-03-17 09:34:38 -04:00
|
|
|
def sort_items(items)
|
2017-03-30 06:30:02 -04:00
|
|
|
order_by = if ALLOWED_INDEXED_COLUMNS.include?(params[:order_by])
|
2017-03-24 03:06:19 -04:00
|
|
|
params[:order_by]
|
|
|
|
else
|
|
|
|
:id
|
|
|
|
end
|
2017-03-27 01:51:28 -04:00
|
|
|
|
2017-03-24 03:06:19 -04:00
|
|
|
sort = if params[:sort] =~ /\A(ASC|DESC)\z/i
|
|
|
|
params[:sort]
|
|
|
|
else
|
|
|
|
:desc
|
|
|
|
end
|
2017-03-27 01:51:28 -04:00
|
|
|
|
2017-03-24 03:06:19 -04:00
|
|
|
items.order(order_by => sort)
|
2017-02-21 03:20:50 -05:00
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2016-05-09 19:26:13 -04:00
|
|
|
end
|