2019-02-13 22:33:31 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-06-22 15:30:34 +05:30
|
|
|
module Gitlab
|
|
|
|
module ImportExport
|
|
|
|
class Reader
|
2018-06-03 19:52:53 +05:30
|
|
|
attr_reader :tree, :attributes_finder
|
2016-06-22 15:30:34 +05:30
|
|
|
|
|
|
|
def initialize(shared:)
|
|
|
|
@shared = shared
|
|
|
|
config_hash = YAML.load_file(Gitlab::ImportExport.config_file).deep_symbolize_keys
|
|
|
|
@tree = config_hash[:project_tree]
|
|
|
|
@attributes_finder = Gitlab::ImportExport::AttributesFinder.new(included_attributes: config_hash[:included_attributes],
|
|
|
|
excluded_attributes: config_hash[:excluded_attributes],
|
|
|
|
methods: config_hash[:methods])
|
|
|
|
end
|
|
|
|
|
|
|
|
# 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.
|
|
|
|
def project_tree
|
2017-08-17 22:00:37 +05:30
|
|
|
attributes = @attributes_finder.find(:project)
|
|
|
|
project_attributes = attributes.is_a?(Hash) ? attributes[:project] : {}
|
|
|
|
|
|
|
|
project_attributes.merge(include: build_hash(@tree))
|
2016-06-22 15:30:34 +05:30
|
|
|
rescue => e
|
|
|
|
@shared.error(e)
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
def group_members_tree
|
|
|
|
@attributes_finder.find_included(:project_members).merge(include: @attributes_finder.find(:user))
|
|
|
|
end
|
|
|
|
|
2016-06-22 15:30:34 +05:30
|
|
|
private
|
|
|
|
|
|
|
|
# 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
|
|
|
|
def build_hash(model_list)
|
|
|
|
model_list.map do |model_objects|
|
|
|
|
if model_objects.is_a?(Hash)
|
2016-08-24 12:49:21 +05:30
|
|
|
Gitlab::ImportExport::JsonHashBuilder.build(model_objects, @attributes_finder)
|
2016-06-22 15:30:34 +05:30
|
|
|
else
|
|
|
|
@attributes_finder.find(model_objects)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|