Commit f566268a authored by Bob Van Landuyt's avatar Bob Van Landuyt

Merge branch '70495-fj-use-group_tree-ancestors_linear_scopes' into 'master'

Use GroupTree ancestors linear scopes

See merge request gitlab-org/gitlab!70503
parents aa8d3dd8 c1f9f330
...@@ -38,8 +38,13 @@ module GroupTree ...@@ -38,8 +38,13 @@ module GroupTree
# #
# Pagination needs to be applied before loading the ancestors to # Pagination needs to be applied before loading the ancestors to
# make sure ancestors are not cut off by pagination. # make sure ancestors are not cut off by pagination.
Gitlab::ObjectHierarchy.new(Group.where(id: filtered_groups.select(:id))) filtered_groups_relation = Group.where(id: filtered_groups.select(:id))
.base_and_ancestors
if Feature.enabled?(:linear_group_tree_ancestor_scopes, current_user, default_enabled: :yaml)
filtered_groups_relation.self_and_ancestors
else
Gitlab::ObjectHierarchy.new(filtered_groups_relation).base_and_ancestors
end
end end
# rubocop: enable CodeReuse/ActiveRecord # rubocop: enable CodeReuse/ActiveRecord
end end
---
name: linear_group_tree_ancestor_scopes
introduced_by_url: https://gitlab.com/gitlab-org/gitlab/-/merge_requests/70503
rollout_issue_url: https://gitlab.com/gitlab-org/gitlab/-/issues/341117
milestone: '14.4'
type: development
group: group::access
default_enabled: false
...@@ -21,82 +21,94 @@ RSpec.describe GroupTree do ...@@ -21,82 +21,94 @@ RSpec.describe GroupTree do
end end
describe 'GET #index' do describe 'GET #index' do
it 'filters groups' do shared_examples 'returns filtered groups' do
other_group = create(:group, name: 'filter') it 'filters groups' do
other_group.add_owner(user) other_group = create(:group, name: 'filter')
other_group.add_owner(user)
get :index, params: { filter: 'filt' }, format: :json get :index, params: { filter: 'filt' }, format: :json
expect(assigns(:groups)).to contain_exactly(other_group) expect(assigns(:groups)).to contain_exactly(other_group)
end end
context 'for subgroups' do context 'for subgroups' do
it 'only renders root groups when no parent was given' do it 'only renders root groups when no parent was given' do
create(:group, :public, parent: group) create(:group, :public, parent: group)
get :index, format: :json get :index, format: :json
expect(assigns(:groups)).to contain_exactly(group) expect(assigns(:groups)).to contain_exactly(group)
end end
it 'contains only the subgroup when a parent was given' do it 'contains only the subgroup when a parent was given' do
subgroup = create(:group, :public, parent: group) subgroup = create(:group, :public, parent: group)
get :index, params: { parent_id: group.id }, format: :json get :index, params: { parent_id: group.id }, format: :json
expect(assigns(:groups)).to contain_exactly(subgroup) expect(assigns(:groups)).to contain_exactly(subgroup)
end end
it 'allows filtering for subgroups and includes the parents for rendering' do it 'allows filtering for subgroups and includes the parents for rendering' do
subgroup = create(:group, :public, parent: group, name: 'filter') subgroup = create(:group, :public, parent: group, name: 'filter')
get :index, params: { filter: 'filt' }, format: :json get :index, params: { filter: 'filt' }, format: :json
expect(assigns(:groups)).to contain_exactly(group, subgroup) expect(assigns(:groups)).to contain_exactly(group, subgroup)
end end
it 'does not include groups the user does not have access to' do it 'does not include groups the user does not have access to' do
parent = create(:group, :private) parent = create(:group, :private)
subgroup = create(:group, :private, parent: parent, name: 'filter') subgroup = create(:group, :private, parent: parent, name: 'filter')
subgroup.add_developer(user) subgroup.add_developer(user)
_other_subgroup = create(:group, :private, parent: parent, name: 'filte') _other_subgroup = create(:group, :private, parent: parent, name: 'filte')
get :index, params: { filter: 'filt' }, format: :json get :index, params: { filter: 'filt' }, format: :json
expect(assigns(:groups)).to contain_exactly(parent, subgroup) expect(assigns(:groups)).to contain_exactly(parent, subgroup)
end end
it 'preloads parents regardless of pagination' do it 'preloads parents regardless of pagination' do
allow(Kaminari.config).to receive(:default_per_page).and_return(1) allow(Kaminari.config).to receive(:default_per_page).and_return(1)
group = create(:group, :public) group = create(:group, :public)
subgroup = create(:group, :public, parent: group) subgroup = create(:group, :public, parent: group)
search_result = create(:group, :public, name: 'result', parent: subgroup) search_result = create(:group, :public, name: 'result', parent: subgroup)
get :index, params: { filter: 'resu' }, format: :json get :index, params: { filter: 'resu' }, format: :json
expect(assigns(:groups)).to contain_exactly(group, subgroup, search_result) expect(assigns(:groups)).to contain_exactly(group, subgroup, search_result)
end
end end
end
context 'json content' do context 'json content' do
it 'shows groups as json' do it 'shows groups as json' do
get :index, format: :json get :index, format: :json
expect(json_response.first['id']).to eq(group.id) expect(json_response.first['id']).to eq(group.id)
end end
context 'nested groups' do context 'nested groups' do
it 'expands the tree when filtering' do it 'expands the tree when filtering' do
subgroup = create(:group, :public, parent: group, name: 'filter') subgroup = create(:group, :public, parent: group, name: 'filter')
get :index, params: { filter: 'filt' }, format: :json get :index, params: { filter: 'filt' }, format: :json
children_response = json_response.first['children'] children_response = json_response.first['children']
expect(json_response.first['id']).to eq(group.id) expect(json_response.first['id']).to eq(group.id)
expect(children_response.first['id']).to eq(subgroup.id) expect(children_response.first['id']).to eq(subgroup.id)
end
end end
end end
end end
it_behaves_like 'returns filtered groups'
context 'when feature flag :linear_group_tree_ancestor_scopes is disabled' do
before do
stub_feature_flags(linear_group_tree_ancestor_scopes: false)
end
it_behaves_like 'returns filtered groups'
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