Use GroupTree ancestors linear scopes

In this commit we're replacing the recursive ancestors scope in
the `GroupTree` concern, for their linear version.

Changelog: performance
parent 52e0b948
......@@ -38,8 +38,13 @@ module GroupTree
#
# Pagination needs to be applied before loading the ancestors to
# make sure ancestors are not cut off by pagination.
Gitlab::ObjectHierarchy.new(Group.where(id: filtered_groups.select(:id)))
.base_and_ancestors
filtered_groups_relation = Group.where(id: filtered_groups.select(:id))
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
# rubocop: enable CodeReuse/ActiveRecord
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
end
describe 'GET #index' do
it 'filters groups' do
other_group = create(:group, name: 'filter')
other_group.add_owner(user)
shared_examples 'returns filtered groups' do
it 'filters groups' do
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)
end
expect(assigns(:groups)).to contain_exactly(other_group)
end
context 'for subgroups' do
it 'only renders root groups when no parent was given' do
create(:group, :public, parent: group)
context 'for subgroups' do
it 'only renders root groups when no parent was given' do
create(:group, :public, parent: group)
get :index, format: :json
get :index, format: :json
expect(assigns(:groups)).to contain_exactly(group)
end
expect(assigns(:groups)).to contain_exactly(group)
end
it 'contains only the subgroup when a parent was given' do
subgroup = create(:group, :public, parent: group)
it 'contains only the subgroup when a parent was given' do
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)
end
expect(assigns(:groups)).to contain_exactly(subgroup)
end
it 'allows filtering for subgroups and includes the parents for rendering' do
subgroup = create(:group, :public, parent: group, name: 'filter')
it 'allows filtering for subgroups and includes the parents for rendering' do
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)
end
expect(assigns(:groups)).to contain_exactly(group, subgroup)
end
it 'does not include groups the user does not have access to' do
parent = create(:group, :private)
subgroup = create(:group, :private, parent: parent, name: 'filter')
subgroup.add_developer(user)
_other_subgroup = create(:group, :private, parent: parent, name: 'filte')
it 'does not include groups the user does not have access to' do
parent = create(:group, :private)
subgroup = create(:group, :private, parent: parent, name: 'filter')
subgroup.add_developer(user)
_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)
end
expect(assigns(:groups)).to contain_exactly(parent, subgroup)
end
it 'preloads parents regardless of pagination' do
allow(Kaminari.config).to receive(:default_per_page).and_return(1)
group = create(:group, :public)
subgroup = create(:group, :public, parent: group)
search_result = create(:group, :public, name: 'result', parent: subgroup)
it 'preloads parents regardless of pagination' do
allow(Kaminari.config).to receive(:default_per_page).and_return(1)
group = create(:group, :public)
subgroup = create(:group, :public, parent: group)
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
context 'json content' do
it 'shows groups as json' do
get :index, format: :json
context 'json content' do
it 'shows groups as json' do
get :index, format: :json
expect(json_response.first['id']).to eq(group.id)
end
expect(json_response.first['id']).to eq(group.id)
end
context 'nested groups' do
it 'expands the tree when filtering' do
subgroup = create(:group, :public, parent: group, name: 'filter')
context 'nested groups' do
it 'expands the tree when filtering' do
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(children_response.first['id']).to eq(subgroup.id)
expect(json_response.first['id']).to eq(group.id)
expect(children_response.first['id']).to eq(subgroup.id)
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
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