2018-07-17 16:50:37 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2014-05-28 16:02:26 +00:00
|
|
|
# Projects::TransferService class
|
|
|
|
#
|
2022-02-15 00:15:45 +00:00
|
|
|
# Used to transfer a project to another namespace
|
2014-05-28 16:02:26 +00:00
|
|
|
#
|
|
|
|
# Ex.
|
2022-02-15 00:15:45 +00:00
|
|
|
# # Move project to namespace by user
|
|
|
|
# Projects::TransferService.new(project, user).execute(namespace)
|
2014-05-28 16:02:26 +00:00
|
|
|
#
|
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?
|
2019-04-15 12:25:48 +00:00
|
|
|
raise TransferError, s_('TransferProject|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
|
|
|
|
2021-08-20 18:12:04 +00:00
|
|
|
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.")
|
2017-05-22 21:04:21 +00:00
|
|
|
end
|
2017-06-02 13:44:59 +00:00
|
|
|
|
|
|
|
transfer(project)
|
|
|
|
|
2018-04-04 15:14:19 +00:00
|
|
|
current_user.invalidate_personal_projects_count
|
|
|
|
|
2017-06-02 13:44:59 +00:00
|
|
|
true
|
2014-05-28 16:02:26 +00:00
|
|
|
rescue Projects::TransferService::TransferError => ex
|
2019-04-08 13:33:36 +00:00
|
|
|
project.reset
|
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
|
|
|
|
2021-01-12 00:10:42 +00:00
|
|
|
attr_reader :old_path, :new_path, :new_namespace, :old_namespace
|
2020-01-29 18:08:47 +00:00
|
|
|
|
2018-08-27 15:31:01 +00:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
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
|
|
|
|
2018-08-30 06:49:24 +00:00
|
|
|
if Project.where(namespace_id: @new_namespace.try(:id)).where('path = ? or name = ?', project.path, project.name).exists?
|
2021-05-04 15:10:36 +00:00
|
|
|
raise TransferError, s_("TransferProject|Project with same name or path in target namespace already exists")
|
2017-06-02 13:44:59 +00:00
|
|
|
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
|
2021-05-04 15:10:36 +00:00
|
|
|
raise TransferError, s_('TransferProject|Project cannot be transferred, because tags are present in its container registry')
|
2017-06-02 13:44:59 +00:00
|
|
|
end
|
2016-05-09 19:41:48 +00:00
|
|
|
|
2020-07-20 03:09:39 +00:00
|
|
|
if project.has_packages?(:npm) && !new_namespace_has_same_root?(project)
|
2021-05-04 15:10:36 +00:00
|
|
|
raise TransferError, s_("TransferProject|Root namespace can't be updated if project has NPM packages")
|
2020-07-20 03:09:39 +00:00
|
|
|
end
|
|
|
|
|
2020-12-04 09:09:36 +00:00
|
|
|
proceed_to_transfer
|
2017-06-02 13:44:59 +00:00
|
|
|
end
|
2018-08-27 15:31:01 +00:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2017-06-02 13:44:59 +00:00
|
|
|
|
2020-07-20 03:09:39 +00:00
|
|
|
def new_namespace_has_same_root?(project)
|
|
|
|
new_namespace.root_ancestor == project.namespace.root_ancestor
|
|
|
|
end
|
|
|
|
|
2020-12-04 09:09:36 +00:00
|
|
|
def proceed_to_transfer
|
2017-06-02 13:44:59 +00:00
|
|
|
Project.transaction do
|
|
|
|
project.expire_caches_before_rename(@old_path)
|
2016-04-22 07:39:31 +00:00
|
|
|
|
2020-11-18 15:09:08 +00:00
|
|
|
# Apply changes to the project
|
2017-06-02 13:44:59 +00:00
|
|
|
update_namespace_and_visibility(@new_namespace)
|
2021-10-13 15:12:51 +00:00
|
|
|
project.reconcile_shared_runners_setting!
|
2020-11-18 15:09:08 +00:00
|
|
|
project.save!
|
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
|
|
|
|
2020-11-02 21:09:10 +00:00
|
|
|
transfer_missing_group_resources(@old_group)
|
2019-09-04 16:19:31 +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
|
|
|
|
2020-12-04 09:09:36 +00:00
|
|
|
update_integrations
|
|
|
|
|
2021-07-22 00:09:11 +00:00
|
|
|
remove_paid_features
|
|
|
|
|
2017-06-02 13:44:59 +00:00
|
|
|
project.old_path_with_namespace = @old_path
|
2016-04-13 18:28:10 +00:00
|
|
|
|
2018-12-18 19:11:35 +00:00
|
|
|
update_repository_configuration(@new_path)
|
2017-12-19 19:18:26 +00:00
|
|
|
|
2022-02-15 00:15:45 +00:00
|
|
|
remove_issue_contacts
|
|
|
|
|
2017-06-02 13:44:59 +00:00
|
|
|
execute_system_hooks
|
2014-05-28 16:02:26 +00:00
|
|
|
end
|
2020-08-27 15:10:21 +00:00
|
|
|
|
2021-11-30 12:10:26 +00:00
|
|
|
update_pending_builds
|
|
|
|
|
2021-01-12 00:10:42 +00:00
|
|
|
post_update_hooks(project)
|
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
|
|
|
|
|
2021-01-12 00:10:42 +00:00
|
|
|
# Overridden in EE
|
|
|
|
def post_update_hooks(project)
|
2022-03-28 12:07:26 +00:00
|
|
|
ensure_personal_project_owner_membership(project)
|
2022-07-29 15:12:25 +00:00
|
|
|
|
|
|
|
publish_event
|
2021-01-12 00:10:42 +00:00
|
|
|
end
|
|
|
|
|
2021-07-22 00:09:11 +00:00
|
|
|
# Overridden in EE
|
|
|
|
def remove_paid_features
|
|
|
|
end
|
|
|
|
|
2020-11-02 21:09:10 +00:00
|
|
|
def transfer_missing_group_resources(group)
|
|
|
|
Labels::TransferService.new(current_user, group, project).execute
|
|
|
|
|
|
|
|
Milestones::TransferService.new(current_user, group, project).execute
|
|
|
|
end
|
|
|
|
|
2021-08-20 18:12:04 +00:00
|
|
|
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)
|
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?
|
|
|
|
end
|
|
|
|
|
2018-12-18 19:11:35 +00:00
|
|
|
def update_repository_configuration(full_path)
|
2021-07-29 06:10:15 +00:00
|
|
|
project.set_full_path(gl_full_path: full_path)
|
2018-12-18 19:11:35 +00:00
|
|
|
project.track_project_repository
|
2017-12-19 19:18:26 +00:00
|
|
|
end
|
|
|
|
|
2022-03-28 12:07:26 +00:00
|
|
|
def ensure_personal_project_owner_membership(project)
|
|
|
|
# In case of personal projects, we want to make sure that
|
|
|
|
# a membership record with `OWNER` access level exists for the owner of the namespace.
|
|
|
|
return unless project.personal?
|
|
|
|
|
|
|
|
namespace_owner = project.namespace.owner
|
|
|
|
existing_membership_record = project.member(namespace_owner)
|
|
|
|
|
|
|
|
return if existing_membership_record.present? && existing_membership_record.access_level == Gitlab::Access::OWNER
|
|
|
|
|
|
|
|
project.add_owner(namespace_owner)
|
|
|
|
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
|
|
|
|
2021-10-01 21:11:37 +00:00
|
|
|
AuthorizedProjectUpdate::ProjectRecalculateWorker.perform_async(project.id)
|
|
|
|
|
|
|
|
# Until we compare the inconsistency rates of the new specialized worker and
|
|
|
|
# the old approach, we still run AuthorizedProjectsWorker
|
|
|
|
# but with some delay and lower urgency as a safety net.
|
|
|
|
UserProjectAccessChangedService.new(user_ids).execute(
|
|
|
|
blocking: false,
|
|
|
|
priority: UserProjectAccessChangedService::LOW_PRIORITY
|
|
|
|
)
|
2017-02-07 13:55:42 +00:00
|
|
|
end
|
2017-06-02 13:44:59 +00:00
|
|
|
|
|
|
|
def rollback_side_effects
|
|
|
|
rollback_folder_move
|
2019-04-08 13:33:36 +00:00
|
|
|
project.reset
|
2017-06-02 13:44:59 +00:00
|
|
|
update_namespace_and_visibility(@old_namespace)
|
2018-12-18 19:11:35 +00:00
|
|
|
update_repository_configuration(@old_path)
|
2017-06-02 13:44:59 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def rollback_folder_move
|
2020-01-29 18:08:47 +00:00
|
|
|
return if project.hashed_storage?(:repository)
|
|
|
|
|
2017-06-02 13:44:59 +00:00
|
|
|
move_repo_folder(@new_path, @old_path)
|
2020-05-13 18:08:47 +00: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 13:44:59 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def move_repo_folder(from_name, to_name)
|
2018-04-13 10:57:19 +00:00
|
|
|
gitlab_shell.mv_repository(project.repository_storage, from_name, to_name)
|
2017-06-02 13:44:59 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def execute_system_hooks
|
2021-10-07 21:11:49 +00:00
|
|
|
system_hook_service.execute_hooks_for(project, :transfer)
|
2017-06-02 13:44:59 +00:00
|
|
|
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)
|
2021-05-04 15:10:36 +00:00
|
|
|
raise TransferError, s_("TransferProject|Cannot move project")
|
2017-11-17 06:39:29 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# Disk path is changed; we need to ensure we reload it
|
|
|
|
project.reload_repository!
|
|
|
|
|
2020-05-13 18:08:47 +00: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 06:39:29 +00:00
|
|
|
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
|
2020-05-13 18:08:47 +00: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 15:09:08 +00:00
|
|
|
|
2020-12-04 09:09:36 +00:00
|
|
|
def update_integrations
|
2021-08-03 09:15:56 +00:00
|
|
|
project.integrations.with_default_settings.delete_all
|
2021-05-12 12:10:24 +00:00
|
|
|
Integration.create_from_active_default_integrations(project, :project_id)
|
2020-12-04 09:09:36 +00:00
|
|
|
end
|
2021-09-20 15:12:39 +00:00
|
|
|
|
2021-11-30 12:10:26 +00:00
|
|
|
def update_pending_builds
|
2021-12-06 06:10:20 +00:00
|
|
|
::Ci::PendingBuilds::UpdateProjectWorker.perform_async(project.id, pending_builds_params)
|
2021-11-30 12:10:26 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def pending_builds_params
|
|
|
|
{
|
2021-09-20 15:12:39 +00:00
|
|
|
namespace_id: new_namespace.id,
|
|
|
|
namespace_traversal_ids: new_namespace.traversal_ids
|
|
|
|
}
|
|
|
|
end
|
2022-02-15 00:15:45 +00:00
|
|
|
|
|
|
|
def remove_issue_contacts
|
|
|
|
return unless @old_group&.root_ancestor != @new_namespace&.root_ancestor
|
|
|
|
|
|
|
|
CustomerRelations::IssueContact.delete_for_project(project.id)
|
|
|
|
end
|
2022-07-29 15:12:25 +00:00
|
|
|
|
|
|
|
def publish_event
|
|
|
|
event = ::Projects::ProjectTransferedEvent.new(data: {
|
|
|
|
project_id: project.id,
|
|
|
|
old_namespace_id: old_namespace.id,
|
|
|
|
old_root_namespace_id: old_namespace.root_ancestor.id,
|
|
|
|
new_namespace_id: new_namespace.id,
|
|
|
|
new_root_namespace_id: new_namespace.root_ancestor.id
|
|
|
|
})
|
|
|
|
|
|
|
|
Gitlab::EventStore.publish(event)
|
|
|
|
end
|
2013-03-25 08:47:22 +00:00
|
|
|
end
|
|
|
|
end
|
2019-09-13 13:26:31 +00:00
|
|
|
|
2021-05-11 21:10:21 +00:00
|
|
|
Projects::TransferService.prepend_mod_with('Projects::TransferService')
|