Commit 69aaa30d authored by Filipa Lacerda's avatar Filipa Lacerda

Merge branch '22548-reopen-error-message' into 'master'

Improve error message when attempting to reopen MR and there's a new open MR for the same branch

Closes #22548

See merge request gitlab-org/gitlab-ce!22326
parents b081614b 1e3881b0
...@@ -247,15 +247,19 @@ Please check your network connection and try again.`; ...@@ -247,15 +247,19 @@ Please check your network connection and try again.`;
} else { } else {
this.reopenIssue() this.reopenIssue()
.then(() => this.enableButton()) .then(() => this.enableButton())
.catch(() => { .catch(({ data }) => {
this.enableButton(); this.enableButton();
this.toggleStateButtonLoading(false); this.toggleStateButtonLoading(false);
Flash( let errorMessage = sprintf(
sprintf( __('Something went wrong while reopening the %{issuable}. Please try again later'),
__('Something went wrong while reopening the %{issuable}. Please try again later'), { issuable: this.noteableDisplayName },
{ issuable: this.noteableDisplayName },
),
); );
if (data) {
errorMessage = Object.values(data).join('\n');
}
Flash(errorMessage);
}); });
} }
}, },
......
...@@ -122,17 +122,21 @@ class Projects::MergeRequestsController < Projects::MergeRequests::ApplicationCo ...@@ -122,17 +122,21 @@ class Projects::MergeRequestsController < Projects::MergeRequests::ApplicationCo
respond_to do |format| respond_to do |format|
format.html do format.html do
if @merge_request.valid? if @merge_request.errors.present?
redirect_to([@merge_request.target_project.namespace.becomes(Namespace), @merge_request.target_project, @merge_request])
else
define_edit_vars define_edit_vars
render :edit render :edit
else
redirect_to project_merge_request_path(@merge_request.target_project, @merge_request)
end end
end end
format.json do format.json do
render json: serializer.represent(@merge_request, serializer: 'basic') if merge_request.errors.present?
render json: @merge_request.errors, status: :bad_request
else
render json: serializer.represent(@merge_request, serializer: 'basic')
end
end end
end end
rescue ActiveRecord::StaleObjectError rescue ActiveRecord::StaleObjectError
......
...@@ -539,15 +539,26 @@ class MergeRequest < ActiveRecord::Base ...@@ -539,15 +539,26 @@ class MergeRequest < ActiveRecord::Base
def validate_branches def validate_branches
if target_project == source_project && target_branch == source_branch if target_project == source_project && target_branch == source_branch
errors.add :branch_conflict, "You can not use same project/branch for source and target" errors.add :branch_conflict, "You can't use same project/branch for source and target"
return
end end
if opened? if opened?
similar_mrs = self.target_project.merge_requests.where(source_branch: source_branch, target_branch: target_branch, source_project_id: source_project.try(:id)).opened similar_mrs = target_project
similar_mrs = similar_mrs.where('id not in (?)', self.id) if self.id .merge_requests
if similar_mrs.any? .where(source_branch: source_branch, target_branch: target_branch)
errors.add :validate_branches, .where(source_project_id: source_project&.id)
"Cannot Create: This merge request already exists: #{similar_mrs.pluck(:title)}" .opened
similar_mrs = similar_mrs.where.not(id: id) if persisted?
conflict = similar_mrs.first
if conflict.present?
errors.add(
:validate_branches,
"Another open merge request already exists for this source branch: #{conflict.to_reference}"
)
end end
end end
end end
......
---
title: Show error message when attempting to reopen an MR and there is an open MR
for the same branch
merge_request: 16447
author: Akos Gyimesi
type: fixed
...@@ -290,6 +290,20 @@ describe Projects::MergeRequestsController do ...@@ -290,6 +290,20 @@ describe Projects::MergeRequestsController do
it_behaves_like 'update invalid issuable', MergeRequest it_behaves_like 'update invalid issuable', MergeRequest
end end
context 'two merge requests with the same source branch' do
it 'does not allow a closed merge request to be reopened if another one is open' do
merge_request.close!
create(:merge_request, source_project: merge_request.source_project, source_branch: merge_request.source_branch)
update_merge_request(state_event: 'reopen')
errors = assigns[:merge_request].errors
expect(errors[:validate_branches]).to include(/Another open merge request already exists for this source branch/)
expect(merge_request.reload).to be_closed
end
end
end end
describe 'POST merge' do describe 'POST merge' do
......
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