debian-mirror-gitlab/lib/gitlab/import_export/json/legacy_reader.rb

124 lines
3.1 KiB
Ruby
Raw Normal View History

2020-04-08 14:13:33 +05:30
# frozen_string_literal: true
module Gitlab
module ImportExport
module JSON
class LegacyReader
class File < LegacyReader
2020-04-22 19:07:51 +05:30
include Gitlab::Utils::StrongMemoize
def initialize(path, relation_names:, allowed_path: nil)
2020-04-08 14:13:33 +05:30
@path = path
2020-04-22 19:07:51 +05:30
super(
relation_names: relation_names,
allowed_path: allowed_path)
2020-04-08 14:13:33 +05:30
end
2020-04-22 19:07:51 +05:30
def exist?
2020-04-08 14:13:33 +05:30
::File.exist?(@path)
end
2020-04-22 19:07:51 +05:30
protected
2020-04-08 14:13:33 +05:30
def tree_hash
2020-04-22 19:07:51 +05:30
strong_memoize(:tree_hash) do
read_hash
end
2020-04-08 14:13:33 +05:30
end
def read_hash
ActiveSupport::JSON.decode(IO.read(@path))
2021-06-08 01:23:25 +05:30
rescue StandardError => e
2020-04-08 14:13:33 +05:30
Gitlab::ErrorTracking.log_exception(e)
2021-06-08 01:23:25 +05:30
raise Gitlab::ImportExport::Error, 'Incorrect JSON format'
2020-04-08 14:13:33 +05:30
end
end
2020-04-22 19:07:51 +05:30
class Hash < LegacyReader
def initialize(tree_hash, relation_names:, allowed_path: nil)
2020-04-08 14:13:33 +05:30
@tree_hash = tree_hash
2020-04-22 19:07:51 +05:30
super(
relation_names: relation_names,
allowed_path: allowed_path)
2020-04-08 14:13:33 +05:30
end
2020-04-22 19:07:51 +05:30
def exist?
2020-04-08 14:13:33 +05:30
@tree_hash.present?
end
protected
attr_reader :tree_hash
end
2020-04-22 19:07:51 +05:30
def initialize(relation_names:, allowed_path:)
2020-04-08 14:13:33 +05:30
@relation_names = relation_names.map(&:to_s)
2020-04-22 19:07:51 +05:30
@consumed_relations = Set.new
# This is legacy reader, to be used in transition
# period before `.ndjson`,
# we strong validate what is being readed
@allowed_path = allowed_path
2020-04-08 14:13:33 +05:30
end
2020-04-22 19:07:51 +05:30
def exist?
2020-04-08 14:13:33 +05:30
raise NotImplementedError
end
def legacy?
true
end
2020-04-22 19:07:51 +05:30
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-04-08 14:13:33 +05:30
end
2020-04-22 19:07:51 +05:30
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 14:13:33 +05:30
2020-04-22 19:07:51 +05:30
Enumerator.new do |documents|
next unless @consumed_relations.add?("#{importable_path}/#{key}")
2020-04-08 14:13:33 +05:30
2020-04-22 19:07:51 +05:30
value = relations.delete(key)
next if value.nil?
2020-04-08 14:13:33 +05:30
2020-04-22 19:07:51 +05:30
if value.is_a?(Array)
value.each.with_index do |item, idx|
documents << [item, idx]
end
else
documents << [value, 0]
2020-04-08 14:13:33 +05:30
end
end
end
def sort_ci_pipelines_by_id
relations['ci_pipelines']&.sort_by! { |hash| hash['id'] }
end
private
2020-04-22 19:07:51 +05:30
attr_reader :relation_names, :allowed_path
2020-04-08 14:13:33 +05:30
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