2018-07-17 12:50:37 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2014-01-24 07:18:32 -05:00
|
|
|
module MergeRequests
|
|
|
|
# MergeService class
|
|
|
|
#
|
2015-08-11 08:33:31 -04:00
|
|
|
# Do git merge and in case of success
|
|
|
|
# mark merge request as merged and execute all hooks and notifications
|
|
|
|
# Executed when you do merge via GitLab UI
|
|
|
|
#
|
2019-01-31 13:32:44 -05:00
|
|
|
class MergeService < MergeRequests::MergeBaseService
|
2021-04-26 11:10:20 -04:00
|
|
|
include Gitlab::Utils::StrongMemoize
|
|
|
|
|
2021-02-12 19:09:10 -05:00
|
|
|
GENERIC_ERROR_MESSAGE = 'An error occurred while merging'
|
2021-04-26 11:10:20 -04:00
|
|
|
LEASE_TIMEOUT = 15.minutes.to_i
|
2021-02-12 19:09:10 -05:00
|
|
|
|
2017-11-16 09:49:01 -05:00
|
|
|
delegate :merge_jid, :state, to: :@merge_request
|
|
|
|
|
2020-09-07 08:08:27 -04:00
|
|
|
def execute(merge_request, options = {})
|
2017-09-13 10:11:10 -04:00
|
|
|
if project.merge_requests_ff_only_enabled && !self.is_a?(FfMergeService)
|
2021-05-11 23:10:21 -04:00
|
|
|
FfMergeService.new(project: project, current_user: current_user, params: params).execute(merge_request)
|
2017-09-13 10:11:10 -04:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2021-04-26 11:10:20 -04:00
|
|
|
return if merge_request.merged?
|
|
|
|
return unless exclusive_lease(merge_request.id).try_obtain
|
|
|
|
|
2015-08-11 08:33:31 -04:00
|
|
|
@merge_request = merge_request
|
2020-09-07 08:08:27 -04:00
|
|
|
@options = options
|
2014-01-24 07:18:32 -05:00
|
|
|
|
2019-02-11 10:14:11 -05:00
|
|
|
validate!
|
2017-02-01 09:28:04 -05:00
|
|
|
|
2015-08-11 08:33:31 -04:00
|
|
|
merge_request.in_locked_state do
|
2015-08-14 11:52:49 -04:00
|
|
|
if commit
|
2015-08-11 08:33:31 -04:00
|
|
|
after_merge
|
2017-08-28 15:41:05 -04:00
|
|
|
clean_merge_jid
|
2015-08-11 08:33:31 -04:00
|
|
|
success
|
|
|
|
end
|
|
|
|
end
|
2020-07-02 11:09:08 -04:00
|
|
|
|
2017-11-16 09:49:01 -05:00
|
|
|
log_info("Merge process finished on JID #{merge_jid} with state #{state}")
|
2017-02-28 06:01:04 -05:00
|
|
|
rescue MergeError => e
|
2017-09-27 16:38:23 -04:00
|
|
|
handle_merge_error(log_message: e.message, save_message_on_model: true)
|
2021-04-26 11:10:20 -04:00
|
|
|
ensure
|
|
|
|
exclusive_lease(merge_request.id).cancel
|
2015-08-11 08:33:31 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2019-02-11 10:14:11 -05:00
|
|
|
def validate!
|
|
|
|
authorization_check!
|
|
|
|
error_check!
|
2019-11-15 10:06:12 -05:00
|
|
|
updated_check!
|
2019-02-11 10:14:11 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def authorization_check!
|
|
|
|
unless @merge_request.can_be_merged_by?(current_user)
|
|
|
|
raise_error('You are not allowed to merge this merge request')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-11-09 11:55:45 -05:00
|
|
|
def error_check!
|
2019-02-12 16:21:14 -05:00
|
|
|
super
|
|
|
|
|
2019-07-05 15:31:10 -04:00
|
|
|
check_source
|
|
|
|
|
2017-11-09 11:55:45 -05:00
|
|
|
error =
|
|
|
|
if @merge_request.should_be_rebased?
|
|
|
|
'Only fast-forward merge is allowed for your project. Please update your source branch'
|
2020-09-07 08:08:27 -04:00
|
|
|
elsif !@merge_request.mergeable?(skip_discussions_check: @options[:skip_discussions_check])
|
2017-11-09 11:55:45 -05:00
|
|
|
'Merge request is not mergeable'
|
2020-06-30 14:09:13 -04:00
|
|
|
elsif !@merge_request.squash && project.squash_always?
|
|
|
|
'This project requires squashing commits when merge requests are accepted.'
|
2017-11-09 11:55:45 -05:00
|
|
|
end
|
|
|
|
|
2019-02-11 10:14:11 -05:00
|
|
|
raise_error(error) if error
|
2017-11-09 11:55:45 -05:00
|
|
|
end
|
|
|
|
|
2019-11-15 10:06:12 -05:00
|
|
|
def updated_check!
|
|
|
|
unless source_matches?
|
|
|
|
raise_error('Branch has been updated since the merge was requested. '\
|
|
|
|
'Please review the changes.')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-08-11 08:33:31 -04:00
|
|
|
def commit
|
2017-11-16 09:49:01 -05:00
|
|
|
log_info("Git merge started on JID #{merge_jid}")
|
2018-04-29 11:30:49 -04:00
|
|
|
commit_id = try_merge
|
|
|
|
|
|
|
|
if commit_id
|
|
|
|
log_info("Git merge finished on JID #{merge_jid} commit #{commit_id}")
|
|
|
|
else
|
2021-02-12 19:09:10 -05:00
|
|
|
raise_error(GENERIC_ERROR_MESSAGE)
|
2018-04-29 11:30:49 -04:00
|
|
|
end
|
2016-07-29 14:14:53 -04:00
|
|
|
|
2018-04-29 11:30:49 -04:00
|
|
|
merge_request.update!(merge_commit_sha: commit_id)
|
2020-02-10 07:08:59 -05:00
|
|
|
ensure
|
2020-09-23 08:09:58 -04:00
|
|
|
merge_request.update_and_mark_in_progress_merge_commit_sha(nil)
|
2018-04-29 11:30:49 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def try_merge
|
2021-01-13 10:10:40 -05:00
|
|
|
repository.merge(current_user, source, merge_request, commit_message).tap do
|
|
|
|
merge_request.update_column(:squash_commit_sha, source) if merge_request.squash_on_merge?
|
2021-01-07 13:10:38 -05:00
|
|
|
end
|
2018-06-11 06:42:09 -04:00
|
|
|
rescue Gitlab::Git::PreReceiveError => e
|
2019-03-20 00:08:51 -04:00
|
|
|
raise MergeError,
|
|
|
|
"Something went wrong during merge pre-receive hook. #{e.message}".strip
|
2021-04-26 08:09:44 -04:00
|
|
|
rescue StandardError => e
|
2018-04-29 11:30:49 -04:00
|
|
|
handle_merge_error(log_message: e.message)
|
2021-02-12 19:09:10 -05:00
|
|
|
raise_error(GENERIC_ERROR_MESSAGE)
|
2015-08-11 08:33:31 -04:00
|
|
|
end
|
2014-01-24 07:18:32 -05:00
|
|
|
|
2015-08-11 08:33:31 -04:00
|
|
|
def after_merge
|
2017-11-16 09:49:01 -05:00
|
|
|
log_info("Post merge started on JID #{merge_jid} with state #{state}")
|
2021-05-11 23:10:21 -04:00
|
|
|
MergeRequests::PostMergeService.new(project: project, current_user: current_user).execute(merge_request)
|
2017-11-16 09:49:01 -05:00
|
|
|
log_info("Post merge finished on JID #{merge_jid} with state #{state}")
|
2015-11-18 05:17:41 -05:00
|
|
|
|
2017-10-11 16:29:37 -04:00
|
|
|
if delete_source_branch?
|
2021-03-02 04:10:43 -05:00
|
|
|
MergeRequests::DeleteSourceBranchWorker.perform_async(@merge_request.id, @merge_request.source_branch_sha, branch_deletion_user.id)
|
2015-11-18 05:17:41 -05:00
|
|
|
end
|
2014-01-24 07:18:32 -05:00
|
|
|
end
|
2016-02-12 15:41:31 -05:00
|
|
|
|
2017-08-28 15:41:05 -04:00
|
|
|
def clean_merge_jid
|
|
|
|
merge_request.update_column(:merge_jid, nil)
|
|
|
|
end
|
|
|
|
|
2016-02-12 15:41:31 -05:00
|
|
|
def branch_deletion_user
|
|
|
|
@merge_request.force_remove_source_branch? ? @merge_request.author : current_user
|
|
|
|
end
|
2017-10-12 09:16:00 -04:00
|
|
|
|
2017-10-12 09:14:27 -04:00
|
|
|
# Verify again that the source branch can be removed, since branch may be protected,
|
|
|
|
# or the source branch may have been updated, or the user may not have permission
|
|
|
|
#
|
2017-10-11 16:29:37 -04:00
|
|
|
def delete_source_branch?
|
|
|
|
params.fetch('should_remove_source_branch', @merge_request.force_remove_source_branch?) &&
|
|
|
|
@merge_request.can_remove_source_branch?(branch_deletion_user)
|
|
|
|
end
|
|
|
|
|
2017-09-27 16:38:23 -04:00
|
|
|
def handle_merge_error(log_message:, save_message_on_model: false)
|
2020-05-21 17:08:31 -04:00
|
|
|
Gitlab::AppLogger.error("MergeService ERROR: #{merge_request_info} - #{log_message}")
|
2017-10-27 16:11:21 -04:00
|
|
|
@merge_request.update(merge_error: log_message) if save_message_on_model
|
2016-10-19 00:26:20 -04:00
|
|
|
end
|
2016-10-19 00:59:57 -04:00
|
|
|
|
2017-11-20 09:22:06 -05:00
|
|
|
def log_info(message)
|
2020-05-21 17:08:31 -04:00
|
|
|
@logger ||= Gitlab::AppLogger
|
2017-11-20 09:22:06 -05:00
|
|
|
@logger.info("#{merge_request_info} - #{message}")
|
|
|
|
end
|
|
|
|
|
2016-10-19 00:59:57 -04:00
|
|
|
def merge_request_info
|
2017-02-01 09:28:04 -05:00
|
|
|
merge_request.to_reference(full: true)
|
|
|
|
end
|
2019-11-15 10:06:12 -05:00
|
|
|
|
|
|
|
def source_matches?
|
|
|
|
# params-keys are symbols coming from the controller, but when they get
|
|
|
|
# loaded from the database they're strings
|
|
|
|
params.with_indifferent_access[:sha] == merge_request.diff_head_sha
|
|
|
|
end
|
2021-04-26 11:10:20 -04:00
|
|
|
|
|
|
|
def exclusive_lease(merge_request_id)
|
|
|
|
strong_memoize(:"exclusive_lease_#{merge_request_id}") do
|
|
|
|
lease_key = ['merge_requests_merge_service', merge_request_id].join(':')
|
|
|
|
|
|
|
|
Gitlab::ExclusiveLease.new(lease_key, timeout: LEASE_TIMEOUT)
|
|
|
|
end
|
|
|
|
end
|
2014-01-24 07:18:32 -05:00
|
|
|
end
|
|
|
|
end
|