debian-mirror-gitlab/lib/gitlab/ci/config/external/rules.rb

48 lines
1.1 KiB
Ruby
Raw Normal View History

2021-10-27 15:23:28 +05:30
# frozen_string_literal: true
module Gitlab
module Ci
class Config
module External
class Rules
2021-11-18 22:05:49 +05:30
ALLOWED_KEYS = Entry::Include::Rules::Rule::ALLOWED_KEYS
InvalidIncludeRulesError = Class.new(Mapper::Error)
2021-10-27 15:23:28 +05:30
def initialize(rule_hashes)
2021-11-18 22:05:49 +05:30
validate(rule_hashes)
2021-10-27 15:23:28 +05:30
@rule_list = Build::Rules::Rule.fabricate_list(rule_hashes)
end
def evaluate(context)
Result.new(@rule_list.nil? || match_rule(context))
end
private
def match_rule(context)
@rule_list.find { |rule| rule.matches?(nil, context) }
end
2021-11-18 22:05:49 +05:30
def validate(rule_hashes)
return unless rule_hashes.is_a?(Array)
rule_hashes.each do |rule_hash|
next if (rule_hash.keys - ALLOWED_KEYS).empty?
raise InvalidIncludeRulesError, "invalid include rule: #{rule_hash}"
end
end
2021-10-27 15:23:28 +05:30
Result = Struct.new(:result) do
def pass?
!!result
end
end
end
end
end
end
end