Class: Projects::AfterRenameService

Inherits:
Object
  • Object
show all
Includes:
BaseServiceUtility
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

Methods included from BaseServiceUtility

#deny_visibility_level, #event_service, #log_error, #log_info, #notification_service, #system_hook_service, #todo_service, #visibility_level

Methods included from Gitlab::Allowable

#can?, #can_all?, #can_any?

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.



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

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.



27
28
29
# File 'app/services/projects/after_rename_service.rb', line 27

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.



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

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.



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

def path_before
  @path_before
end

#projectString (readonly)

Returns The Project being renamed.

Returns:

  • (String)

    The Project being renamed.



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

def project
  @project
end

Instance Method Details

#ensure_registry_tags_can_be_handledObject



66
67
68
69
70
71
# File 'app/services/projects/after_rename_service.rb', line 66

def ensure_registry_tags_can_be_handled
  return if ContainerRegistry::GitlabApiClient.supports_gitlab_api?

  rename_failed!("Project #{full_path_before} cannot be renamed because images are " \
  "present in its container registry")
end

#executeObject



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

def execute
  rename_base_repository_in_registry!
  expire_caches_before_rename
  rename_or_migrate_repository!
  send_move_instructions
  execute_system_hooks
  update_repository_configuration
  rename_transferred_documents
  log_completion
  publish_event
end

#execute_system_hooksObject



94
95
96
97
# File 'app/services/projects/after_rename_service.rb', line 94

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

#expire_caches_before_renameObject



73
74
75
# File 'app/services/projects/after_rename_service.rb', line 73

def expire_caches_before_rename
  project.expire_caches_before_rename(full_path_before)
end

#log_completionObject



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

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

#namespace_full_pathObject



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

def namespace_full_path
  project.namespace.full_path
end

#project_pathObject



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

def project_path
  project.path
end

#publish_eventObject



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

def publish_event
  event = Projects::ProjectPathChangedEvent.new(data: {
    project_id: project.id,
    namespace_id: project.namespace_id,
    root_namespace_id: project.root_namespace.id,
    old_path: full_path_before,
    new_path: full_path_after
  })

  Gitlab::EventStore.publish(event)
end

#rename_base_repository_in_registry!Object



52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'app/services/projects/after_rename_service.rb', line 52

def rename_base_repository_in_registry!
  return unless project.has_container_registry_tags?

  ensure_registry_tags_can_be_handled

  result = ContainerRegistry::GitlabApiClient.rename_base_repository_path(
    full_path_before, name: project_path, project: project
  )

  return if result == :ok

  rename_failed!("Renaming the base repository in the registry failed with error #{result}.")
end

#rename_failed!(error) ⇒ Object

Raises:



135
136
137
138
139
# File 'app/services/projects/after_rename_service.rb', line 135

def rename_failed!(error)
  log_error(error)

  raise RenameFailedError, error
end

#rename_or_migrate_repository!Object



77
78
79
80
81
82
83
84
85
86
# File 'app/services/projects/after_rename_service.rb', line 77

def rename_or_migrate_repository!
  success =
    ::Projects::HashedStorage::MigrationService
      .new(project, full_path_before)
      .execute

  return if success

  rename_failed!("Repository #{full_path_before} could not be renamed to #{full_path_after}")
end

#rename_transferred_documentsObject



104
105
106
107
108
109
110
# File 'app/services/projects/after_rename_service.rb', line 104

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

#rename_uploads?Boolean

Returns:

  • (Boolean)


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

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

#send_move_instructionsObject



88
89
90
91
92
# File 'app/services/projects/after_rename_service.rb', line 88

def send_move_instructions
  return unless send_move_instructions?

  project.send_move_instructions(full_path_before)
end

#send_move_instructions?Boolean

Returns:

  • (Boolean)


119
120
121
# File 'app/services/projects/after_rename_service.rb', line 119

def send_move_instructions?
  !project.import_started?
end

#update_repository_configurationObject



99
100
101
102
# File 'app/services/projects/after_rename_service.rb', line 99

def update_repository_configuration
  project.reload_repository!
  project.track_project_repository
end