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

70 lines
2.1 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
class TransferError < StandardError; end
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)
Project.transaction do
old_path = project.path_with_namespace
2015-10-24 18:46:33 +05:30
old_namespace = project.namespace
2014-09-02 18:07:02 +05:30
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
# Apply new namespace id
project.namespace = new_namespace
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
unless gitlab_shell.mv_repository(old_path, new_path)
raise TransferError.new('Cannot move project')
end
# Move wiki repo also if present
gitlab_shell.mv_repository("#{old_path}.wiki", "#{new_path}.wiki")
# clear project cached events
project.reset_events_cache
2015-10-24 18:46:33 +05:30
# Move uploads
Gitlab::UploadsTransfer.new.move_project(project.path, old_namespace.path, new_namespace.path)
2014-09-02 18:07:02 +05:30
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
end
end