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
b71250ca
Commit
b71250ca
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
2fec78ea
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
65 additions
and
1 deletion
+65
-1
app/finders/runner_jobs_finder.rb
app/finders/runner_jobs_finder.rb
+21
-1
changelogs/unreleased/51794-add-ordering-to-runner-jobs-api.yml
...logs/unreleased/51794-add-ordering-to-runner-jobs-api.yml
+5
-0
doc/api/runners.md
doc/api/runners.md
+2
-0
spec/finders/runner_jobs_finder_spec.rb
spec/finders/runner_jobs_finder_spec.rb
+37
-0
No files found.
app/finders/runner_jobs_finder.rb
View file @
b71250ca
...
...
@@ -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
changelogs/unreleased/51794-add-ordering-to-runner-jobs-api.yml
0 → 100644
View file @
b71250ca
---
title
:
51794-add-order-by-to-list-runner-jobs-api
merge_request
:
author
:
Sujay Patel
type
:
added
doc/api/runners.md
View file @
b71250ca
...
...
@@ -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"
...
...
spec/finders/runner_jobs_finder_spec.rb
View file @
b71250ca
...
...
@@ -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
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