2017-09-19 07:11:09 -04:00
|
|
|
module GroupDescendant
|
2017-10-10 09:45:35 -04:00
|
|
|
# Returns the hierarchy of a project or group in the from of a hash upto a
|
|
|
|
# given top.
|
|
|
|
#
|
|
|
|
# > project.hierarchy
|
|
|
|
# => { parent_group => { child_group => project } }
|
2017-10-02 12:21:18 -04:00
|
|
|
def hierarchy(hierarchy_top = nil, preloaded = nil)
|
|
|
|
preloaded ||= ancestors_upto(hierarchy_top)
|
2017-09-26 05:22:52 -04:00
|
|
|
expand_hierarchy_for_child(self, self, hierarchy_top, preloaded)
|
2017-09-06 10:29:52 -04:00
|
|
|
end
|
|
|
|
|
2017-10-10 09:45:35 -04:00
|
|
|
# Merges all hierarchies of the given groups or projects into an array of
|
|
|
|
# hashes. All ancestors need to be loaded into the given `descendants` to avoid
|
|
|
|
# queries down the line.
|
|
|
|
#
|
|
|
|
# > GroupDescendant.merge_hierarchy([project, child_group, child_group2, parent])
|
|
|
|
# => { parent => [{ child_group => project}, child_group2] }
|
2017-09-22 11:36:39 -04:00
|
|
|
def self.build_hierarchy(descendants, hierarchy_top = nil)
|
2017-10-04 10:56:42 -04:00
|
|
|
descendants = Array.wrap(descendants).uniq
|
2017-10-03 09:32:32 -04:00
|
|
|
return [] if descendants.empty?
|
2017-09-07 13:08:56 -04:00
|
|
|
|
2017-09-22 11:36:39 -04:00
|
|
|
unless descendants.all? { |hierarchy| hierarchy.is_a?(GroupDescendant) }
|
2017-09-07 13:08:56 -04:00
|
|
|
raise ArgumentError.new('element is not a hierarchy')
|
|
|
|
end
|
|
|
|
|
2017-10-04 10:56:42 -04:00
|
|
|
all_hierarchies = descendants.map do |descendant|
|
|
|
|
descendant.hierarchy(hierarchy_top, descendants)
|
2017-09-07 13:08:56 -04:00
|
|
|
end
|
|
|
|
|
2017-10-04 10:56:42 -04:00
|
|
|
Gitlab::Utils::MergeHash.merge(all_hierarchies)
|
2017-09-07 13:08:56 -04:00
|
|
|
end
|
|
|
|
|
2017-10-02 08:23:36 -04:00
|
|
|
private
|
|
|
|
|
2017-10-02 12:21:18 -04:00
|
|
|
def expand_hierarchy_for_child(child, hierarchy, hierarchy_top, preloaded)
|
2017-10-04 10:57:33 -04:00
|
|
|
parent = hierarchy_top if hierarchy_top && child.parent_id == hierarchy_top.id
|
|
|
|
parent ||= preloaded.detect { |possible_parent| possible_parent.is_a?(Group) && possible_parent.id == child.parent_id }
|
|
|
|
|
|
|
|
if parent.nil? && !child.parent_id.nil?
|
2018-04-13 12:52:28 -04:00
|
|
|
parent = child.parent
|
|
|
|
|
|
|
|
exception = ArgumentError.new <<~MSG
|
|
|
|
parent: [GroupDescendant: #{parent.inspect}] was not preloaded for [#{child.inspect}]")
|
|
|
|
This error is not user facing, but causes a +1 query.
|
|
|
|
MSG
|
|
|
|
extras = {
|
|
|
|
parent: parent,
|
|
|
|
child: child,
|
|
|
|
preloaded: preloaded.map(&:full_path)
|
|
|
|
}
|
|
|
|
issue_url = 'https://gitlab.com/gitlab-org/gitlab-ce/issues/40785'
|
|
|
|
|
|
|
|
Gitlab::Sentry.track_exception(exception, issue_url: issue_url, extra: extras)
|
2017-10-04 10:57:33 -04:00
|
|
|
end
|
2017-10-02 08:23:36 -04:00
|
|
|
|
|
|
|
if parent.nil? && hierarchy_top.present?
|
2017-10-02 12:21:18 -04:00
|
|
|
raise ArgumentError.new('specified top is not part of the tree')
|
2017-10-02 08:23:36 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
if parent && parent != hierarchy_top
|
|
|
|
expand_hierarchy_for_child(parent,
|
|
|
|
{ parent => hierarchy },
|
|
|
|
hierarchy_top,
|
|
|
|
preloaded)
|
|
|
|
else
|
|
|
|
hierarchy
|
|
|
|
end
|
|
|
|
end
|
2017-09-06 10:29:52 -04:00
|
|
|
end
|