summaryrefslogtreecommitdiff
path: root/lib/gitlab/ci/config/entry/policy.rb
blob: 7b14218d3ea9ca22847863e979d45a108e323cbe (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
# frozen_string_literal: true

module Gitlab
  module Ci
    class Config
      module Entry
        ##
        # Entry that represents an only/except trigger policy for the job.
        #
        class Policy < ::Gitlab::Config::Entry::Simplifiable
          strategy :RefsPolicy, if: -> (config) { config.is_a?(Array) }
          strategy :ComplexPolicy, if: -> (config) { config.is_a?(Hash) }

          DEFAULT_ONLY = { refs: %w[branches tags] }.freeze

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

            validations do
              validates :config, array_of_strings_or_regexps_with_fallback: true
            end

            def value
              { refs: @config }
            end
          end

          class ComplexPolicy < ::Gitlab::Config::Entry::Node
            include ::Gitlab::Config::Entry::Validatable
            include ::Gitlab::Config::Entry::Attributable

            ALLOWED_KEYS = %i[refs kubernetes variables changes].freeze
            attributes :refs, :kubernetes, :variables, :changes

            validations do
              validates :config, presence: true
              validates :config, allowed_keys: ALLOWED_KEYS
              validate :variables_expressions_syntax

              with_options allow_nil: true do
                validates :refs, array_of_strings_or_regexps_with_fallback: true
                validates :kubernetes, allowed_values: %w[active]
                validates :variables, array_of_strings: true
                validates :changes, array_of_strings: true
              end

              def variables_expressions_syntax
                return unless variables.is_a?(Array)

                statements = variables.map do |statement|
                  ::Gitlab::Ci::Pipeline::Expression::Statement.new(statement)
                end

                statements.each do |statement|
                  unless statement.valid?
                    errors.add(:variables, "Invalid expression syntax")
                  end
                end
              end
            end
          end

          class UnknownStrategy < ::Gitlab::Config::Entry::Node
            def errors
              ["#{location} has to be either an array of conditions or a hash"]
            end
          end

          def value
            default.to_h.deep_merge(subject.value.to_h)
          end
        end
      end
    end
  end
end