project_transfer_service.rb
1.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# ProjectTransferService class
#
# Used for transfer project to another namespace
#
class ProjectTransferService
  include Gitlab::ShellAdapter
  class TransferError < StandardError; end
  attr_accessor :project
  def transfer(project, new_namespace)
    Project.transaction do
      old_path = project.path_with_namespace
      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
      project.namespace = new_namespace
      project.save!
      # 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")
      # create satellite repo
      project.ensure_satellite_exists
      # clear project cached events
      project.reset_events_cache
      true
    end
  end
end