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
|
2017-11-16 09:49:01 -05:00
|
|
|
delegate :merge_jid, :state, to: :@merge_request
|
|
|
|
|
2015-11-18 05:17:41 -05:00
|
|
|
def execute(merge_request)
|
2017-09-13 10:11:10 -04:00
|
|
|
if project.merge_requests_ff_only_enabled && !self.is_a?(FfMergeService)
|
|
|
|
FfMergeService.new(project, current_user, params).execute(merge_request)
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2015-08-11 08:33:31 -04:00
|
|
|
@merge_request = merge_request
|
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
|
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)
|
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-04-06 23:09:15 -04:00
|
|
|
elsif !@merge_request.mergeable?
|
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
|
2019-02-11 10:14:11 -05:00
|
|
|
raise_error('Conflicts detected during merge')
|
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
|
|
|
|
merge_request.update_column(:in_progress_merge_commit_sha, nil)
|
2018-04-29 11:30:49 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def try_merge
|
2019-01-31 13:32:44 -05:00
|
|
|
repository.merge(current_user, source, merge_request, commit_message)
|
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
|
2018-04-29 11:30:49 -04:00
|
|
|
rescue => e
|
|
|
|
handle_merge_error(log_message: e.message)
|
2019-02-11 10:14:11 -05:00
|
|
|
raise_error('Something went wrong during merge')
|
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}")
|
2015-08-11 08:33:31 -04:00
|
|
|
MergeRequests::PostMergeService.new(project, 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?
|
2019-12-03 13:06:49 -05:00
|
|
|
::Branches::DeleteService.new(@merge_request.source_project, branch_deletion_user)
|
2017-10-11 16:29:37 -04:00
|
|
|
.execute(merge_request.source_branch)
|
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
|
2014-01-24 07:18:32 -05:00
|
|
|
end
|
|
|
|
end
|