summaryrefslogtreecommitdiff
path: root/app
diff options
context:
space:
mode:
authorGitLab Bot <gitlab-bot@gitlab.com>2019-11-28 03:06:32 +0000
committerGitLab Bot <gitlab-bot@gitlab.com>2019-11-28 03:06:32 +0000
commit284ae7dd7536df63fc6dd971f65ca420e26d2f05 (patch)
tree356c0c422685367487d15a8634e978d1f5e8f4ca /app
parent2c0b1b6259d83e37c2a2b456a1f9afdb8817a3d5 (diff)
downloadgitlab-ce-284ae7dd7536df63fc6dd971f65ca420e26d2f05.tar.gz
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'app')
-rw-r--r--app/finders/deployments_finder.rb48
-rw-r--r--app/models/deployment.rb1
2 files changed, 49 insertions, 0 deletions
diff --git a/app/finders/deployments_finder.rb b/app/finders/deployments_finder.rb
new file mode 100644
index 00000000000..085c6a04fa6
--- /dev/null
+++ b/app/finders/deployments_finder.rb
@@ -0,0 +1,48 @@
+# frozen_string_literal: true
+
+class DeploymentsFinder
+ attr_reader :project, :params
+
+ ALLOWED_SORT_VALUES = %w[id iid created_at updated_at ref].freeze
+ DEFAULT_SORT_VALUE = 'id'.freeze
+
+ ALLOWED_SORT_DIRECTIONS = %w[asc desc].freeze
+ DEFAULT_SORT_DIRECTION = 'asc'.freeze
+
+ def initialize(project, params = {})
+ @project = project
+ @params = params
+ end
+
+ def execute
+ items = init_collection
+ items = by_updated_at(items)
+ sort(items)
+ end
+
+ private
+
+ def init_collection
+ project.deployments
+ end
+
+ # 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
+
+ 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 }
+ end
+end
diff --git a/app/models/deployment.rb b/app/models/deployment.rb
index 4a38912db9b..8ef49fc56c6 100644
--- a/app/models/deployment.rb
+++ b/app/models/deployment.rb
@@ -4,6 +4,7 @@ class Deployment < ApplicationRecord
include AtomicInternalId
include IidRoutes
include AfterCommitQueue
+ include UpdatedAtFilterable
belongs_to :project, required: true
belongs_to :environment, required: true