diff options
author | sujay patel <sujaypatel1996@gmail.com> | 2019-06-13 01:33:21 +0530 |
---|---|---|
committer | sujay <sujay.patel@caavo.com> | 2019-07-05 14:19:28 +0530 |
commit | b71250ca0f1b9df4f728bdb322502e3544058ca5 (patch) | |
tree | 19aecdb71975d81b256c001cd29006ddb198f5a5 /app/finders | |
parent | 2fec78ead4ce46b9728be02693b6e50cce740726 (diff) | |
download | gitlab-ce-b71250ca0f1b9df4f728bdb322502e3544058ca5.tar.gz |
Adding order by to list runner jobs api.
Diffstat (limited to 'app/finders')
-rw-r--r-- | app/finders/runner_jobs_finder.rb | 22 |
1 files changed, 21 insertions, 1 deletions
diff --git a/app/finders/runner_jobs_finder.rb b/app/finders/runner_jobs_finder.rb index 4fca4ec94f3..f1ee1d38255 100644 --- a/app/finders/runner_jobs_finder.rb +++ b/app/finders/runner_jobs_finder.rb @@ -3,6 +3,8 @@ class RunnerJobsFinder attr_reader :runner, :params + ALLOWED_INDEXED_COLUMNS = %w[id created_at].freeze + def initialize(runner, params = {}) @runner = runner @params = params @@ -11,7 +13,7 @@ class RunnerJobsFinder def execute items = @runner.builds items = by_status(items) - items + sort_items(items) end private @@ -23,4 +25,22 @@ class RunnerJobsFinder items.where(status: params[:status]) end # rubocop: enable CodeReuse/ActiveRecord + + # rubocop: disable CodeReuse/ActiveRecord + def sort_items(items) + order_by = if ALLOWED_INDEXED_COLUMNS.include?(params[:order_by]) + params[:order_by] + else + :id + end + + sort = if params[:sort] =~ /\A(ASC|DESC)\z/i + params[:sort] + else + :desc + end + + items.order(order_by => sort) + end + # rubocop: enable CodeReuse/ActiveRecord end |