debian-mirror-gitlab/app/services/auto_merge/base_service.rb

75 lines
1.7 KiB
Ruby
Raw Normal View History

2019-09-04 21:01:54 +05:30
# frozen_string_literal: true
module AutoMerge
class BaseService < ::BaseService
include Gitlab::Utils::StrongMemoize
2019-10-31 01:37:42 +05:30
include MergeRequests::AssignsMergeParams
2019-09-04 21:01:54 +05:30
def execute(merge_request)
2019-10-31 01:37:42 +05:30
assign_allowed_merge_params(merge_request, params.merge(auto_merge_strategy: strategy))
2019-09-04 21:01:54 +05:30
merge_request.auto_merge_enabled = true
merge_request.merge_user = current_user
return :failed unless merge_request.save
yield if block_given?
# Notify the event that auto merge is enabled or merge param is updated
AutoMergeProcessWorker.perform_async(merge_request.id)
strategy.to_sym
end
def update(merge_request)
2019-10-31 01:37:42 +05:30
assign_allowed_merge_params(merge_request, params.merge(auto_merge_strategy: strategy))
2019-09-04 21:01:54 +05:30
return :failed unless merge_request.save
strategy.to_sym
end
def cancel(merge_request)
2019-09-30 21:07:59 +05:30
if clear_auto_merge_parameters(merge_request)
2019-09-04 21:01:54 +05:30
yield if block_given?
success
else
error("Can't cancel the automatic merge", 406)
end
end
2019-09-30 21:07:59 +05:30
def abort(merge_request, reason)
if clear_auto_merge_parameters(merge_request)
yield if block_given?
success
else
error("Can't abort the automatic merge", 406)
end
end
2019-09-04 21:01:54 +05:30
private
def strategy
strong_memoize(:strategy) do
self.class.name.demodulize.remove('Service').underscore
end
end
2019-09-30 21:07:59 +05:30
def clear_auto_merge_parameters(merge_request)
2019-09-04 21:01:54 +05:30
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'
)
merge_request.save
end
end
end