2019-06-04 03:11:03 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module AutoMerge
|
|
|
|
class BaseService < ::BaseService
|
|
|
|
include Gitlab::Utils::StrongMemoize
|
2019-09-25 12:25:40 -04:00
|
|
|
include MergeRequests::AssignsMergeParams
|
2019-06-04 03:11:03 -04:00
|
|
|
|
|
|
|
def execute(merge_request)
|
2021-12-20 07:10:29 -05:00
|
|
|
ApplicationRecord.transaction do
|
2020-06-02 05:08:01 -04:00
|
|
|
register_auto_merge_parameters!(merge_request)
|
|
|
|
yield if block_given?
|
|
|
|
end
|
2019-06-04 03:11:03 -04:00
|
|
|
|
2020-07-16 20:09:37 -04:00
|
|
|
notify(merge_request)
|
2019-06-10 07:03:32 -04:00
|
|
|
AutoMergeProcessWorker.perform_async(merge_request.id)
|
|
|
|
|
2019-06-04 03:11:03 -04:00
|
|
|
strategy.to_sym
|
2021-04-26 08:09:44 -04:00
|
|
|
rescue StandardError => e
|
2020-06-02 05:08:01 -04:00
|
|
|
track_exception(e, merge_request)
|
|
|
|
:failed
|
2019-06-04 03:11:03 -04:00
|
|
|
end
|
|
|
|
|
2019-06-06 03:25:06 -04:00
|
|
|
def update(merge_request)
|
2019-09-25 12:25:40 -04:00
|
|
|
assign_allowed_merge_params(merge_request, params.merge(auto_merge_strategy: strategy))
|
2019-06-06 03:25:06 -04:00
|
|
|
|
|
|
|
return :failed unless merge_request.save
|
|
|
|
|
|
|
|
strategy.to_sym
|
|
|
|
end
|
|
|
|
|
2019-06-04 03:11:03 -04:00
|
|
|
def cancel(merge_request)
|
2021-12-20 07:10:29 -05:00
|
|
|
ApplicationRecord.transaction do
|
2020-06-02 05:08:01 -04:00
|
|
|
clear_auto_merge_parameters!(merge_request)
|
2019-06-04 03:11:03 -04:00
|
|
|
yield if block_given?
|
|
|
|
end
|
2020-06-02 05:08:01 -04:00
|
|
|
|
|
|
|
success
|
2021-04-26 08:09:44 -04:00
|
|
|
rescue StandardError => e
|
2020-06-02 05:08:01 -04:00
|
|
|
track_exception(e, merge_request)
|
|
|
|
error("Can't cancel the automatic merge", 406)
|
2019-06-04 03:11:03 -04:00
|
|
|
end
|
|
|
|
|
2019-06-26 08:24:09 -04:00
|
|
|
def abort(merge_request, reason)
|
2021-12-20 07:10:29 -05:00
|
|
|
ApplicationRecord.transaction do
|
2020-06-02 05:08:01 -04:00
|
|
|
clear_auto_merge_parameters!(merge_request)
|
2019-06-26 08:24:09 -04:00
|
|
|
yield if block_given?
|
|
|
|
end
|
2020-06-02 05:08:01 -04:00
|
|
|
|
|
|
|
success
|
2021-04-26 08:09:44 -04:00
|
|
|
rescue StandardError => e
|
2020-06-02 05:08:01 -04:00
|
|
|
track_exception(e, merge_request)
|
|
|
|
error("Can't abort the automatic merge", 406)
|
2019-06-26 08:24:09 -04:00
|
|
|
end
|
|
|
|
|
2020-04-21 11:21:10 -04:00
|
|
|
def available_for?(merge_request)
|
|
|
|
strong_memoize("available_for_#{merge_request.id}") do
|
|
|
|
merge_request.can_be_merged_by?(current_user) &&
|
2022-05-31 02:09:17 -04:00
|
|
|
merge_request.open? &&
|
|
|
|
!merge_request.broken? &&
|
|
|
|
!merge_request.draft? &&
|
|
|
|
merge_request.mergeable_discussions_state? &&
|
2022-08-03 14:11:59 -04:00
|
|
|
!merge_request.merge_blocked_by_other_mrs? &&
|
2020-04-21 11:21:10 -04:00
|
|
|
yield
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-06-04 03:11:03 -04:00
|
|
|
private
|
|
|
|
|
2020-07-16 20:09:37 -04:00
|
|
|
# Overridden in child classes
|
|
|
|
def notify(merge_request)
|
|
|
|
end
|
|
|
|
|
2019-06-04 03:11:03 -04:00
|
|
|
def strategy
|
|
|
|
strong_memoize(:strategy) do
|
|
|
|
self.class.name.demodulize.remove('Service').underscore
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-06-02 05:08:01 -04:00
|
|
|
def register_auto_merge_parameters!(merge_request)
|
|
|
|
assign_allowed_merge_params(merge_request, params.merge(auto_merge_strategy: strategy))
|
|
|
|
merge_request.auto_merge_enabled = true
|
|
|
|
merge_request.merge_user = current_user
|
|
|
|
merge_request.save!
|
|
|
|
end
|
|
|
|
|
|
|
|
def clear_auto_merge_parameters!(merge_request)
|
2019-06-04 03:11:03 -04:00
|
|
|
merge_request.auto_merge_enabled = false
|
|
|
|
merge_request.merge_user = nil
|
|
|
|
|
|
|
|
merge_request.merge_params&.except!(
|
|
|
|
'should_remove_source_branch',
|
|
|
|
'commit_message',
|
|
|
|
'squash_commit_message',
|
|
|
|
'auto_merge_strategy'
|
|
|
|
)
|
|
|
|
|
2020-06-02 05:08:01 -04:00
|
|
|
merge_request.save!
|
|
|
|
end
|
|
|
|
|
|
|
|
def track_exception(error, merge_request)
|
|
|
|
Gitlab::ErrorTracking.track_exception(error, merge_request_id: merge_request&.id)
|
2019-06-04 03:11:03 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|