blob: 95470537de3b951772ab208be7e383463f92c674 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
# frozen_string_literal: true
module Gitlab
module Ci
class Config
module External
class Rules
ALLOWED_KEYS = Entry::Include::Rules::Rule::ALLOWED_KEYS
InvalidIncludeRulesError = Class.new(Mapper::Error)
def initialize(rule_hashes)
validate(rule_hashes)
@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
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
Result = Struct.new(:result) do
def pass?
!!result
end
end
end
end
end
end
end
|