Commit f100790b authored by Eugenia Grieff's avatar Eugenia Grieff

Rename finder param to include_parent_descendants

- Add tests for new finder param
include_parent_descendants
parent 1fda2307
......@@ -12,7 +12,7 @@
# all_available: boolean (defaults to true)
# min_access_level: integer
# exclude_group_ids: array of integers
# include_descendants: boolean (defaults to false)
# include_parent_descendants: boolean (defaults to false)
#
# Users with full private access can see all groups. The `owned` and `parent`
# params can be used to restrict the groups that are returned.
......@@ -85,8 +85,9 @@ class GroupsFinder < UnionFinder
def by_parent(groups)
return groups unless params[:parent]
if params[:include_descendants]
groups.id_in(hierarchy_for_parent(params[:parent]).descendants.pluck(:id))
if include_parent_descendants?
descendants = Gitlab::ObjectHierarchy.new(Group.where(id: params[:parent])).descendants
groups.id_in(descendants.pluck(:id))
else
groups.where(parent: params[:parent])
end
......@@ -105,13 +106,11 @@ class GroupsFinder < UnionFinder
params.fetch(:all_available, true)
end
def min_access_level?
current_user && params[:min_access_level].present?
def include_parent_descendants?
params.fetch(:include_parent_descendants, false)
end
# rubocop: disable CodeReuse/ActiveRecord
def hierarchy_for_parent(parent_group_id)
@hierarchy ||= Gitlab::ObjectHierarchy.new(Group.where(id: parent_group_id))
def min_access_level?
current_user && params[:min_access_level].present?
end
# rubocop: enable CodeReuse/ActiveRecord
end
......@@ -33,7 +33,7 @@ module API
:all_available,
:custom_attributes,
:owned, :min_access_level,
:include_descendants
:include_parent_descendants
)
find_params[:parent] = if params[:top_level_only]
......@@ -322,7 +322,7 @@ module API
use :with_custom_attributes
end
get ":id/descendant_groups" do
finder_params = declared_params(include_missing: false).merge(include_descendants: true)
finder_params = declared_params(include_missing: false).merge(include_parent_descendants: true)
groups = find_groups(finder_params, params[:id])
present_groups params, groups
end
......
......@@ -161,5 +161,61 @@ RSpec.describe GroupsFinder do
end
end
end
context 'with include parent group descendants' do
let_it_be(:user) { create(:user) }
let_it_be(:parent_group) { create(:group, :public) }
let_it_be(:public_subgroup) { create(:group, :public, parent: parent_group) }
let_it_be(:internal_sub_subgroup) { create(:group, :internal, parent: public_subgroup) }
let_it_be(:private_sub_subgroup) { create(:group, :private, parent: public_subgroup) }
let_it_be(:public_sub_subgroup) { create(:group, :public, parent: public_subgroup) }
let(:params) { { include_parent_descendants: true, parent: parent_group } }
context 'with nil parent' do
it 'returns all accessible groups' do
params[:parent] = nil
expect(described_class.new(user, params).execute).to contain_exactly(
parent_group,
public_subgroup,
internal_sub_subgroup,
public_sub_subgroup
)
end
end
context 'without a user' do
it 'only returns the group public descendants' do
expect(described_class.new(nil, params).execute).to contain_exactly(
public_subgroup,
public_sub_subgroup
)
end
end
context 'when a user is present' do
it 'returns the group public and internal descendants' do
expect(described_class.new(user, params).execute).to contain_exactly(
public_subgroup,
public_sub_subgroup,
internal_sub_subgroup
)
end
end
context 'when a parent group member is present' do
before do
parent_group.add_developer(user)
end
it 'returns all group descendants' do
expect(described_class.new(user, params).execute).to contain_exactly(
public_subgroup,
public_sub_subgroup,
internal_sub_subgroup,
private_sub_subgroup
)
end
end
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment