transfer_service.rb 4.82 KB
Newer Older
1
2
# frozen_string_literal: true

3
4
5
6
7
8
9
10
# 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
#
11
12
module Projects
  class TransferService < BaseService
13
    include Gitlab::ShellAdapter
14
    TransferError = Class.new(StandardError)
15

16
    def execute(new_namespace)
17
18
19
      @new_namespace = new_namespace

      if @new_namespace.blank?
20
        raise TransferError, 'Please select a new namespace for your project.'
21
      end
22
23

      unless allowed_transfer?(current_user, project)
24
25
        raise TransferError, 'Transfer failed, please contact an admin.'
      end
26
27
28

      transfer(project)

29
30
      current_user.invalidate_personal_projects_count

31
      true
32
    rescue Projects::TransferService::TransferError => ex
33
      project.reload
34
      project.errors.add(:new_namespace, ex.message)
35
36
      false
    end
37

38
    private
39

40
    def transfer(project)
41
      @old_path = project.full_path
42
43
44
      @old_group = project.group
      @new_path = File.join(@new_namespace.try(:full_path) || '', project.path)
      @old_namespace = project.namespace
45

James Lopez's avatar
James Lopez committed
46
      if Project.where(namespace_id: @new_namespace.try(:id)).where('path = ? or name = ?', project.path, project.name).exists?
47
        raise TransferError.new("Project with same name or path in target namespace already exists")
48
      end
49

50
51
52
53
      if project.has_container_registry_tags?
        # We currently don'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
54

55
56
      attempt_transfer_transaction
    end
57

58
59
60
61
62
    def attempt_transfer_transaction
      Project.transaction do
        project.expire_caches_before_rename(@old_path)

        update_namespace_and_visibility(@new_namespace)
63

Valery Sizov's avatar
Valery Sizov committed
64
        # Notifications
65
        project.send_move_instructions(@old_path)
Valery Sizov's avatar
Valery Sizov committed
66

67
68
        # Directories on disk
        move_project_folders(project)
69

70
        # Move missing group labels to project
71
        Labels::TransferService.new(current_user, @old_group, project).execute
72

73
        # Move uploads
74
        move_project_uploads(project)
75

76
        # Move pages
77
        Gitlab::PagesTransfer.new.move_project(project.path, @old_namespace.full_path, @new_namespace.full_path)
78

79
        project.old_path_with_namespace = @old_path
80

81
82
        write_repository_config(@new_path)

83
        execute_system_hooks
84
      end
85
86
87
88
89
    rescue Exception # rubocop:disable Lint/RescueException
      rollback_side_effects
      raise
    ensure
      refresh_permissions
90
91
    end

92
93
    def allowed_transfer?(current_user, project)
      @new_namespace &&
94
        can?(current_user, :change_namespace, project) &&
95
96
        @new_namespace.id != project.namespace_id &&
        current_user.can?(:create_projects, @new_namespace)
97
    end
98

99
100
101
102
103
104
105
    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?
      project.save!
    end

106
    def write_repository_config(full_path)
107
      project.write_repository_config(gl_full_path: full_path)
108
109
    end

110
    def refresh_permissions
111
112
      # This ensures we only schedule 1 job for every user that has access to
      # the namespaces.
113
114
      user_ids = @old_namespace.user_ids_for_project_authorizations |
        @new_namespace.user_ids_for_project_authorizations
115
116
117

      UserProjectAccessChangedService.new(user_ids).execute
    end
118
119
120

    def rollback_side_effects
      rollback_folder_move
121
      project.reload
122
      update_namespace_and_visibility(@old_namespace)
123
      write_repository_config(@old_path)
124
125
126
127
128
129
130
131
    end

    def rollback_folder_move
      move_repo_folder(@new_path, @old_path)
      move_repo_folder("#{@new_path}.wiki", "#{@old_path}.wiki")
    end

    def move_repo_folder(from_name, to_name)
132
      gitlab_shell.mv_repository(project.repository_storage, from_name, to_name)
133
134
135
136
137
    end

    def execute_system_hooks
      SystemHooksService.new.execute_hooks_for(project, :transfer)
    end
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152

    def move_project_folders(project)
      return if project.hashed_storage?(:repository)

      # Move main repository
      unless move_repo_folder(@old_path, @new_path)
        raise TransferError.new("Cannot move project")
      end

      # Disk path is changed; we need to ensure we reload it
      project.reload_repository!

      # Move wiki repo also if present
      move_repo_folder("#{@old_path}.wiki", "#{@new_path}.wiki")
    end
153
154
155
156
157
158
159
160
161
162

    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
163
164
  end
end