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

120 lines
3.1 KiB
Ruby
Raw Normal View History

2020-01-01 13:55:28 +05:30
# frozen_string_literal: true
2021-03-11 19:13:27 +05:30
# WARNING: This finder does not check permissions!
#
# Arguments:
# params:
# project: Project model - Find deployments for this project
# updated_after: DateTime
# updated_before: DateTime
# finished_after: DateTime
# finished_before: DateTime
# environment: String
# status: String (see Deployment.statuses)
# order_by: String (see ALLOWED_SORT_VALUES constant)
# sort: String (asc | desc)
2020-01-01 13:55:28 +05:30
class DeploymentsFinder
2021-03-11 19:13:27 +05:30
attr_reader :params
2020-01-01 13:55:28 +05:30
2021-03-11 19:13:27 +05:30
ALLOWED_SORT_VALUES = %w[id iid created_at updated_at ref finished_at].freeze
2021-03-08 18:12:59 +05:30
DEFAULT_SORT_VALUE = 'id'
2020-01-01 13:55:28 +05:30
ALLOWED_SORT_DIRECTIONS = %w[asc desc].freeze
2021-03-08 18:12:59 +05:30
DEFAULT_SORT_DIRECTION = 'asc'
2020-01-01 13:55:28 +05:30
2021-03-11 19:13:27 +05:30
def initialize(params = {})
2020-01-01 13:55:28 +05:30
@params = params
end
def execute
items = init_collection
items = by_updated_at(items)
2021-03-11 19:13:27 +05:30
items = by_finished_at(items)
2020-03-13 15:44:24 +05:30
items = by_environment(items)
items = by_status(items)
2021-03-11 19:13:27 +05:30
items = preload_associations(items)
items = sort(items)
items
2020-01-01 13:55:28 +05:30
end
private
def init_collection
2021-03-11 19:13:27 +05:30
if params[:project]
params[:project].deployments
else
Deployment.none
end
2020-01-01 13:55:28 +05:30
end
def sort(items)
2021-03-11 19:13:27 +05:30
items.order(sort_params) # rubocop: disable CodeReuse/ActiveRecord
2020-01-01 13:55:28 +05:30
end
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
2021-03-11 19:13:27 +05:30
def by_finished_at(items)
items = items.finished_before(params[:finished_before]) if params[:finished_before].present?
items = items.finished_after(params[:finished_after]) if params[:finished_after].present?
items
end
2020-03-13 15:44:24 +05:30
def by_environment(items)
if params[:environment].present?
items.for_environment_name(params[:environment])
else
items
end
end
def by_status(items)
return items unless params[:status].present?
unless Deployment.statuses.key?(params[:status])
raise ArgumentError, "The deployment status #{params[:status]} is invalid"
end
items.for_status(params[:status])
end
2020-01-01 13:55:28 +05:30
def sort_params
order_by = ALLOWED_SORT_VALUES.include?(params[:order_by]) ? params[:order_by] : DEFAULT_SORT_VALUE
order_direction = ALLOWED_SORT_DIRECTIONS.include?(params[:sort]) ? params[:sort] : DEFAULT_SORT_DIRECTION
{ order_by => order_direction }.tap do |sort_values|
sort_values['id'] = 'desc' if sort_values['updated_at']
sort_values['id'] = sort_values.delete('created_at') if sort_values['created_at'] # Sorting by `id` produces the same result as sorting by `created_at`
end
end
2021-03-11 19:13:27 +05:30
# rubocop: disable CodeReuse/ActiveRecord
def preload_associations(scope)
scope.includes(
:user,
environment: [],
deployable: {
job_artifacts: [],
pipeline: {
project: {
route: [],
namespace: :route
}
},
project: {
namespace: :route
}
}
)
end
# rubocop: enable CodeReuse/ActiveRecord
2020-01-01 13:55:28 +05:30
end
2021-03-11 19:13:27 +05:30
DeploymentsFinder.prepend_if_ee('EE::DeploymentsFinder')