2019-11-27 22:06:32 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class DeploymentsFinder
|
|
|
|
attr_reader :project, :params
|
|
|
|
|
|
|
|
ALLOWED_SORT_VALUES = %w[id iid created_at updated_at ref].freeze
|
2021-01-07 16:10:18 -05:00
|
|
|
DEFAULT_SORT_VALUE = 'id'
|
2019-11-27 22:06:32 -05:00
|
|
|
|
|
|
|
ALLOWED_SORT_DIRECTIONS = %w[asc desc].freeze
|
2021-01-07 16:10:18 -05:00
|
|
|
DEFAULT_SORT_DIRECTION = 'asc'
|
2019-11-27 22:06:32 -05:00
|
|
|
|
|
|
|
def initialize(project, params = {})
|
|
|
|
@project = project
|
|
|
|
@params = params
|
|
|
|
end
|
|
|
|
|
|
|
|
def execute
|
|
|
|
items = init_collection
|
|
|
|
items = by_updated_at(items)
|
2020-01-17 16:08:29 -05:00
|
|
|
items = by_environment(items)
|
|
|
|
items = by_status(items)
|
2019-11-27 22:06:32 -05:00
|
|
|
sort(items)
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2019-12-05 22:08:02 -05:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2019-11-27 22:06:32 -05:00
|
|
|
def init_collection
|
2019-12-05 22:08:02 -05:00
|
|
|
project
|
|
|
|
.deployments
|
|
|
|
.includes(
|
|
|
|
:user,
|
|
|
|
environment: [],
|
|
|
|
deployable: {
|
|
|
|
job_artifacts: [],
|
|
|
|
pipeline: {
|
|
|
|
project: {
|
|
|
|
route: [],
|
|
|
|
namespace: :route
|
|
|
|
}
|
|
|
|
},
|
|
|
|
project: {
|
|
|
|
namespace: :route
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
2019-11-27 22:06:32 -05:00
|
|
|
end
|
2019-12-05 22:08:02 -05:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2019-11-27 22:06:32 -05:00
|
|
|
|
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
|
|
|
def sort(items)
|
|
|
|
items.order(sort_params)
|
|
|
|
end
|
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2020-01-17 16:08:29 -05:00
|
|
|
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
|
|
|
|
|
2019-11-27 22:06:32 -05:00
|
|
|
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
|
|
|
|
|
2019-12-05 22:08:02 -05:00
|
|
|
{ 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
|
2019-11-27 22:06:32 -05:00
|
|
|
end
|
|
|
|
end
|