2016-04-14 14:57:25 +00:00
|
|
|
module Gitlab
|
2016-04-06 16:12:41 +00:00
|
|
|
module ImportExport
|
2016-06-13 19:18:26 +00:00
|
|
|
class Reader
|
2016-04-06 16:12:41 +00:00
|
|
|
|
2016-05-11 15:22:45 +00:00
|
|
|
attr_reader :tree
|
|
|
|
|
2016-06-13 07:37:58 +00:00
|
|
|
def initialize(shared:)
|
2016-05-10 15:15:20 +00:00
|
|
|
@shared = shared
|
2016-06-13 08:55:54 +00:00
|
|
|
config_hash = YAML.load_file(Gitlab::ImportExport.config_file).deep_symbolize_keys
|
2016-05-09 09:10:12 +00:00
|
|
|
@tree = config_hash[:project_tree]
|
2016-06-13 07:37:58 +00:00
|
|
|
@attributes_finder = Gitlab::ImportExport::AttributesFinder.new(included_attributes: config_hash[:included_attributes],
|
2016-05-18 15:48:15 +00:00
|
|
|
excluded_attributes: config_hash[:excluded_attributes],
|
|
|
|
methods: config_hash[:methods])
|
2016-04-06 16:12:41 +00:00
|
|
|
end
|
|
|
|
|
2016-06-13 10:32:15 +00:00
|
|
|
# Outputs a hash in the format described here: http://api.rubyonrails.org/classes/ActiveModel/Serializers/JSON.html
|
|
|
|
# for outputting a project in JSON format, including its relations and sub relations.
|
2016-05-06 15:55:06 +00:00
|
|
|
def project_tree
|
2016-06-13 07:37:58 +00:00
|
|
|
@attributes_finder.find_included(:project).merge(include: build_hash(@tree))
|
2016-05-10 15:15:20 +00:00
|
|
|
rescue => e
|
2016-05-13 10:33:13 +00:00
|
|
|
@shared.error(e)
|
2016-05-16 08:13:00 +00:00
|
|
|
false
|
2016-04-12 13:32:25 +00:00
|
|
|
end
|
|
|
|
|
2016-04-07 11:19:57 +00:00
|
|
|
private
|
|
|
|
|
2016-06-13 10:32:15 +00:00
|
|
|
# Builds a hash in the format described here: http://api.rubyonrails.org/classes/ActiveModel/Serializers/JSON.html
|
|
|
|
#
|
|
|
|
# +model_list+ - List of models as a relation tree to be included in the generated JSON, from the _import_export.yml_ file
|
2016-05-06 15:55:06 +00:00
|
|
|
def build_hash(model_list)
|
2016-05-09 16:40:31 +00:00
|
|
|
model_list.map do |model_objects|
|
|
|
|
if model_objects.is_a?(Hash)
|
|
|
|
build_json_config_hash(model_objects)
|
2016-04-07 16:14:42 +00:00
|
|
|
else
|
2016-06-13 07:37:58 +00:00
|
|
|
@attributes_finder.find(model_objects)
|
2016-04-07 16:14:42 +00:00
|
|
|
end
|
|
|
|
end
|
2016-04-06 16:12:41 +00:00
|
|
|
end
|
|
|
|
|
2016-06-13 10:32:15 +00:00
|
|
|
# Called when the model is actually a hash containing other relations (more models)
|
|
|
|
# Returns the config in the right format for calling +to_json+
|
|
|
|
# +model_object_hash+ - A model relationship such as:
|
|
|
|
# {:merge_requests=>[:merge_request_diff, :notes]}
|
2016-05-09 15:58:43 +00:00
|
|
|
def build_json_config_hash(model_object_hash)
|
2016-05-10 15:15:20 +00:00
|
|
|
@json_config_hash = {}
|
|
|
|
|
2016-05-09 10:15:50 +00:00
|
|
|
model_object_hash.values.flatten.each do |model_object|
|
|
|
|
current_key = model_object_hash.keys.first
|
2016-05-09 15:58:43 +00:00
|
|
|
|
2016-06-13 07:37:58 +00:00
|
|
|
@attributes_finder.parse(current_key) { |hash| @json_config_hash[current_key] ||= hash }
|
2016-05-09 15:58:43 +00:00
|
|
|
|
|
|
|
handle_model_object(current_key, model_object)
|
2016-05-12 12:27:41 +00:00
|
|
|
process_sub_model(current_key, model_object) if model_object.is_a?(Hash)
|
2016-04-06 16:12:41 +00:00
|
|
|
end
|
2016-05-09 15:58:43 +00:00
|
|
|
@json_config_hash
|
2016-04-06 16:12:41 +00:00
|
|
|
end
|
|
|
|
|
2016-06-13 10:32:15 +00:00
|
|
|
|
|
|
|
# If the model is a hash, process the sub_models, which could also be hashes
|
|
|
|
# If there is a list, add to an existing array, otherwise use hash syntax
|
|
|
|
# +current_key+ main model that will be a key in the hash
|
|
|
|
# +model_object+ model or list of models to include in the hash
|
2016-05-12 12:27:41 +00:00
|
|
|
def process_sub_model(current_key, model_object)
|
|
|
|
sub_model_json = build_json_config_hash(model_object).dup
|
|
|
|
@json_config_hash.slice!(current_key)
|
|
|
|
|
|
|
|
if @json_config_hash[current_key] && @json_config_hash[current_key][:include]
|
|
|
|
@json_config_hash[current_key][:include] << sub_model_json
|
|
|
|
else
|
|
|
|
@json_config_hash[current_key] = { include: sub_model_json }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-06-13 10:32:15 +00:00
|
|
|
# Creates or adds to an existing hash an individual model or list
|
|
|
|
# +current_key+ main model that will be a key in the hash
|
|
|
|
# +model_object+ model or list of models to include in the hash
|
2016-05-09 15:58:43 +00:00
|
|
|
def handle_model_object(current_key, model_object)
|
|
|
|
if @json_config_hash[current_key]
|
|
|
|
add_model_value(current_key, model_object)
|
|
|
|
else
|
|
|
|
create_model_value(current_key, model_object)
|
|
|
|
end
|
2016-04-07 11:19:57 +00:00
|
|
|
end
|
|
|
|
|
2016-06-13 10:32:15 +00:00
|
|
|
# Constructs a new hash that will hold the configuration for that particular object
|
|
|
|
# It may include exceptions or other attribute detail configuration, parsed by +@attributes_finder+
|
|
|
|
# +current_key+ main model that will be a key in the hash
|
|
|
|
# +value+ existing model to be included in the hash
|
2016-05-09 15:58:43 +00:00
|
|
|
def create_model_value(current_key, value)
|
2016-05-04 13:13:44 +00:00
|
|
|
parsed_hash = { include: value }
|
2016-05-09 15:58:43 +00:00
|
|
|
|
2016-06-13 07:37:58 +00:00
|
|
|
@attributes_finder.parse(value) do |hash|
|
2016-05-12 09:29:06 +00:00
|
|
|
parsed_hash = { include: hash_or_merge(value, hash) }
|
2016-05-04 13:13:44 +00:00
|
|
|
end
|
2016-05-09 15:58:43 +00:00
|
|
|
@json_config_hash[current_key] = parsed_hash
|
|
|
|
end
|
|
|
|
|
2016-06-13 10:32:15 +00:00
|
|
|
# Adds new model configuration to an existing hash with key +current_key+
|
|
|
|
# It may include exceptions or other attribute detail configuration, parsed by +@attributes_finder+
|
|
|
|
# +current_key+ main model that will be a key in the hash
|
|
|
|
# +value+ existing model to be included in the hash
|
2016-05-09 15:58:43 +00:00
|
|
|
def add_model_value(current_key, value)
|
2016-06-13 07:37:58 +00:00
|
|
|
@attributes_finder.parse(value) { |hash| value = { value => hash } }
|
2016-05-09 15:58:43 +00:00
|
|
|
old_values = @json_config_hash[current_key][:include]
|
|
|
|
@json_config_hash[current_key][:include] = ([old_values] + [value]).compact.flatten
|
2016-04-07 11:19:57 +00:00
|
|
|
end
|
|
|
|
|
2016-06-13 10:32:15 +00:00
|
|
|
# Construct a new hash or merge with an existing one a model configuration
|
|
|
|
# This is to fulfil +to_json+ requirements.
|
|
|
|
# +value+ existing model to be included in the hash
|
|
|
|
# +hash+ hash containing configuration generated mainly from +@attributes_finder+
|
2016-05-09 15:58:43 +00:00
|
|
|
def hash_or_merge(value, hash)
|
2016-05-10 15:15:20 +00:00
|
|
|
value.is_a?(Hash) ? value.merge(hash) : { value => hash }
|
2016-04-07 11:19:57 +00:00
|
|
|
end
|
2016-04-06 16:12:41 +00:00
|
|
|
end
|
|
|
|
end
|
2016-04-08 13:07:12 +00:00
|
|
|
end
|