2019-05-22 07:45:27 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class AutoMergeService < BaseService
|
2020-04-21 11:21:10 -04:00
|
|
|
include Gitlab::Utils::StrongMemoize
|
|
|
|
|
2019-08-31 15:22:19 -04:00
|
|
|
STRATEGY_MERGE_WHEN_PIPELINE_SUCCEEDS = 'merge_when_pipeline_succeeds'
|
2019-05-22 07:45:27 -04:00
|
|
|
STRATEGIES = [STRATEGY_MERGE_WHEN_PIPELINE_SUCCEEDS].freeze
|
|
|
|
|
|
|
|
class << self
|
2020-04-21 11:21:10 -04:00
|
|
|
def all_strategies_ordered_by_preference
|
2019-05-22 07:45:27 -04:00
|
|
|
STRATEGIES
|
|
|
|
end
|
|
|
|
|
|
|
|
def get_service_class(strategy)
|
2020-04-21 11:21:10 -04:00
|
|
|
return unless all_strategies_ordered_by_preference.include?(strategy)
|
2019-05-22 07:45:27 -04:00
|
|
|
|
|
|
|
"::AutoMerge::#{strategy.camelize}Service".constantize
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-04-21 11:21:10 -04:00
|
|
|
def execute(merge_request, strategy = nil)
|
|
|
|
strategy ||= preferred_strategy(merge_request)
|
|
|
|
service = get_service_instance(merge_request, strategy)
|
2019-05-22 07:45:27 -04:00
|
|
|
|
|
|
|
return :failed unless service&.available_for?(merge_request)
|
|
|
|
|
|
|
|
service.execute(merge_request)
|
|
|
|
end
|
|
|
|
|
2019-06-06 03:25:06 -04:00
|
|
|
def update(merge_request)
|
|
|
|
return :failed unless merge_request.auto_merge_enabled?
|
|
|
|
|
2020-04-21 11:21:10 -04:00
|
|
|
strategy = merge_request.auto_merge_strategy
|
|
|
|
get_service_instance(merge_request, strategy).update(merge_request)
|
2019-06-06 03:25:06 -04:00
|
|
|
end
|
|
|
|
|
2019-05-22 07:45:27 -04:00
|
|
|
def process(merge_request)
|
|
|
|
return unless merge_request.auto_merge_enabled?
|
|
|
|
|
2020-04-21 11:21:10 -04:00
|
|
|
strategy = merge_request.auto_merge_strategy
|
|
|
|
get_service_instance(merge_request, strategy).process(merge_request)
|
2019-05-22 07:45:27 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def cancel(merge_request)
|
|
|
|
return error("Can't cancel the automatic merge", 406) unless merge_request.auto_merge_enabled?
|
|
|
|
|
2020-04-21 11:21:10 -04:00
|
|
|
strategy = merge_request.auto_merge_strategy
|
|
|
|
get_service_instance(merge_request, strategy).cancel(merge_request)
|
2019-05-22 07:45:27 -04:00
|
|
|
end
|
|
|
|
|
2019-06-26 08:24:09 -04:00
|
|
|
def abort(merge_request, reason)
|
|
|
|
return error("Can't abort the automatic merge", 406) unless merge_request.auto_merge_enabled?
|
|
|
|
|
2020-04-21 11:21:10 -04:00
|
|
|
strategy = merge_request.auto_merge_strategy
|
|
|
|
get_service_instance(merge_request, strategy).abort(merge_request, reason)
|
2019-06-26 08:24:09 -04:00
|
|
|
end
|
|
|
|
|
2019-05-22 07:45:27 -04:00
|
|
|
def available_strategies(merge_request)
|
2020-04-21 11:21:10 -04:00
|
|
|
self.class.all_strategies_ordered_by_preference.select do |strategy|
|
|
|
|
get_service_instance(merge_request, strategy).available_for?(merge_request)
|
2019-05-22 07:45:27 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-04-21 11:21:10 -04:00
|
|
|
def preferred_strategy(merge_request)
|
|
|
|
available_strategies(merge_request).first
|
|
|
|
end
|
|
|
|
|
2019-05-22 07:45:27 -04:00
|
|
|
private
|
|
|
|
|
2020-04-21 11:21:10 -04:00
|
|
|
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
|
2019-05-22 07:45:27 -04:00
|
|
|
end
|
|
|
|
end
|
2019-09-13 09:26:31 -04:00
|
|
|
|
2021-05-11 17:10:21 -04:00
|
|
|
AutoMergeService.prepend_mod_with('AutoMergeService')
|