debian-mirror-gitlab/app/services/projects/overwrite_project_service.rb

91 lines
3.7 KiB
Ruby
Raw Normal View History

2018-11-18 11:00:15 +05:30
# frozen_string_literal: true
2018-05-09 12:01:36 +05:30
module Projects
class OverwriteProjectService < BaseService
def execute(source_project)
2021-11-18 22:05:49 +05:30
return unless source_project && source_project.namespace_id == @project.namespace_id
2018-05-09 12:01:36 +05:30
2021-10-27 15:23:28 +05:30
start_time = ::Gitlab::Metrics::System.monotonic_time
2018-05-09 12:01:36 +05:30
Project.transaction do
move_before_destroy_relationships(source_project)
2020-01-01 13:55:28 +05:30
# Reset is required in order to get the proper
# uncached fork network method calls value.
destroy_old_project(source_project.reset)
2018-05-09 12:01:36 +05:30
rename_project(source_project.name, source_project.path)
@project
end
# Projects::DestroyService can raise Exceptions, but we don't want
# to pass that kind of exception to the caller. Instead, we change it
# for a StandardError exception
rescue Exception => e # rubocop:disable Lint/RescueException
attempt_restore_repositories(source_project)
2021-09-30 23:02:18 +05:30
if e.instance_of?(Exception)
2018-05-09 12:01:36 +05:30
raise StandardError, e.message
else
raise
end
2021-10-27 15:23:28 +05:30
ensure
track_service(start_time, source_project, e)
2018-05-09 12:01:36 +05:30
end
private
2021-10-27 15:23:28 +05:30
def track_service(start_time, source_project, exception)
return if ::Feature.disabled?(:project_overwrite_service_tracking, source_project, default_enabled: :yaml)
duration = ::Gitlab::Metrics::System.monotonic_time - start_time
Gitlab::AppJsonLogger.info(class: self.class.name,
2021-11-18 22:05:49 +05:30
namespace_id: source_project.namespace_id,
2021-10-27 15:23:28 +05:30
project_id: source_project.id,
duration_s: duration.to_f,
error: exception.class.name)
end
2018-05-09 12:01:36 +05:30
def move_before_destroy_relationships(source_project)
options = { remove_remaining_elements: false }
2021-01-03 14:25:43 +05:30
::Projects::MoveUsersStarProjectsService.new(@project, @current_user).execute(source_project, **options)
::Projects::MoveAccessService.new(@project, @current_user).execute(source_project, **options)
::Projects::MoveDeployKeysProjectsService.new(@project, @current_user).execute(source_project, **options)
::Projects::MoveNotificationSettingsService.new(@project, @current_user).execute(source_project, **options)
::Projects::MoveForksService.new(@project, @current_user).execute(source_project, **options)
::Projects::MoveLfsObjectsProjectsService.new(@project, @current_user).execute(source_project, **options)
2018-05-09 12:01:36 +05:30
add_source_project_to_fork_network(source_project)
end
def destroy_old_project(source_project)
# Delete previous project (synchronously) and unlink relations
::Projects::DestroyService.new(source_project, @current_user).execute
end
def rename_project(name, path)
# Update de project's name and path to the original name/path
::Projects::UpdateService.new(@project,
@current_user,
{ name: name, path: path })
.execute
end
def attempt_restore_repositories(project)
2020-03-13 15:44:24 +05:30
::Projects::DestroyRollbackService.new(project, @current_user).execute
2018-05-09 12:01:36 +05:30
end
def add_source_project_to_fork_network(source_project)
return unless @project.fork_network
2020-03-13 15:44:24 +05:30
# Because they have moved all references in the fork network from the source_project
2018-05-09 12:01:36 +05:30
# we won't be able to query the database (only through its cached data),
# for its former relationships. That's why we're adding it to the network
# as a fork of the target project
ForkNetworkMember.create!(fork_network: @project.fork_network,
project: source_project,
forked_from_project: @project)
end
end
end