2014-01-24 12:18:32 +00:00
|
|
|
module MergeRequests
|
|
|
|
# MergeService class
|
|
|
|
#
|
2015-08-11 12:33:31 +00: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
|
2015-11-18 10:17:41 +00:00
|
|
|
attr_reader :merge_request
|
2015-08-11 12:33:31 +00:00
|
|
|
|
2015-11-18 10:17:41 +00:00
|
|
|
def execute(merge_request)
|
2015-08-11 12:33:31 +00:00
|
|
|
@merge_request = merge_request
|
2014-01-24 12:18:32 +00:00
|
|
|
|
2015-11-18 10:17:41 +00:00
|
|
|
return error('Merge request is not mergeable') unless @merge_request.mergeable?
|
2015-08-11 12:33:31 +00:00
|
|
|
|
|
|
|
merge_request.in_locked_state do
|
2015-08-14 15:52:49 +00:00
|
|
|
if commit
|
2015-08-11 12:33:31 +00:00
|
|
|
after_merge
|
|
|
|
success
|
|
|
|
else
|
|
|
|
error('Can not merge changes')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def commit
|
2015-09-17 05:45:22 +00:00
|
|
|
committer = repository.user_to_committer(current_user)
|
2015-08-11 12:33:31 +00:00
|
|
|
|
|
|
|
options = {
|
2015-11-18 10:17:41 +00:00
|
|
|
message: params[:commit_message] || merge_request.merge_commit_message,
|
2015-08-11 12:33:31 +00:00
|
|
|
author: committer,
|
|
|
|
committer: committer
|
|
|
|
}
|
|
|
|
|
2016-07-18 08:16:56 +00:00
|
|
|
commit_id = repository.merge(current_user, merge_request, options)
|
2016-07-29 18:14:53 +00:00
|
|
|
|
|
|
|
if commit_id
|
|
|
|
merge_request.update(merge_commit_sha: commit_id)
|
|
|
|
else
|
|
|
|
merge_request.update(merge_error: 'Conflicts detected during merge')
|
|
|
|
false
|
|
|
|
end
|
2016-07-04 17:04:29 +00:00
|
|
|
rescue GitHooksService::PreReceiveError => e
|
|
|
|
merge_request.update(merge_error: e.message)
|
|
|
|
false
|
2015-11-24 21:12:25 +00:00
|
|
|
rescue StandardError => e
|
2015-10-01 06:31:48 +00:00
|
|
|
merge_request.update(merge_error: "Something went wrong during merge")
|
|
|
|
Rails.logger.error(e.message)
|
2016-07-04 17:04:29 +00:00
|
|
|
false
|
2016-07-18 08:16:56 +00:00
|
|
|
ensure
|
|
|
|
merge_request.update(in_progress_merge_commit_sha: nil)
|
2015-08-11 12:33:31 +00:00
|
|
|
end
|
2014-01-24 12:18:32 +00:00
|
|
|
|
2015-08-11 12:33:31 +00:00
|
|
|
def after_merge
|
|
|
|
MergeRequests::PostMergeService.new(project, current_user).execute(merge_request)
|
2015-11-18 10:17:41 +00:00
|
|
|
|
2016-02-12 20:41:31 +00: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 10:17:41 +00:00
|
|
|
execute(merge_request.source_branch)
|
|
|
|
end
|
2014-01-24 12:18:32 +00:00
|
|
|
end
|
2016-02-12 20:41:31 +00:00
|
|
|
|
|
|
|
def branch_deletion_user
|
|
|
|
@merge_request.force_remove_source_branch? ? @merge_request.author : current_user
|
|
|
|
end
|
2014-01-24 12:18:32 +00:00
|
|
|
end
|
|
|
|
end
|