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