gitlab-org--gitlab-foss/lib/gitlab/import_export/reader.rb

48 lines
1.8 KiB
Ruby
Raw Normal View History

module Gitlab
module ImportExport
2016-06-13 19:18:26 +00:00
class Reader
attr_reader :tree
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],
2016-05-18 15:48:15 +00:00
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
attributes = @attributes_finder.find(:project)
project_attributes = attributes.is_a?(Hash) ? attributes[:project] : {}
project_attributes.merge(include: build_hash(@tree))
rescue => e
@shared.error(e)
2016-05-16 08:13:00 +00:00
false
end
def group_members_tree
@attributes_finder.find_included(:project_members).merge(include: @attributes_finder.find(:user))
end
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)
2016-05-09 16:40:31 +00:00
model_list.map do |model_objects|
if model_objects.is_a?(Hash)
Gitlab::ImportExport::JsonHashBuilder.build(model_objects, @attributes_finder)
2016-04-07 16:14:42 +00:00
else
@attributes_finder.find(model_objects)
2016-04-07 16:14:42 +00:00
end
end
end
end
end
2016-04-08 13:07:12 +00:00
end