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

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

116 lines
3.1 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)
2022-03-02 08:16:31 +05:30
ApplicationRecord.transaction do
2020-06-23 00:09:42 +05:30
register_auto_merge_parameters!(merge_request)
yield if block_given?
end
2019-09-04 21:01:54 +05:30
2020-07-28 23:09:34 +05:30
notify(merge_request)
2019-09-04 21:01:54 +05:30
AutoMergeProcessWorker.perform_async(merge_request.id)
strategy.to_sym
2021-06-08 01:23:25 +05:30
rescue StandardError => e
2020-06-23 00:09:42 +05:30
track_exception(e, merge_request)
:failed
2019-09-04 21:01:54 +05:30
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)
2022-03-02 08:16:31 +05:30
ApplicationRecord.transaction do
2020-06-23 00:09:42 +05:30
clear_auto_merge_parameters!(merge_request)
2019-09-04 21:01:54 +05:30
yield if block_given?
end
2020-06-23 00:09:42 +05:30
success
2021-06-08 01:23:25 +05:30
rescue StandardError => e
2020-06-23 00:09:42 +05:30
track_exception(e, merge_request)
error("Can't cancel the automatic merge", 406)
2019-09-04 21:01:54 +05:30
end
2019-09-30 21:07:59 +05:30
def abort(merge_request, reason)
2022-03-02 08:16:31 +05:30
ApplicationRecord.transaction do
2020-06-23 00:09:42 +05:30
clear_auto_merge_parameters!(merge_request)
2019-09-30 21:07:59 +05:30
yield if block_given?
end
2020-06-23 00:09:42 +05:30
success
2021-06-08 01:23:25 +05:30
rescue StandardError => e
2020-06-23 00:09:42 +05:30
track_exception(e, merge_request)
error("Can't abort the automatic merge", 406)
2019-09-30 21:07:59 +05:30
end
2020-04-22 19:07:51 +05:30
def available_for?(merge_request)
strong_memoize("available_for_#{merge_request.id}") do
merge_request.can_be_merged_by?(current_user) &&
merge_request.mergeable_state?(skip_ci_check: true) &&
yield
end
end
2020-11-24 15:15:51 +05:30
##
# NOTE: This method is to be removed when `disallow_to_create_merge_request_pipelines_in_target_project`
# feature flag is removed.
def self.can_add_to_merge_train?(merge_request)
2022-01-26 12:08:38 +05:30
if ::Feature.enabled?(:ci_disallow_to_create_merge_request_pipelines_in_target_project, merge_request.target_project)
2020-11-24 15:15:51 +05:30
merge_request.for_same_project?
else
true
end
end
def can_add_to_merge_train?(merge_request)
self.class.can_add_to_merge_train?(merge_request)
end
2019-09-04 21:01:54 +05:30
private
2020-07-28 23:09:34 +05:30
# Overridden in child classes
def notify(merge_request)
end
2019-09-04 21:01:54 +05:30
def strategy
strong_memoize(:strategy) do
self.class.name.demodulize.remove('Service').underscore
end
end
2020-06-23 00:09:42 +05:30
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-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'
)
2020-06-23 00:09:42 +05:30
merge_request.save!
end
def track_exception(error, merge_request)
Gitlab::ErrorTracking.track_exception(error, merge_request_id: merge_request&.id)
2019-09-04 21:01:54 +05:30
end
end
end