2018-06-27 07:23:28 +00:00
# frozen_string_literal: true
2020-02-19 18:09:10 +00:00
class RepositoryForkWorker # rubocop:disable Scalability/IdempotentWorker
2017-11-28 16:08:30 +00:00
include ApplicationWorker
2017-10-10 09:09:49 +00:00
include ProjectStartImport
2017-12-11 11:35:03 +00:00
include ProjectImportOptions
2017-08-17 09:00:31 +00:00
2019-10-18 11:11:44 +00:00
feature_category :source_code_management
2018-03-26 18:21:49 +00:00
def perform ( * args )
target_project_id = args . shift
target_project = Project . find ( target_project_id )
2017-08-17 09:00:31 +00:00
2018-06-19 10:17:54 +00:00
source_project = target_project . forked_from_project
unless source_project
2018-11-27 09:41:27 +00:00
return target_project . import_state . mark_as_failed ( _ ( 'Source project cannot be found.' ) )
2018-03-26 18:21:49 +00:00
end
2018-06-19 10:17:54 +00:00
2018-12-23 07:34:35 +00:00
fork_repository ( target_project , source_project )
2017-06-01 14:27:35 +00:00
end
private
2018-12-23 07:34:35 +00:00
def fork_repository ( target_project , source_project )
2018-03-26 18:21:49 +00:00
return unless start_fork ( target_project )
2018-06-19 17:03:25 +00:00
Gitlab :: Metrics . add_event ( :fork_repository )
2018-03-26 18:21:49 +00:00
2020-03-17 21:09:16 +00:00
gitaly_fork! ( source_project , target_project )
link_lfs_objects ( source_project , target_project )
2018-03-26 18:21:49 +00:00
target_project . after_import
end
2017-08-17 09:00:31 +00:00
def start_fork ( project )
2018-11-27 09:41:27 +00:00
return true if start ( project . import_state )
2017-08-17 09:00:31 +00:00
2019-07-10 19:26:47 +00:00
Rails . logger . info ( " Project #{ project . full_path } was in inconsistent state ( #{ project . import_status } ) while forking. " ) # rubocop:disable Gitlab/RailsLogger
2017-08-17 09:00:31 +00:00
false
end
2020-02-07 12:09:13 +00:00
2020-03-17 21:09:16 +00:00
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-02-07 12:09:13 +00:00
def link_lfs_objects ( source_project , target_project )
Projects :: LfsPointers :: LfsLinkService
. new ( target_project )
2020-03-05 03:07:52 +00:00
. execute ( source_project . all_lfs_objects_oids )
2020-02-07 12:09:13 +00:00
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-01 07:56:40 +00:00
end