2018-07-17 12:50:37 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
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
|
2017-03-01 06:00:37 -05:00
|
|
|
TransferError = Class.new(StandardError)
|
2014-05-28 12:02:26 -04:00
|
|
|
|
2015-07-02 08:31:25 -04:00
|
|
|
def execute(new_namespace)
|
2017-06-02 09:44:59 -04:00
|
|
|
@new_namespace = new_namespace
|
|
|
|
|
|
|
|
if @new_namespace.blank?
|
2019-04-15 08:25:48 -04:00
|
|
|
raise TransferError, s_('TransferProject|Please select a new namespace for your project.')
|
2014-05-28 12:02:26 -04:00
|
|
|
end
|
2017-06-02 09:44:59 -04:00
|
|
|
|
|
|
|
unless allowed_transfer?(current_user, project)
|
2019-04-15 08:25:48 -04:00
|
|
|
raise TransferError, s_('TransferProject|Transfer failed, please contact an admin.')
|
2017-05-22 17:04:21 -04:00
|
|
|
end
|
2017-06-02 09:44:59 -04:00
|
|
|
|
|
|
|
transfer(project)
|
|
|
|
|
2018-04-04 11:14:19 -04:00
|
|
|
current_user.invalidate_personal_projects_count
|
|
|
|
|
2017-06-02 09:44:59 -04:00
|
|
|
true
|
2014-05-28 12:02:26 -04:00
|
|
|
rescue Projects::TransferService::TransferError => ex
|
2019-04-08 09:33:36 -04:00
|
|
|
project.reset
|
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
|
|
|
|
2017-06-02 09:44:59 -04:00
|
|
|
private
|
2017-02-07 08:55:42 -05:00
|
|
|
|
2021-01-11 19:10:42 -05:00
|
|
|
attr_reader :old_path, :new_path, :new_namespace, :old_namespace
|
2020-01-29 13:08:47 -05:00
|
|
|
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2017-06-02 09:44:59 -04:00
|
|
|
def transfer(project)
|
2017-07-20 05:34:09 -04:00
|
|
|
@old_path = project.full_path
|
2017-06-02 09:44:59 -04:00
|
|
|
@old_group = project.group
|
|
|
|
@new_path = File.join(@new_namespace.try(:full_path) || '', project.path)
|
|
|
|
@old_namespace = project.namespace
|
2014-05-28 12:02:26 -04:00
|
|
|
|
2018-08-30 02:49:24 -04:00
|
|
|
if Project.where(namespace_id: @new_namespace.try(:id)).where('path = ? or name = ?', project.path, project.name).exists?
|
2021-05-04 11:10:36 -04:00
|
|
|
raise TransferError, s_("TransferProject|Project with same name or path in target namespace already exists")
|
2017-06-02 09:44:59 -04:00
|
|
|
end
|
2014-05-28 12:02:26 -04:00
|
|
|
|
2017-06-02 09:44:59 -04:00
|
|
|
if project.has_container_registry_tags?
|
|
|
|
# We currently don't support renaming repository if it contains tags in container registry
|
2021-05-04 11:10:36 -04:00
|
|
|
raise TransferError, s_('TransferProject|Project cannot be transferred, because tags are present in its container registry')
|
2017-06-02 09:44:59 -04:00
|
|
|
end
|
2016-05-09 15:41:48 -04:00
|
|
|
|
2020-07-19 23:09:39 -04:00
|
|
|
if project.has_packages?(:npm) && !new_namespace_has_same_root?(project)
|
2021-05-04 11:10:36 -04:00
|
|
|
raise TransferError, s_("TransferProject|Root namespace can't be updated if project has NPM packages")
|
2020-07-19 23:09:39 -04:00
|
|
|
end
|
|
|
|
|
2020-12-04 04:09:36 -05:00
|
|
|
proceed_to_transfer
|
2017-06-02 09:44:59 -04:00
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2017-06-02 09:44:59 -04:00
|
|
|
|
2020-07-19 23:09:39 -04:00
|
|
|
def new_namespace_has_same_root?(project)
|
|
|
|
new_namespace.root_ancestor == project.namespace.root_ancestor
|
|
|
|
end
|
|
|
|
|
2020-12-04 04:09:36 -05:00
|
|
|
def proceed_to_transfer
|
2017-06-02 09:44:59 -04:00
|
|
|
Project.transaction do
|
|
|
|
project.expire_caches_before_rename(@old_path)
|
2016-04-22 03:39:31 -04:00
|
|
|
|
2020-11-18 10:09:08 -05:00
|
|
|
# Apply changes to the project
|
2017-06-02 09:44:59 -04:00
|
|
|
update_namespace_and_visibility(@new_namespace)
|
2020-11-18 10:09:08 -05:00
|
|
|
update_shared_runners_settings
|
|
|
|
project.save!
|
2014-05-28 12:02:26 -04:00
|
|
|
|
2015-03-25 12:18:26 -04:00
|
|
|
# Notifications
|
2017-06-02 09:44:59 -04:00
|
|
|
project.send_move_instructions(@old_path)
|
2015-03-25 12:18:26 -04:00
|
|
|
|
2017-11-17 01:39:29 -05:00
|
|
|
# Directories on disk
|
|
|
|
move_project_folders(project)
|
2014-05-28 12:02:26 -04:00
|
|
|
|
2020-11-02 16:09:10 -05:00
|
|
|
transfer_missing_group_resources(@old_group)
|
2019-09-04 12:19:31 -04:00
|
|
|
|
2015-10-06 10:09:03 -04:00
|
|
|
# Move uploads
|
2017-11-17 10:32:33 -05:00
|
|
|
move_project_uploads(project)
|
2015-10-06 10:09:03 -04:00
|
|
|
|
2020-12-04 04:09:36 -05:00
|
|
|
update_integrations
|
|
|
|
|
2017-06-02 09:44:59 -04:00
|
|
|
project.old_path_with_namespace = @old_path
|
2016-04-13 14:28:10 -04:00
|
|
|
|
2018-12-18 14:11:35 -05:00
|
|
|
update_repository_configuration(@new_path)
|
2017-12-19 14:18:26 -05:00
|
|
|
|
2017-06-02 09:44:59 -04:00
|
|
|
execute_system_hooks
|
2014-05-28 12:02:26 -04:00
|
|
|
end
|
2020-08-27 11:10:21 -04:00
|
|
|
|
2021-01-11 19:10:42 -05:00
|
|
|
post_update_hooks(project)
|
2017-06-02 09:44:59 -04:00
|
|
|
rescue Exception # rubocop:disable Lint/RescueException
|
|
|
|
rollback_side_effects
|
|
|
|
raise
|
|
|
|
ensure
|
|
|
|
refresh_permissions
|
2014-05-28 12:02:26 -04:00
|
|
|
end
|
|
|
|
|
2021-01-11 19:10:42 -05:00
|
|
|
# Overridden in EE
|
|
|
|
def post_update_hooks(project)
|
|
|
|
move_pages(project)
|
|
|
|
end
|
|
|
|
|
2020-11-02 16:09:10 -05:00
|
|
|
def transfer_missing_group_resources(group)
|
|
|
|
Labels::TransferService.new(current_user, group, project).execute
|
|
|
|
|
|
|
|
Milestones::TransferService.new(current_user, group, project).execute
|
|
|
|
end
|
|
|
|
|
2017-06-02 09:44:59 -04:00
|
|
|
def allowed_transfer?(current_user, project)
|
|
|
|
@new_namespace &&
|
2014-05-28 12:02:26 -04:00
|
|
|
can?(current_user, :change_namespace, project) &&
|
2017-06-02 09:44:59 -04:00
|
|
|
@new_namespace.id != project.namespace_id &&
|
2019-09-23 03:42:26 -04:00
|
|
|
current_user.can?(:transfer_projects, @new_namespace)
|
2014-05-28 12:02:26 -04:00
|
|
|
end
|
2017-02-07 08:55:42 -05:00
|
|
|
|
2017-06-02 09:44:59 -04: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?
|
|
|
|
end
|
|
|
|
|
2018-12-18 14:11:35 -05:00
|
|
|
def update_repository_configuration(full_path)
|
2017-12-21 14:08:00 -05:00
|
|
|
project.write_repository_config(gl_full_path: full_path)
|
2018-12-18 14:11:35 -05:00
|
|
|
project.track_project_repository
|
2017-12-19 14:18:26 -05:00
|
|
|
end
|
|
|
|
|
2017-06-02 09:44:59 -04:00
|
|
|
def refresh_permissions
|
2017-02-07 08:55:42 -05:00
|
|
|
# This ensures we only schedule 1 job for every user that has access to
|
|
|
|
# the namespaces.
|
2017-06-02 09:44:59 -04:00
|
|
|
user_ids = @old_namespace.user_ids_for_project_authorizations |
|
|
|
|
@new_namespace.user_ids_for_project_authorizations
|
2017-02-07 08:55:42 -05:00
|
|
|
|
|
|
|
UserProjectAccessChangedService.new(user_ids).execute
|
|
|
|
end
|
2017-06-02 09:44:59 -04:00
|
|
|
|
|
|
|
def rollback_side_effects
|
|
|
|
rollback_folder_move
|
2019-04-08 09:33:36 -04:00
|
|
|
project.reset
|
2017-06-02 09:44:59 -04:00
|
|
|
update_namespace_and_visibility(@old_namespace)
|
2018-12-18 14:11:35 -05:00
|
|
|
update_repository_configuration(@old_path)
|
2017-06-02 09:44:59 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def rollback_folder_move
|
2020-01-29 13:08:47 -05:00
|
|
|
return if project.hashed_storage?(:repository)
|
|
|
|
|
2017-06-02 09:44:59 -04:00
|
|
|
move_repo_folder(@new_path, @old_path)
|
2020-05-13 14:08:47 -04:00
|
|
|
move_repo_folder(new_wiki_repo_path, old_wiki_repo_path)
|
|
|
|
move_repo_folder(new_design_repo_path, old_design_repo_path)
|
2017-06-02 09:44:59 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def move_repo_folder(from_name, to_name)
|
2018-04-13 06:57:19 -04:00
|
|
|
gitlab_shell.mv_repository(project.repository_storage, from_name, to_name)
|
2017-06-02 09:44:59 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def execute_system_hooks
|
|
|
|
SystemHooksService.new.execute_hooks_for(project, :transfer)
|
|
|
|
end
|
2017-11-17 01:39:29 -05:00
|
|
|
|
|
|
|
def move_project_folders(project)
|
|
|
|
return if project.hashed_storage?(:repository)
|
|
|
|
|
|
|
|
# Move main repository
|
|
|
|
unless move_repo_folder(@old_path, @new_path)
|
2021-05-04 11:10:36 -04:00
|
|
|
raise TransferError, s_("TransferProject|Cannot move project")
|
2017-11-17 01:39:29 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
# Disk path is changed; we need to ensure we reload it
|
|
|
|
project.reload_repository!
|
|
|
|
|
2020-05-13 14:08:47 -04:00
|
|
|
# Move wiki and design repos also if present
|
|
|
|
move_repo_folder(old_wiki_repo_path, new_wiki_repo_path)
|
|
|
|
move_repo_folder(old_design_repo_path, new_design_repo_path)
|
2017-11-17 01:39:29 -05:00
|
|
|
end
|
2017-11-17 10:32:33 -05: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
|
2020-05-13 14:08:47 -04:00
|
|
|
|
2020-08-27 11:10:21 -04:00
|
|
|
def move_pages(project)
|
2020-09-04 08:08:27 -04:00
|
|
|
return unless project.pages_deployed?
|
2020-08-27 11:10:21 -04:00
|
|
|
|
2020-09-04 08:08:27 -04:00
|
|
|
transfer = Gitlab::PagesTransfer.new.async
|
2020-08-27 11:10:21 -04:00
|
|
|
transfer.move_project(project.path, @old_namespace.full_path, @new_namespace.full_path)
|
|
|
|
end
|
|
|
|
|
2020-05-13 14:08:47 -04:00
|
|
|
def old_wiki_repo_path
|
|
|
|
"#{old_path}#{::Gitlab::GlRepository::WIKI.path_suffix}"
|
|
|
|
end
|
|
|
|
|
|
|
|
def new_wiki_repo_path
|
|
|
|
"#{new_path}#{::Gitlab::GlRepository::WIKI.path_suffix}"
|
|
|
|
end
|
|
|
|
|
|
|
|
def old_design_repo_path
|
|
|
|
"#{old_path}#{::Gitlab::GlRepository::DESIGN.path_suffix}"
|
|
|
|
end
|
|
|
|
|
|
|
|
def new_design_repo_path
|
|
|
|
"#{new_path}#{::Gitlab::GlRepository::DESIGN.path_suffix}"
|
|
|
|
end
|
2020-11-18 10:09:08 -05:00
|
|
|
|
|
|
|
def update_shared_runners_settings
|
|
|
|
# If a project is being transferred to another group it means it can already
|
|
|
|
# have shared runners enabled but we need to check whether the new group allows that.
|
|
|
|
if project.group && project.group.shared_runners_setting == 'disabled_and_unoverridable'
|
|
|
|
project.shared_runners_enabled = false
|
|
|
|
end
|
|
|
|
end
|
2020-12-04 04:09:36 -05:00
|
|
|
|
|
|
|
def update_integrations
|
|
|
|
project.services.inherit.delete_all
|
|
|
|
Service.create_from_active_default_integrations(project, :project_id)
|
|
|
|
end
|
2013-03-25 04:47:22 -04:00
|
|
|
end
|
|
|
|
end
|
2019-09-13 09:26:31 -04:00
|
|
|
|
|
|
|
Projects::TransferService.prepend_if_ee('EE::Projects::TransferService')
|