Class: Gitlab::ImportExport::Base::RelationFactory

Inherits:
Object
  • Object
show all
Includes:
Utils::StrongMemoize
Defined in:
lib/gitlab/import_export/base/relation_factory.rb

Constant Summary collapse

IMPORTED_OBJECT_MAX_RETRIES =
5.freeze
OVERRIDES =
{}.freeze
EXISTING_OBJECT_RELATIONS =
%i[].freeze
UNIQUE_RELATIONS =

This represents all relations that have unique key on `project_id` or `group_id`

%i[].freeze
USER_REFERENCES =
%w[
  author_id
  assignee_id
  updated_by_id
  merged_by_id
  latest_closed_by_id
  user_id
  created_by_id
  last_edited_by_id
  merge_user_id
  resolved_by_id
  closed_by_id
  owner_id
].freeze
TOKEN_RESET_MODELS =
%i[Project Namespace Group Ci::Trigger Ci::Build Ci::Runner ProjectHook].freeze

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Utils::StrongMemoize

#clear_memoization, #strong_memoize, #strong_memoized?

Constructor Details

#initialize(relation_sym:, relation_hash:, members_mapper:, object_builder:, user:, importable:, excluded_keys: []) ⇒ RelationFactory

Returns a new instance of RelationFactory.


47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/gitlab/import_export/base/relation_factory.rb', line 47

def initialize(relation_sym:, relation_hash:, members_mapper:, object_builder:, user:, importable:, excluded_keys: [])
  @relation_name = self.class.overrides[relation_sym]&.to_sym || relation_sym
  @relation_hash = relation_hash.except('noteable_id')
  @members_mapper = members_mapper
  @object_builder = object_builder
  @user = user
  @importable = importable
  @imported_object_retries = 0
  @relation_hash[importable_column_name] = @importable.id

  # Remove excluded keys from relation_hash
  # We don't do this in the parsed_relation_hash because of the 'transformed attributes'
  # For example, MergeRequestDiffFiles exports its diff attribute as utf8_diff. Then,
  # in the create method that attribute is renamed to diff. And because diff is an excluded key,
  # if we clean the excluded keys in the parsed_relation_hash, it will be removed
  # from the object attributes and the export will fail.
  @relation_hash.except!(*excluded_keys)
end

Class Method Details

.create(*args) ⇒ Object


34
35
36
# File 'lib/gitlab/import_export/base/relation_factory.rb', line 34

def self.create(*args)
  new(*args).create
end

.existing_object_relationsObject


82
83
84
# File 'lib/gitlab/import_export/base/relation_factory.rb', line 82

def self.existing_object_relations
  self::EXISTING_OBJECT_RELATIONS
end

.overridesObject


78
79
80
# File 'lib/gitlab/import_export/base/relation_factory.rb', line 78

def self.overrides
  self::OVERRIDES
end

.relation_class(relation_name) ⇒ Object


38
39
40
41
42
43
44
45
# File 'lib/gitlab/import_export/base/relation_factory.rb', line 38

def self.relation_class(relation_name)
  # There are scenarios where the model is pluralized (e.g.
  # MergeRequest::Metrics), and we don't want to force it to singular
  # with #classify.
  relation_name.to_s.classify.constantize
rescue NameError
  relation_name.to_s.constantize
end

Instance Method Details

#createObject

Creates an object from an actual model with name “relation_sym” with params from the relation_hash, updating references with new object IDs, mapping users using the “members_mapper” object, also updating notes if required.


69
70
71
72
73
74
75
76
# File 'lib/gitlab/import_export/base/relation_factory.rb', line 69

def create
  return if invalid_relation? || predefined_relation?

  setup_base_models
  setup_models

  generate_imported_object
end