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
928e328a
Commit
928e328a
authored
Feb 04, 2021
by
Luke Duncalfe
Committed by
Toon Claes
Mar 18, 2021
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add sort param to NotesFinder
https://gitlab.com/gitlab-org/gitlab/-/issues/301037
parent
d4da1cc5
Changes
8
Show whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
118 additions
and
50 deletions
+118
-50
app/finders/notes_finder.rb
app/finders/notes_finder.rb
+10
-2
app/models/concerns/sortable.rb
app/models/concerns/sortable.rb
+1
-0
app/models/note.rb
app/models/note.rb
+11
-2
app/services/concerns/integrations/project_test_data.rb
app/services/concerns/integrations/project_test_data.rb
+1
-1
lib/gitlab/updated_notes_paginator.rb
lib/gitlab/updated_notes_paginator.rb
+1
-1
spec/finders/notes_finder_spec.rb
spec/finders/notes_finder_spec.rb
+18
-0
spec/models/concerns/sortable_spec.rb
spec/models/concerns/sortable_spec.rb
+25
-0
spec/models/note_spec.rb
spec/models/note_spec.rb
+51
-44
No files found.
app/finders/notes_finder.rb
View file @
928e328a
...
...
@@ -17,6 +17,7 @@ class NotesFinder
# target_id: integer
# last_fetched_at: time
# search: string
# sort: string
#
def
initialize
(
current_user
,
params
=
{})
@project
=
params
[
:project
]
...
...
@@ -29,8 +30,7 @@ class NotesFinder
notes
=
init_collection
notes
=
since_fetch_at
(
notes
)
notes
=
notes
.
with_notes_filter
(
@params
[
:notes_filter
])
if
notes_filter?
notes
.
fresh
sort
(
notes
)
end
def
target
...
...
@@ -173,6 +173,14 @@ class NotesFinder
def
notes_filter?
@params
[
:notes_filter
].
present?
end
def
sort
(
notes
)
sort
=
@params
[
:sort
].
presence
return
notes
.
fresh
unless
sort
notes
.
order_by
(
sort
)
end
end
NotesFinder
.
prepend_if_ee
(
'EE::NotesFinder'
)
app/models/concerns/sortable.rb
View file @
928e328a
...
...
@@ -9,6 +9,7 @@ module Sortable
included
do
scope
:with_order_id_desc
,
->
{
order
(
self
.
arel_table
[
'id'
].
desc
)
}
scope
:with_order_id_asc
,
->
{
order
(
self
.
arel_table
[
'id'
].
asc
)
}
scope
:order_id_desc
,
->
{
reorder
(
self
.
arel_table
[
'id'
].
desc
)
}
scope
:order_id_asc
,
->
{
reorder
(
self
.
arel_table
[
'id'
].
asc
)
}
scope
:order_created_desc
,
->
{
reorder
(
self
.
arel_table
[
'created_at'
].
desc
)
}
...
...
app/models/note.rb
View file @
928e328a
...
...
@@ -19,6 +19,7 @@ class Note < ApplicationRecord
include
Gitlab
::
SQL
::
Pattern
include
ThrottledTouch
include
FromUnion
include
Sortable
cache_markdown_field
:note
,
pipeline: :note
,
issuable_state_filter_enabled:
true
...
...
@@ -103,10 +104,9 @@ class Note < ApplicationRecord
scope
:system
,
->
{
where
(
system:
true
)
}
scope
:user
,
->
{
where
(
system:
false
)
}
scope
:common
,
->
{
where
(
noteable_type:
[
""
,
nil
])
}
scope
:fresh
,
->
{
order
(
created_at: :asc
,
id: :asc
)
}
scope
:fresh
,
->
{
order
_created_asc
.
with_order_id_asc
}
scope
:updated_after
,
->
(
time
)
{
where
(
'updated_at > ?'
,
time
)
}
scope
:with_updated_at
,
->
(
time
)
{
where
(
updated_at:
time
)
}
scope
:by_updated_at
,
->
{
reorder
(
:updated_at
,
:id
)
}
scope
:inc_author_project
,
->
{
includes
(
:project
,
:author
)
}
scope
:inc_author
,
->
{
includes
(
:author
)
}
scope
:inc_relations_for_view
,
->
do
...
...
@@ -148,6 +148,8 @@ class Note < ApplicationRecord
after_commit
:notify_after_destroy
,
on: :destroy
class
<<
self
extend
Gitlab
::
Utils
::
Override
def
model_name
ActiveModel
::
Name
.
new
(
self
,
nil
,
'note'
)
end
...
...
@@ -204,6 +206,13 @@ class Note < ApplicationRecord
def
search
(
query
)
fuzzy_search
(
query
,
[
:note
])
end
# Override the `Sortable` module's `.simple_sorts` to remove name sorting,
# as a `Note` does not have any property that correlates to a "name".
override
:simple_sorts
def
simple_sorts
super
.
except
(
'name_asc'
,
'name_desc'
)
end
end
# rubocop: disable CodeReuse/ServiceClass
...
...
app/services/concerns/integrations/project_test_data.rb
View file @
928e328a
...
...
@@ -9,7 +9,7 @@ module Integrations
end
def
note_events_data
note
=
NotesFinder
.
new
(
current_user
,
project:
project
,
target:
project
).
execute
.
reorder
(
nil
).
last
# rubocop: disable CodeReuse/ActiveRecord
note
=
NotesFinder
.
new
(
current_user
,
project:
project
,
target:
project
,
sort:
'id_desc'
).
execute
.
first
return
{
error:
s_
(
'TestHooks|Ensure the project has notes.'
)
}
unless
note
.
present?
...
...
lib/gitlab/updated_notes_paginator.rb
View file @
928e328a
...
...
@@ -37,7 +37,7 @@ module Gitlab
end
def
fetch_page
(
relation
)
relation
=
relation
.
by_updated_at
relation
=
relation
.
order_updated_asc
.
with_order_id_asc
notes
=
relation
.
limit
(
LIMIT
+
1
).
to_a
return
[
notes
,
false
]
unless
notes
.
size
>
LIMIT
...
...
spec/finders/notes_finder_spec.rb
View file @
928e328a
...
...
@@ -213,6 +213,24 @@ RSpec.describe NotesFinder do
expect
{
described_class
.
new
(
user
,
params
).
execute
}.
to
raise_error
(
RuntimeError
)
end
end
describe
'sorting'
do
it
'allows sorting'
do
params
=
{
project:
project
,
sort:
'id_desc'
}
expect
(
Note
).
to
receive
(
:order_id_desc
).
once
described_class
.
new
(
user
,
params
).
execute
end
it
'defaults to sort by .fresh'
do
params
=
{
project:
project
}
expect
(
Note
).
to
receive
(
:fresh
).
once
described_class
.
new
(
user
,
params
).
execute
end
end
end
describe
'.search'
do
...
...
spec/models/concerns/sortable_spec.rb
View file @
928e328a
...
...
@@ -3,6 +3,31 @@
require
'spec_helper'
RSpec
.
describe
Sortable
do
describe
'scopes'
do
describe
'secondary ordering by id'
do
let
(
:sorted_relation
)
{
Group
.
all
.
order_created_asc
}
def
arel_orders
(
relation
)
relation
.
arel
.
orders
end
it
'allows secondary ordering by id ascending'
do
orders
=
arel_orders
(
sorted_relation
.
with_order_id_asc
)
expect
(
orders
.
map
{
|
arel
|
arel
.
expr
.
name
}).
to
eq
(
%w(created_at id)
)
expect
(
orders
).
to
all
(
be_kind_of
(
Arel
::
Nodes
::
Ascending
))
end
it
'allows secondary ordering by id descending'
do
orders
=
arel_orders
(
sorted_relation
.
with_order_id_desc
)
expect
(
orders
.
map
{
|
arel
|
arel
.
expr
.
name
}).
to
eq
(
%w(created_at id)
)
expect
(
orders
.
first
).
to
be_kind_of
(
Arel
::
Nodes
::
Ascending
)
expect
(
orders
.
last
).
to
be_kind_of
(
Arel
::
Nodes
::
Descending
)
end
end
end
describe
'.order_by'
do
let
(
:arel_table
)
{
Group
.
arel_table
}
let
(
:relation
)
{
Group
.
all
}
...
...
spec/models/note_spec.rb
View file @
928e328a
...
...
@@ -20,6 +20,7 @@ RSpec.describe Note do
it
{
is_expected
.
to
include_module
(
Participable
)
}
it
{
is_expected
.
to
include_module
(
Mentionable
)
}
it
{
is_expected
.
to
include_module
(
Awardable
)
}
it
{
is_expected
.
to
include_module
(
Sortable
)
}
end
describe
'validation'
do
...
...
@@ -856,6 +857,12 @@ RSpec.describe Note do
end
end
describe
'.simple_sorts'
do
it
'does not contain name sorts'
do
expect
(
described_class
.
simple_sorts
.
grep
(
/name/
)).
to
be_empty
end
end
describe
'#for_project_snippet?'
do
it
'returns true for a project snippet note'
do
expect
(
build
(
:note_on_project_snippet
).
for_project_snippet?
).
to
be
true
...
...
@@ -1322,7 +1329,7 @@ RSpec.describe Note do
let_it_be
(
:note1
)
{
create
(
:note
,
note:
'Test 345'
)
}
let_it_be
(
:note2
)
{
create
(
:note
,
note:
'Test 789'
)
}
describe
'
#
for_note_or_capitalized_note'
do
describe
'
.
for_note_or_capitalized_note'
do
it
'returns the expected matching note'
do
notes
=
described_class
.
for_note_or_capitalized_note
(
'Test 345'
)
...
...
@@ -1344,7 +1351,7 @@ RSpec.describe Note do
end
end
describe
'
#
like_note_or_capitalized_note'
do
describe
'
.
like_note_or_capitalized_note'
do
it
'returns the expected matching note'
do
notes
=
described_class
.
like_note_or_capitalized_note
(
'Test 345'
)
...
...
@@ -1367,8 +1374,9 @@ RSpec.describe Note do
expect
(
notes
.
second
.
id
).
to
eq
(
note2
.
id
)
end
end
end
describe
'#noteable_assignee_or_author
'
do
describe
'#noteable_assignee_or_author?
'
do
let
(
:user
)
{
create
(
:user
)
}
let
(
:noteable
)
{
create
(
:issue
)
}
let
(
:note
)
{
create
(
:note
,
project:
noteable
.
project
,
noteable:
noteable
)
}
...
...
@@ -1431,7 +1439,6 @@ RSpec.describe Note do
it_behaves_like
'author check'
end
end
end
describe
'banzai_render_context'
do
let
(
:project
)
{
build
(
:project_empty_repo
)
}
...
...
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