Commit 7bda030d authored by Douglas Barbosa Alexandre's avatar Douglas Barbosa Alexandre

Merge branch '31092-user-search-filtered-to-group-times-out' into 'master'

RUN AS-IF-FOSS Fix group search users scope times out

See merge request gitlab-org/gitlab!38701
parents 3ca6d55b 70534bc5
---
title: Improve group search users scope performance
merge_request: 38701
author:
type: performance
......@@ -12,20 +12,24 @@ module Gitlab
# rubocop:disable CodeReuse/ActiveRecord
def users
# 1: get all groups the current user has access to
groups = GroupsFinder.new(current_user).execute.joins(:users)
# get all groups the current user has access to
# ignore order inherited from GroupsFinder to improve performance
current_user_groups = GroupsFinder.new(current_user).execute.unscope(:order)
# 2: Get the group's whole hierarchy
group_users = @group.direct_and_indirect_users
# the hierarchy of the current group
group_groups = @group.self_and_hierarchy.unscope(:order)
# 3: get all users the current user has access to (->
# `SearchResults#users`), which also applies the query.
# the groups where the above hierarchies intersect
intersect_groups = group_groups.where(id: current_user_groups)
# members of @group hierarchy where the user has access to the groups
members = GroupMember.where(group: intersect_groups).non_invite
# get all users the current user has access to (-> `SearchResults#users`), which also applies the query
users = super
# 4: filter for users that belong to the previously selected groups
users
.where(id: group_users.select('id'))
.where(id: groups.select('members.user_id'))
# filter users that belong to the previously selected groups
users.where(id: members.select(:user_id))
end
# rubocop:enable CodeReuse/ActiveRecord
......
......@@ -3,8 +3,10 @@
require 'spec_helper'
RSpec.describe Gitlab::GroupSearchResults do
# group creation calls GroupFinder, so need to create the group
# before so expect(GroupsFinder) check works
let_it_be(:group) { create(:group) }
let(:user) { create(:user) }
let(:group) { create(:group) }
subject(:results) { described_class.new(user, 'gob', anything, group: group) }
......@@ -60,6 +62,19 @@ RSpec.describe Gitlab::GroupSearchResults do
is_expected.to be_empty
end
it 'does not return the user invited to the group' do
user = create(:user, username: 'gob_bluth')
create(:group_member, :invited, :developer, user: user, group: group)
is_expected.to be_empty
end
it 'calls GroupFinder during execution' do
expect(GroupsFinder).to receive(:new).with(user).and_call_original
subject
end
end
describe "#issuable_params" do
......
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