gitlab-org--gitlab-foss/app/finders/pipelines_finder.rb

124 lines
2.4 KiB
Ruby
Raw Normal View History

2016-05-09 19:26:13 -04:00
class PipelinesFinder
attr_reader :project, :pipelines, :params
2016-05-09 19:26:13 -04:00
ALLOWED_INDEXED_COLUMNS = %w[id status ref user_id].freeze
def initialize(project, params = {})
2016-05-09 19:26:13 -04:00
@project = project
2016-08-29 12:02:08 -04:00
@pipelines = project.pipelines
@params = params
2016-05-09 19:26:13 -04:00
end
def execute
items = pipelines
items = by_scope(items)
items = by_status(items)
items = by_ref(items)
items = by_sha(items)
2017-03-08 07:05:33 -05:00
items = by_name(items)
items = by_username(items)
items = by_yaml_errors(items)
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
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)
return items unless HasStatus::AVAILABLE_STATUSES.include?(params[:status])
items.where(status: params[:status])
end
def by_ref(items)
if params[:ref].present?
items.where(ref: params[:ref])
else
items
end
end
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
def by_username(items)
if params[:username].present?
2017-03-14 10:47:06 -04:00
items.joins(:user).where(users: { username: params[:username] })
else
items
end
end
2017-02-21 04:19:27 -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")
else
items
end
end
def sort_items(items)
order_by = if ALLOWED_INDEXED_COLUMNS.include?(params[:order_by])
params[:order_by]
else
:id
end
2017-03-27 01:51:28 -04:00
sort = if params[:sort] =~ /\A(ASC|DESC)\z/i
params[:sort]
else
:desc
end
2017-03-27 01:51:28 -04:00
items.order(order_by => sort)
end
2016-05-09 19:26:13 -04:00
end