Merge branch '50847-automatically-created-mr-uses-wrong-source-branch-when-branching-from-tag' into 'master'

Resolve "Automatically created MR uses wrong source branch (when branching from Tag)"

Closes #50847

See merge request gitlab-org/gitlab-ce!32392
This commit is contained in:
Rémy Coutable 2019-09-12 10:23:25 +00:00
commit 8ace644217
3 changed files with 49 additions and 2 deletions

View file

@ -52,8 +52,26 @@ module MergeRequests
end
def ref
return @ref if target_project.repository.branch_exists?(@ref)
if valid_ref?
@ref
else
default_branch
end
end
def valid_ref?
ref_is_branch? || ref_is_tag?
end
def ref_is_branch?
target_project.repository.branch_exists?(@ref)
end
def ref_is_tag?
target_project.repository.tag_exists?(@ref)
end
def default_branch
target_project.default_branch || 'master'
end
@ -67,10 +85,18 @@ module MergeRequests
source_project_id: target_project.id,
source_branch: branch_name,
target_project_id: target_project.id,
target_branch: ref
target_branch: target_branch
}
end
def target_branch
if ref_is_branch?
@ref
else
default_branch
end
end
def success(merge_request)
super().merge(merge_request: merge_request)
end

View file

@ -0,0 +1,5 @@
---
title: Fix create MR from issue using a tag as ref
merge_request: 32392
author: Jacopo Beschi @jacopo-beschi
type: fixed

View file

@ -112,6 +112,22 @@ describe MergeRequests::CreateFromIssueService do
expect(subject[:merge_request].target_branch).to eq('feature')
end
context 'when the ref is a tag' do
subject { described_class.new(project, user, ref: 'v1.0.0', **service_params).execute }
it 'sets the merge request source branch to the new issue branch' do
expect(subject[:merge_request].source_branch).to eq(issue.to_branch_name)
end
it 'creates a merge request' do
expect { subject }.to change(target_project.merge_requests, :count).by(1)
end
it 'sets the merge request target branch to the project default branch' do
expect(subject[:merge_request].target_branch).to eq(target_project.default_branch)
end
end
context 'when ref branch does not exist' do
subject { described_class.new(project, user, ref: 'no-such-branch', **service_params).execute }