summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSean McGivern <sean@mcgivern.me.uk>2017-02-22 10:13:33 +0000
committerSean McGivern <sean@mcgivern.me.uk>2017-02-22 10:13:33 +0000
commit7bf28a4adaabac7b974ef7d829e604d77eb9d9df (patch)
treef0d7abf8571a9c382b2ea8f735e3b8fc35c6becf
parent7e8a6a63af2138c5f3f2030ef8c65e8d4815eed3 (diff)
parent31409030d67858d21281fc72b654246b32f7c6eb (diff)
downloadgitlab-ce-7bf28a4adaabac7b974ef7d829e604d77eb9d9df.tar.gz
Merge branch 'issue_28051_2' into 'master'
Create compare object with default branch when target branch is missing Closes #28051 See merge request !9263
-rw-r--r--app/services/merge_requests/build_service.rb26
-rw-r--r--changelogs/unreleased/issue_28051_2.yml4
-rw-r--r--spec/services/merge_requests/build_service_spec.rb13
3 files changed, 25 insertions, 18 deletions
diff --git a/app/services/merge_requests/build_service.rb b/app/services/merge_requests/build_service.rb
index f4d52e3ebbd..9d4739e37bb 100644
--- a/app/services/merge_requests/build_service.rb
+++ b/app/services/merge_requests/build_service.rb
@@ -2,18 +2,14 @@ module MergeRequests
class BuildService < MergeRequests::BaseService
def execute
self.merge_request = MergeRequest.new(params)
- merge_request.can_be_created = true
merge_request.compare_commits = []
merge_request.source_project = find_source_project
merge_request.target_project = find_target_project
merge_request.target_branch = find_target_branch
+ merge_request.can_be_created = branches_valid? && source_branch_specified? && target_branch_specified?
- if branches_specified? && branches_valid?
- compare_branches
- assign_title_and_description
- else
- merge_request.can_be_created = false
- end
+ compare_branches if branches_present?
+ assign_title_and_description if merge_request.can_be_created
merge_request
end
@@ -37,11 +33,17 @@ module MergeRequests
target_branch || target_project.default_branch
end
- def branches_specified?
- params[:source_branch] && params[:target_branch]
+ def source_branch_specified?
+ params[:source_branch].present?
+ end
+
+ def target_branch_specified?
+ params[:target_branch].present?
end
def branches_valid?
+ return false unless source_branch_specified? || target_branch_specified?
+
validate_branches
errors.blank?
end
@@ -55,8 +57,10 @@ module MergeRequests
target_branch
)
- merge_request.compare_commits = compare.commits
- merge_request.compare = compare
+ if compare
+ merge_request.compare_commits = compare.commits
+ merge_request.compare = compare
+ end
end
def validate_branches
diff --git a/changelogs/unreleased/issue_28051_2.yml b/changelogs/unreleased/issue_28051_2.yml
new file mode 100644
index 00000000000..8cc32ad8493
--- /dev/null
+++ b/changelogs/unreleased/issue_28051_2.yml
@@ -0,0 +1,4 @@
+---
+title: Use default branch as target_branch when parameter is missing
+merge_request:
+author:
diff --git a/spec/services/merge_requests/build_service_spec.rb b/spec/services/merge_requests/build_service_spec.rb
index dc945ca4868..0768f644036 100644
--- a/spec/services/merge_requests/build_service_spec.rb
+++ b/spec/services/merge_requests/build_service_spec.rb
@@ -44,15 +44,14 @@ describe MergeRequests::BuildService, services: true do
end
end
- context 'missing target branch' do
- let(:target_branch) { '' }
+ context 'when target branch is missing' do
+ let(:target_branch) { nil }
+ let(:commits) { Commit.decorate([commit_1], project) }
- it 'forbids the merge request from being created' do
+ it 'creates compare object with target branch as default branch' do
expect(merge_request.can_be_created).to eq(false)
- end
-
- it 'adds an error message to the merge request' do
- expect(merge_request.errors).to contain_exactly('You must select source and target branch')
+ expect(merge_request.compare).to be_present
+ expect(merge_request.target_branch).to eq(project.default_branch)
end
end