Commit e175d2a1 authored by Stan Hu's avatar Stan Hu

Fix force_remove_source_branch not working in API

When an MR is merged via the API without `should_remove_source_branch`
set, the branch is always retained, even if `force_remove_source_branch`
is set to true in the MR merge params. This was happening because the
API always passed a `should_remove_source_branch` with a `nil` value,
and `MergeService` interpreted this as `false`.

To fix this, we strip `nil` values from the `merge_params`.

Relates to https://gitlab.com/gitlab-org/gitlab/-/issues/219991
parent 88bc5b9e
---
title: Fix force_remove_source_branch not working in API
merge_request: 33804
author:
type: fixed
......@@ -477,7 +477,7 @@ module API
squash_commit_message: params[:squash_commit_message],
should_remove_source_branch: params[:should_remove_source_branch],
sha: params[:sha] || merge_request.diff_head_sha
)
).compact
if immediately_mergeable
::MergeRequests::MergeService
......
......@@ -2029,6 +2029,34 @@ describe API::MergeRequests do
end
end
context "with a merge request that has force_remove_source_branch enabled" do
let(:source_repository) { merge_request.source_project.repository }
let(:source_branch) { merge_request.source_branch }
before do
merge_request.update(merge_params: { 'force_remove_source_branch' => true })
end
it 'removes the source branch' do
put(
api("/projects/#{project.id}/merge_requests/#{merge_request.iid}/merge", user)
)
expect(response).to have_gitlab_http_status(:ok)
expect(source_repository.branch_exists?(source_branch)).to be_falsy
end
it 'does not remove the source branch' do
put(
api("/projects/#{project.id}/merge_requests/#{merge_request.iid}/merge", user),
params: { should_remove_source_branch: false }
)
expect(response).to have_gitlab_http_status(:ok)
expect(source_repository.branch_exists?(source_branch)).to be_truthy
end
end
context "performing a ff-merge with squash" do
let(:merge_request) { create(:merge_request, :rebased, source_project: project, squash: true) }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment