Commit 1b93b3b6 authored by Oswaldo Ferreira's avatar Oswaldo Ferreira

Adjusts suggestions unable to be applied

1. Presents the system error message when available,
when applying suggestions

2. Adjusts target branch updates affecting MRs pointing
to it when there are previously created suggestions in
the MR. We just need to compare the HEAD shas from
position and MR source branch.
parent dc609187
...@@ -415,12 +415,13 @@ export const submitSuggestion = ( ...@@ -415,12 +415,13 @@ export const submitSuggestion = (
commit(types.APPLY_SUGGESTION, { discussionId, noteId, suggestionId }); commit(types.APPLY_SUGGESTION, { discussionId, noteId, suggestionId });
callback(); callback();
}) })
.catch(() => { .catch(err => {
Flash( const defaultMessage = __(
__('Something went wrong while applying the suggestion. Please try again.'), 'Something went wrong while applying the suggestion. Please try again.',
'alert',
flashContainer,
); );
const flashMessage = err.response.data ? `${err.response.data.message}.` : defaultMessage;
Flash(__(flashMessage), 'alert', flashContainer);
callback(); callback();
}); });
}; };
......
...@@ -11,7 +11,7 @@ module Suggestions ...@@ -11,7 +11,7 @@ module Suggestions
return error('Suggestion is not appliable') return error('Suggestion is not appliable')
end end
unless latest_diff_refs?(suggestion) unless latest_source_head?(suggestion)
return error('The file has been changed') return error('The file has been changed')
end end
...@@ -29,12 +29,13 @@ module Suggestions ...@@ -29,12 +29,13 @@ module Suggestions
private private
# Checks whether the latest diff refs for the branch matches with # Checks whether the latest source branch HEAD matches with
# the position refs we're using to update the file content. Since # the position HEAD we're using to update the file content. Since
# the persisted refs are updated async (for MergeRequest), # the persisted HEAD is updated async (for MergeRequest),
# it's more consistent to fetch this data directly from the repository. # it's more consistent to fetch this data directly from the
def latest_diff_refs?(suggestion) # repository.
suggestion.position.diff_refs == suggestion.noteable.repository_diff_refs def latest_source_head?(suggestion)
suggestion.position.head_sha == suggestion.noteable.source_branch_sha
end end
def file_update_params(suggestion) def file_update_params(suggestion)
......
---
title: Adjusts suggestions unable to be applied
merge_request: 24603
author:
type: fixed
...@@ -134,12 +134,11 @@ describe Suggestions::ApplyService do ...@@ -134,12 +134,11 @@ describe Suggestions::ApplyService do
end end
end end
context 'when diff ref from position is different from repo diff refs' do context 'when HEAD from position is different from source branch HEAD on repo' do
it 'returns error message' do it 'returns error message' do
outdated_refs = Gitlab::Diff::DiffRefs.new(base_sha: 'foo', start_sha: 'bar', head_sha: 'outdated')
allow(suggestion).to receive(:appliable?) { true } allow(suggestion).to receive(:appliable?) { true }
allow(suggestion.position).to receive(:diff_refs) { outdated_refs } allow(suggestion.position).to receive(:head_sha) { 'old-sha' }
allow(suggestion.noteable).to receive(:source_branch_sha) { 'new-sha' }
result = subject.execute(suggestion) result = subject.execute(suggestion)
......
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