summaryrefslogtreecommitdiff
path: root/app/services/projects/after_rename_service.rb
blob: 4131da44f5ae70db17be2ef5672276236fe3227e (plain)
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# frozen_string_literal: true

module Projects
  # Service class for performing operations that should take place after a
  # project has been renamed.
  #
  # Example usage:
  #
  #     project = Project.find(42)
  #
  #     project.update(...)
  #
  #     Projects::AfterRenameService.new(project).execute
  class AfterRenameService
    attr_reader :project, :full_path_before, :full_path_after, :path_before

    RenameFailedError = Class.new(StandardError)

    # @param [Project] project The Project of the repository to rename.
    def initialize(project)
      @project = project

      # The full path of the namespace + project, before the rename took place.
      @full_path_before = project.full_path_was

      # The full path of the namespace + project, after the rename took place.
      @full_path_after = project.build_full_path

      # The path of just the project, before the rename took place.
      @path_before = project.path_was
    end

    def execute
      first_ensure_no_registry_tags_are_present
      expire_caches_before_rename
      rename_or_migrate_repository!
      send_move_instructions
      execute_system_hooks
      update_repository_configuration
      rename_transferred_documents
      log_completion
    end

    def first_ensure_no_registry_tags_are_present
      return unless project.has_container_registry_tags?

      raise RenameFailedError.new(
        "Project #{full_path_before} cannot be renamed because images are " \
          "present in its container registry"
      )
    end

    def expire_caches_before_rename
      project.expire_caches_before_rename(full_path_before)
    end

    def rename_or_migrate_repository!
      success =
        if migrate_to_hashed_storage?
          ::Projects::HashedStorageMigrationService
            .new(project, full_path_before)
            .execute
        else
          project.storage.rename_repo
        end

      rename_failed! unless success
    end

    def send_move_instructions
      return unless send_move_instructions?

      project.send_move_instructions(full_path_before)
    end

    def execute_system_hooks
      project.old_path_with_namespace = full_path_before
      SystemHooksService.new.execute_hooks_for(project, :rename)
    end

    def update_repository_configuration
      project.reload_repository!
      project.write_repository_config
    end

    def rename_transferred_documents
      if rename_uploads?
        Gitlab::UploadsTransfer
          .new
          .rename_project(path_before, project_path, namespace_full_path)
      end

      Gitlab::PagesTransfer
        .new
        .rename_project(path_before, project_path, namespace_full_path)
    end

    def log_completion
      Gitlab::AppLogger.info(
        "Project #{project.id} has been renamed from " \
          "#{full_path_before} to #{full_path_after}"
      )
    end

    def migrate_to_hashed_storage?
      Gitlab::CurrentSettings.hashed_storage_enabled? &&
        project.storage_upgradable? &&
        Feature.disabled?(:skip_hashed_storage_upgrade)
    end

    def send_move_instructions?
      !project.import_started?
    end

    def rename_uploads?
      !project.hashed_storage?(:attachments)
    end

    def project_path
      project.path
    end

    def namespace_full_path
      project.namespace.full_path
    end

    def rename_failed!
      error = "Repository #{full_path_before} could not be renamed to #{full_path_after}"

      Gitlab::AppLogger.error(error)

      raise RenameFailedError.new(error)
    end
  end
end