diff --git a/app/controllers/concerns/group_tree.rb b/app/controllers/concerns/group_tree.rb index 6d5682ff769..d094216aa58 100644 --- a/app/controllers/concerns/group_tree.rb +++ b/app/controllers/concerns/group_tree.rb @@ -1,8 +1,12 @@ module GroupTree def render_group_tree(groups) - # Only show root groups if no parent-id is given - @groups = groups.where(parent_id: params[:parent_id]) - @groups = @groups.search(params[:filter]) if params[:filter].present? + if params[:filter].present? + @groups = Gitlab::GroupHierarchy.new(groups).all_groups + @groups = @groups.search(params[:filter]) + else + # Only show root groups if no parent-id is given + @groups = groups.where(parent_id: params[:parent_id]) + end @groups = @groups.includes(:route) @groups = @groups.sort(@sort = params[:sort]) @groups = @groups.page(params[:page]) @@ -12,6 +16,7 @@ module GroupTree format.json do serializer = GroupChildSerializer.new(current_user: current_user) .with_pagination(request, response) + serializer.expand_hierarchy if params[:filter].present? render json: serializer.represent(@groups) end end diff --git a/spec/controllers/concerns/group_tree_spec.rb b/spec/controllers/concerns/group_tree_spec.rb index 19387f2d271..5d4fb66b492 100644 --- a/spec/controllers/concerns/group_tree_spec.rb +++ b/spec/controllers/concerns/group_tree_spec.rb @@ -5,7 +5,8 @@ describe GroupTree do let(:user) { create(:user) } controller(ApplicationController) do - include GroupTree # rubocop:disable Rspec/DescribedClass + # `described_class` is not available in this context + include GroupTree # rubocop:disable RSpec/DescribedClass def index render_group_tree Group.all @@ -43,6 +44,14 @@ describe GroupTree do expect(assigns(:groups)).to contain_exactly(subgroup) end + + it 'allows filtering for subgroups' do + subgroup = create(:group, :public, parent: group, name: 'filter') + + get :index, filter: 'filt', format: :json + + expect(assigns(:groups)).to contain_exactly(subgroup) + end end context 'json content' do @@ -51,6 +60,19 @@ describe GroupTree do expect(json_response.first['id']).to eq(group.id) end + + context 'nested groups', :nested_groups do + it 'expands the tree when filtering' do + subgroup = create(:group, :public, parent: group, name: 'filter') + + get :index, filter: 'filt', format: :json + + children_response = json_response.first['children'] + + expect(json_response.first['id']).to eq(group.id) + expect(children_response.first['id']).to eq(subgroup.id) + end + end end end end diff --git a/spec/controllers/explore/groups_controller_spec.rb b/spec/controllers/explore/groups_controller_spec.rb index 1923d054e95..9e0ad9ea86f 100644 --- a/spec/controllers/explore/groups_controller_spec.rb +++ b/spec/controllers/explore/groups_controller_spec.rb @@ -20,5 +20,4 @@ describe Explore::GroupsController do expect(assigns(:groups)).to contain_exactly(member_of_group, public_group) end - end