2019-01-31 13:32:44 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module MergeRequests
|
2019-06-20 08:45:46 -04:00
|
|
|
# Performs the merge between source SHA and the target branch or the specified first parent ref. Instead
|
2019-01-31 13:32:44 -05:00
|
|
|
# of writing the result to the MR target branch, it targets the `target_ref`.
|
|
|
|
#
|
|
|
|
# Ideally this should leave the `target_ref` state with the same state the
|
|
|
|
# target branch would have if we used the regular `MergeService`, but without
|
|
|
|
# every side-effect that comes with it (MR updates, mails, source branch
|
|
|
|
# deletion, etc). This service should be kept idempotent (i.e. can
|
|
|
|
# be executed regardless of the `target_ref` current state).
|
|
|
|
#
|
|
|
|
class MergeToRefService < MergeRequests::MergeBaseService
|
2019-05-21 17:14:22 -04:00
|
|
|
extend ::Gitlab::Utils::Override
|
|
|
|
|
2019-01-31 13:32:44 -05:00
|
|
|
def execute(merge_request)
|
|
|
|
@merge_request = merge_request
|
|
|
|
|
2019-07-05 15:31:10 -04:00
|
|
|
error_check!
|
2019-01-31 13:32:44 -05:00
|
|
|
|
|
|
|
commit_id = commit
|
|
|
|
|
2019-02-11 10:14:11 -05:00
|
|
|
raise_error('Conflicts detected during merge') unless commit_id
|
2019-01-31 13:32:44 -05:00
|
|
|
|
2019-06-11 12:08:25 -04:00
|
|
|
commit = project.commit(commit_id)
|
|
|
|
target_id, source_id = commit.parent_ids
|
|
|
|
|
|
|
|
success(commit_id: commit.id,
|
|
|
|
target_id: target_id,
|
|
|
|
source_id: source_id)
|
2019-05-21 17:14:22 -04:00
|
|
|
rescue MergeError, ArgumentError => error
|
2019-01-31 13:32:44 -05:00
|
|
|
error(error.message)
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2019-05-21 17:14:22 -04:00
|
|
|
override :source
|
|
|
|
def source
|
|
|
|
merge_request.diff_head_sha
|
|
|
|
end
|
|
|
|
|
2019-07-05 15:31:10 -04:00
|
|
|
override :error_check!
|
2019-01-31 13:32:44 -05:00
|
|
|
def error_check!
|
2019-07-05 15:31:10 -04:00
|
|
|
check_source
|
2019-02-11 10:14:11 -05:00
|
|
|
end
|
|
|
|
|
2019-06-20 08:45:46 -04:00
|
|
|
##
|
|
|
|
# The parameter `target_ref` is where the merge result will be written.
|
|
|
|
# Default is the merge ref i.e. `refs/merge-requests/:iid/merge`.
|
2019-01-31 13:32:44 -05:00
|
|
|
def target_ref
|
2019-06-20 08:45:46 -04:00
|
|
|
params[:target_ref] || merge_request.merge_ref_path
|
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
# The parameter `first_parent_ref` is the main line of the merge commit.
|
|
|
|
# Default is the target branch ref of the merge request.
|
|
|
|
def first_parent_ref
|
|
|
|
params[:first_parent_ref] || merge_request.target_branch_ref
|
2019-01-31 13:32:44 -05:00
|
|
|
end
|
|
|
|
|
2020-10-16 08:09:33 -04:00
|
|
|
##
|
|
|
|
# The parameter `allow_conflicts` is a flag whether merge conflicts should be merged into diff
|
|
|
|
# Default is false
|
|
|
|
def allow_conflicts
|
|
|
|
params[:allow_conflicts] || false
|
|
|
|
end
|
|
|
|
|
2019-01-31 13:32:44 -05:00
|
|
|
def commit
|
2020-10-16 08:09:33 -04:00
|
|
|
repository.merge_to_ref(current_user, source, merge_request, target_ref, commit_message, first_parent_ref, allow_conflicts)
|
2020-03-05 13:08:19 -05:00
|
|
|
rescue Gitlab::Git::PreReceiveError, Gitlab::Git::CommandError => error
|
2019-01-31 13:32:44 -05:00
|
|
|
raise MergeError, error.message
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|