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
22aa0344
Commit
22aa0344
authored
Sep 19, 2017
by
Bob Van Landuyt
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Rename `GroupHierarchy` to `GroupDescendant`
parent
fb7a0f8c
Changes
9
Hide whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
59 additions
and
34 deletions
+59
-34
app/controllers/groups_controller.rb
app/controllers/groups_controller.rb
+3
-3
app/finders/group_descendants_finder.rb
app/finders/group_descendants_finder.rb
+9
-7
app/models/concerns/group_descendant.rb
app/models/concerns/group_descendant.rb
+12
-12
app/models/group.rb
app/models/group.rb
+1
-1
app/models/project.rb
app/models/project.rb
+1
-5
app/serializers/group_child_entity.rb
app/serializers/group_child_entity.rb
+2
-2
app/serializers/group_child_serializer.rb
app/serializers/group_child_serializer.rb
+2
-2
spec/finders/group_descendants_finder_spec.rb
spec/finders/group_descendants_finder_spec.rb
+1
-1
spec/models/concerns/group_descendant_spec.rb
spec/models/concerns/group_descendant_spec.rb
+28
-1
No files found.
app/controllers/groups_controller.rb
View file @
22aa0344
...
...
@@ -118,9 +118,9 @@ class GroupsController < Groups::ApplicationController
protected
def
setup_children
(
parent
)
@children
=
Group
Children
Finder
.
new
(
current_user:
current_user
,
parent_group:
parent
,
params:
params
).
execute
@children
=
Group
Descendants
Finder
.
new
(
current_user:
current_user
,
parent_group:
parent
,
params:
params
).
execute
@children
=
@children
.
page
(
params
[
:page
])
end
...
...
app/finders/group_
children
_finder.rb
→
app/finders/group_
descendants
_finder.rb
View file @
22aa0344
class
Group
Children
Finder
class
Group
Descendants
Finder
include
Gitlab
::
Allowable
attr_reader
:current_user
,
:parent_group
,
:params
...
...
@@ -38,31 +38,33 @@ class GroupChildrenFinder
private
def
children
@children
||=
subgroups
.
with_route
.
includes
(
:
route
,
:parent
)
+
projects
.
with_route
.
includes
(
:route
,
:namespace
)
@children
||=
subgroups
.
with_route
.
includes
(
:
parent
)
+
projects
.
with_route
.
includes
(
:namespace
)
end
def
direct_
sub
groups
def
direct_
child_
groups
GroupsFinder
.
new
(
current_user
,
parent:
parent_group
,
all_available:
true
).
execute
end
def
all_
sub
groups
def
all_
descendant_
groups
Gitlab
::
GroupHierarchy
.
new
(
Group
.
where
(
id:
parent_group
)).
base_and_descendants
end
def
subgroups_matching_filter
all_
sub
groups
.
where
.
not
(
id:
parent_group
).
search
(
params
[
:filter
])
all_
descendant_
groups
.
where
.
not
(
id:
parent_group
).
search
(
params
[
:filter
])
end
def
subgroups
return
Group
.
none
unless
Group
.
supports_nested_groups?
return
Group
.
none
unless
can?
(
current_user
,
:read_group
,
parent_group
)
# When filtering subgroups, we want to find all matches withing the tree of
# descendants to show to the user
groups
=
if
params
[
:filter
]
subgroups_matching_filter
else
direct_
sub
groups
direct_
child_
groups
end
groups
.
sort
(
params
[
:sort
])
end
...
...
@@ -74,7 +76,7 @@ class GroupChildrenFinder
def
projects_matching_filter
ProjectsFinder
.
new
(
current_user:
current_user
).
execute
.
search
(
params
[
:filter
])
.
where
(
namespace:
all_
sub
groups
)
.
where
(
namespace:
all_
descendant_
groups
)
end
def
projects
...
...
app/models/concerns/group_
hierarchy
.rb
→
app/models/concerns/group_
descendant
.rb
View file @
22aa0344
module
Group
Hierarchy
module
Group
Descendant
def
hierarchy
(
hierarchy_base
=
nil
)
tree
_for_child
(
self
,
self
,
hierarchy_base
)
expand_hierarchy
_for_child
(
self
,
self
,
hierarchy_base
)
end
def
parent
...
...
@@ -11,29 +11,29 @@ module GroupHierarchy
end
end
def
tree_for_child
(
child
,
tree
,
hierarchy_base
)
def
expand_hierarchy_for_child
(
child
,
hierarchy
,
hierarchy_base
)
if
child
.
parent
.
nil?
&&
hierarchy_base
.
present?
raise
ArgumentError
.
new
(
'specified base is not part of the tree'
)
end
if
child
.
parent
&&
child
.
parent
!=
hierarchy_base
tree
_for_child
(
child
.
parent
,
{
child
.
parent
=>
tree
},
hierarchy_base
)
expand_hierarchy
_for_child
(
child
.
parent
,
{
child
.
parent
=>
hierarchy
},
hierarchy_base
)
else
tree
hierarchy
end
end
def
merge_hierarchy
(
other_element
,
hierarchy_base
=
nil
)
Group
Hierarchy
.
merge_hierarchies
([
self
,
other_element
],
hierarchy_base
)
Group
Descendant
.
merge_hierarchies
([
self
,
other_element
],
hierarchy_base
)
end
def
self
.
merge_hierarchies
(
hierarchies
,
hierarchy_base
=
nil
)
hierarchies
=
Array
.
wrap
(
hierarchies
)
return
if
hierarchies
.
empty?
unless
hierarchies
.
all?
{
|
hierarchy
|
hierarchy
.
is_a?
(
Group
Hierarchy
)
}
unless
hierarchies
.
all?
{
|
hierarchy
|
hierarchy
.
is_a?
(
Group
Descendant
)
}
raise
ArgumentError
.
new
(
'element is not a hierarchy'
)
end
...
...
@@ -68,16 +68,16 @@ module GroupHierarchy
# we can check if its already in the hash. If so, we don't need to do anything
#
# Handled cases
# [Hash, Group
Hierarchy
]
# [Hash, Group
Descendant
]
elsif
first_child
.
is_a?
(
Hash
)
&&
first_child
.
keys
.
include?
(
second_child
)
first_child
elsif
second_child
.
is_a?
(
Hash
)
&&
second_child
.
keys
.
include?
(
first_child
)
second_child
# If one or both elements are a Group
Hierarchy
, we wrap create an array
# If one or both elements are a Group
Descendant
, we wrap create an array
# combining them.
#
# Handled cases:
# [Group
Hierarchy, Array], [GroupHierarchy, GroupHierarchy
], [Array, Array]
# [Group
Descendant, Array], [GroupDescendant, GroupDescendant
], [Array, Array]
else
Array
.
wrap
(
first_child
)
+
Array
.
wrap
(
second_child
)
end
...
...
app/models/group.rb
View file @
22aa0344
...
...
@@ -6,7 +6,7 @@ class Group < Namespace
include
Avatarable
include
Referable
include
SelectForProjectAuthorization
include
Group
Hierarchy
include
Group
Descendant
has_many
:group_members
,
->
{
where
(
requested_at:
nil
)
},
dependent: :destroy
,
as: :source
# rubocop:disable Cop/ActiveRecordDependent
alias_method
:members
,
:group_members
...
...
app/models/project.rb
View file @
22aa0344
...
...
@@ -17,7 +17,7 @@ class Project < ActiveRecord::Base
include
ProjectFeaturesCompatibility
include
SelectForProjectAuthorization
include
Routable
include
Group
Hierarchy
include
Group
Descendant
extend
Gitlab
::
ConfigHelper
extend
Gitlab
::
CurrentSettings
...
...
@@ -1521,10 +1521,6 @@ class Project < ActiveRecord::Base
map
.
public_path_for_source_path
(
path
)
end
def
parent
namespace
end
def
parent_changed?
namespace_id_changed?
end
...
...
app/serializers/group_child_entity.rb
View file @
22aa0344
...
...
@@ -55,8 +55,8 @@ class GroupChildEntity < Grape::Entity
unless:
lambda
{
|
_instance
,
_options
|
project?
}
def
children_finder
@children_finder
||=
Group
Children
Finder
.
new
(
current_user:
request
.
current_user
,
parent_group:
object
)
@children_finder
||=
Group
Descendants
Finder
.
new
(
current_user:
request
.
current_user
,
parent_group:
object
)
end
def
children_count
...
...
app/serializers/group_child_serializer.rb
View file @
22aa0344
...
...
@@ -24,10 +24,10 @@ class GroupChildSerializer < BaseSerializer
protected
def
represent_hierarchies
(
children
,
opts
)
if
children
.
is_a?
(
Group
Hierarchy
)
if
children
.
is_a?
(
Group
Descendant
)
represent_hierarchy
(
children
.
hierarchy
(
hierarchy_root
),
opts
).
first
else
hierarchies
=
Array
.
wrap
(
Group
Hierarchy
.
merge_hierarchies
(
children
,
hierarchy_root
))
hierarchies
=
Array
.
wrap
(
Group
Descendant
.
merge_hierarchies
(
children
,
hierarchy_root
))
hierarchies
.
map
{
|
hierarchy
|
represent_hierarchy
(
hierarchy
,
opts
)
}.
flatten
end
end
...
...
spec/finders/group_
children
_finder_spec.rb
→
spec/finders/group_
descendants
_finder_spec.rb
View file @
22aa0344
require
'spec_helper'
describe
Group
Children
Finder
do
describe
Group
Descendants
Finder
do
let
(
:user
)
{
create
(
:user
)
}
let
(
:group
)
{
create
(
:group
)
}
let
(
:params
)
{
{}
}
...
...
spec/models/concerns/group_
hierarchy
_spec.rb
→
spec/models/concerns/group_
descendant
_spec.rb
View file @
22aa0344
require
'spec_helper'
describe
Group
Hierarchy
,
:nested_groups
do
describe
Group
Descendant
,
:nested_groups
do
let
(
:parent
)
{
create
(
:group
)
}
let
(
:subgroup
)
{
create
(
:group
,
parent:
parent
)
}
let
(
:subsub_group
)
{
create
(
:group
,
parent:
subgroup
)
}
...
...
@@ -141,6 +141,33 @@ describe GroupHierarchy, :nested_groups do
expect
(
described_class
.
merge_hierarchies
([
parent
,
subgroup
])).
to
eq
(
expected_hierarchy
)
end
it
'merges complex hierarchies'
do
project
=
create
(
:project
,
namespace:
parent
)
sub_project
=
create
(
:project
,
namespace:
subgroup
)
subsubsub_group
=
create
(
:group
,
parent:
subsub_group
)
subsub_project
=
create
(
:project
,
namespace:
subsub_group
)
subsubsub_project
=
create
(
:project
,
namespace:
subsubsub_group
)
other_subgroup
=
create
(
:group
,
parent:
parent
)
other_subproject
=
create
(
:project
,
namespace:
other_subgroup
)
projects
=
[
project
,
subsubsub_project
,
sub_project
,
other_subproject
,
subsub_project
]
expected_hierarchy
=
[
project
,
{
subgroup
=>
[
{
subsub_group
=>
[{
subsubsub_group
=>
subsubsub_project
},
subsub_project
]
},
sub_project
]
},
{
other_subgroup
=>
other_subproject
}
]
actual_hierarchy
=
described_class
.
merge_hierarchies
(
projects
,
parent
)
expect
(
actual_hierarchy
).
to
eq
(
expected_hierarchy
)
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