debian-mirror-gitlab/app/services/merge_requests/merge_service.rb

54 lines
1.4 KiB
Ruby
Raw Normal View History

2014-09-02 18:07:02 +05:30
module MergeRequests
# MergeService class
#
2015-09-25 12:07:36 +05:30
# 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-12-23 02:04:40 +05:30
attr_reader :merge_request
2015-09-25 12:07:36 +05:30
2015-12-23 02:04:40 +05:30
def execute(merge_request)
2015-09-25 12:07:36 +05:30
@merge_request = merge_request
2015-12-23 02:04:40 +05:30
return error('Merge request is not mergeable') unless @merge_request.mergeable?
2015-09-25 12:07:36 +05:30
merge_request.in_locked_state do
if commit
after_merge
success
else
error('Can not merge changes')
end
end
end
2014-09-02 18:07:02 +05:30
2015-09-25 12:07:36 +05:30
private
def commit
2015-10-24 18:46:33 +05:30
committer = repository.user_to_committer(current_user)
2015-09-25 12:07:36 +05:30
options = {
2015-12-23 02:04:40 +05:30
message: params[:commit_message] || merge_request.merge_commit_message,
2015-09-25 12:07:36 +05:30
author: committer,
committer: committer
}
repository.merge(current_user, merge_request.source_sha, merge_request.target_branch, options)
2015-12-23 02:04:40 +05:30
rescue StandardError => e
2015-10-24 18:46:33 +05:30
merge_request.update(merge_error: "Something went wrong during merge")
Rails.logger.error(e.message)
return false
2015-09-25 12:07:36 +05:30
end
2014-09-02 18:07:02 +05:30
2015-09-25 12:07:36 +05:30
def after_merge
MergeRequests::PostMergeService.new(project, current_user).execute(merge_request)
2015-12-23 02:04:40 +05:30
if params[:should_remove_source_branch]
DeleteBranchService.new(@merge_request.source_project, current_user).
execute(merge_request.source_branch)
end
2014-09-02 18:07:02 +05:30
end
end
end