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 | |
parent | 2fec78ead4ce46b9728be02693b6e50cce740726 (diff) | |
download | gitlab-ce-b71250ca0f1b9df4f728bdb322502e3544058ca5.tar.gz |
Adding order by to list runner jobs api.
-rw-r--r-- | app/finders/runner_jobs_finder.rb | 22 | ||||
-rw-r--r-- | changelogs/unreleased/51794-add-ordering-to-runner-jobs-api.yml | 5 | ||||
-rw-r--r-- | doc/api/runners.md | 2 | ||||
-rw-r--r-- | spec/finders/runner_jobs_finder_spec.rb | 37 |
4 files changed, 65 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 diff --git a/changelogs/unreleased/51794-add-ordering-to-runner-jobs-api.yml b/changelogs/unreleased/51794-add-ordering-to-runner-jobs-api.yml new file mode 100644 index 00000000000..6af61d7b145 --- /dev/null +++ b/changelogs/unreleased/51794-add-ordering-to-runner-jobs-api.yml @@ -0,0 +1,5 @@ +--- +title: 51794-add-order-by-to-list-runner-jobs-api +merge_request: +author: Sujay Patel +type: added diff --git a/doc/api/runners.md b/doc/api/runners.md index 90e9fbff247..425b9aeef1b 100644 --- a/doc/api/runners.md +++ b/doc/api/runners.md @@ -291,6 +291,8 @@ GET /runners/:id/jobs |-----------|---------|----------|---------------------| | `id` | integer | yes | The ID of a runner | | `status` | string | no | Status of the job; one of: `running`, `success`, `failed`, `canceled` | +| `order_by`| string | no | Order jobs by `id` or `created_at` (default: id) | +| `sort` | string | no | Sort jobs in `asc` or `desc` order (default: `desc`) | ``` curl --header "PRIVATE-TOKEN: <your_access_token>" "https://gitlab.example.com/api/v4/runners/1/jobs?status=running" diff --git a/spec/finders/runner_jobs_finder_spec.rb b/spec/finders/runner_jobs_finder_spec.rb index 97304170c4e..9ed6f50ddfb 100644 --- a/spec/finders/runner_jobs_finder_spec.rb +++ b/spec/finders/runner_jobs_finder_spec.rb @@ -35,5 +35,42 @@ describe RunnerJobsFinder do end end end + + context 'when order_by and sort are specified' do + context 'when order_by created_at' do + let(:params) { { order_by: 'created_at', sort: 'asc' } } + let!(:jobs) { Array.new(2) { create(:ci_build, runner: runner, project: project, user: create(:user)) } } + + it 'sorts as created_at: :asc' do + is_expected.to match_array(jobs) + end + + context 'when sort is invalid' do + let(:params) { { order_by: 'created_at', sort: 'invalid_sort' } } + + it 'sorts as created_at: :desc' do + is_expected.to eq(jobs.sort_by { |p| -p.user.id }) + end + end + end + + context 'when order_by is invalid' do + let(:params) { { order_by: 'invalid_column', sort: 'asc' } } + let!(:jobs) { Array.new(2) { create(:ci_build, runner: runner, project: project, user: create(:user)) } } + + it 'sorts as id: :asc' do + is_expected.to eq(jobs.sort_by { |p| p.id }) + end + end + + context 'when both are nil' do + let(:params) { { order_by: nil, sort: nil } } + let!(:jobs) { Array.new(2) { create(:ci_build, runner: runner, project: project, user: create(:user)) } } + + it 'sorts as id: :desc' do + is_expected.to eq(jobs.sort_by { |p| -p.id }) + end + end + end end end |