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
|
|
|
|
#
|
|
|
|
class MergeService < MergeRequests::BaseService
|
2017-02-28 06:01:04 -05:00
|
|
|
MergeError = Class.new(StandardError)
|
|
|
|
|
2017-02-01 09:28:04 -05:00
|
|
|
attr_reader :merge_request, :source
|
2015-08-11 08:33:31 -04:00
|
|
|
|
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
|
|
|
|
2017-11-09 11:55:45 -05:00
|
|
|
error_check!
|
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
|
|
|
|
|
2017-11-09 11:55:45 -05:00
|
|
|
def error_check!
|
|
|
|
error =
|
|
|
|
if @merge_request.should_be_rebased?
|
|
|
|
'Only fast-forward merge is allowed for your project. Please update your source branch'
|
|
|
|
elsif !@merge_request.mergeable?
|
|
|
|
'Merge request is not mergeable'
|
|
|
|
elsif !source
|
|
|
|
'No source for merge'
|
|
|
|
end
|
|
|
|
|
|
|
|
raise MergeError, error if error
|
|
|
|
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
|
|
|
|
raise MergeError, 'Conflicts detected during merge'
|
|
|
|
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)
|
|
|
|
end
|
|
|
|
|
|
|
|
def try_merge
|
|
|
|
message = params[:commit_message] || merge_request.merge_commit_message
|
2017-02-28 06:01:04 -05:00
|
|
|
|
2018-04-29 11:30:49 -04:00
|
|
|
repository.merge(current_user, source, merge_request, message)
|
2017-08-22 08:18:09 -04:00
|
|
|
rescue Gitlab::Git::HooksService::PreReceiveError => e
|
2018-04-29 11:30:49 -04:00
|
|
|
handle_merge_error(log_message: e.message)
|
|
|
|
raise MergeError, 'Something went wrong during merge pre-receive hook'
|
|
|
|
rescue => e
|
|
|
|
handle_merge_error(log_message: e.message)
|
|
|
|
raise MergeError, 'Something went wrong during merge'
|
2016-07-18 04:16:56 -04:00
|
|
|
ensure
|
2018-04-29 11:30:49 -04:00
|
|
|
merge_request.update!(in_progress_merge_commit_sha: nil)
|
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?
|
|
|
|
DeleteBranchService.new(@merge_request.source_project, branch_deletion_user)
|
|
|
|
.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)
|
|
|
|
Rails.logger.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)
|
|
|
|
@logger ||= Rails.logger
|
|
|
|
@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
|
2016-10-19 00:59:57 -04:00
|
|
|
|
2017-11-09 11:55:45 -05:00
|
|
|
def source
|
|
|
|
@source ||= @merge_request.diff_head_sha
|
2016-10-19 00:59:57 -04:00
|
|
|
end
|
2014-01-24 07:18:32 -05:00
|
|
|
end
|
|
|
|
end
|