diff options
Diffstat (limited to 'app/finders/deployments_finder.rb')
-rw-r--r-- | app/finders/deployments_finder.rb | 85 |
1 files changed, 57 insertions, 28 deletions
diff --git a/app/finders/deployments_finder.rb b/app/finders/deployments_finder.rb index 4038f93cf2d..89a28d9dfb8 100644 --- a/app/finders/deployments_finder.rb +++ b/app/finders/deployments_finder.rb @@ -1,57 +1,56 @@ # frozen_string_literal: true +# 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) class DeploymentsFinder - attr_reader :project, :params + attr_reader :params - ALLOWED_SORT_VALUES = %w[id iid created_at updated_at ref].freeze + ALLOWED_SORT_VALUES = %w[id iid created_at updated_at ref finished_at].freeze DEFAULT_SORT_VALUE = 'id' ALLOWED_SORT_DIRECTIONS = %w[asc desc].freeze DEFAULT_SORT_DIRECTION = 'asc' - def initialize(project, params = {}) - @project = project + def initialize(params = {}) @params = params end def execute items = init_collection items = by_updated_at(items) + items = by_finished_at(items) items = by_environment(items) items = by_status(items) - sort(items) + items = preload_associations(items) + items = sort(items) + + items end private - # rubocop: disable CodeReuse/ActiveRecord def init_collection - project - .deployments - .includes( - :user, - environment: [], - deployable: { - job_artifacts: [], - pipeline: { - project: { - route: [], - namespace: :route - } - }, - project: { - namespace: :route - } - } - ) + if params[:project] + params[:project].deployments + else + Deployment.none + end end - # rubocop: enable CodeReuse/ActiveRecord - # rubocop: disable CodeReuse/ActiveRecord def sort(items) - items.order(sort_params) + items.order(sort_params) # rubocop: disable CodeReuse/ActiveRecord end - # rubocop: enable CodeReuse/ActiveRecord def by_updated_at(items) items = items.updated_before(params[:updated_before]) if params[:updated_before].present? @@ -60,6 +59,13 @@ class DeploymentsFinder items end + 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 + def by_environment(items) if params[:environment].present? items.for_environment_name(params[:environment]) @@ -87,4 +93,27 @@ class DeploymentsFinder 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 + + # 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 end + +DeploymentsFinder.prepend_if_ee('EE::DeploymentsFinder') |