Welcome to mirror list, hosted at ThFree Co, Russian Federation.

rules.rb « external « config « ci « gitlab « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 0e6209460e0c28f098ba652199a50659e6e2de1d (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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# frozen_string_literal: true

module Gitlab
  module Ci
    class Config
      module External
        class Rules
          # Remove these two constants when FF `ci_refactor_external_rules` is removed
          ALLOWED_KEYS = Entry::Include::Rules::Rule::ALLOWED_KEYS
          ALLOWED_WHEN = Entry::Include::Rules::Rule::ALLOWED_WHEN

          InvalidIncludeRulesError = Class.new(Mapper::Error)

          def initialize(rule_hashes)
            if Feature.enabled?(:ci_refactor_external_rules)
              return unless rule_hashes

              # We must compose the include rules entry here because included
              # files are expanded before `@root.compose!` runs in Ci::Config.
              rules_entry = Entry::Include::Rules.new(rule_hashes)
              rules_entry.compose!

              raise InvalidIncludeRulesError, "include:#{rules_entry.errors.first}" unless rules_entry.valid?

              @rule_list = Build::Rules::Rule.fabricate_list(rules_entry.value)
            else
              validate(rule_hashes)

              @rule_list = Build::Rules::Rule.fabricate_list(rule_hashes)
            end
          end

          def evaluate(context)
            if @rule_list.nil?
              Result.new('always')
            elsif matched_rule = match_rule(context)
              Result.new(matched_rule.attributes[:when])
            else
              Result.new('never')
            end
          end

          private

          def match_rule(context)
            @rule_list.find { |rule| rule.matches?(nil, context) }
          end

          # Remove this method when FF `ci_refactor_external_rules` is removed
          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? && valid_when?(rule_hash)

              raise InvalidIncludeRulesError, "invalid include rule: #{rule_hash}"
            end
          end

          # Remove this method when FF `ci_refactor_external_rules` is removed
          def valid_when?(rule_hash)
            rule_hash[:when].nil? || rule_hash[:when].in?(ALLOWED_WHEN)
          end

          Result = Struct.new(:when) do
            def pass?
              self.when != 'never'
            end
          end
        end
      end
    end
  end
end