gitlab-org--gitlab-foss/app/services/merge_requests/merge_service.rb

59 lines
1.7 KiB
Ruby
Raw Normal View History

module MergeRequests
# MergeService class
#
# 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 05:17:41 -05:00
attr_reader :merge_request
2015-11-18 05:17:41 -05:00
def execute(merge_request)
@merge_request = merge_request
2015-11-18 05:17:41 -05:00
return error('Merge request is not mergeable') unless @merge_request.mergeable?
merge_request.in_locked_state do
if commit
after_merge
success
else
error('Can not merge changes')
end
end
end
private
def commit
committer = repository.user_to_committer(current_user)
options = {
2015-11-18 05:17:41 -05:00
message: params[:commit_message] || merge_request.merge_commit_message,
author: committer,
committer: committer
}
2016-01-29 12:04:48 -05:00
commit_id = repository.merge(current_user, merge_request.source_sha, merge_request.target_branch, options)
merge_request.update(merge_commit_sha: commit_id)
2015-11-24 16:12:25 -05:00
rescue StandardError => e
merge_request.update(merge_error: "Something went wrong during merge")
Rails.logger.error(e.message)
return false
end
def after_merge
MergeRequests::PostMergeService.new(project, current_user).execute(merge_request)
2015-11-18 05:17:41 -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
end
def branch_deletion_user
@merge_request.force_remove_source_branch? ? @merge_request.author : current_user
end
end
end