2018-10-26 00:12:43 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
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
|
|
|
##
|
2018-12-13 05:39:55 -05:00
|
|
|
# Entry that represents an only/except trigger policy for the job.
|
2016-07-18 06:37:42 -04:00
|
|
|
#
|
2018-11-29 06:44:48 -05:00
|
|
|
class Policy < ::Gitlab::Config::Entry::Simplifiable
|
2018-12-13 05:39:55 -05:00
|
|
|
strategy :RefsPolicy, if: -> (config) { config.is_a?(Array) }
|
|
|
|
strategy :ComplexPolicy, if: -> (config) { config.is_a?(Hash) }
|
|
|
|
|
2018-11-29 06:44:48 -05:00
|
|
|
class RefsPolicy < ::Gitlab::Config::Entry::Node
|
|
|
|
include ::Gitlab::Config::Entry::Validatable
|
2017-08-25 03:49:18 -04:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2018-11-29 06:44:48 -05:00
|
|
|
class ComplexPolicy < ::Gitlab::Config::Entry::Node
|
|
|
|
include ::Gitlab::Config::Entry::Validatable
|
|
|
|
include ::Gitlab::Config::Entry::Attributable
|
2017-08-25 04:27:00 -04:00
|
|
|
|
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
|
|
|
|
|
2018-11-29 06:44:48 -05:00
|
|
|
class UnknownStrategy < ::Gitlab::Config::Entry::Node
|
2017-08-25 04:27:00 -04:00
|
|
|
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
|
|
|
|
2019-01-14 08:22:13 -05:00
|
|
|
def value
|
2019-01-14 08:41:12 -05:00
|
|
|
self.class.default(key: @subject.key).yield_self do |default|
|
2019-01-14 08:22:13 -05:00
|
|
|
default.to_h.deep_merge(@subject.value.to_h)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.default(**attributes)
|
|
|
|
{ refs: %w(branches tags) } if attributes[:key] == :only
|
2017-08-25 05:42:40 -04:00
|
|
|
end
|
2016-07-18 06:37:42 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|