Welcome to mirror list, hosted at ThFree Co, Russian Federation.

legacy_reader.rb « json « import_export « gitlab « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 477e41ae3eb67a0a5bb0c23de88411853526f396 (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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# frozen_string_literal: true

module Gitlab
  module ImportExport
    module JSON
      class LegacyReader
        class File < LegacyReader
          def initialize(path, relation_names)
            @path = path
            super(relation_names)
          end

          def valid?
            ::File.exist?(@path)
          end

          private

          def tree_hash
            @tree_hash ||= read_hash
          end

          def read_hash
            ActiveSupport::JSON.decode(IO.read(@path))
          rescue => e
            Gitlab::ErrorTracking.log_exception(e)
            raise Gitlab::ImportExport::Error.new('Incorrect JSON format')
          end
        end

        class User < LegacyReader
          def initialize(tree_hash, relation_names)
            @tree_hash = tree_hash
            super(relation_names)
          end

          def valid?
            @tree_hash.present?
          end

          protected

          attr_reader :tree_hash
        end

        def initialize(relation_names)
          @relation_names = relation_names.map(&:to_s)
        end

        def valid?
          raise NotImplementedError
        end

        def legacy?
          true
        end

        def root_attributes(excluded_attributes = [])
          attributes.except(*excluded_attributes.map(&:to_s))
        end

        def consume_relation(key)
          value = relations.delete(key)

          return value unless block_given?

          return if value.nil?

          if value.is_a?(Array)
            value.each.with_index do |item, idx|
              yield(item, idx)
            end
          else
            yield(value, 0)
          end
        end

        def consume_attribute(key)
          attributes.delete(key)
        end

        def sort_ci_pipelines_by_id
          relations['ci_pipelines']&.sort_by! { |hash| hash['id'] }
        end

        private

        attr_reader :relation_names

        def tree_hash
          raise NotImplementedError
        end

        def attributes
          @attributes ||= tree_hash.slice!(*relation_names)
        end

        def relations
          @relations ||= tree_hash.extract!(*relation_names)
        end
      end
    end
  end
end