2014-05-28 16:02:26 +00:00
|
|
|
# Projects::TransferService class
|
|
|
|
#
|
|
|
|
# Used for transfer project to another namespace
|
|
|
|
#
|
|
|
|
# Ex.
|
|
|
|
# # Move projects to namespace with ID 17 by user
|
|
|
|
# Projects::TransferService.new(project, user, namespace_id: 17).execute
|
|
|
|
#
|
2013-03-25 08:47:22 +00:00
|
|
|
module Projects
|
2014-01-16 17:03:42 +00:00
|
|
|
class TransferService < BaseService
|
2014-05-28 16:02:26 +00:00
|
|
|
include Gitlab::ShellAdapter
|
|
|
|
class TransferError < StandardError; end
|
|
|
|
|
2015-07-02 12:31:25 +00:00
|
|
|
def execute(new_namespace)
|
|
|
|
if allowed_transfer?(current_user, project, new_namespace)
|
|
|
|
transfer(project, new_namespace)
|
2014-05-28 16:02:26 +00:00
|
|
|
else
|
2015-07-02 12:31:25 +00:00
|
|
|
project.errors.add(:new_namespace, 'is invalid')
|
2014-05-28 16:02:26 +00:00
|
|
|
false
|
|
|
|
end
|
|
|
|
rescue Projects::TransferService::TransferError => ex
|
2013-03-25 08:47:22 +00:00
|
|
|
project.reload
|
2015-07-02 12:31:25 +00:00
|
|
|
project.errors.add(:new_namespace, ex.message)
|
2013-03-25 08:47:22 +00:00
|
|
|
false
|
|
|
|
end
|
2014-05-28 16:02:26 +00:00
|
|
|
|
|
|
|
def transfer(project, new_namespace)
|
|
|
|
Project.transaction do
|
|
|
|
old_path = project.path_with_namespace
|
2015-10-06 14:09:03 +00:00
|
|
|
old_namespace = project.namespace
|
2016-09-26 20:10:54 +00:00
|
|
|
old_group = project.group
|
2014-05-28 16:02:26 +00:00
|
|
|
new_path = File.join(new_namespace.try(:path) || '', project.path)
|
|
|
|
|
|
|
|
if Project.where(path: project.path, namespace_id: new_namespace.try(:id)).present?
|
|
|
|
raise TransferError.new("Project with same path in target namespace already exists")
|
|
|
|
end
|
|
|
|
|
2016-05-09 19:41:48 +00:00
|
|
|
if project.has_container_registry_tags?
|
|
|
|
# we currently doesn't support renaming repository if it contains tags in container registry
|
2016-05-12 18:03:04 +00:00
|
|
|
raise TransferError.new('Project cannot be transferred, because tags are present in its container registry')
|
2016-05-09 19:41:48 +00:00
|
|
|
end
|
|
|
|
|
2016-04-22 07:39:31 +00:00
|
|
|
project.expire_caches_before_rename(old_path)
|
|
|
|
|
2016-04-13 18:28:10 +00:00
|
|
|
# Apply new namespace id and visibility level
|
2016-04-14 14:34:42 +00:00
|
|
|
project.namespace = new_namespace
|
|
|
|
project.visibility_level = new_namespace.visibility_level unless project.visibility_level_allowed_by_group?
|
|
|
|
project.save!
|
2014-05-28 16:02:26 +00:00
|
|
|
|
2015-03-25 16:18:26 +00:00
|
|
|
# Notifications
|
2015-09-29 13:37:50 +00:00
|
|
|
project.send_move_instructions(old_path)
|
2015-03-25 16:18:26 +00:00
|
|
|
|
2014-05-28 16:02:26 +00:00
|
|
|
# Move main repository
|
2016-06-22 21:04:51 +00:00
|
|
|
unless gitlab_shell.mv_repository(project.repository_storage_path, old_path, new_path)
|
2014-05-28 16:02:26 +00:00
|
|
|
raise TransferError.new('Cannot move project')
|
|
|
|
end
|
|
|
|
|
|
|
|
# Move wiki repo also if present
|
2016-06-22 21:04:51 +00:00
|
|
|
gitlab_shell.mv_repository(project.repository_storage_path, "#{old_path}.wiki", "#{new_path}.wiki")
|
2014-05-28 16:02:26 +00:00
|
|
|
|
2016-09-26 20:10:54 +00:00
|
|
|
# Move missing group labels to project
|
|
|
|
Labels::TransferService.new(current_user, old_group, project).execute
|
|
|
|
|
2015-10-06 14:09:03 +00:00
|
|
|
# Move uploads
|
|
|
|
Gitlab::UploadsTransfer.new.move_project(project.path, old_namespace.path, new_namespace.path)
|
|
|
|
|
2015-05-05 15:13:11 +00:00
|
|
|
project.old_path_with_namespace = old_path
|
2016-04-13 18:28:10 +00:00
|
|
|
|
2015-05-05 15:13:11 +00:00
|
|
|
SystemHooksService.new.execute_hooks_for(project, :transfer)
|
2014-05-28 16:02:26 +00:00
|
|
|
true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def allowed_transfer?(current_user, project, namespace)
|
|
|
|
namespace &&
|
|
|
|
can?(current_user, :change_namespace, project) &&
|
|
|
|
namespace.id != project.namespace_id &&
|
|
|
|
current_user.can?(:create_projects, namespace)
|
|
|
|
end
|
2013-03-25 08:47:22 +00:00
|
|
|
end
|
|
|
|
end
|