diff options
author | Sean McGivern <sean@mcgivern.me.uk> | 2017-11-10 14:55:39 +0000 |
---|---|---|
committer | Sean McGivern <sean@mcgivern.me.uk> | 2017-11-10 14:55:39 +0000 |
commit | f7537ce03a29b1af85461b2883c33bb41b2382d5 (patch) | |
tree | 658422c1e0fd693905073f053019ae42c69b9bdb | |
parent | 30e466b56abe595cba4b4c767f707eda351753d5 (diff) | |
parent | bddbb90fd985df50d4b17d0728ef5fa90bf64af7 (diff) | |
download | gitlab-ce-f7537ce03a29b1af85461b2883c33bb41b2382d5.tar.gz |
Merge branch 'issue_39773_be' into 'master'
Prevent fast forward merge when rebase is required
Closes #39773
See merge request gitlab-org/gitlab-ce!15296
-rw-r--r-- | app/services/merge_requests/merge_service.rb | 27 | ||||
-rw-r--r-- | changelogs/unreleased/issue_39773_be.yml | 5 | ||||
-rw-r--r-- | spec/services/merge_requests/merge_service_spec.rb | 22 |
3 files changed, 43 insertions, 11 deletions
diff --git a/app/services/merge_requests/merge_service.rb b/app/services/merge_requests/merge_service.rb index 156e7b2f078..1da4dbd9e96 100644 --- a/app/services/merge_requests/merge_service.rb +++ b/app/services/merge_requests/merge_service.rb @@ -18,15 +18,7 @@ module MergeRequests @merge_request = merge_request - unless @merge_request.mergeable? - return handle_merge_error(log_message: 'Merge request is not mergeable', save_message_on_model: true) - end - - @source = find_merge_source - - unless @source - return handle_merge_error(log_message: 'No source for merge', save_message_on_model: true) - end + error_check! merge_request.in_locked_state do if commit @@ -41,6 +33,19 @@ module MergeRequests private + def error_check! + error = + if @merge_request.should_be_rebased? + 'Only fast-forward merge is allowed for your project. Please update your source branch' + elsif !@merge_request.mergeable? + 'Merge request is not mergeable' + elsif !source + 'No source for merge' + end + + raise MergeError, error if error + end + def commit message = params[:commit_message] || merge_request.merge_commit_message @@ -91,8 +96,8 @@ module MergeRequests merge_request.to_reference(full: true) end - def find_merge_source - merge_request.diff_head_sha + def source + @source ||= @merge_request.diff_head_sha end end end diff --git a/changelogs/unreleased/issue_39773_be.yml b/changelogs/unreleased/issue_39773_be.yml new file mode 100644 index 00000000000..db30267d71f --- /dev/null +++ b/changelogs/unreleased/issue_39773_be.yml @@ -0,0 +1,5 @@ +--- +title: Show error message when fast-forward merge is not possible +merge_request: +author: +type: fixed diff --git a/spec/services/merge_requests/merge_service_spec.rb b/spec/services/merge_requests/merge_service_spec.rb index ac196e92601..f86f1ac2443 100644 --- a/spec/services/merge_requests/merge_service_spec.rb +++ b/spec/services/merge_requests/merge_service_spec.rb @@ -248,6 +248,28 @@ describe MergeRequests::MergeService do expect(merge_request.merge_error).to include(error_message) expect(Rails.logger).to have_received(:error).with(a_string_matching(error_message)) end + + context "when fast-forward merge is not allowed" do + before do + allow_any_instance_of(Repository).to receive(:ancestor?).and_return(nil) + end + + %w(semi-linear ff).each do |merge_method| + it "logs and saves error if merge is #{merge_method} only" do + merge_method = 'rebase_merge' if merge_method == 'semi-linear' + merge_request.project.update(merge_method: merge_method) + error_message = 'Only fast-forward merge is allowed for your project. Please update your source branch' + allow(service).to receive(:execute_hooks) + + service.execute(merge_request) + + expect(merge_request).to be_open + expect(merge_request.merge_commit_sha).to be_nil + expect(merge_request.merge_error).to include(error_message) + expect(Rails.logger).to have_received(:error).with(a_string_matching(error_message)) + end + end + end end end end |