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)
|
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?
|
|
|
|
return log_merge_error('Merge request is not mergeable', save_message_on_model: true)
|
|
|
|
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-02-17 12:48:32 -05:00
|
|
|
return log_merge_error('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
|
|
|
|
success
|
|
|
|
end
|
|
|
|
end
|
2017-02-28 06:01:04 -05:00
|
|
|
rescue MergeError => e
|
|
|
|
log_merge_error(e.message, save_message_on_model: true)
|
2015-08-11 08:33:31 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def commit
|
2015-09-17 01:45:22 -04:00
|
|
|
committer = repository.user_to_committer(current_user)
|
2015-08-11 08:33:31 -04:00
|
|
|
|
|
|
|
options = {
|
2015-11-18 05:17:41 -05:00
|
|
|
message: params[:commit_message] || merge_request.merge_commit_message,
|
2015-08-11 08:33:31 -04:00
|
|
|
author: committer,
|
|
|
|
committer: committer
|
|
|
|
}
|
|
|
|
|
2017-02-01 09:28:04 -05:00
|
|
|
commit_id = repository.merge(current_user, source, merge_request, options)
|
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)
|
2016-07-04 13:04:29 -04:00
|
|
|
rescue GitHooksService::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
|
|
|
|
2016-02-12 15:41:31 -05:00
|
|
|
if params[:should_remove_source_branch].present? || @merge_request.force_remove_source_branch?
|
|
|
|
DeleteBranchService.new(@merge_request.source_project, branch_deletion_user).
|
2015-11-18 05:17:41 -05:00
|
|
|
execute(merge_request.source_branch)
|
|
|
|
end
|
2014-01-24 07:18:32 -05:00
|
|
|
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
|
2016-10-19 00:26:20 -04:00
|
|
|
|
2017-02-14 09:13:35 -05:00
|
|
|
def log_merge_error(message, save_message_on_model: false)
|
2016-10-19 00:59:57 -04:00
|
|
|
Rails.logger.error("MergeService ERROR: #{merge_request_info} - #{message}")
|
2016-10-19 00:26:20 -04:00
|
|
|
|
2017-02-14 09:13:35 -05:00
|
|
|
@merge_request.update(merge_error: message) if save_message_on_model
|
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
|