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

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

Constant Summary collapse

IMPORTED_OBJECT_MAX_RETRIES =
5
OVERRIDES =
{ user_contributions: :user, merge_schedule: 'MergeRequests::MergeSchedule' }.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

Constants included from Gitlab::Import::UsernameMentionRewriter

Gitlab::Import::UsernameMentionRewriter::MENTION_REGEX

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Gitlab::Import::UsernameMentionRewriter

#update_username_mentions, #wrap_mentions_in_backticks

Constructor Details

#initialize(relation_sym:, relation_index:, relation_hash:, members_mapper:, object_builder:, user:, importable:, import_source:, excluded_keys: [], original_users_map: nil, rewrite_mentions: false) ⇒ RelationFactory

rubocop:disable Metrics/ParameterLists – Keyword arguments are not adding complexity to initializer



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/gitlab/import_export/base/relation_factory.rb', line 54

def initialize(relation_sym:, relation_index:, relation_hash:, members_mapper:, object_builder:, user:, importable:, import_source:, excluded_keys: [], original_users_map: nil, rewrite_mentions: false)
  @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
  @import_source = import_source
  @imported_object_retries = 0
  @relation_hash[importable_column_name] = @importable.id
  @original_user = {}
  @original_users_map = original_users_map
  @rewrite_mentions = rewrite_mentions

  # 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.



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

def importable
  @importable
end

#relation_hashObject (readonly)

Returns the value of attribute relation_hash.



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

def relation_hash
  @relation_hash
end

#relation_nameObject (readonly)

Returns the value of attribute relation_name.



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

def relation_name
  @relation_name
end

Class Method Details

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



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

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

.existing_object_relationsObject



99
100
101
# File 'lib/gitlab/import_export/base/relation_factory.rb', line 99

def self.existing_object_relations
  self::EXISTING_OBJECT_RELATIONS
end

.overridesObject



95
96
97
# File 'lib/gitlab/import_export/base/relation_factory.rb', line 95

def self.overrides
  self::OVERRIDES
end

.relation_class(relation_name) ⇒ Object



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

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.
  overridden_relation = OVERRIDES.with_indifferent_access[relation_name]
  relation_name = overridden_relation if overridden_relation

  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.



83
84
85
86
87
88
89
90
91
92
93
# File 'lib/gitlab/import_export/base/relation_factory.rb', line 83

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

  setup_base_models
  setup_models

  return if @relation_hash.empty?

  generate_imported_object
end