diff --git a/changelogs/unreleased/fix-40798-namespace-forking.yml b/changelogs/unreleased/fix-40798-namespace-forking.yml new file mode 100644 index 00000000000..095235725f8 --- /dev/null +++ b/changelogs/unreleased/fix-40798-namespace-forking.yml @@ -0,0 +1,5 @@ +--- +title: Fix forking to subgroup via API when namespace is given by name +merge_request: 17815 +author: Jan Beckmann +type: fixed diff --git a/lib/api/projects.rb b/lib/api/projects.rb index b552b0e0c5d..467bc78dad8 100644 --- a/lib/api/projects.rb +++ b/lib/api/projects.rb @@ -228,11 +228,7 @@ module API namespace_id = fork_params[:namespace] if namespace_id.present? - fork_params[:namespace] = if namespace_id =~ /^\d+$/ - Namespace.find_by(id: namespace_id) - else - Namespace.find_by_path_or_name(namespace_id) - end + fork_params[:namespace] = find_namespace(namespace_id) unless fork_params[:namespace] && can?(current_user, :create_projects, fork_params[:namespace]) not_found!('Target Namespace') diff --git a/lib/api/v3/projects.rb b/lib/api/v3/projects.rb index 7d8b1f369fe..a2df969d819 100644 --- a/lib/api/v3/projects.rb +++ b/lib/api/v3/projects.rb @@ -268,11 +268,7 @@ module API namespace_id = fork_params[:namespace] if namespace_id.present? - fork_params[:namespace] = if namespace_id =~ /^\d+$/ - Namespace.find_by(id: namespace_id) - else - Namespace.find_by_path_or_name(namespace_id) - end + fork_params[:namespace] = find_namespace(namespace_id) unless fork_params[:namespace] && can?(current_user, :create_projects, fork_params[:namespace]) not_found!('Target Namespace') diff --git a/spec/requests/api/projects_spec.rb b/spec/requests/api/projects_spec.rb index cee93f6ed14..d73a42f48ad 100644 --- a/spec/requests/api/projects_spec.rb +++ b/spec/requests/api/projects_spec.rb @@ -1718,6 +1718,12 @@ describe API::Projects do group end + let(:group3) do + group = create(:group, name: 'group3_name', parent: group2) + group.add_owner(user2) + group + end + before do project.add_reporter(user2) end @@ -1813,6 +1819,15 @@ describe API::Projects do expect(json_response['namespace']['name']).to eq(group2.name) end + it 'forks to owned subgroup' do + full_path = "#{group2.path}/#{group3.path}" + post api("/projects/#{project.id}/fork", user2), namespace: full_path + + expect(response).to have_gitlab_http_status(201) + expect(json_response['namespace']['name']).to eq(group3.name) + expect(json_response['namespace']['full_path']).to eq(full_path) + end + it 'fails to fork to not owned group' do post api("/projects/#{project.id}/fork", user2), namespace: group.name