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

92 lines
2.7 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
2017-08-17 22:00:37 +05:30
MergeError = Class.new(StandardError)
attr_reader :merge_request, :source
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
2017-08-17 22:00:37 +05:30
unless @merge_request.mergeable?
return log_merge_error('Merge request is not mergeable', save_message_on_model: true)
end
@source = find_merge_source
unless @source
return log_merge_error('No source for merge', save_message_on_model: true)
end
2015-09-25 12:07:36 +05:30
merge_request.in_locked_state do
if commit
after_merge
success
end
end
2017-08-17 22:00:37 +05:30
rescue MergeError => e
log_merge_error(e.message, save_message_on_model: true)
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
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
}
2017-08-17 22:00:37 +05:30
commit_id = repository.merge(current_user, source, merge_request, options)
2016-08-24 12:49:21 +05:30
2017-08-17 22:00:37 +05:30
raise MergeError, 'Conflicts detected during merge' unless commit_id
merge_request.update(merge_commit_sha: commit_id)
2016-08-24 12:49:21 +05:30
rescue GitHooksService::PreReceiveError => e
2017-08-17 22:00:37 +05:30
raise MergeError, e.message
2015-12-23 02:04:40 +05:30
rescue StandardError => e
2017-08-17 22:00:37 +05:30
raise MergeError, "Something went wrong during merge: #{e.message}"
2016-08-24 12:49:21 +05:30
ensure
merge_request.update(in_progress_merge_commit_sha: nil)
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
2016-06-02 11:05:42 +05:30
if params[:should_remove_source_branch].present? || @merge_request.force_remove_source_branch?
2017-09-10 17:25:29 +05:30
# Verify again that the source branch can be removed, since branch may be protected,
# or the source branch may have been updated.
if @merge_request.can_remove_source_branch?(branch_deletion_user)
DeleteBranchService.new(@merge_request.source_project, branch_deletion_user)
.execute(merge_request.source_branch)
end
2015-12-23 02:04:40 +05:30
end
2014-09-02 18:07:02 +05:30
end
2016-06-02 11:05:42 +05:30
def branch_deletion_user
@merge_request.force_remove_source_branch? ? @merge_request.author : current_user
end
2016-11-03 12:29:30 +05:30
2017-08-17 22:00:37 +05:30
def log_merge_error(message, save_message_on_model: false)
2016-11-03 12:29:30 +05:30
Rails.logger.error("MergeService ERROR: #{merge_request_info} - #{message}")
2017-08-17 22:00:37 +05:30
@merge_request.update(merge_error: message) if save_message_on_model
2016-11-03 12:29:30 +05:30
end
def merge_request_info
2017-08-17 22:00:37 +05:30
merge_request.to_reference(full: true)
end
2016-11-03 12:29:30 +05:30
2017-08-17 22:00:37 +05:30
def find_merge_source
merge_request.diff_head_sha
2016-11-03 12:29:30 +05:30
end
2014-09-02 18:07:02 +05:30
end
end