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

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

module Gitlab
  module Ci
    class Config
      module Interpolation
        ##
        # Performs CI config file interpolation and either returns the interpolated result or interpolation errors.
        #
        class TextInterpolator
          attr_reader :errors

          def initialize(config, input_args, variables)
            @config = config
            @input_args = input_args.to_h
            @variables = variables
            @errors = []
            @interpolated = false
          end

          def valid?
            errors.none?
          end

          def to_result
            @result
          end

          def error_message
            # Interpolator can have multiple error messages, like: ["interpolation interrupted by errors", "unknown
            # interpolation key: `abc`"] ?
            #
            # We are joining them together into a single one, because only one error can be surfaced when an external
            # file gets included and is invalid. The limit to three error messages combined is more than required.
            #
            errors.first(3).join(', ')
          end

          def interpolate!
            return errors.concat(config.errors) unless config.valid?

            if inputs_without_header?
              return errors.push(
                _('Given inputs not defined in the `spec` section of the included configuration file'))
            end

            return @result ||= config.content unless config.header

            return errors.concat(header.errors) unless header.valid?
            return errors.concat(inputs.errors) unless inputs.valid?
            return errors.concat(context.errors) unless context.valid?
            return errors.concat(template.errors) unless template.valid?

            @interpolated = true

            @result ||= template.interpolated
          end

          def interpolated?
            @interpolated
          end

          private

          attr_reader :config, :input_args, :variables

          def inputs_without_header?
            input_args.any? && !config.header
          end

          def header
            @header ||= Header::Root.new(config.header).tap do |header|
              header.key = 'header'

              header.compose!
            end
          end

          def content
            @content ||= config.content
          end

          def spec
            @spec ||= header.inputs_value
          end

          def inputs
            @inputs ||= Inputs.new(spec, input_args)
          end

          def context
            @context ||= Context.new({ inputs: inputs.to_hash }, variables: variables)
          end

          def template
            @template ||= TextTemplate.new(content, context)
          end
        end
      end
    end
  end
end