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

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

module Gitlab
  module Ci
    module Interpolation
      class Template
        include Gitlab::Utils::StrongMemoize

        attr_reader :blocks, :ctx

        TooManyBlocksError = Class.new(StandardError)
        InvalidBlockError = Class.new(StandardError)

        MAX_BLOCKS = 10_000

        def initialize(config, ctx)
          @config = Interpolation::Config.fabricate(config)
          @ctx = Interpolation::Context.fabricate(ctx)
          @errors = []
          @blocks = {}

          interpolate! if valid?
        end

        def valid?
          errors.none?
        end

        def errors
          @errors + @config.errors + @ctx.errors + @blocks.values.flat_map(&:errors)
        end

        def size
          @blocks.size
        end

        def interpolated
          @result if valid?
        end

        private

        def interpolate!
          @result = @config.replace! do |data|
            Interpolation::Block.match(data) do |block, data|
              evaluate_block(block, data)
            end
          end
        rescue TooManyBlocksError
          @errors.push('too many interpolation blocks')
        rescue InvalidBlockError
          @errors.push('interpolation interrupted by errors')
        end
        strong_memoize_attr :interpolate!

        def evaluate_block(block, data)
          block = (@blocks[block] ||= Interpolation::Block.new(block, data, ctx))

          raise TooManyBlocksError if @blocks.count > MAX_BLOCKS
          raise InvalidBlockError unless block.valid?

          block.value
        end
      end
    end
  end
end