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
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 ErrorTracking::ProjectErrorTrackingSetting].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of RelationFactory.



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

def initialize(relation_sym:, relation_index:, relation_hash:, members_mapper:, object_builder:, user:, importable:, excluded_keys: [])
  @relation_sym = relation_sym
  @relation_name = self.class.overrides[relation_sym]&.to_sym || relation_sym
  @relation_index = relation_index
  @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
  @original_user = {}

  # 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

Instance Attribute Details

#importableObject (readonly)

Returns the value of attribute importable.



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

def importable
  @importable
end

#relation_nameObject (readonly)

Returns the value of attribute relation_name.



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

def relation_name
  @relation_name
end

Class Method Details

.create(*args, **kwargs) ⇒ Object



36
37
38
# File 'lib/gitlab/import_export/base/relation_factory.rb', line 36

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

.existing_object_relationsObject



88
89
90
# File 'lib/gitlab/import_export/base/relation_factory.rb', line 88

def self.existing_object_relations
  self::EXISTING_OBJECT_RELATIONS
end

.overridesObject



84
85
86
# File 'lib/gitlab/import_export/base/relation_factory.rb', line 84

def self.overrides
  self::OVERRIDES
end

.relation_class(relation_name) ⇒ Object



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

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.



74
75
76
77
78
79
80
81
82
# File 'lib/gitlab/import_export/base/relation_factory.rb', line 74

def create
  return @relation_hash if author_relation?
  return if invalid_relation? || predefined_relation?

  setup_base_models
  setup_models

  generate_imported_object
end