2020-03-17 09:09:20 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Gitlab
|
|
|
|
module ImportExport
|
|
|
|
module JSON
|
|
|
|
class LegacyReader
|
|
|
|
class File < LegacyReader
|
2020-03-26 18:08:03 +00:00
|
|
|
include Gitlab::Utils::StrongMemoize
|
|
|
|
|
|
|
|
def initialize(path, relation_names:, allowed_path: nil)
|
2020-03-17 09:09:20 +00:00
|
|
|
@path = path
|
2020-03-26 18:08:03 +00:00
|
|
|
super(
|
|
|
|
relation_names: relation_names,
|
|
|
|
allowed_path: allowed_path)
|
2020-03-17 09:09:20 +00:00
|
|
|
end
|
|
|
|
|
2020-03-26 18:08:03 +00:00
|
|
|
def exist?
|
2020-03-17 09:09:20 +00:00
|
|
|
::File.exist?(@path)
|
|
|
|
end
|
|
|
|
|
2020-03-26 18:08:03 +00:00
|
|
|
protected
|
2020-03-17 09:09:20 +00:00
|
|
|
|
|
|
|
def tree_hash
|
2020-03-26 18:08:03 +00:00
|
|
|
strong_memoize(:tree_hash) do
|
|
|
|
read_hash
|
|
|
|
end
|
2020-03-17 09:09:20 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def read_hash
|
|
|
|
ActiveSupport::JSON.decode(IO.read(@path))
|
2021-04-26 12:09:44 +00:00
|
|
|
rescue StandardError => e
|
2020-03-17 09:09:20 +00:00
|
|
|
Gitlab::ErrorTracking.log_exception(e)
|
2021-05-04 15:10:36 +00:00
|
|
|
raise Gitlab::ImportExport::Error, 'Incorrect JSON format'
|
2020-03-17 09:09:20 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-03-26 18:08:03 +00:00
|
|
|
class Hash < LegacyReader
|
|
|
|
def initialize(tree_hash, relation_names:, allowed_path: nil)
|
2020-03-17 09:09:20 +00:00
|
|
|
@tree_hash = tree_hash
|
2020-03-26 18:08:03 +00:00
|
|
|
super(
|
|
|
|
relation_names: relation_names,
|
|
|
|
allowed_path: allowed_path)
|
2020-03-17 09:09:20 +00:00
|
|
|
end
|
|
|
|
|
2020-03-26 18:08:03 +00:00
|
|
|
def exist?
|
2020-03-17 09:09:20 +00:00
|
|
|
@tree_hash.present?
|
|
|
|
end
|
|
|
|
|
|
|
|
protected
|
|
|
|
|
|
|
|
attr_reader :tree_hash
|
|
|
|
end
|
|
|
|
|
2020-03-26 18:08:03 +00:00
|
|
|
def initialize(relation_names:, allowed_path:)
|
2020-03-17 09:09:20 +00:00
|
|
|
@relation_names = relation_names.map(&:to_s)
|
2020-04-08 03:09:31 +00:00
|
|
|
@consumed_relations = Set.new
|
2020-03-26 18:08:03 +00:00
|
|
|
|
|
|
|
# This is legacy reader, to be used in transition
|
|
|
|
# period before `.ndjson`,
|
|
|
|
# we strong validate what is being readed
|
|
|
|
@allowed_path = allowed_path
|
2020-03-17 09:09:20 +00:00
|
|
|
end
|
|
|
|
|
2020-03-26 18:08:03 +00:00
|
|
|
def exist?
|
2020-03-17 09:09:20 +00:00
|
|
|
raise NotImplementedError
|
|
|
|
end
|
|
|
|
|
|
|
|
def legacy?
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2020-03-26 18:08:03 +00:00
|
|
|
def consume_attributes(importable_path)
|
|
|
|
unless importable_path == @allowed_path
|
|
|
|
raise ArgumentError, "Invalid #{importable_path} passed to `consume_attributes`. Use #{@allowed_path} instead."
|
|
|
|
end
|
|
|
|
|
|
|
|
attributes
|
2020-03-17 09:09:20 +00:00
|
|
|
end
|
|
|
|
|
2020-03-26 18:08:03 +00:00
|
|
|
def consume_relation(importable_path, key)
|
|
|
|
unless importable_path == @allowed_path
|
|
|
|
raise ArgumentError, "Invalid #{importable_name} passed to `consume_relation`. Use #{@allowed_path} instead."
|
|
|
|
end
|
|
|
|
|
2020-04-08 03:09:31 +00:00
|
|
|
Enumerator.new do |documents|
|
|
|
|
next unless @consumed_relations.add?("#{importable_path}/#{key}")
|
2020-03-17 09:09:20 +00:00
|
|
|
|
2020-04-08 03:09:31 +00:00
|
|
|
value = relations.delete(key)
|
|
|
|
next if value.nil?
|
2020-03-17 09:09:20 +00:00
|
|
|
|
2020-04-08 03:09:31 +00:00
|
|
|
if value.is_a?(Array)
|
|
|
|
value.each.with_index do |item, idx|
|
|
|
|
documents << [item, idx]
|
|
|
|
end
|
|
|
|
else
|
|
|
|
documents << [value, 0]
|
2020-03-17 09:09:20 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def sort_ci_pipelines_by_id
|
|
|
|
relations['ci_pipelines']&.sort_by! { |hash| hash['id'] }
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2020-03-26 18:08:03 +00:00
|
|
|
attr_reader :relation_names, :allowed_path
|
2020-03-17 09:09:20 +00:00
|
|
|
|
|
|
|
def tree_hash
|
|
|
|
raise NotImplementedError
|
|
|
|
end
|
|
|
|
|
|
|
|
def attributes
|
|
|
|
@attributes ||= tree_hash.slice!(*relation_names)
|
|
|
|
end
|
|
|
|
|
|
|
|
def relations
|
|
|
|
@relations ||= tree_hash.extract!(*relation_names)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|