repository_import_worker.rb 1.11 KB
Newer Older
1 2
# frozen_string_literal: true

3
class RepositoryImportWorker
4
  include ApplicationWorker
5
  include ExceptionBacktrace
6
  include ProjectStartImport
7
  include ProjectImportOptions
8

9
  def perform(project_id)
Francisco Javier López's avatar
Francisco Javier López committed
10
    @project = Project.find(project_id)
11

Francisco Javier López's avatar
Francisco Javier López committed
12
    return unless start_import
13

14
    Gitlab::Metrics.add_event(:import_repository)
15

16 17 18 19 20 21 22
    service = Projects::ImportService.new(project, project.creator)
    result = service.execute

    # Some importers may perform their work asynchronously. In this case it's up
    # to those importers to mark the import process as complete.
    return if service.async?

23
    if result[:status] == :error
Francisco Javier López's avatar
Francisco Javier López committed
24
      fail_import(result[:message]) if template_import?
25 26 27

      raise result[:message]
    end
28

29
    project.after_import
30 31 32 33
  end

  private

34 35
  attr_reader :project

Francisco Javier López's avatar
Francisco Javier López committed
36
  def start_import
37
    return true if start(project)
38 39 40 41

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

Francisco Javier López's avatar
Francisco Javier López committed
43
  def fail_import(message)
44 45
    project.mark_import_as_failed(message)
  end
Francisco Javier López's avatar
Francisco Javier López committed
46 47 48 49

  def template_import?
    project.gitlab_project_import?
  end
50
end