2019-08-20 16:03:43 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Gitlab
|
|
|
|
module Ci
|
|
|
|
module Build
|
|
|
|
class Rules
|
|
|
|
include ::Gitlab::Utils::StrongMemoize
|
|
|
|
|
2020-02-12 01:09:05 -05:00
|
|
|
Result = Struct.new(:when, :start_in, :allow_failure) do
|
2019-09-09 09:21:26 -04:00
|
|
|
def build_attributes
|
|
|
|
{
|
|
|
|
when: self.when,
|
2020-02-12 01:09:05 -05:00
|
|
|
options: { start_in: start_in }.compact,
|
|
|
|
allow_failure: allow_failure
|
2019-09-09 09:21:26 -04:00
|
|
|
}.compact
|
|
|
|
end
|
2019-11-17 16:06:14 -05:00
|
|
|
|
|
|
|
def pass?
|
|
|
|
self.when != 'never'
|
|
|
|
end
|
2019-09-09 09:21:26 -04:00
|
|
|
end
|
2019-08-20 16:03:43 -04:00
|
|
|
|
2019-11-17 16:06:14 -05:00
|
|
|
def initialize(rule_hashes, default_when:)
|
2019-08-20 16:03:43 -04:00
|
|
|
@rule_list = Rule.fabricate_list(rule_hashes)
|
|
|
|
@default_when = default_when
|
|
|
|
end
|
|
|
|
|
2019-11-17 16:06:14 -05:00
|
|
|
def evaluate(pipeline, context)
|
2019-08-20 16:03:43 -04:00
|
|
|
if @rule_list.nil?
|
|
|
|
Result.new(@default_when)
|
2019-11-17 16:06:14 -05:00
|
|
|
elsif matched_rule = match_rule(pipeline, context)
|
2019-08-20 16:03:43 -04:00
|
|
|
Result.new(
|
|
|
|
matched_rule.attributes[:when] || @default_when,
|
2020-02-12 01:09:05 -05:00
|
|
|
matched_rule.attributes[:start_in],
|
|
|
|
matched_rule.attributes[:allow_failure]
|
2019-08-20 16:03:43 -04:00
|
|
|
)
|
|
|
|
else
|
|
|
|
Result.new('never')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2019-11-17 16:06:14 -05:00
|
|
|
def match_rule(pipeline, context)
|
|
|
|
@rule_list.find { |rule| rule.matches?(pipeline, context) }
|
2019-08-20 16:03:43 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|