Read about our upcoming Code of Conduct on this issue

repository_fork_worker.rb 1.33 KB
Newer Older
1
2
# frozen_string_literal: true

3
class RepositoryForkWorker
4
  include ApplicationWorker
5
  include Gitlab::ShellAdapter
6
  include ProjectStartImport
7
  include ProjectImportOptions
8

9
10
11
  def perform(*args)
    target_project_id = args.shift
    target_project = Project.find(target_project_id)
12

Zeger-Jan van de Weg's avatar
Zeger-Jan van de Weg committed
13
14
    source_project = target_project.forked_from_project
    unless source_project
15
      return target_project.import_state.mark_as_failed(_('Source project cannot be found.'))
16
    end
Zeger-Jan van de Weg's avatar
Zeger-Jan van de Weg committed
17
18

    fork_repository(target_project, source_project.repository_storage, source_project.disk_path)
19
20
21
22
  end

  private

23
24
25
  def fork_repository(target_project, source_repository_storage_name, source_disk_path)
    return unless start_fork(target_project)

26
    Gitlab::Metrics.add_event(:fork_repository)
27
28
29
30
31
32
33
34

    result = gitlab_shell.fork_repository(source_repository_storage_name, source_disk_path,
                                          target_project.repository_storage, target_project.disk_path)
    raise "Unable to fork project #{target_project.id} for repository #{source_disk_path} -> #{target_project.disk_path}" unless result

    target_project.after_import
  end

35
  def start_fork(project)
36
    return true if start(project.import_state)
37
38
39
40

    Rails.logger.info("Project #{project.full_path} was in inconsistent state (#{project.import_status}) while forking.")
    false
  end
41
end