Commit acfa0b69 authored by James Lopez's avatar James Lopez

continuing to refactor config, added spec and fixed a few problems

parent b4fe0022
# Class relationships to be included in the import/export
# Class relationships to be included in the project import/export
:project_tree:
- :issues
- :labels
......
......@@ -7,6 +7,8 @@ module Projects
{ only: atts_only[:project], include: build_hash(tree) }
end
private
def config
@config ||= YAML.load_file('app/services/projects/import_export/import_export.yml')
end
......@@ -27,21 +29,56 @@ module Projects
array.map { |el| el.is_a?(Hash) ? process_include(el) : el }
end
def process_include(hash)
included_classes_hash = {}
def process_include(hash, included_classes_hash = {})
hash.values.flatten.each do |value|
value = value.is_a?(Hash) ? process_include(hash) : value
new_hash = { :include => value }
new_hash.merge!(check_only(value))
included_classes_hash[hash.keys.first] = new_hash
current_key, value = process_current_class(hash, included_classes_hash, value)
if included_classes_hash[current_key]
add_class(current_key, included_classes_hash, value)
else
add_new_class(current_key, included_classes_hash, value)
end
end
included_classes_hash
end
def process_current_class(hash, included_classes_hash, value)
value = value.is_a?(Hash) ? process_include(hash, included_classes_hash) : value
current_key = hash.keys.first
current_key_only = check_only_and_except(current_key)
included_classes_hash[current_key] ||= current_key_only unless current_key_only.empty?
return current_key, value
end
def add_new_class(current_key, included_classes_hash, value)
new_hash = { :include => value }
new_hash.merge!(check_only_and_except(value))
included_classes_hash[current_key] = new_hash
end
def add_class(current_key, included_classes_hash, value)
check_only_hash = check_only_and_except(value)
value = { value => check_only_hash } unless check_only_hash.empty?
old_values = included_classes_hash[current_key][:include]
included_classes_hash[current_key][:include] = ([old_values] + [value]).compact.flatten
end
def check_only_and_except(value)
check_only(value).merge(check_except(value))
end
def check_only(value)
key = value.is_a?(Hash) ? value.keys.first : value
key = key_from_hash(value)
atts_only[key].nil? ? {} : { only: atts_only[key] }
end
def check_except(value)
key = key_from_hash(value)
atts_except[key].nil? ? {} : { except: atts_except[key] }
end
def key_from_hash(value)
value.is_a?(Hash) ? value.keys.first : value
end
end
end
end
\ No newline at end of file
require 'rspec'
describe Projects::ImportExport::ImportExportReader do
let(:test_config) { 'spec/support/import_export/import_export.yml' }
let(:project_tree_hash) do
{
:only => [:name, :path],
:include => [:issues, :labels,
{ :merge_requests => {
:only => [:id],
:except => [:iid],
:include => [:merge_request_diff, :merge_request_test]
} },
{ :commit_statuses => { :include => :commit } }]
}
end
it 'should generate hash from project tree config' do
allow(described_class).to receive(:config).and_return(YAML.load_file(test_config))
expect(described_class.project_tree).to eq(project_tree_hash)
end
end
\ No newline at end of file
# Class relationships to be included in the project import/export
:project_tree:
- :issues
- :labels
- :merge_requests:
- :merge_request_diff
- :merge_request_test
- :commit_statuses:
- :commit
:attributes_only:
:project:
- :name
- :path
:merge_requests:
- :id
:attributes_except:
:merge_requests:
- :iid
\ No newline at end of file
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