summaryrefslogtreecommitdiff
path: root/app/services/bulk_imports/tree_export_service.rb
blob: 8e885e590d1b7eac98f46ad9e104fa466427b9a3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# frozen_string_literal: true

module BulkImports
  class TreeExportService
    def initialize(portable, export_path, relation)
      @portable = portable
      @export_path = export_path
      @relation = relation
      @config = FileTransfer.config_for(portable)
    end

    def execute
      return serializer.serialize_root(config.class::SELF_RELATION) if self_relation?

      relation_definition = config.tree_relation_definition_for(relation)

      raise BulkImports::Error, 'Unsupported relation export type' unless relation_definition

      serializer.serialize_relation(relation_definition)
    end

    def exported_filename
      return "#{relation}.json" if self_relation?

      "#{relation}.ndjson"
    end

    private

    attr_reader :export_path, :portable, :relation, :config

    # rubocop: disable CodeReuse/Serializer
    def serializer
      ::Gitlab::ImportExport::Json::StreamingSerializer.new(
        portable,
        config.portable_tree,
        json_writer,
        exportable_path: ''
      )
    end
    # rubocop: enable CodeReuse/Serializer

    def json_writer
      ::Gitlab::ImportExport::Json::NdjsonWriter.new(export_path)
    end

    def self_relation?
      relation == config.class::SELF_RELATION
    end
  end
end