Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Boxiang Sun
gitlab-ce
Commits
e241c899
Commit
e241c899
authored
Jun 13, 2019
by
sujay patel
Committed by
sujay
Jul 05, 2019
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Adding order by to list runner jobs api.
parent
b71250ca
Changes
6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
63 additions
and
35 deletions
+63
-35
app/finders/runner_jobs_finder.rb
app/finders/runner_jobs_finder.rb
+4
-7
changelogs/unreleased/51794-add-ordering-to-runner-jobs-api.yml
...logs/unreleased/51794-add-ordering-to-runner-jobs-api.yml
+2
-2
doc/api/runners.md
doc/api/runners.md
+1
-1
lib/api/runners.rb
lib/api/runners.rb
+2
-0
spec/finders/runner_jobs_finder_spec.rb
spec/finders/runner_jobs_finder_spec.rb
+10
-25
spec/requests/api/runners_spec.rb
spec/requests/api/runners_spec.rb
+44
-0
No files found.
app/finders/runner_jobs_finder.rb
View file @
e241c899
...
@@ -3,7 +3,7 @@
...
@@ -3,7 +3,7 @@
class
RunnerJobsFinder
class
RunnerJobsFinder
attr_reader
:runner
,
:params
attr_reader
:runner
,
:params
ALLOWED_INDEXED_COLUMNS
=
%w[id
created_at
]
.
freeze
ALLOWED_INDEXED_COLUMNS
=
%w[id]
.
freeze
def
initialize
(
runner
,
params
=
{})
def
initialize
(
runner
,
params
=
{})
@runner
=
runner
@runner
=
runner
...
@@ -28,13 +28,10 @@ class RunnerJobsFinder
...
@@ -28,13 +28,10 @@ class RunnerJobsFinder
# rubocop: disable CodeReuse/ActiveRecord
# rubocop: disable CodeReuse/ActiveRecord
def
sort_items
(
items
)
def
sort_items
(
items
)
order_by
=
if
ALLOWED_INDEXED_COLUMNS
.
include?
(
params
[
:order_by
])
return
items
unless
ALLOWED_INDEXED_COLUMNS
.
include?
(
params
[
:order_by
])
params
[
:order_by
]
else
:id
end
sort
=
if
params
[
:sort
]
=~
/\A(ASC|DESC)\z/i
order_by
=
params
[
:order_by
]
sort
=
if
/\A(ASC|DESC)\z/i
.
match?
(
params
[
:sort
])
params
[
:sort
]
params
[
:sort
]
else
else
:desc
:desc
...
...
changelogs/unreleased/51794-add-ordering-to-runner-jobs-api.yml
View file @
e241c899
---
---
title
:
51794-add-order-by-to-list-runner-jobs-
api
title
:
Add order_by and sort params to list runner jobs
api
merge_request
:
merge_request
:
29629
author
:
Sujay Patel
author
:
Sujay Patel
type
:
added
type
:
added
doc/api/runners.md
View file @
e241c899
...
@@ -291,7 +291,7 @@ GET /runners/:id/jobs
...
@@ -291,7 +291,7 @@ 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`
or
`created_at`
(default: id)
|
|
`order_by`
| string | no | Order jobs by
`id`
.
|
|
`sort`
| string | no | Sort jobs in
`asc`
or
`desc`
order (default:
`desc`
) |
|
`sort`
| string | no | Sort jobs in
`asc`
or
`desc`
order (default:
`desc`
) |
```
```
...
...
lib/api/runners.rb
View file @
e241c899
...
@@ -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
...
...
spec/finders/runner_jobs_finder_spec.rb
View file @
e241c899
...
@@ -37,38 +37,23 @@ describe RunnerJobsFinder do
...
@@ -37,38 +37,23 @@ describe RunnerJobsFinder do
end
end
context
'when order_by and sort are specified'
do
context
'when order_by and sort are specified'
do
context
'when order_by created_at'
do
context
'when order_by id and sort is asc'
do
let
(
:params
)
{
{
order_by:
'created_at'
,
sort:
'asc'
}
}
let
(
:params
)
{
{
order_by:
'id'
,
sort:
'asc'
}
}
let!
(
:jobs
)
{
Array
.
new
(
2
)
{
create
(
:ci_build
,
runner:
runner
,
project:
project
,
user:
create
(
:user
))
}
}
let!
(
:jobs
)
{
create_list
(
:ci_build
,
2
,
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
it
'sorts as id: :asc'
do
is_expected
.
to
eq
(
jobs
.
sort_by
{
|
p
|
p
.
id
}
)
is_expected
.
to
eq
(
jobs
.
sort_by
(
&
:id
)
)
end
end
end
end
end
context
'when both are nil'
do
context
'when order_by is specified and sort is not specified'
do
let
(
:params
)
{
{
order_by:
nil
,
sort:
nil
}
}
context
'when order_by id and sort is not specified'
do
let!
(
:jobs
)
{
Array
.
new
(
2
)
{
create
(
:ci_build
,
runner:
runner
,
project:
project
,
user:
create
(
:user
))
}
}
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
it
'sorts as id: :desc'
do
is_expected
.
to
eq
(
jobs
.
sort_by
{
|
p
|
-
p
.
id
}
)
is_expected
.
to
eq
(
jobs
.
sort_by
(
&
:id
).
reverse
)
end
end
end
end
end
end
...
...
spec/requests/api/runners_spec.rb
View file @
e241c899
...
@@ -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
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment