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

access.rb « interpolation « ci « gitlab « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: f9bbd3e118d2a0c3740a096056b9b8927a35245e (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
# frozen_string_literal: true

module Gitlab
  module Ci
    module Interpolation
      class Access
        attr_reader :content, :errors

        MAX_ACCESS_OBJECTS = 5
        MAX_ACCESS_BYTESIZE = 1024

        def initialize(access, ctx)
          @content = access
          @ctx = ctx
          @errors = []

          if objects.count <= 1 # rubocop:disable Style/IfUnlessModifier
            @errors.push('invalid interpolation access pattern')
          end

          if access.bytesize > MAX_ACCESS_BYTESIZE # rubocop:disable Style/IfUnlessModifier
            @errors.push('maximum interpolation expression size exceeded')
          end

          evaluate! if valid?
        end

        def valid?
          errors.none?
        end

        def objects
          @objects ||= @content.split('.', MAX_ACCESS_OBJECTS)
        end

        def value
          raise ArgumentError, 'access path invalid' unless valid?

          @value
        end

        private

        def evaluate!
          raise ArgumentError, 'access path invalid' unless valid?

          @value ||= objects.inject(@ctx) do |memo, value|
            key = value.to_sym

            break @errors.push("unknown interpolation key: `#{key}`") unless memo.key?(key)

            memo.fetch(key)
          end
        rescue KeyError => e
          @errors.push(e)
        end
      end
    end
  end
end