2020-04-08 14:13:33 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Ci
|
|
|
|
class RunnerJobsFinder
|
|
|
|
attr_reader :runner, :params
|
|
|
|
|
|
|
|
ALLOWED_INDEXED_COLUMNS = %w[id].freeze
|
|
|
|
|
2022-07-01 11:34:44 +05:30
|
|
|
def initialize(runner, current_user, params = {})
|
2020-04-08 14:13:33 +05:30
|
|
|
@runner = runner
|
2022-07-01 11:34:44 +05:30
|
|
|
@user = current_user
|
2020-04-08 14:13:33 +05:30
|
|
|
@params = params
|
|
|
|
end
|
|
|
|
|
|
|
|
def execute
|
|
|
|
items = @runner.builds
|
2022-07-01 11:34:44 +05:30
|
|
|
items = by_permission(items)
|
2020-04-08 14:13:33 +05:30
|
|
|
items = by_status(items)
|
|
|
|
sort_items(items)
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2022-07-01 11:34:44 +05:30
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
|
|
|
def by_permission(items)
|
|
|
|
return items if @user.can_read_all_resources?
|
|
|
|
|
|
|
|
items.for_project(@user.authorized_project_mirrors(Gitlab::Access::REPORTER).select(:project_id))
|
|
|
|
end
|
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
|
|
|
def by_status(items)
|
2020-07-28 23:09:34 +05:30
|
|
|
return items unless Ci::HasStatus::AVAILABLE_STATUSES.include?(params[:status])
|
2020-04-08 14:13:33 +05:30
|
|
|
|
|
|
|
items.where(status: params[:status])
|
|
|
|
end
|
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
|
|
|
|
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
|
|
|
def sort_items(items)
|
|
|
|
return items unless ALLOWED_INDEXED_COLUMNS.include?(params[:order_by])
|
|
|
|
|
|
|
|
order_by = params[:order_by]
|
|
|
|
sort = if /\A(ASC|DESC)\z/i.match?(params[:sort])
|
|
|
|
params[:sort]
|
|
|
|
else
|
|
|
|
:desc
|
|
|
|
end
|
|
|
|
|
|
|
|
items.order(order_by => sort)
|
|
|
|
end
|
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
|
|
|
end
|
|
|
|
end
|