2018-11-08 19:23:39 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
class RepositoryForkWorker # rubocop:disable Scalability/IdempotentWorker
|
2018-03-17 18:26:18 +05:30
|
|
|
include ApplicationWorker
|
2021-06-08 01:23:25 +05:30
|
|
|
|
2021-10-27 15:23:28 +05:30
|
|
|
data_consistency :always
|
|
|
|
|
2021-06-08 01:23:25 +05:30
|
|
|
sidekiq_options retry: 3
|
2018-03-17 18:26:18 +05:30
|
|
|
include ProjectStartImport
|
|
|
|
include ProjectImportOptions
|
2016-09-13 17:45:13 +05:30
|
|
|
|
2019-12-21 20:55:43 +05:30
|
|
|
feature_category :source_code_management
|
|
|
|
|
2018-05-09 12:01:36 +05:30
|
|
|
def perform(*args)
|
|
|
|
target_project_id = args.shift
|
|
|
|
target_project = Project.find(target_project_id)
|
2015-09-25 12:07:36 +05:30
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
source_project = target_project.forked_from_project
|
|
|
|
unless source_project
|
2019-02-15 15:39:39 +05:30
|
|
|
return target_project.import_state.mark_as_failed(_('Source project cannot be found.'))
|
2018-05-09 12:01:36 +05:30
|
|
|
end
|
2018-11-08 19:23:39 +05:30
|
|
|
|
2019-03-02 22:35:43 +05:30
|
|
|
fork_repository(target_project, source_project)
|
2017-09-10 17:25:29 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2019-03-02 22:35:43 +05:30
|
|
|
def fork_repository(target_project, source_project)
|
2018-05-09 12:01:36 +05:30
|
|
|
return unless start_fork(target_project)
|
|
|
|
|
2018-10-15 14:42:47 +05:30
|
|
|
Gitlab::Metrics.add_event(:fork_repository)
|
2018-05-09 12:01:36 +05:30
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
gitaly_fork!(source_project, target_project)
|
|
|
|
link_lfs_objects(source_project, target_project)
|
2018-05-09 12:01:36 +05:30
|
|
|
target_project.after_import
|
|
|
|
end
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
def start_fork(project)
|
2019-02-15 15:39:39 +05:30
|
|
|
return true if start(project.import_state)
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2020-11-24 15:15:51 +05:30
|
|
|
Gitlab::AppLogger.info("Project #{project.full_path} was in inconsistent state (#{project.import_status}) while forking.")
|
2018-03-17 18:26:18 +05:30
|
|
|
false
|
2015-09-25 12:07:36 +05:30
|
|
|
end
|
2020-03-13 15:44:24 +05:30
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
def gitaly_fork!(source_project, target_project)
|
|
|
|
source_repo = source_project.repository.raw
|
|
|
|
target_repo = target_project.repository.raw
|
|
|
|
|
|
|
|
::Gitlab::GitalyClient::RepositoryService.new(target_repo).fork_repository(source_repo)
|
|
|
|
rescue GRPC::BadStatus => e
|
|
|
|
Gitlab::ErrorTracking.track_exception(e, source_project_id: source_project.id, target_project_id: target_project.id)
|
|
|
|
|
|
|
|
raise_fork_failure(source_project, target_project, 'Failed to create fork repository')
|
|
|
|
end
|
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
def link_lfs_objects(source_project, target_project)
|
|
|
|
Projects::LfsPointers::LfsLinkService
|
|
|
|
.new(target_project)
|
2020-11-24 15:15:51 +05:30
|
|
|
.execute(source_project.lfs_objects_oids)
|
2020-03-13 15:44:24 +05:30
|
|
|
rescue Projects::LfsPointers::LfsLinkService::TooManyOidsError
|
|
|
|
raise_fork_failure(
|
|
|
|
source_project,
|
|
|
|
target_project,
|
|
|
|
'Source project has too many LFS objects'
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def raise_fork_failure(source_project, target_project, reason)
|
|
|
|
raise "Unable to fork project #{target_project.id} for repository #{source_project.disk_path} -> #{target_project.disk_path}: #{reason}"
|
|
|
|
end
|
2015-09-25 12:07:36 +05:30
|
|
|
end
|