Commit 2b40566e authored by Heinrich Lee Yu's avatar Heinrich Lee Yu

Merge branch '329751_fix_n_1_problem' into 'master'

Fix N+1 problem for fork controller

See merge request gitlab-org/gitlab!63436
parents 70264626 945abee1
......@@ -31,7 +31,11 @@ class ForkNamespaceEntity < Grape::Entity
end
expose :can_create_project do |namespace, options|
options[:current_user].can?(:create_projects, namespace)
if Feature.enabled?(:fork_project_form, options[:project], default_enabled: :yaml)
true
else
options[:current_user].can?(:create_projects, namespace)
end
end
private
......
......@@ -211,11 +211,7 @@ RSpec.describe Projects::ForksController do
create(:group, :public).add_owner(user)
# TODO: There is another N+1 caused by user.can?(:create_projects, namespace)
# Defined in ForkNamespaceEntity
extra_count = 1
expect { do_request.call }.not_to exceed_query_limit(control.count + extra_count)
expect { do_request.call }.not_to exceed_query_limit(control)
end
end
end
......
......@@ -60,15 +60,25 @@ RSpec.describe ForkNamespaceEntity do
expect(json[:permission]).to eql 'Developer'
end
it 'sets can_create_project to true when user can create projects in namespace' do
allow(user).to receive(:can?).with(:create_projects, namespace).and_return(true)
it 'exposes can_create_project' do
expect(json[:can_create_project]).to be true
end
it 'sets can_create_project to false when user is not allowed create projects in namespace' do
allow(user).to receive(:can?).with(:create_projects, namespace).and_return(false)
context 'when fork_project_form feature flag is disabled' do
before do
stub_feature_flags(fork_project_form: false)
end
it 'sets can_create_project to true when user can create projects in namespace' do
allow(user).to receive(:can?).with(:create_projects, namespace).and_return(true)
expect(json[:can_create_project]).to be false
expect(json[:can_create_project]).to be true
end
it 'sets can_create_project to false when user is not allowed create projects in namespace' do
allow(user).to receive(:can?).with(:create_projects, namespace).and_return(false)
expect(json[:can_create_project]).to be false
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