debian-mirror-gitlab/app/finders/pipelines_finder.rb

157 lines
3.6 KiB
Ruby
Raw Normal View History

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