summaryrefslogtreecommitdiff
path: root/lib/gitlab/ci/config/entry/policy.rb
blob: 41a3737b34aa28c43c62f074b80c45798768ab75 (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
module Gitlab
  module Ci
    class Config
      module Entry
        ##
        # Entry that represents an only/except trigger policy for the job.
        #
        class Policy < Simplifiable
          strategy :RefsPolicy, if: -> (config) { config.is_a?(Array) }
          strategy :ExpressionsPolicy, if: -> (config) { config.is_a?(Hash) }

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

            validations do
              validates :config, array_of_strings_or_regexps: true
            end

            def value
              { refs: @config }
            end
          end

          class ExpressionsPolicy < Entry::Node
            include Entry::Validatable
            include Entry::Attributable

            attributes :refs, :expressions

            validations do
              validates :config, presence: true
              validates :config, allowed_keys: %i[refs expressions]

              with_options allow_nil: true do
                validates :refs, array_of_strings_or_regexps: true
                validates :expressions, type: Array
                validates :expressions, presence: true
              end
            end
          end

          class UnknownStrategy < Entry::Node
            def errors
              ['policy has to be either an array of conditions or a hash']
            end
          end

          def self.default
          end
        end
      end
    end
  end
end