2018-12-13 13:39:08 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
module Gitlab
|
|
|
|
module Ci
|
|
|
|
class Config
|
|
|
|
module Entry
|
|
|
|
##
|
2019-01-03 12:48:30 +05:30
|
|
|
# Entry that represents an only/except trigger policy for the job.
|
2018-03-17 18:26:18 +05:30
|
|
|
#
|
2019-02-15 15:39:39 +05:30
|
|
|
class Policy < ::Gitlab::Config::Entry::Simplifiable
|
2018-03-17 18:26:18 +05:30
|
|
|
strategy :RefsPolicy, if: -> (config) { config.is_a?(Array) }
|
|
|
|
strategy :ComplexPolicy, if: -> (config) { config.is_a?(Hash) }
|
|
|
|
|
2019-03-02 22:35:43 +05:30
|
|
|
DEFAULT_ONLY = { refs: %w[branches tags] }.freeze
|
|
|
|
|
2019-02-15 15:39:39 +05:30
|
|
|
class RefsPolicy < ::Gitlab::Config::Entry::Node
|
|
|
|
include ::Gitlab::Config::Entry::Validatable
|
2018-03-17 18:26:18 +05:30
|
|
|
|
|
|
|
validations do
|
2022-05-07 20:08:51 +05:30
|
|
|
validates :config, array_of_strings_or_regexps: true
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def value
|
|
|
|
{ refs: @config }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-02-15 15:39:39 +05:30
|
|
|
class ComplexPolicy < ::Gitlab::Config::Entry::Node
|
|
|
|
include ::Gitlab::Config::Entry::Validatable
|
|
|
|
include ::Gitlab::Config::Entry::Attributable
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2018-12-05 23:21:45 +05:30
|
|
|
ALLOWED_KEYS = %i[refs kubernetes variables changes].freeze
|
|
|
|
attributes :refs, :kubernetes, :variables, :changes
|
2018-03-17 18:26:18 +05:30
|
|
|
|
|
|
|
validations do
|
|
|
|
validates :config, presence: true
|
2018-12-05 23:21:45 +05:30
|
|
|
validates :config, allowed_keys: ALLOWED_KEYS
|
2018-05-09 12:01:36 +05:30
|
|
|
validate :variables_expressions_syntax
|
2018-03-17 18:26:18 +05:30
|
|
|
|
|
|
|
with_options allow_nil: true do
|
2022-05-07 20:08:51 +05:30
|
|
|
validates :refs, array_of_strings_or_regexps: true
|
2018-03-17 18:26:18 +05:30
|
|
|
validates :kubernetes, allowed_values: %w[active]
|
2018-05-09 12:01:36 +05:30
|
|
|
validates :variables, array_of_strings: true
|
2018-12-05 23:21:45 +05:30
|
|
|
validates :changes, array_of_strings: true
|
2018-05-09 12:01:36 +05:30
|
|
|
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
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-02-15 15:39:39 +05:30
|
|
|
class UnknownStrategy < ::Gitlab::Config::Entry::Node
|
2018-03-17 18:26:18 +05:30
|
|
|
def errors
|
|
|
|
["#{location} has to be either an array of conditions or a hash"]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-03-02 22:35:43 +05:30
|
|
|
def value
|
|
|
|
default.to_h.deep_merge(subject.value.to_h)
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|