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
|
|
|
|
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-02-14 09:13:35 -05:00
|
|
|
unless @merge_request.mergeable?
|
2017-09-27 16:38:23 -04:00
|
|
|
return handle_merge_error(log_message: 'Merge request is not mergeable', save_message_on_model: true)
|
2017-02-14 09:13:35 -05:00
|
|
|
end
|
2015-08-11 08:33:31 -04:00
|
|
|
|
2017-02-01 09:28:04 -05:00
|
|
|
@source = find_merge_source
|
|
|
|
|
2017-02-14 09:13:35 -05:00
|
|
|
unless @source
|
2017-09-27 16:38:23 -04:00
|
|
|
return handle_merge_error(log_message: 'No source for merge', save_message_on_model: true)
|
2017-02-14 09:13:35 -05:00
|
|
|
end
|
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-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
|
|
|
|
|
|
|
|
def commit
|
2017-09-08 08:00:53 -04:00
|
|
|
message = params[:commit_message] || merge_request.merge_commit_message
|
2015-08-11 08:33:31 -04:00
|
|
|
|
2017-09-08 08:00:53 -04:00
|
|
|
commit_id = repository.merge(current_user, source, merge_request, message)
|
2016-07-29 14:14:53 -04:00
|
|
|
|
2017-02-28 06:01:04 -05:00
|
|
|
raise MergeError, 'Conflicts detected during merge' unless commit_id
|
|
|
|
|
|
|
|
merge_request.update(merge_commit_sha: commit_id)
|
2017-08-22 08:18:09 -04:00
|
|
|
rescue Gitlab::Git::HooksService::PreReceiveError => e
|
2017-02-28 06:01:04 -05:00
|
|
|
raise MergeError, e.message
|
2015-11-24 16:12:25 -05:00
|
|
|
rescue StandardError => e
|
2017-02-28 06:01:04 -05:00
|
|
|
raise MergeError, "Something went wrong during merge: #{e.message}"
|
2016-07-18 04:16:56 -04:00
|
|
|
ensure
|
|
|
|
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
|
|
|
|
MergeRequests::PostMergeService.new(project, current_user).execute(merge_request)
|
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
|
|
|
# Logs merge error message and cleans `MergeRequest#merge_jid`.
|
|
|
|
#
|
|
|
|
def handle_merge_error(log_message:, save_message_on_model: false)
|
|
|
|
Rails.logger.error("MergeService ERROR: #{merge_request_info} - #{log_message}")
|
2016-10-19 00:26:20 -04:00
|
|
|
|
2017-09-27 16:38:23 -04:00
|
|
|
if save_message_on_model
|
|
|
|
@merge_request.update(merge_error: log_message, merge_jid: nil)
|
|
|
|
else
|
|
|
|
clean_merge_jid
|
|
|
|
end
|
2016-10-19 00:26:20 -04:00
|
|
|
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-02-01 09:28:04 -05:00
|
|
|
def find_merge_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
|