Class: Projects::AfterRenameService

Inherits:
Object
  • Object
show all
Defined in:
app/services/projects/after_rename_service.rb

Overview

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

Constant Summary collapse

RenameFailedError =
Class.new(StandardError)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(project, path_before:, full_path_before:) ⇒ AfterRenameService

Returns a new instance of AfterRenameService.

Parameters:

  • project (Project)

    The Project being renamed.

  • path_before (String)

    The path slug the project was using, before the rename took place.


31
32
33
34
35
36
# File 'app/services/projects/after_rename_service.rb', line 31

def initialize(project, path_before:, full_path_before:)
  @project = project
  @path_before = path_before
  @full_path_before = full_path_before
  @full_path_after = project.full_path
end

Instance Attribute Details

#full_path_afterString (readonly)

Returns The full path of the namespace + project, after the rename took place.

Returns:

  • (String)

    The full path of the namespace + project, after the rename took place.


25
26
27
# File 'app/services/projects/after_rename_service.rb', line 25

def full_path_after
  @full_path_after
end

#full_path_beforeString (readonly)

Returns The full path of the namespace + project, before the rename took place.

Returns:

  • (String)

    The full path of the namespace + project, before the rename took place.


22
23
24
# File 'app/services/projects/after_rename_service.rb', line 22

def full_path_before
  @full_path_before
end

#path_beforeString (readonly)

Returns The path slug the project was using, before the rename took place.

Returns:

  • (String)

    The path slug the project was using, before the rename took place.


19
20
21
# File 'app/services/projects/after_rename_service.rb', line 19

def path_before
  @path_before
end

#projectString (readonly)

Returns The Project being renamed.

Returns:

  • (String)

    The Project being renamed.


16
17
18
# File 'app/services/projects/after_rename_service.rb', line 16

def project
  @project
end

Instance Method Details

#executeObject


38
39
40
41
42
43
44
45
46
47
# File 'app/services/projects/after_rename_service.rb', line 38

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

#execute_system_hooksObject


81
82
83
84
# File 'app/services/projects/after_rename_service.rb', line 81

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

#expire_caches_before_renameObject


58
59
60
# File 'app/services/projects/after_rename_service.rb', line 58

def expire_caches_before_rename
  project.expire_caches_before_rename(full_path_before)
end

#first_ensure_no_registry_tags_are_presentObject

Raises:


49
50
51
52
53
54
55
56
# File 'app/services/projects/after_rename_service.rb', line 49

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

#log_completionObject


114
115
116
117
118
119
# File 'app/services/projects/after_rename_service.rb', line 114

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

#migrate_to_hashed_storage?Boolean

Returns:

  • (Boolean)

121
122
123
124
# File 'app/services/projects/after_rename_service.rb', line 121

def migrate_to_hashed_storage?
  Gitlab::CurrentSettings.hashed_storage_enabled? &&
    project.storage_upgradable?
end

#namespace_full_pathObject


138
139
140
# File 'app/services/projects/after_rename_service.rb', line 138

def namespace_full_path
  project.namespace.full_path
end

#project_pathObject


134
135
136
# File 'app/services/projects/after_rename_service.rb', line 134

def project_path
  project.path
end

#rename_failed!Object

Raises:


142
143
144
145
146
147
148
# File 'app/services/projects/after_rename_service.rb', line 142

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

#rename_or_migrate_repository!Object


62
63
64
65
66
67
68
69
70
71
72
73
# File 'app/services/projects/after_rename_service.rb', line 62

def rename_or_migrate_repository!
  success =
    if migrate_to_hashed_storage?
      ::Projects::HashedStorage::MigrationService
        .new(project, full_path_before)
        .execute
    else
      project.storage.rename_repo(old_full_path: full_path_before, new_full_path: full_path_after)
    end

  rename_failed! unless success
end

#rename_transferred_documentsObject


92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'app/services/projects/after_rename_service.rb', line 92

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

  if project.pages_deployed?
    # Block will be evaluated in the context of project so we need
    # to bind to a local variable to capture it, as the instance
    # variable and method aren't available on Project
    path_before_local = @path_before

    project.run_after_commit_or_now do
      Gitlab::PagesTransfer
        .new
        .async
        .rename_project(path_before_local, path, namespace.full_path)
    end
  end
end

#rename_uploads?Boolean

Returns:

  • (Boolean)

130
131
132
# File 'app/services/projects/after_rename_service.rb', line 130

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

#send_move_instructionsObject


75
76
77
78
79
# File 'app/services/projects/after_rename_service.rb', line 75

def send_move_instructions
  return unless send_move_instructions?

  project.send_move_instructions(full_path_before)
end

#send_move_instructions?Boolean

Returns:

  • (Boolean)

126
127
128
# File 'app/services/projects/after_rename_service.rb', line 126

def send_move_instructions?
  !project.import_started?
end

#update_repository_configurationObject


86
87
88
89
90
# File 'app/services/projects/after_rename_service.rb', line 86

def update_repository_configuration
  project.reload_repository!
  project.write_repository_config
  project.track_project_repository
end