Commit 21b9cf49 authored by Vasilii Iakliushin's avatar Vasilii Iakliushin

Improve error message for TransferService

* Contrbutes to https://gitlab.com/gitlab-org/gitlab/-/issues/27843
* Inspired by
https://gitlab.com/gitlab-org/gitlab/-/merge_requests/68371 (Thanks
@1ace)

**Problem**

Previous error message is not clear and can be triggered by multiple
reasons.

**Solution**

Add descriptive error messages for each error case

Changelog: changed
parent c075322e
......@@ -20,8 +20,16 @@ module Projects
raise TransferError, s_('TransferProject|Please select a new namespace for your project.')
end
unless allowed_transfer?(current_user, project)
raise TransferError, s_('TransferProject|Transfer failed, please contact an admin.')
if @new_namespace.id == project.namespace_id
raise TransferError, s_('TransferProject|Project is already in this namespace.')
end
unless allowed_transfer_project?(current_user, project)
raise TransferError, s_("TransferProject|You don't have permission to transfer this project.")
end
unless allowed_to_transfer_to_namespace?(current_user, @new_namespace)
raise TransferError, s_("TransferProject|You don't have permission to transfer projects into that namespace.")
end
transfer(project)
......@@ -121,11 +129,12 @@ module Projects
Milestones::TransferService.new(current_user, group, project).execute
end
def allowed_transfer?(current_user, project)
@new_namespace &&
can?(current_user, :change_namespace, project) &&
@new_namespace.id != project.namespace_id &&
current_user.can?(:transfer_projects, @new_namespace)
def allowed_transfer_project?(current_user, project)
current_user.can?(:change_namespace, project)
end
def allowed_to_transfer_to_namespace?(current_user, namespace)
current_user.can?(:transfer_projects, namespace)
end
def update_namespace_and_visibility(to_namespace)
......
......@@ -35180,13 +35180,19 @@ msgstr ""
msgid "TransferProject|Project cannot be transferred, because tags are present in its container registry"
msgstr ""
msgid "TransferProject|Project is already in this namespace."
msgstr ""
msgid "TransferProject|Project with same name or path in target namespace already exists"
msgstr ""
msgid "TransferProject|Root namespace can't be updated if project has NPM packages"
msgstr ""
msgid "TransferProject|Transfer failed, please contact an admin."
msgid "TransferProject|You don't have permission to transfer projects into that namespace."
msgstr ""
msgid "TransferProject|You don't have permission to transfer this project."
msgstr ""
msgid "Tree view"
......
......@@ -292,10 +292,37 @@ RSpec.describe Projects::TransferService do
end
end
context 'target namespace allows developers to create projects' do
context 'target namespace matches current namespace' do
let(:group) { user.namespace }
it 'does not allow project transfer' do
transfer_result = execute_transfer
expect(transfer_result).to eq false
expect(project.namespace).to eq(user.namespace)
expect(project.errors[:new_namespace]).to include('Project is already in this namespace.')
end
end
context 'when user does not own the project' do
let(:project) { create(:project, :repository, :legacy_storage) }
before do
project.add_developer(user)
end
it 'does not allow project transfer to the target namespace' do
transfer_result = execute_transfer
expect(transfer_result).to eq false
expect(project.errors[:new_namespace]).to include("You don't have permission to transfer this project.")
end
end
context 'when user can create projects in the target namespace' do
let(:group) { create(:group, project_creation_level: ::Gitlab::Access::DEVELOPER_MAINTAINER_PROJECT_ACCESS) }
context 'the user is a member of the target namespace with developer permissions' do
context 'but has only developer permissions in the target namespace' do
before do
group.add_developer(user)
end
......@@ -305,7 +332,7 @@ RSpec.describe Projects::TransferService do
expect(transfer_result).to eq false
expect(project.namespace).to eq(user.namespace)
expect(project.errors[:new_namespace]).to include('Transfer failed, please contact an admin.')
expect(project.errors[:new_namespace]).to include("You don't have permission to transfer projects into that namespace.")
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