summaryrefslogtreecommitdiff
path: root/lib/gitlab/ci/build/rules.rb
blob: 89623a809c9a92320ad36238e8590a079beb5d7c (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
# frozen_string_literal: true

module Gitlab
  module Ci
    module Build
      class Rules
        include ::Gitlab::Utils::StrongMemoize

        Result = Struct.new(:when, :start_in)

        def initialize(rule_hashes, default_when = 'on_success')
          @rule_list    = Rule.fabricate_list(rule_hashes)
          @default_when = default_when
        end

        def evaluate(pipeline, build)
          if @rule_list.nil?
            Result.new(@default_when)
          elsif matched_rule = match_rule(pipeline, build)
            Result.new(
              matched_rule.attributes[:when] || @default_when,
              matched_rule.attributes[:start_in]
            )
          else
            Result.new('never')
          end
        end

        private

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