diff options
Diffstat (limited to 'app/services/auto_merge_service.rb')
-rw-r--r-- | app/services/auto_merge_service.rb | 37 |
1 files changed, 25 insertions, 12 deletions
diff --git a/app/services/auto_merge_service.rb b/app/services/auto_merge_service.rb index eee227be202..c5cbcc7c93b 100644 --- a/app/services/auto_merge_service.rb +++ b/app/services/auto_merge_service.rb @@ -1,23 +1,26 @@ # frozen_string_literal: true class AutoMergeService < BaseService + include Gitlab::Utils::StrongMemoize + STRATEGY_MERGE_WHEN_PIPELINE_SUCCEEDS = 'merge_when_pipeline_succeeds' STRATEGIES = [STRATEGY_MERGE_WHEN_PIPELINE_SUCCEEDS].freeze class << self - def all_strategies + def all_strategies_ordered_by_preference STRATEGIES end def get_service_class(strategy) - return unless all_strategies.include?(strategy) + return unless all_strategies_ordered_by_preference.include?(strategy) "::AutoMerge::#{strategy.camelize}Service".constantize end end - def execute(merge_request, strategy) - service = get_service_instance(strategy) + def execute(merge_request, strategy = nil) + strategy ||= preferred_strategy(merge_request) + service = get_service_instance(merge_request, strategy) return :failed unless service&.available_for?(merge_request) @@ -27,37 +30,47 @@ class AutoMergeService < BaseService def update(merge_request) return :failed unless merge_request.auto_merge_enabled? - get_service_instance(merge_request.auto_merge_strategy).update(merge_request) + strategy = merge_request.auto_merge_strategy + get_service_instance(merge_request, strategy).update(merge_request) end def process(merge_request) return unless merge_request.auto_merge_enabled? - get_service_instance(merge_request.auto_merge_strategy).process(merge_request) + strategy = merge_request.auto_merge_strategy + get_service_instance(merge_request, strategy).process(merge_request) end def cancel(merge_request) return error("Can't cancel the automatic merge", 406) unless merge_request.auto_merge_enabled? - get_service_instance(merge_request.auto_merge_strategy).cancel(merge_request) + strategy = merge_request.auto_merge_strategy + get_service_instance(merge_request, strategy).cancel(merge_request) end def abort(merge_request, reason) return error("Can't abort the automatic merge", 406) unless merge_request.auto_merge_enabled? - get_service_instance(merge_request.auto_merge_strategy).abort(merge_request, reason) + strategy = merge_request.auto_merge_strategy + get_service_instance(merge_request, strategy).abort(merge_request, reason) end def available_strategies(merge_request) - self.class.all_strategies.select do |strategy| - get_service_instance(strategy).available_for?(merge_request) + self.class.all_strategies_ordered_by_preference.select do |strategy| + get_service_instance(merge_request, strategy).available_for?(merge_request) end end + def preferred_strategy(merge_request) + available_strategies(merge_request).first + end + private - def get_service_instance(strategy) - self.class.get_service_class(strategy)&.new(project, current_user, params) + def get_service_instance(merge_request, strategy) + strong_memoize("service_instance_#{merge_request.id}_#{strategy}") do + self.class.get_service_class(strategy)&.new(project, current_user, params) + end end end |