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
1
Merge Requests
1
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
nexedi
gitlab-ce
Commits
e3d21409
Commit
e3d21409
authored
Mar 30, 2022
by
Illya Klymov
Committed by
Mayra Cabrera
Mar 30, 2022
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Implement sort param for bulk imports API
* Make sort descending by default Changelog: added
parent
caf1c66a
Changes
9
Show whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
126 additions
and
33 deletions
+126
-33
app/finders/bulk_imports/entities_finder.rb
app/finders/bulk_imports/entities_finder.rb
+12
-5
app/finders/bulk_imports/imports_finder.rb
app/finders/bulk_imports/imports_finder.rb
+12
-5
app/models/bulk_import.rb
app/models/bulk_import.rb
+1
-0
app/models/bulk_imports/entity.rb
app/models/bulk_imports/entity.rb
+2
-0
doc/api/bulk_imports.md
doc/api/bulk_imports.md
+18
-15
lib/api/bulk_imports.rb
lib/api/bulk_imports.rb
+7
-3
spec/finders/bulk_imports/entities_finder_spec.rb
spec/finders/bulk_imports/entities_finder_spec.rb
+29
-3
spec/finders/bulk_imports/imports_finder_spec.rb
spec/finders/bulk_imports/imports_finder_spec.rb
+22
-2
spec/requests/api/bulk_imports_spec.rb
spec/requests/api/bulk_imports_spec.rb
+23
-0
No files found.
app/finders/bulk_imports/entities_finder.rb
View file @
e3d21409
...
@@ -2,10 +2,10 @@
...
@@ -2,10 +2,10 @@
module
BulkImports
module
BulkImports
class
EntitiesFinder
class
EntitiesFinder
def
initialize
(
user
:,
bulk_import:
nil
,
status:
nil
)
def
initialize
(
user
:,
bulk_import:
nil
,
params:
{}
)
@user
=
user
@user
=
user
@bulk_import
=
bulk_import
@bulk_import
=
bulk_import
@
status
=
statu
s
@
params
=
param
s
end
end
def
execute
def
execute
...
@@ -14,6 +14,7 @@ module BulkImports
...
@@ -14,6 +14,7 @@ module BulkImports
.
by_user_id
(
user
.
id
)
.
by_user_id
(
user
.
id
)
.
then
(
&
method
(
:filter_by_bulk_import
))
.
then
(
&
method
(
:filter_by_bulk_import
))
.
then
(
&
method
(
:filter_by_status
))
.
then
(
&
method
(
:filter_by_status
))
.
then
(
&
method
(
:sort
))
end
end
private
private
...
@@ -23,13 +24,19 @@ module BulkImports
...
@@ -23,13 +24,19 @@ module BulkImports
def
filter_by_bulk_import
(
entities
)
def
filter_by_bulk_import
(
entities
)
return
entities
unless
bulk_import
return
entities
unless
bulk_import
entities
.
where
(
bulk_import_id:
bulk_import
.
id
)
# rubocop: disable CodeReuse/ActiveRecord
entities
.
by_bulk_import_id
(
bulk_import
.
id
)
end
end
def
filter_by_status
(
entities
)
def
filter_by_status
(
entities
)
return
entities
unless
::
BulkImports
::
Entity
.
all_human_statuses
.
include?
(
status
)
return
entities
unless
::
BulkImports
::
Entity
.
all_human_statuses
.
include?
(
@params
[
:status
]
)
entities
.
with_status
(
status
)
entities
.
with_status
(
@params
[
:status
])
end
def
sort
(
entities
)
return
entities
unless
@params
[
:sort
]
entities
.
order_by_created_at
(
@params
[
:sort
])
end
end
end
end
end
end
app/finders/bulk_imports/imports_finder.rb
View file @
e3d21409
...
@@ -2,13 +2,14 @@
...
@@ -2,13 +2,14 @@
module
BulkImports
module
BulkImports
class
ImportsFinder
class
ImportsFinder
def
initialize
(
user
:,
status:
nil
)
def
initialize
(
user
:,
params:
{}
)
@user
=
user
@user
=
user
@
status
=
statu
s
@
params
=
param
s
end
end
def
execute
def
execute
filter_by_status
(
user
.
bulk_imports
)
imports
=
filter_by_status
(
user
.
bulk_imports
)
sort
(
imports
)
end
end
private
private
...
@@ -16,9 +17,15 @@ module BulkImports
...
@@ -16,9 +17,15 @@ module BulkImports
attr_reader
:user
,
:status
attr_reader
:user
,
:status
def
filter_by_status
(
imports
)
def
filter_by_status
(
imports
)
return
imports
unless
BulkImport
.
all_human_statuses
.
include?
(
status
)
return
imports
unless
BulkImport
.
all_human_statuses
.
include?
(
@params
[
:status
]
)
imports
.
with_status
(
status
)
imports
.
with_status
(
@params
[
:status
])
end
def
sort
(
imports
)
return
imports
unless
@params
[
:sort
]
imports
.
order_by_created_at
(
@params
[
:sort
])
end
end
end
end
end
end
app/models/bulk_import.rb
View file @
e3d21409
...
@@ -17,6 +17,7 @@ class BulkImport < ApplicationRecord
...
@@ -17,6 +17,7 @@ class BulkImport < ApplicationRecord
enum
source_type:
{
gitlab:
0
}
enum
source_type:
{
gitlab:
0
}
scope
:stale
,
->
{
where
(
'created_at < ?'
,
8
.
hours
.
ago
).
where
(
status:
[
0
,
1
])
}
scope
:stale
,
->
{
where
(
'created_at < ?'
,
8
.
hours
.
ago
).
where
(
status:
[
0
,
1
])
}
scope
:order_by_created_at
,
->
(
direction
)
{
order
(
created_at:
direction
)
}
state_machine
:status
,
initial: :created
do
state_machine
:status
,
initial: :created
do
state
:created
,
value:
0
state
:created
,
value:
0
...
...
app/models/bulk_imports/entity.rb
View file @
e3d21409
...
@@ -52,6 +52,8 @@ class BulkImports::Entity < ApplicationRecord
...
@@ -52,6 +52,8 @@ class BulkImports::Entity < ApplicationRecord
scope
:by_user_id
,
->
(
user_id
)
{
joins
(
:bulk_import
).
where
(
bulk_imports:
{
user_id:
user_id
})
}
scope
:by_user_id
,
->
(
user_id
)
{
joins
(
:bulk_import
).
where
(
bulk_imports:
{
user_id:
user_id
})
}
scope
:stale
,
->
{
where
(
'created_at < ?'
,
8
.
hours
.
ago
).
where
(
status:
[
0
,
1
])
}
scope
:stale
,
->
{
where
(
'created_at < ?'
,
8
.
hours
.
ago
).
where
(
status:
[
0
,
1
])
}
scope
:by_bulk_import_id
,
->
(
bulk_import_id
)
{
where
(
bulk_import_id:
bulk_import_id
)}
scope
:order_by_created_at
,
->
(
direction
)
{
order
(
created_at:
direction
)
}
state_machine
:status
,
initial: :created
do
state_machine
:status
,
initial: :created
do
state
:created
,
value:
0
state
:created
,
value:
0
...
...
doc/api/bulk_imports.md
View file @
e3d21409
...
@@ -59,9 +59,10 @@ GET /bulk_imports
...
@@ -59,9 +59,10 @@ GET /bulk_imports
```
```
| Attribute | Type | Required | Description |
| Attribute | Type | Required | Description |
|:-----------|:--------|:---------|:---------------------------------------|
|:-----------|:--------|:---------|:---------------------------------------
-----------------------------------------------------
|
|
`per_page`
| integer | no | Number of records to return per page. |
|
`per_page`
| integer | no | Number of records to return per page. |
|
`page`
| integer | no | Page to retrieve. |
|
`page`
| integer | no | Page to retrieve. |
|
`sort`
| string | no | Return GitLab migration sorted in
`asc`
or
`desc`
order by creation date. Default is
`desc`
|
|
`status`
| string | no | Import status. |
|
`status`
| string | no | Import status. |
The status can be one of the following:
The status can be one of the following:
...
@@ -101,9 +102,10 @@ GET /bulk_imports/entities
...
@@ -101,9 +102,10 @@ GET /bulk_imports/entities
```
```
| Attribute | Type | Required | Description |
| Attribute | Type | Required | Description |
|:-----------|:--------|:---------|:---------------------------------------|
|:-----------|:--------|:---------|:---------------------------------------
--------------------------------------------------------------
|
|
`per_page`
| integer | no | Number of records to return per page. |
|
`per_page`
| integer | no | Number of records to return per page. |
|
`page`
| integer | no | Page to retrieve. |
|
`page`
| integer | no | Page to retrieve. |
|
`sort`
| string | no | Return GitLab migration entities sorted in
`asc`
or
`desc`
order by creation date. Default is
`desc`
|
|
`status`
| string | no | Import status. |
|
`status`
| string | no | Import status. |
The status can be one of the following:
The status can be one of the following:
...
@@ -185,9 +187,10 @@ GET /bulk_imports/:id/entities
...
@@ -185,9 +187,10 @@ GET /bulk_imports/:id/entities
```
```
| Attribute | Type | Required | Description |
| Attribute | Type | Required | Description |
|:-----------|:--------|:---------|:---------------------------------------|
|:-----------|:--------|:---------|:---------------------------------------
-----------------------------------------------------
|
|
`per_page`
| integer | no | Number of records to return per page. |
|
`per_page`
| integer | no | Number of records to return per page. |
|
`page`
| integer | no | Page to retrieve. |
|
`page`
| integer | no | Page to retrieve. |
|
`sort`
| string | no | Return GitLab migration sorted in
`asc`
or
`desc`
order by creation date. Default is
`desc`
|
|
`status`
| string | no | Import status. |
|
`status`
| string | no | Import status. |
The status can be one of the following:
The status can be one of the following:
...
...
lib/api/bulk_imports.rb
View file @
e3d21409
...
@@ -10,7 +10,7 @@ module API
...
@@ -10,7 +10,7 @@ module API
def
bulk_imports
def
bulk_imports
@bulk_imports
||=
::
BulkImports
::
ImportsFinder
.
new
(
@bulk_imports
||=
::
BulkImports
::
ImportsFinder
.
new
(
user:
current_user
,
user:
current_user
,
status:
params
[
:status
]
params:
params
).
execute
).
execute
end
end
...
@@ -22,7 +22,7 @@ module API
...
@@ -22,7 +22,7 @@ module API
@bulk_import_entities
||=
::
BulkImports
::
EntitiesFinder
.
new
(
@bulk_import_entities
||=
::
BulkImports
::
EntitiesFinder
.
new
(
user:
current_user
,
user:
current_user
,
bulk_import:
bulk_import
,
bulk_import:
bulk_import
,
status:
params
[
:status
]
params:
params
).
execute
).
execute
end
end
...
@@ -70,6 +70,8 @@ module API
...
@@ -70,6 +70,8 @@ module API
end
end
params
do
params
do
use
:pagination
use
:pagination
optional
:sort
,
type:
String
,
values:
%w[asc desc]
,
default:
'desc'
,
desc:
'Return GitLab Migrations sorted in created by `asc` or `desc` order.'
optional
:status
,
type:
String
,
values:
BulkImport
.
all_human_statuses
,
optional
:status
,
type:
String
,
values:
BulkImport
.
all_human_statuses
,
desc:
'Return GitLab Migrations with specified status'
desc:
'Return GitLab Migrations with specified status'
end
end
...
@@ -82,13 +84,15 @@ module API
...
@@ -82,13 +84,15 @@ module API
end
end
params
do
params
do
use
:pagination
use
:pagination
optional
:sort
,
type:
String
,
values:
%w[asc desc]
,
default:
'desc'
,
desc:
'Return GitLab Migrations sorted in created by `asc` or `desc` order.'
optional
:status
,
type:
String
,
values:
::
BulkImports
::
Entity
.
all_human_statuses
,
optional
:status
,
type:
String
,
values:
::
BulkImports
::
Entity
.
all_human_statuses
,
desc:
"Return all GitLab Migrations' entities with specified status"
desc:
"Return all GitLab Migrations' entities with specified status"
end
end
get
:entities
do
get
:entities
do
entities
=
::
BulkImports
::
EntitiesFinder
.
new
(
entities
=
::
BulkImports
::
EntitiesFinder
.
new
(
user:
current_user
,
user:
current_user
,
status:
params
[
:status
]
params:
params
).
execute
).
execute
present
paginate
(
entities
),
with:
Entities
::
BulkImports
::
Entity
present
paginate
(
entities
),
with:
Entities
::
BulkImports
::
Entity
...
...
spec/finders/bulk_imports/entities_finder_spec.rb
View file @
e3d21409
...
@@ -51,7 +51,7 @@ RSpec.describe BulkImports::EntitiesFinder do
...
@@ -51,7 +51,7 @@ RSpec.describe BulkImports::EntitiesFinder do
end
end
context
'when status is specified'
do
context
'when status is specified'
do
subject
{
described_class
.
new
(
user:
user
,
status:
'failed'
)
}
subject
{
described_class
.
new
(
user:
user
,
params:
{
status:
'failed'
}
)
}
it
'returns a list of import entities filtered by status'
do
it
'returns a list of import entities filtered by status'
do
expect
(
subject
.
execute
)
expect
(
subject
.
execute
)
...
@@ -61,7 +61,7 @@ RSpec.describe BulkImports::EntitiesFinder do
...
@@ -61,7 +61,7 @@ RSpec.describe BulkImports::EntitiesFinder do
end
end
context
'when invalid status is specified'
do
context
'when invalid status is specified'
do
subject
{
described_class
.
new
(
user:
user
,
status:
'invalid'
)
}
subject
{
described_class
.
new
(
user:
user
,
params:
{
status:
'invalid'
}
)
}
it
'does not filter entities by status'
do
it
'does not filter entities by status'
do
expect
(
subject
.
execute
)
expect
(
subject
.
execute
)
...
@@ -74,11 +74,37 @@ RSpec.describe BulkImports::EntitiesFinder do
...
@@ -74,11 +74,37 @@ RSpec.describe BulkImports::EntitiesFinder do
end
end
context
'when bulk import and status are specified'
do
context
'when bulk import and status are specified'
do
subject
{
described_class
.
new
(
user:
user
,
bulk_import:
user_import_2
,
status:
'finished'
)
}
subject
{
described_class
.
new
(
user:
user
,
bulk_import:
user_import_2
,
params:
{
status:
'finished'
}
)
}
it
'returns matched import entities'
do
it
'returns matched import entities'
do
expect
(
subject
.
execute
).
to
contain_exactly
(
finished_entity_2
)
expect
(
subject
.
execute
).
to
contain_exactly
(
finished_entity_2
)
end
end
end
end
context
'when order is specifed'
do
subject
{
described_class
.
new
(
user:
user
,
params:
{
sort:
order
})
}
context
'when order is specified as asc'
do
let
(
:order
)
{
:asc
}
it
'returns entities sorted ascending'
do
expect
(
subject
.
execute
).
to
eq
([
started_entity_1
,
finished_entity_1
,
failed_entity_1
,
started_entity_2
,
finished_entity_2
,
failed_entity_2
])
end
end
context
'when order is specified as desc'
do
let
(
:order
)
{
:desc
}
it
'returns entities sorted descending'
do
expect
(
subject
.
execute
).
to
eq
([
failed_entity_2
,
finished_entity_2
,
started_entity_2
,
failed_entity_1
,
finished_entity_1
,
started_entity_1
])
end
end
end
end
end
end
end
spec/finders/bulk_imports/imports_finder_spec.rb
View file @
e3d21409
...
@@ -16,19 +16,39 @@ RSpec.describe BulkImports::ImportsFinder do
...
@@ -16,19 +16,39 @@ RSpec.describe BulkImports::ImportsFinder do
end
end
context
'when status is specified'
do
context
'when status is specified'
do
subject
{
described_class
.
new
(
user:
user
,
status:
'started'
)
}
subject
{
described_class
.
new
(
user:
user
,
params:
{
status:
'started'
}
)
}
it
'returns a list of import entities filtered by status'
do
it
'returns a list of import entities filtered by status'
do
expect
(
subject
.
execute
).
to
contain_exactly
(
started_import
)
expect
(
subject
.
execute
).
to
contain_exactly
(
started_import
)
end
end
context
'when invalid status is specified'
do
context
'when invalid status is specified'
do
subject
{
described_class
.
new
(
user:
user
,
status:
'invalid'
)
}
subject
{
described_class
.
new
(
user:
user
,
params:
{
status:
'invalid'
}
)
}
it
'does not filter entities by status'
do
it
'does not filter entities by status'
do
expect
(
subject
.
execute
).
to
contain_exactly
(
started_import
,
finished_import
)
expect
(
subject
.
execute
).
to
contain_exactly
(
started_import
,
finished_import
)
end
end
end
end
end
end
context
'when order is specifed'
do
subject
{
described_class
.
new
(
user:
user
,
params:
{
sort:
order
})
}
context
'when order is specified as asc'
do
let
(
:order
)
{
:asc
}
it
'returns entities sorted ascending'
do
expect
(
subject
.
execute
).
to
eq
([
started_import
,
finished_import
])
end
end
context
'when order is specified as desc'
do
let
(
:order
)
{
:desc
}
it
'returns entities sorted descending'
do
expect
(
subject
.
execute
).
to
eq
([
finished_import
,
started_import
])
end
end
end
end
end
end
end
spec/requests/api/bulk_imports_spec.rb
View file @
e3d21409
...
@@ -18,6 +18,29 @@ RSpec.describe API::BulkImports do
...
@@ -18,6 +18,29 @@ RSpec.describe API::BulkImports do
expect
(
response
).
to
have_gitlab_http_status
(
:ok
)
expect
(
response
).
to
have_gitlab_http_status
(
:ok
)
expect
(
json_response
.
pluck
(
'id'
)).
to
contain_exactly
(
import_1
.
id
,
import_2
.
id
)
expect
(
json_response
.
pluck
(
'id'
)).
to
contain_exactly
(
import_1
.
id
,
import_2
.
id
)
end
end
context
'sort parameter'
do
it
'sorts by created_at descending by default'
do
get
api
(
'/bulk_imports'
,
user
)
expect
(
response
).
to
have_gitlab_http_status
(
:ok
)
expect
(
json_response
.
pluck
(
'id'
)).
to
eq
([
import_2
.
id
,
import_1
.
id
])
end
it
'sorts by created_at descending when explicitly specified'
do
get
api
(
'/bulk_imports'
,
user
),
params:
{
sort:
'desc'
}
expect
(
response
).
to
have_gitlab_http_status
(
:ok
)
expect
(
json_response
.
pluck
(
'id'
)).
to
eq
([
import_2
.
id
,
import_1
.
id
])
end
it
'sorts by created_at ascending when explicitly specified'
do
get
api
(
'/bulk_imports'
,
user
),
params:
{
sort:
'asc'
}
expect
(
response
).
to
have_gitlab_http_status
(
:ok
)
expect
(
json_response
.
pluck
(
'id'
)).
to
eq
([
import_1
.
id
,
import_2
.
id
])
end
end
end
end
describe
'POST /bulk_imports'
do
describe
'POST /bulk_imports'
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