Module: Gitlab::GithubImport::ObjectImporter

Overview

ObjectImporter defines the base behaviour for every Sidekiq worker that imports a single resource such as a note or pull request.

Constant Summary collapse

NotRetriableError =
Class.new(StandardError)

Instance Method Summary collapse

Instance Method Details

#import(project, client, hash) ⇒ Object

project - An instance of ‘Project` to import the data into. client - An instance of `Gitlab::GithubImport::Client` hash - A Hash containing the details of the object to import.



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
# File 'app/workers/concerns/gitlab/github_import/object_importer.rb', line 40

def import(project, client, hash)
  if project.import_state&.completed?
    info(
      project.id,
      message: 'Project import is no longer running. Stopping worker.',
      import_status: project.import_state.status
    )

    return
  end

  object = representation_class.from_json_hash(hash)

  # To better express in the logs what object is being imported.
  self.github_identifiers = object.github_identifiers
  info(project.id, message: 'starting importer')

  importer_class.new(object, project, client).execute

  if increment_object_counter?(object)
    Gitlab::GithubImport::ObjectCounter.increment(project, object_type, :imported)
  end

  info(project.id, message: 'importer finished')
rescue NoMethodError => e
  # This exception will be more useful in development when a new
  # Representation is created but the developer forgot to add a
  # `:github_identifiers` field.
  track_and_raise_exception(project, e, fail_import: true)
rescue ActiveRecord::RecordInvalid, NotRetriableError => e
  # We do not raise exception to prevent job retry
  failure = track_exception(project, e)
  add_identifiers_to_failure(failure, object.github_identifiers)
rescue StandardError => e
  track_and_raise_exception(project, e)
end

#importer_classObject

Returns the class to use for importing the object.

Raises:

  • (NotImplementedError)


105
106
107
# File 'app/workers/concerns/gitlab/github_import/object_importer.rb', line 105

def importer_class
  raise NotImplementedError
end

#increment_object_counter?(_object) ⇒ Boolean

Returns:

  • (Boolean)


90
91
92
# File 'app/workers/concerns/gitlab/github_import/object_importer.rb', line 90

def increment_object_counter?(_object)
  true
end

#object_typeObject

Raises:

  • (NotImplementedError)


94
95
96
# File 'app/workers/concerns/gitlab/github_import/object_importer.rb', line 94

def object_type
  raise NotImplementedError
end

#perform_failure(project_id, hash, correlation_id) ⇒ Object

hash - A Hash containing the details of the object to import.



78
79
80
81
82
83
84
85
86
87
88
# File 'app/workers/concerns/gitlab/github_import/object_importer.rb', line 78

def perform_failure(project_id, hash, correlation_id)
  project = Project.find_by_id(project_id)
  return unless project

  failure = project.import_failures.failures_by_correlation_id(correlation_id).first
  return unless failure

  object = representation_class.from_json_hash(hash)

  add_identifiers_to_failure(failure, object.github_identifiers)
end

#representation_classObject

Returns the representation class to use for the object. This class must define the class method ‘from_json_hash`.

Raises:

  • (NotImplementedError)


100
101
102
# File 'app/workers/concerns/gitlab/github_import/object_importer.rb', line 100

def representation_class
  raise NotImplementedError
end