debian-mirror-gitlab/app/services/projects/transfer_service.rb

98 lines
3.3 KiB
Ruby
Raw Normal View History

2014-09-02 18:07:02 +05:30
# 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
#
module Projects
class TransferService < BaseService
include Gitlab::ShellAdapter
2017-08-17 22:00:37 +05:30
TransferError = Class.new(StandardError)
2014-09-02 18:07:02 +05:30
2015-09-11 14:41:01 +05:30
def execute(new_namespace)
if allowed_transfer?(current_user, project, new_namespace)
transfer(project, new_namespace)
2014-09-02 18:07:02 +05:30
else
2015-09-11 14:41:01 +05:30
project.errors.add(:new_namespace, 'is invalid')
2014-09-02 18:07:02 +05:30
false
end
rescue Projects::TransferService::TransferError => ex
project.reload
2015-09-11 14:41:01 +05:30
project.errors.add(:new_namespace, ex.message)
2014-09-02 18:07:02 +05:30
false
end
def transfer(project, new_namespace)
2017-08-17 22:00:37 +05:30
old_namespace = project.namespace
2014-09-02 18:07:02 +05:30
Project.transaction do
old_path = project.path_with_namespace
2016-11-03 12:29:30 +05:30
old_group = project.group
2017-08-17 22:00:37 +05:30
new_path = File.join(new_namespace.try(:full_path) || '', project.path)
2014-09-02 18:07:02 +05:30
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-06-02 11:05:42 +05:30
if project.has_container_registry_tags?
# we currently doesn'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
project.expire_caches_before_rename(old_path)
# Apply new namespace id and visibility level
2014-09-02 18:07:02 +05:30
project.namespace = new_namespace
2016-06-02 11:05:42 +05:30
project.visibility_level = new_namespace.visibility_level unless project.visibility_level_allowed_by_group?
2014-09-02 18:07:02 +05:30
project.save!
2015-04-26 12:48:37 +05:30
# Notifications
2015-10-24 18:46:33 +05:30
project.send_move_instructions(old_path)
2015-04-26 12:48:37 +05:30
2014-09-02 18:07:02 +05:30
# Move main repository
2016-08-24 12:49:21 +05:30
unless gitlab_shell.mv_repository(project.repository_storage_path, old_path, new_path)
2014-09-02 18:07:02 +05:30
raise TransferError.new('Cannot move project')
end
# Move wiki repo also if present
2016-08-24 12:49:21 +05:30
gitlab_shell.mv_repository(project.repository_storage_path, "#{old_path}.wiki", "#{new_path}.wiki")
2014-09-02 18:07:02 +05:30
2016-11-03 12:29:30 +05:30
# Move missing group labels to project
Labels::TransferService.new(current_user, old_group, project).execute
2015-10-24 18:46:33 +05:30
# Move uploads
2017-08-17 22:00:37 +05:30
Gitlab::UploadsTransfer.new.move_project(project.path, old_namespace.full_path, new_namespace.full_path)
# Move pages
Gitlab::PagesTransfer.new.move_project(project.path, old_namespace.full_path, new_namespace.full_path)
2015-10-24 18:46:33 +05:30
project.old_path_with_namespace = old_path
2016-06-02 11:05:42 +05:30
SystemHooksService.new.execute_hooks_for(project, :transfer)
2014-09-02 18:07:02 +05:30
end
2017-08-17 22:00:37 +05:30
refresh_permissions(old_namespace, new_namespace)
true
2014-09-02 18:07:02 +05:30
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
2017-08-17 22:00:37 +05:30
def refresh_permissions(old_namespace, new_namespace)
# This ensures we only schedule 1 job for every user that has access to
# the namespaces.
user_ids = old_namespace.user_ids_for_project_authorizations |
new_namespace.user_ids_for_project_authorizations
UserProjectAccessChangedService.new(user_ids).execute
end
2014-09-02 18:07:02 +05:30
end
end