Commit 6a41209d authored by Sean McGivern's avatar Sean McGivern

Improve error messages when squashing fails

The `find_merge_source` method was added for CE compatibility, but 'no
source for merge' is not a useful message for users. Use the actual
message returned from the squash service in this case, and a custom
exception to save that on the model.
parent 02b67f1f
...@@ -6,6 +6,8 @@ module MergeRequests ...@@ -6,6 +6,8 @@ module MergeRequests
# Executed when you do merge via GitLab UI # Executed when you do merge via GitLab UI
# #
class MergeService < MergeRequests::BaseService class MergeService < MergeRequests::BaseService
MergeError = Class.new(StandardError)
attr_reader :merge_request, :source attr_reader :merge_request, :source
def execute(merge_request) def execute(merge_request)
...@@ -38,6 +40,8 @@ module MergeRequests ...@@ -38,6 +40,8 @@ module MergeRequests
success success
end end
end end
rescue MergeError => e
log_merge_error(e.message, save_message_on_model: true)
end end
def hooks_validation_pass?(merge_request) def hooks_validation_pass?(merge_request)
...@@ -119,12 +123,11 @@ module MergeRequests ...@@ -119,12 +123,11 @@ module MergeRequests
squash_result = SquashService.new(project, current_user, params).execute(merge_request) squash_result = SquashService.new(project, current_user, params).execute(merge_request)
if squash_result[:status] == :success case squash_result[:status]
when :success
squash_result[:squash_sha] squash_result[:squash_sha]
else when :error
log_merge_error("Squashing #{merge_request_info} failed") raise MergeError, squash_result[:message]
nil
end end
end end
end end
......
...@@ -18,8 +18,7 @@ module MergeRequests ...@@ -18,8 +18,7 @@ module MergeRequests
end end
if merge_request.squash_in_progress? if merge_request.squash_in_progress?
log_error('Squash task canceled: Another squash is already in progress') return error('Squash task canceled: another squash is already in progress')
return false
end end
run_git_command( run_git_command(
......
---
title: Improve error messages when squashing fails
merge_request:
author:
...@@ -206,6 +206,36 @@ describe MergeRequests::MergeService, services: true do ...@@ -206,6 +206,36 @@ describe MergeRequests::MergeService, services: true do
expect(merge_request.merge_error).to include(error_message) expect(merge_request.merge_error).to include(error_message)
expect(Rails.logger).to have_received(:error).with(a_string_matching(error_message)) expect(Rails.logger).to have_received(:error).with(a_string_matching(error_message))
end end
context 'when squashing' do
it 'logs and saves error if there is an error when squashing' do
error_message = 'Failed to squash. Should be done manually'
allow_any_instance_of(MergeRequests::SquashService).to receive(:squash).and_return(nil)
merge_request.update(squash: true)
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
it 'logs and saves error if there is a squash in progress' do
error_message = 'another squash is already in progress'
allow_any_instance_of(MergeRequest).to receive(:squash_in_progress?).and_return(true)
merge_request.update(squash: true)
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
......
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