File: repository_fork_worker.rb

package info (click to toggle)
gitlab 17.6.5-19
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 629,368 kB
  • sloc: ruby: 1,915,304; javascript: 557,307; sql: 60,639; xml: 6,509; sh: 4,567; makefile: 1,239; python: 406
file content (85 lines) | stat: -rw-r--r-- 2,495 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# frozen_string_literal: true

class RepositoryForkWorker # rubocop:disable Scalability/IdempotentWorker
  include ApplicationWorker
  include Gitlab::Utils::StrongMemoize

  data_consistency :always

  sidekiq_options retry: 3
  include ProjectStartImport
  include ProjectImportOptions

  feature_category :source_code_management

  def perform(*args)
    @target_project_id = args.shift

    unless source_project
      return target_project.import_state.mark_as_failed(_('Source project cannot be found.'))
    end

    fork_repository(target_project, source_project)
  end

  private

  def target_project
    Project.find(@target_project_id)
  end
  strong_memoize_attr :target_project

  def source_project
    @source_project ||= target_project.forked_from_project
  end

  def branch
    return unless target_project.import_data&.data

    target_project.import_data.data['fork_branch']
  end

  def fork_repository(target_project, source_project)
    return unless start_fork(target_project)

    Gitlab::Metrics.add_event(:fork_repository)

    gitaly_fork!(source_project, target_project)
    link_lfs_objects(source_project, target_project)
    target_project.after_import
  end

  def start_fork(project)
    return true if start(project.import_state)

    Gitlab::AppLogger.info("Project #{project.full_path} was in inconsistent state (#{project.import_status}) while forking.")
    false
  end

  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, branch)
  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

  def link_lfs_objects(source_project, target_project)
    Projects::LfsPointers::LfsLinkService
        .new(target_project)
        .execute(source_project.lfs_objects_oids)
  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
end