summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author🙈 jacopo beschi 🙉 <intrip@gmail.com>2019-09-12 10:23:24 +0000
committerRémy Coutable <remy@rymai.me>2019-09-12 10:23:24 +0000
commit58de7d8f485d34861b077575da29a7cc1ba49bc1 (patch)
tree0364a41e6dfdc806265536bbac913f8053be199a
parent5abf3186d408752901621aa4fb7054396128c8ce (diff)
downloadgitlab-ce-58de7d8f485d34861b077575da29a7cc1ba49bc1.tar.gz
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.
-rw-r--r--app/services/merge_requests/create_from_issue_service.rb30
-rw-r--r--changelogs/unreleased/50847-automatically-created-mr-uses-wrong-source-branch-when-branching-fr.yml5
-rw-r--r--spec/services/merge_requests/create_from_issue_service_spec.rb16
3 files changed, 49 insertions, 2 deletions
diff --git a/app/services/merge_requests/create_from_issue_service.rb b/app/services/merge_requests/create_from_issue_service.rb
index 2a217a6f689..200a34cae04 100644
--- a/app/services/merge_requests/create_from_issue_service.rb
+++ b/app/services/merge_requests/create_from_issue_service.rb
@@ -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
diff --git a/changelogs/unreleased/50847-automatically-created-mr-uses-wrong-source-branch-when-branching-fr.yml b/changelogs/unreleased/50847-automatically-created-mr-uses-wrong-source-branch-when-branching-fr.yml
new file mode 100644
index 00000000000..85582880923
--- /dev/null
+++ b/changelogs/unreleased/50847-automatically-created-mr-uses-wrong-source-branch-when-branching-fr.yml
@@ -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
diff --git a/spec/services/merge_requests/create_from_issue_service_spec.rb b/spec/services/merge_requests/create_from_issue_service_spec.rb
index 0e0da6a13ab..07e0218e1df 100644
--- a/spec/services/merge_requests/create_from_issue_service_spec.rb
+++ b/spec/services/merge_requests/create_from_issue_service_spec.rb
@@ -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 }