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
|
|
|
|
2017-03-30 06:30:02 -04:00
|
|
|
ALLOWED_INDEXED_COLUMNS = %w[id status ref user_id].freeze
|
|
|
|
|
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
|
2016-08-29 12:02:08 -04:00
|
|
|
@pipelines = project.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
|
|
|
|
|
2017-02-21 03:20:50 -05:00
|
|
|
items = pipelines
|
|
|
|
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)
|
2017-03-17 09:34:38 -04:00
|
|
|
sort_items(items)
|
2016-05-09 19:26:13 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
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
|
|
|
|
|
2016-08-29 12:02:08 -04:00
|
|
|
def from_ids(ids)
|
2016-05-09 19:26:13 -04:00
|
|
|
pipelines.unscoped.where(id: ids)
|
|
|
|
end
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
def by_ref(items)
|
|
|
|
if params[:ref].present?
|
|
|
|
items.where(ref: params[:ref])
|
|
|
|
else
|
|
|
|
items
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
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
|
|
|
|
|
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
|
|
|
|
|
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
|
2017-02-21 04:19:27 -05:00
|
|
|
|
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
|
|
|
|
|
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
|
2016-05-09 19:26:13 -04:00
|
|
|
end
|