debian-mirror-gitlab/lib/bulk_imports/ndjson_pipeline.rb

110 lines
3 KiB
Ruby
Raw Normal View History

2021-09-04 01:27:46 +05:30
# frozen_string_literal: true
module BulkImports
module NdjsonPipeline
extend ActiveSupport::Concern
include Pipeline
included do
ndjson_pipeline!
def transform(context, data)
2022-01-26 12:08:38 +05:30
return unless data
2021-09-04 01:27:46 +05:30
relation_hash, relation_index = data
2022-01-26 12:08:38 +05:30
return unless relation_hash
2021-09-04 01:27:46 +05:30
relation_definition = import_export_config.top_relation_tree(relation)
2021-12-11 22:18:48 +05:30
relation_object = deep_transform_relation!(relation_hash, relation, relation_definition) do |key, hash|
2021-11-18 22:05:49 +05:30
relation_factory.create(
2021-09-04 01:27:46 +05:30
relation_index: relation_index,
relation_sym: key.to_sym,
relation_hash: hash,
importable: context.portable,
members_mapper: members_mapper,
object_builder: object_builder,
user: context.current_user,
excluded_keys: import_export_config.relation_excluded_keys(key)
)
end
2021-12-11 22:18:48 +05:30
relation_object.assign_attributes(portable_class_sym => portable)
relation_object
2021-09-04 01:27:46 +05:30
end
def load(_, object)
2022-01-26 12:08:38 +05:30
object&.save!
2021-09-04 01:27:46 +05:30
end
def deep_transform_relation!(relation_hash, relation_key, relation_definition, &block)
relation_key = relation_key_override(relation_key)
relation_definition.each do |sub_relation_key, sub_relation_definition|
sub_relation = relation_hash[sub_relation_key]
next unless sub_relation
current_item =
if sub_relation.is_a?(Array)
sub_relation
.map { |entry| deep_transform_relation!(entry, sub_relation_key, sub_relation_definition, &block) }
.tap { |entry| entry.compact! }
.presence
else
deep_transform_relation!(sub_relation, sub_relation_key, sub_relation_definition, &block)
end
if current_item
relation_hash[sub_relation_key] = current_item
else
relation_hash.delete(sub_relation_key)
end
end
yield(relation_key, relation_hash)
end
def after_run(_)
extractor.remove_tmp_dir if extractor.respond_to?(:remove_tmp_dir)
end
def relation_class(relation_key)
relation_key.to_s.classify.constantize
rescue NameError
relation_key.to_s.constantize
end
def relation_key_override(relation_key)
relation_key_overrides[relation_key.to_sym]&.to_s || relation_key
end
def relation_key_overrides
"Gitlab::ImportExport::#{portable.class}::RelationFactory::OVERRIDES".constantize
end
def object_builder
"Gitlab::ImportExport::#{portable.class}::ObjectBuilder".constantize
end
2021-11-18 22:05:49 +05:30
def relation_factory
"Gitlab::ImportExport::#{portable.class}::RelationFactory".constantize
end
2021-09-04 01:27:46 +05:30
def relation
self.class.relation
end
def members_mapper
2021-09-30 23:02:18 +05:30
@members_mapper ||= BulkImports::UsersMapper.new(context: context)
2021-09-04 01:27:46 +05:30
end
2021-12-11 22:18:48 +05:30
def portable_class_sym
portable.class.to_s.downcase.to_sym
end
2021-09-04 01:27:46 +05:30
end
end
end