rule.rb 1.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
# frozen_string_literal: true

module Gitlab
  module Ci
    class Config
      module Entry
        class Rules::Rule < ::Gitlab::Config::Entry::Node
          include ::Gitlab::Config::Entry::Validatable
          include ::Gitlab::Config::Entry::Attributable

11 12
          CLAUSES      = %i[if changes exists].freeze
          ALLOWED_KEYS = %i[if changes exists when start_in].freeze
13 14
          ALLOWED_WHEN = %w[on_success on_failure always never manual delayed].freeze

15
          attributes :if, :changes, :exists, :when, :start_in
16 17 18 19 20 21 22 23 24 25 26

          validations do
            validates :config, presence: true
            validates :config, type: { with: Hash }
            validates :config, allowed_keys: ALLOWED_KEYS
            validates :config, disallowed_keys: %i[start_in], unless: :specifies_delay?
            validates :start_in, presence: true, if: :specifies_delay?
            validates :start_in, duration: { limit: '1 day' }, if: :specifies_delay?

            with_options allow_nil: true do
              validates :if, expression: true
27
              validates :changes, :exists, array_of_strings: true, length: { maximum: 50 }
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
              validates :when, allowed_values: { in: ALLOWED_WHEN }
            end
          end

          def specifies_delay?
            self.when == 'delayed'
          end

          def default
          end
        end
      end
    end
  end
end