summaryrefslogtreecommitdiff
path: root/lib/gitlab/import_export/json/ndjson_writer.rb
blob: e303ac6eefa7bae6a7a409129a4d3a3214bd2e48 (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
52
53
54
55
56
57
58
59
# frozen_string_literal: true

module Gitlab
  module ImportExport
    module Json
      class NdjsonWriter
        include Gitlab::ImportExport::CommandLineUtil

        def initialize(dir_path)
          @dir_path = dir_path
        end

        def close
        end

        def write_attributes(exportable_path, hash)
          # It will create:
          # tree/project.json
          with_file("#{exportable_path}.json") do |file|
            file.write(hash.to_json)
          end
        end

        def write_relation(exportable_path, relation, value)
          # It will create:
          # tree/project/ci_cd_setting.ndjson
          with_file(exportable_path, "#{relation}.ndjson") do |file|
            file.write(value.to_json)
          end
        end

        def write_relation_array(exportable_path, relation, items)
          # It will create:
          # tree/project/merge_requests.ndjson
          with_file(exportable_path, "#{relation}.ndjson") do |file|
            items.each do |item|
              file.write(item.to_json)
              file.write("\n")
            end
          end
        end

        private

        def with_file(*path)
          file_path = File.join(@dir_path, *path)
          raise ArgumentError, "The #{file_path} already exist" if File.exist?(file_path)

          # ensure that path is created
          mkdir_p(File.dirname(file_path))

          File.open(file_path, "wb") do |file|
            yield(file)
          end
        end
      end
    end
  end
end