2016-07-18 06:37:42 -04:00
|
|
|
module Gitlab
|
|
|
|
module Ci
|
|
|
|
class Config
|
2016-11-14 04:31:45 -05:00
|
|
|
module Entry
|
2016-07-18 06:37:42 -04:00
|
|
|
##
|
2017-08-25 04:27:00 -04:00
|
|
|
# Entry that represents an only/except trigger policy for the job.
|
2016-07-18 06:37:42 -04:00
|
|
|
#
|
2017-08-25 03:49:18 -04:00
|
|
|
class Policy < Simplifiable
|
|
|
|
strategy :RefsPolicy, if: -> (config) { config.is_a?(Array) }
|
2017-08-26 06:45:36 -04:00
|
|
|
strategy :ComplexPolicy, if: -> (config) { config.is_a?(Hash) }
|
2016-07-18 06:37:42 -04:00
|
|
|
|
2017-08-25 03:49:18 -04:00
|
|
|
class RefsPolicy < Entry::Node
|
|
|
|
include Entry::Validatable
|
|
|
|
|
|
|
|
validations do
|
|
|
|
validates :config, array_of_strings_or_regexps: true
|
|
|
|
end
|
2017-08-26 05:11:28 -04:00
|
|
|
|
|
|
|
def value
|
|
|
|
{ refs: @config }
|
|
|
|
end
|
2017-08-25 03:49:18 -04:00
|
|
|
end
|
|
|
|
|
2017-08-26 06:45:36 -04:00
|
|
|
class ComplexPolicy < Entry::Node
|
2017-08-25 03:49:18 -04:00
|
|
|
include Entry::Validatable
|
2017-08-25 04:27:00 -04:00
|
|
|
include Entry::Attributable
|
|
|
|
|
2018-10-02 09:56:56 -04:00
|
|
|
ALLOWED_KEYS = %i[refs kubernetes variables changes].freeze
|
2018-10-02 08:03:20 -04:00
|
|
|
attributes :refs, :kubernetes, :variables, :changes
|
2017-08-25 03:49:18 -04:00
|
|
|
|
|
|
|
validations do
|
2017-08-25 04:27:00 -04:00
|
|
|
validates :config, presence: true
|
2018-10-02 08:03:20 -04:00
|
|
|
validates :config, allowed_keys: ALLOWED_KEYS
|
2018-02-28 04:50:02 -05:00
|
|
|
validate :variables_expressions_syntax
|
2017-08-25 04:27:00 -04:00
|
|
|
|
|
|
|
with_options allow_nil: true do
|
|
|
|
validates :refs, array_of_strings_or_regexps: true
|
2017-09-01 07:03:43 -04:00
|
|
|
validates :kubernetes, allowed_values: %w[active]
|
2018-02-28 04:50:02 -05:00
|
|
|
validates :variables, array_of_strings: true
|
2018-10-02 08:03:20 -04:00
|
|
|
validates :changes, array_of_strings: true
|
2018-02-28 04:50:02 -05:00
|
|
|
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?
|
2018-03-27 08:46:58 -04:00
|
|
|
errors.add(:variables, "Invalid expression syntax")
|
2018-02-28 04:50:02 -05:00
|
|
|
end
|
|
|
|
end
|
2017-08-25 04:27:00 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class UnknownStrategy < Entry::Node
|
|
|
|
def errors
|
2017-08-25 09:16:09 -04:00
|
|
|
["#{location} has to be either an array of conditions or a hash"]
|
2017-08-25 03:49:18 -04:00
|
|
|
end
|
2016-07-18 06:37:42 -04:00
|
|
|
end
|
2017-08-25 05:42:40 -04:00
|
|
|
|
|
|
|
def self.default
|
|
|
|
end
|
2016-07-18 06:37:42 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|