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
|
2017-03-01 11:00:37 +00:00
|
|
|
TransferError = Class.new(StandardError)
|
2014-05-28 16:02:26 +00:00
|
|
|
|
2015-07-02 12:31:25 +00:00
|
|
|
def execute(new_namespace)
|
2017-06-02 13:44:59 +00:00
|
|
|
@new_namespace = new_namespace
|
|
|
|
|
|
|
|
if @new_namespace.blank?
|
2017-05-22 21:04:21 +00:00
|
|
|
raise TransferError, 'Please select a new namespace for your project.'
|
2014-05-28 16:02:26 +00:00
|
|
|
end
|
2017-06-02 13:44:59 +00:00
|
|
|
|
|
|
|
unless allowed_transfer?(current_user, project)
|
2017-05-22 21:04:21 +00:00
|
|
|
raise TransferError, 'Transfer failed, please contact an admin.'
|
|
|
|
end
|
2017-06-02 13:44:59 +00:00
|
|
|
|
|
|
|
transfer(project)
|
|
|
|
|
|
|
|
true
|
2014-05-28 16:02:26 +00:00
|
|
|
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
|
|
|
|
2017-06-02 13:44:59 +00:00
|
|
|
private
|
2017-02-07 13:55:42 +00:00
|
|
|
|
2017-06-02 13:44:59 +00:00
|
|
|
def transfer(project)
|
2017-07-20 09:34:09 +00:00
|
|
|
@old_path = project.full_path
|
2017-06-02 13:44:59 +00:00
|
|
|
@old_group = project.group
|
|
|
|
@new_path = File.join(@new_namespace.try(:full_path) || '', project.path)
|
|
|
|
@old_namespace = project.namespace
|
2014-05-28 16:02:26 +00:00
|
|
|
|
2017-06-02 13:44:59 +00:00
|
|
|
if Project.where(path: project.path, namespace_id: @new_namespace.try(:id)).exists?
|
|
|
|
raise TransferError.new("Project with same path in target namespace already exists")
|
|
|
|
end
|
2014-05-28 16:02:26 +00:00
|
|
|
|
2017-06-02 13:44:59 +00:00
|
|
|
if project.has_container_registry_tags?
|
|
|
|
# We currently don't support renaming repository if it contains tags in container registry
|
|
|
|
raise TransferError.new('Project cannot be transferred, because tags are present in its container registry')
|
|
|
|
end
|
2016-05-09 19:41:48 +00:00
|
|
|
|
2017-06-02 13:44:59 +00:00
|
|
|
attempt_transfer_transaction
|
|
|
|
end
|
|
|
|
|
|
|
|
def attempt_transfer_transaction
|
|
|
|
Project.transaction do
|
|
|
|
project.expire_caches_before_rename(@old_path)
|
2016-04-22 07:39:31 +00:00
|
|
|
|
2017-06-02 13:44:59 +00:00
|
|
|
update_namespace_and_visibility(@new_namespace)
|
2014-05-28 16:02:26 +00:00
|
|
|
|
2015-03-25 16:18:26 +00:00
|
|
|
# Notifications
|
2017-06-02 13:44:59 +00:00
|
|
|
project.send_move_instructions(@old_path)
|
2015-03-25 16:18:26 +00:00
|
|
|
|
2017-11-17 06:39:29 +00:00
|
|
|
# Directories on disk
|
|
|
|
move_project_folders(project)
|
2014-05-28 16:02:26 +00:00
|
|
|
|
2016-09-26 20:10:54 +00:00
|
|
|
# Move missing group labels to project
|
2017-06-02 13:44:59 +00:00
|
|
|
Labels::TransferService.new(current_user, @old_group, project).execute
|
2016-09-26 20:10:54 +00:00
|
|
|
|
2015-10-06 14:09:03 +00:00
|
|
|
# Move uploads
|
2017-11-17 15:32:33 +00:00
|
|
|
move_project_uploads(project)
|
2015-10-06 14:09:03 +00:00
|
|
|
|
2015-12-18 11:40:34 +00:00
|
|
|
# Move pages
|
2017-06-02 13:44:59 +00:00
|
|
|
Gitlab::PagesTransfer.new.move_project(project.path, @old_namespace.full_path, @new_namespace.full_path)
|
2015-12-18 11:40:34 +00:00
|
|
|
|
2017-06-02 13:44:59 +00:00
|
|
|
project.old_path_with_namespace = @old_path
|
2017-06-29 22:11:33 +00:00
|
|
|
project.expires_full_path_cache
|
2016-04-13 18:28:10 +00:00
|
|
|
|
2017-12-19 19:18:26 +00:00
|
|
|
write_repository_config(@new_path)
|
|
|
|
|
2017-06-02 13:44:59 +00:00
|
|
|
execute_system_hooks
|
2014-05-28 16:02:26 +00:00
|
|
|
end
|
2017-06-02 13:44:59 +00:00
|
|
|
rescue Exception # rubocop:disable Lint/RescueException
|
|
|
|
rollback_side_effects
|
|
|
|
raise
|
|
|
|
ensure
|
|
|
|
refresh_permissions
|
2014-05-28 16:02:26 +00:00
|
|
|
end
|
|
|
|
|
2017-06-02 13:44:59 +00:00
|
|
|
def allowed_transfer?(current_user, project)
|
|
|
|
@new_namespace &&
|
2014-05-28 16:02:26 +00:00
|
|
|
can?(current_user, :change_namespace, project) &&
|
2017-06-02 13:44:59 +00:00
|
|
|
@new_namespace.id != project.namespace_id &&
|
|
|
|
current_user.can?(:create_projects, @new_namespace)
|
2014-05-28 16:02:26 +00:00
|
|
|
end
|
2017-02-07 13:55:42 +00:00
|
|
|
|
2017-06-02 13:44:59 +00:00
|
|
|
def update_namespace_and_visibility(to_namespace)
|
|
|
|
# Apply new namespace id and visibility level
|
|
|
|
project.namespace = to_namespace
|
|
|
|
project.visibility_level = to_namespace.visibility_level unless project.visibility_level_allowed_by_group?
|
|
|
|
project.save!
|
|
|
|
end
|
|
|
|
|
2017-12-19 19:18:26 +00:00
|
|
|
def write_repository_config(full_path)
|
2017-12-21 15:58:36 +00:00
|
|
|
project.write_repository_config(:gl_fullpath, full_path)
|
2017-12-19 19:18:26 +00:00
|
|
|
end
|
|
|
|
|
2017-06-02 13:44:59 +00:00
|
|
|
def refresh_permissions
|
2017-02-07 13:55:42 +00:00
|
|
|
# This ensures we only schedule 1 job for every user that has access to
|
|
|
|
# the namespaces.
|
2017-06-02 13:44:59 +00:00
|
|
|
user_ids = @old_namespace.user_ids_for_project_authorizations |
|
|
|
|
@new_namespace.user_ids_for_project_authorizations
|
2017-02-07 13:55:42 +00:00
|
|
|
|
|
|
|
UserProjectAccessChangedService.new(user_ids).execute
|
|
|
|
end
|
2017-06-02 13:44:59 +00:00
|
|
|
|
|
|
|
def rollback_side_effects
|
|
|
|
rollback_folder_move
|
|
|
|
update_namespace_and_visibility(@old_namespace)
|
2017-12-19 19:18:26 +00:00
|
|
|
write_repository_config(@old_path)
|
2017-06-02 13:44:59 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def rollback_folder_move
|
|
|
|
move_repo_folder(@new_path, @old_path)
|
|
|
|
move_repo_folder("#{@new_path}.wiki", "#{@old_path}.wiki")
|
|
|
|
end
|
|
|
|
|
|
|
|
def move_repo_folder(from_name, to_name)
|
|
|
|
gitlab_shell.mv_repository(project.repository_storage_path, from_name, to_name)
|
|
|
|
end
|
|
|
|
|
|
|
|
def execute_system_hooks
|
|
|
|
SystemHooksService.new.execute_hooks_for(project, :transfer)
|
|
|
|
end
|
2017-11-17 06:39:29 +00:00
|
|
|
|
|
|
|
def move_project_folders(project)
|
|
|
|
return if project.hashed_storage?(:repository)
|
|
|
|
|
|
|
|
# Move main repository
|
|
|
|
unless move_repo_folder(@old_path, @new_path)
|
|
|
|
raise TransferError.new("Cannot move project")
|
|
|
|
end
|
|
|
|
|
|
|
|
# Disk path is changed; we need to ensure we reload it
|
|
|
|
project.reload_repository!
|
|
|
|
|
|
|
|
# Move wiki repo also if present
|
|
|
|
move_repo_folder("#{@old_path}.wiki", "#{@new_path}.wiki")
|
|
|
|
end
|
2017-11-17 15:32:33 +00:00
|
|
|
|
|
|
|
def move_project_uploads(project)
|
|
|
|
return if project.hashed_storage?(:attachments)
|
|
|
|
|
|
|
|
Gitlab::UploadsTransfer.new.move_project(
|
|
|
|
project.path,
|
|
|
|
@old_namespace.full_path,
|
|
|
|
@new_namespace.full_path
|
|
|
|
)
|
|
|
|
end
|
2013-03-25 08:47:22 +00:00
|
|
|
end
|
|
|
|
end
|