Commit 5ecb2db8 authored by Lin Jen-Shin's avatar Lin Jen-Shin

Merge branch '51794-add-ordering-to-runner-jobs-api' into 'master'

Adding order by to list runner jobs api.

Closes #51794

See merge request gitlab-org/gitlab-ce!29629
parents c9748d66 c7acdbf9
...@@ -3,6 +3,8 @@ ...@@ -3,6 +3,8 @@
class RunnerJobsFinder class RunnerJobsFinder
attr_reader :runner, :params attr_reader :runner, :params
ALLOWED_INDEXED_COLUMNS = %w[id].freeze
def initialize(runner, params = {}) def initialize(runner, params = {})
@runner = runner @runner = runner
@params = params @params = params
...@@ -11,7 +13,7 @@ class RunnerJobsFinder ...@@ -11,7 +13,7 @@ class RunnerJobsFinder
def execute def execute
items = @runner.builds items = @runner.builds
items = by_status(items) items = by_status(items)
items sort_items(items)
end end
private private
...@@ -23,4 +25,19 @@ class RunnerJobsFinder ...@@ -23,4 +25,19 @@ class RunnerJobsFinder
items.where(status: params[:status]) items.where(status: params[:status])
end end
# rubocop: enable CodeReuse/ActiveRecord # rubocop: enable CodeReuse/ActiveRecord
# rubocop: disable CodeReuse/ActiveRecord
def sort_items(items)
return items unless ALLOWED_INDEXED_COLUMNS.include?(params[:order_by])
order_by = params[:order_by]
sort = if /\A(ASC|DESC)\z/i.match?(params[:sort])
params[:sort]
else
:desc
end
items.order(order_by => sort)
end
# rubocop: enable CodeReuse/ActiveRecord
end end
---
title: Add order_by and sort params to list runner jobs api
merge_request: 29629
author: Sujay Patel
type: added
...@@ -291,6 +291,8 @@ GET /runners/:id/jobs ...@@ -291,6 +291,8 @@ GET /runners/:id/jobs
|-----------|---------|----------|---------------------| |-----------|---------|----------|---------------------|
| `id` | integer | yes | The ID of a runner | | `id` | integer | yes | The ID of a runner |
| `status` | string | no | Status of the job; one of: `running`, `success`, `failed`, `canceled` | | `status` | string | no | Status of the job; one of: `running`, `success`, `failed`, `canceled` |
| `order_by`| string | no | Order jobs by `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" curl --header "PRIVATE-TOKEN: <your_access_token>" "https://gitlab.example.com/api/v4/runners/1/jobs?status=running"
......
...@@ -115,6 +115,8 @@ module API ...@@ -115,6 +115,8 @@ module API
params do params do
requires :id, type: Integer, desc: 'The ID of the runner' requires :id, type: Integer, desc: 'The ID of the runner'
optional :status, type: String, desc: 'Status of the job', values: Ci::Build::AVAILABLE_STATUSES optional :status, type: String, desc: 'Status of the job', values: Ci::Build::AVAILABLE_STATUSES
optional :order_by, type: String, desc: 'Order by `id` or not', values: RunnerJobsFinder::ALLOWED_INDEXED_COLUMNS
optional :sort, type: String, values: %w[asc desc], default: 'desc', desc: 'Sort by asc (ascending) or desc (descending)'
use :pagination use :pagination
end end
get ':id/jobs' do get ':id/jobs' do
......
...@@ -35,5 +35,27 @@ describe RunnerJobsFinder do ...@@ -35,5 +35,27 @@ describe RunnerJobsFinder do
end end
end end
end end
context 'when order_by and sort are specified' do
context 'when order_by id and sort is asc' do
let(:params) { { order_by: 'id', sort: 'asc' } }
let!(:jobs) { create_list(:ci_build, 2, runner: runner, project: project, user: create(:user)) }
it 'sorts as id: :asc' do
is_expected.to eq(jobs.sort_by(&:id))
end
end
end
context 'when order_by is specified and sort is not specified' do
context 'when order_by id and sort is not specified' do
let(:params) { { order_by: 'id' } }
let!(:jobs) { create_list(:ci_build, 2, runner: runner, project: project, user: create(:user)) }
it 'sorts as id: :desc' do
is_expected.to eq(jobs.sort_by(&:id).reverse)
end
end
end
end end
end end
...@@ -584,6 +584,34 @@ describe API::Runners do ...@@ -584,6 +584,34 @@ describe API::Runners do
end end
end end
context 'when valid order_by is provided' do
context 'when sort order is not specified' do
it 'return jobs in descending order' do
get api("/runners/#{project_runner.id}/jobs?order_by=id", admin)
expect(response).to have_gitlab_http_status(200)
expect(response).to include_pagination_headers
expect(json_response).to be_an(Array)
expect(json_response.length).to eq(2)
expect(json_response.first).to include('id' => job_5.id)
end
end
context 'when sort order is specified as asc' do
it 'return jobs sorted in ascending order' do
get api("/runners/#{project_runner.id}/jobs?order_by=id&sort=asc", admin)
expect(response).to have_gitlab_http_status(200)
expect(response).to include_pagination_headers
expect(json_response).to be_an(Array)
expect(json_response.length).to eq(2)
expect(json_response.first).to include('id' => job_4.id)
end
end
end
context 'when invalid status is provided' do context 'when invalid status is provided' do
it 'return 400' do it 'return 400' do
get api("/runners/#{project_runner.id}/jobs?status=non-existing", admin) get api("/runners/#{project_runner.id}/jobs?status=non-existing", admin)
...@@ -591,6 +619,22 @@ describe API::Runners do ...@@ -591,6 +619,22 @@ describe API::Runners do
expect(response).to have_gitlab_http_status(400) expect(response).to have_gitlab_http_status(400)
end end
end end
context 'when invalid order_by is provided' do
it 'return 400' do
get api("/runners/#{project_runner.id}/jobs?order_by=non-existing", admin)
expect(response).to have_gitlab_http_status(400)
end
end
context 'when invalid sort is provided' do
it 'return 400' do
get api("/runners/#{project_runner.id}/jobs?sort=non-existing", admin)
expect(response).to have_gitlab_http_status(400)
end
end
end end
context "when runner doesn't exist" do context "when runner doesn't exist" do
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment