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