Commit 0f1ab09a authored by nmilojevic1's avatar nmilojevic1

Add project export presenter

- Clean streaming serializer
- Clean TreeSaver
parent 74332275
# frozen_string_literal: true
module Projects
module ImportExport
class ProjectExportPresenter < Gitlab::View::Presenter::Delegated
presents :project
def project_members
group_members.as_json(group_members_tree).each do |group_member|
group_member['source_type'] = 'Project' # Make group members project members of the future import
end
end
def as_json(*_args)
self.respond_to?(:override_description) ? super.merge("description" => override_description) : super
end
private
# rubocop: disable CodeReuse/ActiveRecord
def group_members
return [] unless current_user.can?(:admin_group, project.group)
# We need `.where.not(user_id: nil)` here otherwise when a group has an
# invitee, it would make the following query return 0 rows since a NULL
# user_id would be present in the subquery
# See http://stackoverflow.com/questions/129077/not-in-clause-and-null-values
non_null_user_ids = project.project_members.where.not(user_id: nil).select(:user_id)
GroupMembersFinder.new(project.group).execute.where.not(user_id: non_null_user_ids)
end
# rubocop: enable CodeReuse/ActiveRecord
end
end
end
......@@ -6,9 +6,6 @@ module Gitlab
class StreamingSerializer
include Gitlab::ImportExport::CommandLineUtil
attr_reader :overrides
attr_reader :additional_relations
BATCH_SIZE = 100
class Raw < String
......@@ -20,9 +17,7 @@ module Gitlab
def initialize(exportable, relations_schema, json_writer)
@exportable = exportable
@relations_schema = relations_schema
@overrides = {}
@json_writer = json_writer
@additional_relations = {}
end
def execute
......@@ -40,10 +35,7 @@ module Gitlab
def serialize_root
attributes = exportable.as_json(
relations_schema.merge(include: nil, preloads: nil))
data = attributes.merge(overrides)
json_writer.set(data)
json_writer.set(attributes)
end
def serialize_relation(definition)
......@@ -54,7 +46,6 @@ module Gitlab
options = definition.first.second
record = exportable.public_send(key) # rubocop: disable GitlabSecurity/PublicSend
if record.is_a?(ActiveRecord::Relation)
serialize_many_relations(key, record, options)
else
......@@ -74,12 +65,6 @@ module Gitlab
json_writer.append(key, item)
end
end
additional_relations[key].to_a.each do |item|
item = Raw.new(item.to_json(options))
json_writer.append(key, item)
end
end
def serialize_single_relation(key, record, options)
......
......@@ -17,15 +17,13 @@ module Gitlab
def save
json_writer = ImportExport::JSON::LegacyWriter.new(File.join(@shared.export_path, "project.json"))
serializer = ImportExport::JSON::StreamingSerializer.new(@project, reader.project_tree, json_writer)
serializer.overrides['description'] = @params[:description] if @params[:description].present?
serializer.additional_relations['project_members'] = group_members_array
serializer = ImportExport::JSON::StreamingSerializer.new(exportable, reader.project_tree, json_writer)
serializer.execute
true
#rescue => e
# @shared.error(e)
# false
rescue => e
@shared.error(e)
false
ensure
json_writer&.close
end
......@@ -36,22 +34,22 @@ module Gitlab
@reader ||= Gitlab::ImportExport::Reader.new(shared: @shared)
end
def group_members_array
group_members.as_json(reader.group_members_tree).each do |group_member|
group_member['source_type'] = 'Project' # Make group members project members of the future import
end
def exportable
@project.present(exportable_params)
end
def group_members
return [] unless @current_user.can?(:admin_group, @project.group)
# We need `.where.not(user_id: nil)` here otherwise when a group has an
# invitee, it would make the following query return 0 rows since a NULL
# user_id would be present in the subquery
# See http://stackoverflow.com/questions/129077/not-in-clause-and-null-values
non_null_user_ids = @project.project_members.where.not(user_id: nil).select(:user_id)
def exportable_params
params = {
presenter_class: presenter_class,
current_user: @current_user,
group_members_tree: reader.group_members_tree
}
params[:override_description] = @params[:description] if @params[:description]
params
end
GroupMembersFinder.new(@project.group).execute.where.not(user_id: non_null_user_ids)
def presenter_class
Projects::ImportExport::ProjectExportPresenter
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