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
a05a447b
Commit
a05a447b
authored
Jul 03, 2018
by
Mark Chao
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Refactor Projects::AutocompleteService
parent
ad438f02
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
74 additions
and
17 deletions
+74
-17
app/controllers/projects/autocomplete_sources_controller.rb
app/controllers/projects/autocomplete_sources_controller.rb
+1
-1
app/services/projects/autocomplete_service.rb
app/services/projects/autocomplete_service.rb
+18
-15
ee/spec/services/groups/autocomplete_service_spec.rb
ee/spec/services/groups/autocomplete_service_spec.rb
+1
-1
spec/services/projects/autocomplete_service_spec.rb
spec/services/projects/autocomplete_service_spec.rb
+54
-0
No files found.
app/controllers/projects/autocomplete_sources_controller.rb
View file @
a05a447b
...
...
@@ -14,7 +14,7 @@ class Projects::AutocompleteSourcesController < Projects::ApplicationController
end
def
labels
render
json:
@autocomplete_service
.
labels
(
target
)
render
json:
@autocomplete_service
.
labels
_as_hash
(
target
)
end
def
milestones
...
...
app/services/projects/autocomplete_service.rb
View file @
a05a447b
...
...
@@ -20,24 +20,27 @@ module Projects
MergeRequestsFinder
.
new
(
current_user
,
project_id:
project
.
id
,
state:
'opened'
).
execute
.
select
([
:iid
,
:title
])
end
def
labels
(
target
=
nil
)
labels
=
LabelsFinder
.
new
(
current_user
,
project_id:
project
.
id
,
include_ancestor_groups:
true
)
.
execute
.
select
([
:color
,
:title
])
return
labels
unless
target
&
.
respond_to?
(
:labels
)
issuable_label_titles
=
target
.
labels
.
pluck
(
:title
)
if
issuable_label_titles
labels
=
labels
.
as_json
(
only:
[
:title
,
:color
])
issuable_label_titles
.
each
do
|
issuable_label_title
|
found_label
=
labels
.
find
{
|
label
|
label
[
'title'
]
==
issuable_label_title
}
found_label
[
:set
]
=
true
if
found_label
def
labels_as_hash
(
target
=
nil
)
available_labels
=
LabelsFinder
.
new
(
current_user
,
project_id:
project
.
id
,
include_ancestor_groups:
true
).
execute
hashes
=
available_labels
.
as_json
(
only:
[
:title
,
:color
])
if
target
&
.
respond_to?
(
:labels
)
if
already_set_labels
=
available_labels
&
target
.
labels
titles
=
already_set_labels
.
map
(
&
:title
)
hashes
.
each
do
|
hash
|
if
titles
.
include?
(
hash
[
'title'
])
hash
[
:set
]
=
true
end
end
end
end
label
s
hashe
s
end
def
commands
(
noteable
,
type
)
...
...
ee/spec/services/groups/autocomplete_service_spec.rb
View file @
a05a447b
...
...
@@ -16,7 +16,7 @@ describe Groups::AutocompleteService do
expect
(
labels
.
map
(
&
extract_title
)).
to
eq
(
expected_labels
.
map
(
&
extract_title
))
end
describe
'#labels'
do
describe
'#labels
_as_hash
'
do
let!
(
:label1
)
{
create
(
:group_label
,
group:
group
)
}
let!
(
:label2
)
{
create
(
:group_label
,
group:
group
)
}
let!
(
:sub_group_label
)
{
create
(
:group_label
,
group:
sub_group
)
}
...
...
spec/services/projects/autocomplete_service_spec.rb
View file @
a05a447b
...
...
@@ -131,4 +131,58 @@ describe Projects::AutocompleteService do
end
end
end
describe
'#labels_as_hash'
do
def
expect_labels_to_equal
(
labels
,
expected_labels
)
expect
(
labels
.
size
).
to
eq
(
expected_labels
.
size
)
extract_title
=
lambda
{
|
label
|
label
[
'title'
]
}
expect
(
labels
.
map
(
&
extract_title
)).
to
eq
(
expected_labels
.
map
(
&
extract_title
))
end
let
(
:user
)
{
create
(
:user
)
}
let
(
:group
)
{
create
(
:group
,
:nested
)
}
let!
(
:sub_group
)
{
create
(
:group
,
parent:
group
)
}
let
(
:project
)
{
create
(
:project
,
:public
,
group:
group
)
}
let
(
:issue
)
{
create
(
:issue
,
project:
project
)
}
let!
(
:label1
)
{
create
(
:label
,
project:
project
)
}
let!
(
:label2
)
{
create
(
:label
,
project:
project
)
}
let!
(
:sub_group_label
)
{
create
(
:group_label
,
group:
sub_group
)
}
let!
(
:parent_group_label
)
{
create
(
:group_label
,
group:
group
.
parent
)
}
before
do
create
(
:group_member
,
group:
group
,
user:
user
)
end
it
'returns labels from project and ancestor groups'
do
service
=
described_class
.
new
(
project
,
user
)
results
=
service
.
labels_as_hash
expected_labels
=
[
label1
,
label2
,
parent_group_label
]
expect_labels_to_equal
(
results
,
expected_labels
)
end
context
'some labels are already assigned'
do
before
do
issue
.
labels
<<
label1
end
it
'marks already assigned as set'
do
service
=
described_class
.
new
(
project
,
user
)
results
=
service
.
labels_as_hash
(
issue
)
expected_labels
=
[
label1
,
label2
,
parent_group_label
]
expect_labels_to_equal
(
results
,
expected_labels
)
assigned_label_titles
=
issue
.
labels
.
map
(
&
:title
)
results
.
each
do
|
hash
|
if
assigned_label_titles
.
include?
(
hash
[
'title'
])
expect
(
hash
[
:set
]).
to
eq
(
true
)
else
expect
(
hash
.
key?
(
:set
)).
to
eq
(
false
)
end
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