Commit 8ac53eb5 authored by James Lopez's avatar James Lopez

started refactoring import export reader - WIP

parent b6ab4a31
...@@ -11,7 +11,7 @@ module Gitlab ...@@ -11,7 +11,7 @@ module Gitlab
end end
def project_tree def project_tree
Gitlab::ImportExport::ImportExportReader.project_tree Gitlab::ImportExport::ImportExportReader.new.project_tree
end end
def storage_path def storage_path
......
module Gitlab
module ImportExport
class AttributesFinder
def initialize(included_attributes:, excluded_attributes:)
@included_attributes = included_attributes || {}
@excluded_attributes = excluded_attributes || {}
end
def find(model_object)
parsed_hash = find_attributes_only(model_object)
parsed_hash.empty? ? model_object : { model_object => parsed_hash }
end
def find_attributes_only(value)
find_included(value).merge(find_excluded(value))
end
def find_included(value)
key = key_from_hash(value)
@included_attributes[key].nil? ? {} : { only: @included_attributes[key] }
end
def find_excluded(value)
key = key_from_hash(value)
@excluded_attributes[key].nil? ? {} : { except: @excluded_attributes[key] }
end
private
def key_from_hash(value)
value.is_a?(Hash) ? value.keys.first : value
end
end
end
end
\ No newline at end of file
module Gitlab module Gitlab
module ImportExport module ImportExport
module ImportExportReader class ImportExportReader
extend self #FIXME
def project_tree def initialize(config: 'lib/gitlab/import_export/import_export.yml')
{ only: included_attributes[:project], include: build_hash(tree) } config = YAML.load_file('lib/gitlab/import_export/import_export.yml').with_indifferent_access
@tree = config[:project_tree]
@attributes_parser = Gitlab::ImportExport::AttributesFinder.new(included_attributes: config[:included_attributes],
excluded_attributes: config[:excluded_attributes])
end end
def tree def project_tree
config[:project_tree] { only: @attributes_parser.find_included(:project), include: build_hash(@tree) }
end end
private private
def config def build_hash(model_list)
@config ||= YAML.load_file('lib/gitlab/import_export/import_export.yml').with_indifferent_access model_list.map do |model_object|
end
def included_attributes
config[:included_attributes] || {}
end
def excluded_attributes
config[:excluded_attributes] || {}
end
def build_hash(array)
array.map do |model_object|
if model_object.is_a?(Hash) if model_object.is_a?(Hash)
process_include(model_object) process_include(model_object)
else else
only_except_hash = check_only_and_except(model_object) @attributes_parser.find(model_object)
only_except_hash.empty? ? model_object : { model_object => only_except_hash }
end end
end end
end end
...@@ -51,48 +41,30 @@ module Gitlab ...@@ -51,48 +41,30 @@ module Gitlab
def process_current_class(hash, included_classes_hash, value) def process_current_class(hash, included_classes_hash, value)
value = value.is_a?(Hash) ? process_include(hash, included_classes_hash) : value value = value.is_a?(Hash) ? process_include(hash, included_classes_hash) : value
only_except_hash = check_only_and_except(hash.keys.first) attributes_hash = @attributes_parser.find_attributes_only(hash.keys.first)
included_classes_hash[hash.keys.first] ||= only_except_hash unless only_except_hash.empty? included_classes_hash[hash.keys.first] ||= attributes_hash unless attributes_hash.empty?
value value
end end
def add_new_class(current_key, included_classes_hash, value) def add_new_class(current_key, included_classes_hash, value)
only_except_hash = check_only_and_except(value) attributes_hash = @attributes_parser.find_attributes_only(value)
parsed_hash = { include: value } parsed_hash = { include: value }
unless only_except_hash.empty? unless attributes_hash.empty?
if value.is_a?(Hash) if value.is_a?(Hash)
parsed_hash = { include: value.merge(only_except_hash) } parsed_hash = { include: value.merge(attributes_hash) }
else else
parsed_hash = { include: { value => only_except_hash } } parsed_hash = { include: { value => attributes_hash } }
end end
end end
included_classes_hash[current_key] = parsed_hash included_classes_hash[current_key] = parsed_hash
end end
def add_to_class(current_key, included_classes_hash, value) def add_to_class(current_key, included_classes_hash, value)
only_except_hash = check_only_and_except(value) attributes_hash = @attributes_parser.find_attributes_only(value)
value = { value => only_except_hash } unless only_except_hash.empty? value = { value => attributes_hash } unless attributes_hash.empty?
old_values = included_classes_hash[current_key][:include] old_values = included_classes_hash[current_key][:include]
included_classes_hash[current_key][:include] = ([old_values] + [value]).compact.flatten included_classes_hash[current_key][:include] = ([old_values] + [value]).compact.flatten
end end
def check_only_and_except(value)
check_only(value).merge(check_except(value))
end
def check_only(value)
key = key_from_hash(value)
included_attributes[key].nil? ? {} : { only: included_attributes[key] }
end
def check_except(value)
key = key_from_hash(value)
excluded_attributes[key].nil? ? {} : { except: excluded_attributes[key] }
end
def key_from_hash(value)
value.is_a?(Hash) ? value.keys.first : value
end
end end
end end
end end
...@@ -17,8 +17,6 @@ describe Gitlab::ImportExport::ImportExportReader do ...@@ -17,8 +17,6 @@ describe Gitlab::ImportExport::ImportExportReader do
end end
it 'should generate hash from project tree config' do it 'should generate hash from project tree config' do
allow(described_class).to receive(:config).and_return(YAML.load_file(test_config).deep_symbolize_keys) expect(described_class.new(config: test_config).project_tree).to eq(project_tree_hash)
expect(described_class.project_tree).to eq(project_tree_hash)
end end
end end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment