debian-mirror-gitlab/app/services/merge_requests/merge_service.rb

167 lines
5.2 KiB
Ruby
Raw Normal View History

2018-11-18 11:00:15 +05:30
# frozen_string_literal: true
2014-09-02 18:07:02 +05:30
module MergeRequests
# MergeService class
#
2015-09-25 12:07:36 +05:30
# 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-07-07 11:18:12 +05:30
class MergeService < MergeRequests::MergeBaseService
2021-06-08 01:23:25 +05:30
include Gitlab::Utils::StrongMemoize
2021-03-11 19:13:27 +05:30
GENERIC_ERROR_MESSAGE = 'An error occurred while merging'
2021-06-08 01:23:25 +05:30
LEASE_TIMEOUT = 15.minutes.to_i
2021-03-11 19:13:27 +05:30
2018-03-17 18:26:18 +05:30
delegate :merge_jid, :state, to: :@merge_request
2015-09-25 12:07:36 +05:30
2020-11-24 15:15:51 +05:30
def execute(merge_request, options = {})
2018-03-17 18:26:18 +05:30
if project.merge_requests_ff_only_enabled && !self.is_a?(FfMergeService)
2021-06-08 01:23:25 +05:30
FfMergeService.new(project: project, current_user: current_user, params: params).execute(merge_request)
2018-03-17 18:26:18 +05:30
return
2017-08-17 22:00:37 +05:30
end
2021-06-08 01:23:25 +05:30
return if merge_request.merged?
return unless exclusive_lease(merge_request.id).try_obtain
2018-03-17 18:26:18 +05:30
@merge_request = merge_request
2020-11-24 15:15:51 +05:30
@options = options
2017-08-17 22:00:37 +05:30
2019-07-07 11:18:12 +05:30
validate!
2015-09-25 12:07:36 +05:30
merge_request.in_locked_state do
if commit
after_merge
2018-03-17 18:26:18 +05:30
clean_merge_jid
2015-09-25 12:07:36 +05:30
success
end
end
2020-07-28 23:09:34 +05:30
2018-03-17 18:26:18 +05:30
log_info("Merge process finished on JID #{merge_jid} with state #{state}")
2017-08-17 22:00:37 +05:30
rescue MergeError => e
2018-03-17 18:26:18 +05:30
handle_merge_error(log_message: e.message, save_message_on_model: true)
2021-06-08 01:23:25 +05:30
ensure
exclusive_lease(merge_request.id).cancel
2015-09-25 12:07:36 +05:30
end
2014-09-02 18:07:02 +05:30
2019-07-07 11:18:12 +05:30
private
2018-12-13 13:39:08 +05:30
2019-07-07 11:18:12 +05:30
def validate!
authorization_check!
error_check!
2019-12-26 22:10:19 +05:30
updated_check!
2019-05-18 00:54:41 +05:30
end
2015-09-25 12:07:36 +05:30
2019-07-07 11:18:12 +05:30
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
2019-05-18 00:54:41 +05:30
2019-05-30 16:15:17 +05:30
def error_check!
2019-07-07 11:18:12 +05:30
super
2019-09-30 21:07:59 +05:30
check_source
2018-03-17 18:26:18 +05:30
error =
if @merge_request.should_be_rebased?
'Only fast-forward merge is allowed for your project. Please update your source branch'
2020-11-24 15:15:51 +05:30
elsif !@merge_request.mergeable?(skip_discussions_check: @options[:skip_discussions_check])
2018-03-17 18:26:18 +05:30
'Merge request is not mergeable'
2020-07-28 23:09:34 +05:30
elsif !@merge_request.squash && project.squash_always?
'This project requires squashing commits when merge requests are accepted.'
2018-03-17 18:26:18 +05:30
end
2019-07-07 11:18:12 +05:30
raise_error(error) if error
2018-03-17 18:26:18 +05:30
end
2015-09-25 12:07:36 +05:30
2019-12-26 22:10:19 +05:30
def updated_check!
unless source_matches?
raise_error('Branch has been updated since the merge was requested. '\
'Please review the changes.')
end
end
2018-03-17 18:26:18 +05:30
def commit
log_info("Git merge started on JID #{merge_jid}")
2018-10-15 14:42:47 +05:30
commit_id = try_merge
if commit_id
log_info("Git merge finished on JID #{merge_jid} commit #{commit_id}")
else
2021-03-11 19:13:27 +05:30
raise_error(GENERIC_ERROR_MESSAGE)
2018-10-15 14:42:47 +05:30
end
2016-08-24 12:49:21 +05:30
2018-10-15 14:42:47 +05:30
merge_request.update!(merge_commit_sha: commit_id)
2020-03-13 15:44:24 +05:30
ensure
2021-01-03 14:25:43 +05:30
merge_request.update_and_mark_in_progress_merge_commit_sha(nil)
2018-10-15 14:42:47 +05:30
end
def try_merge
2021-03-08 18:12:59 +05:30
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?
end
2018-11-08 19:23:39 +05:30
rescue Gitlab::Git::PreReceiveError => e
2019-07-07 11:18:12 +05:30
raise MergeError,
"Something went wrong during merge pre-receive hook. #{e.message}".strip
2021-06-08 01:23:25 +05:30
rescue StandardError => e
2018-10-15 14:42:47 +05:30
handle_merge_error(log_message: e.message)
2021-03-11 19:13:27 +05:30
raise_error(GENERIC_ERROR_MESSAGE)
2015-09-25 12:07:36 +05:30
end
2014-09-02 18:07:02 +05:30
2015-09-25 12:07:36 +05:30
def after_merge
2018-03-17 18:26:18 +05:30
log_info("Post merge started on JID #{merge_jid} with state #{state}")
2021-06-08 01:23:25 +05:30
MergeRequests::PostMergeService.new(project: project, current_user: current_user).execute(merge_request)
2018-03-17 18:26:18 +05:30
log_info("Post merge finished on JID #{merge_jid} with state #{state}")
2015-12-23 02:04:40 +05:30
2018-03-17 18:26:18 +05:30
if delete_source_branch?
2021-04-17 20:07:23 +05:30
MergeRequests::DeleteSourceBranchWorker.perform_async(@merge_request.id, @merge_request.source_branch_sha, branch_deletion_user.id)
2015-12-23 02:04:40 +05:30
end
2014-09-02 18:07:02 +05:30
end
2016-06-02 11:05:42 +05:30
2018-03-17 18:26:18 +05:30
def clean_merge_jid
merge_request.update_column(:merge_jid, nil)
end
2016-06-02 11:05:42 +05:30
def branch_deletion_user
@merge_request.force_remove_source_branch? ? @merge_request.author : current_user
end
2016-11-03 12:29:30 +05:30
2018-03-17 18:26:18 +05:30
# 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
#
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
def handle_merge_error(log_message:, save_message_on_model: false)
2020-06-23 00:09:42 +05:30
Gitlab::AppLogger.error("MergeService ERROR: #{merge_request_info} - #{log_message}")
2018-03-17 18:26:18 +05:30
@merge_request.update(merge_error: log_message) if save_message_on_model
end
2016-11-03 12:29:30 +05:30
2018-03-17 18:26:18 +05:30
def log_info(message)
2020-06-23 00:09:42 +05:30
@logger ||= Gitlab::AppLogger
2018-03-17 18:26:18 +05:30
@logger.info("#{merge_request_info} - #{message}")
2016-11-03 12:29:30 +05:30
end
def merge_request_info
2017-08-17 22:00:37 +05:30
merge_request.to_reference(full: true)
end
2019-12-26 22:10:19 +05:30
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-06-08 01:23:25 +05:30
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-09-02 18:07:02 +05:30
end
end