Commit 58de7d8f authored by 🙈  jacopo beschi 🙉's avatar 🙈 jacopo beschi 🙉 Committed by Rémy Coutable

Fix create MR from issue using a tag as ref

Creating a MR from an issue using a tag as ref correctly creates the
source branch starting from the tag.
parent 5abf3186
......@@ -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
......
---
title: Fix create MR from issue using a tag as ref
merge_request: 32392
author: Jacopo Beschi @jacopo-beschi
type: fixed
......@@ -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 }
......
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