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

variable.rb « entry « config « ci « gitlab « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 16091758916e26b121d10847cec69c6b9a17646f (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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# frozen_string_literal: true

module Gitlab
  module Ci
    class Config
      module Entry
        ##
        # Entry that represents a CI/CD variable.
        #
        class Variable < ::Gitlab::Config::Entry::Simplifiable
          strategy :SimpleVariable, if: -> (config) { SimpleVariable.applies_to?(config) }
          strategy :ComplexVariable, if: -> (config) { ComplexVariable.applies_to?(config) }
          strategy :ComplexArrayVariable, if: -> (config) { ComplexArrayVariable.applies_to?(config) }

          class SimpleVariable < ::Gitlab::Config::Entry::Node
            include ::Gitlab::Config::Entry::Validatable

            class << self
              def applies_to?(config)
                Gitlab::Config::Entry::Validators::AlphanumericValidator.validate(config)
              end
            end

            validations do
              validates :key, alphanumeric: true
              validates :config, alphanumeric: true
            end

            def value
              @config.to_s
            end

            def value_with_data
              { value: @config.to_s }
            end

            def value_with_prefill_data
              value_with_data
            end
          end

          class ComplexVariable < ::Gitlab::Config::Entry::Node
            include ::Gitlab::Config::Entry::Validatable

            class << self
              def applies_to?(config)
                config.is_a?(Hash) && !config[:value].is_a?(Array)
              end
            end

            validations do
              validates :key, alphanumeric: true
              validates :config_value, alphanumeric: true, allow_nil: false, if: :config_value_defined?
              validates :config_description, alphanumeric: true, allow_nil: false, if: :config_description_defined?
              validates :config_expand, boolean: true,
                                        allow_nil: false,
                                        if: -> { ci_raw_variables_in_yaml_config_enabled? && config_expand_defined? }

              validate do
                allowed_value_data = Array(opt(:allowed_value_data))

                if allowed_value_data.any?
                  extra_keys = config.keys - allowed_value_data

                  errors.add(:config, "uses invalid data keys: #{extra_keys.join(', ')}") if extra_keys.present?
                else
                  errors.add(:config, "must be a string")
                end
              end
            end

            def value
              config_value.to_s
            end

            def value_with_data
              if ci_raw_variables_in_yaml_config_enabled?
                {
                  value: value,
                  raw: (!config_expand if config_expand_defined?)
                }.compact
              else
                {
                  value: value
                }.compact
              end
            end

            def value_with_prefill_data
              value_with_data.merge(
                description: config_description
              ).compact
            end

            def config_value
              @config[:value]
            end

            def config_description
              @config[:description]
            end

            def config_expand
              @config[:expand]
            end

            def config_value_defined?
              config.key?(:value)
            end

            def config_description_defined?
              config.key?(:description)
            end

            def config_expand_defined?
              config.key?(:expand)
            end

            def ci_raw_variables_in_yaml_config_enabled?
              YamlProcessor::FeatureFlags.enabled?(:ci_raw_variables_in_yaml_config)
            end
          end

          class ComplexArrayVariable < ComplexVariable
            include ::Gitlab::Config::Entry::Validatable

            class << self
              def applies_to?(config)
                config.is_a?(Hash) && config[:value].is_a?(Array)
              end
            end

            validations do
              validates :config_value, array_of_strings: true, allow_nil: false, if: :config_value_defined?

              validate do
                next if opt(:allow_array_value)

                errors.add(:config, 'value must be an alphanumeric string')
              end
            end

            def value
              config_value.first
            end

            def value_with_prefill_data
              super.merge(
                value_options: config_value
              ).compact
            end
          end

          class UnknownStrategy < ::Gitlab::Config::Entry::Node
            def errors
              ["variable definition must be either a string or a hash"]
            end
          end
        end
      end
    end
  end
end