2019-12-04 20:38:33 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Gitlab
|
|
|
|
module Ci
|
|
|
|
module Build
|
|
|
|
class Rules
|
|
|
|
include ::Gitlab::Utils::StrongMemoize
|
|
|
|
|
2023-07-09 08:55:56 +05:30
|
|
|
Result = Struct.new(:when, :start_in, :allow_failure, :variables, :needs, :errors) do
|
2021-03-11 19:13:27 +05:30
|
|
|
def build_attributes
|
2019-12-04 20:38:33 +05:30
|
|
|
{
|
|
|
|
when: self.when,
|
2020-03-13 15:44:24 +05:30
|
|
|
options: { start_in: start_in }.compact,
|
2023-07-09 08:55:56 +05:30
|
|
|
allow_failure: allow_failure,
|
|
|
|
scheduling_type: (:dag if needs),
|
|
|
|
needs_attributes: needs&.[](:job)
|
2019-12-04 20:38:33 +05:30
|
|
|
}.compact
|
|
|
|
end
|
2019-12-26 22:10:19 +05:30
|
|
|
|
|
|
|
def pass?
|
|
|
|
self.when != 'never'
|
|
|
|
end
|
2019-12-04 20:38:33 +05:30
|
|
|
end
|
|
|
|
|
2019-12-26 22:10:19 +05:30
|
|
|
def initialize(rule_hashes, default_when:)
|
2019-12-04 20:38:33 +05:30
|
|
|
@rule_list = Rule.fabricate_list(rule_hashes)
|
|
|
|
@default_when = default_when
|
|
|
|
end
|
|
|
|
|
2019-12-26 22:10:19 +05:30
|
|
|
def evaluate(pipeline, context)
|
2019-12-04 20:38:33 +05:30
|
|
|
if @rule_list.nil?
|
|
|
|
Result.new(@default_when)
|
2019-12-26 22:10:19 +05:30
|
|
|
elsif matched_rule = match_rule(pipeline, context)
|
2019-12-04 20:38:33 +05:30
|
|
|
Result.new(
|
|
|
|
matched_rule.attributes[:when] || @default_when,
|
2020-03-13 15:44:24 +05:30
|
|
|
matched_rule.attributes[:start_in],
|
2021-02-22 17:27:13 +05:30
|
|
|
matched_rule.attributes[:allow_failure],
|
2023-07-09 08:55:56 +05:30
|
|
|
matched_rule.attributes[:variables],
|
|
|
|
(matched_rule.attributes[:needs] if Feature.enabled?(:introduce_rules_with_needs, pipeline.project))
|
2019-12-04 20:38:33 +05:30
|
|
|
)
|
|
|
|
else
|
|
|
|
Result.new('never')
|
|
|
|
end
|
2022-08-27 11:52:29 +05:30
|
|
|
rescue Rule::Clause::ParseError => e
|
2023-07-09 08:55:56 +05:30
|
|
|
Result.new('never', nil, nil, nil, nil, [e.message])
|
2019-12-04 20:38:33 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2019-12-26 22:10:19 +05:30
|
|
|
def match_rule(pipeline, context)
|
|
|
|
@rule_list.find { |rule| rule.matches?(pipeline, context) }
|
2019-12-04 20:38:33 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|