Commit 9542925f authored by Fatih Acet's avatar Fatih Acet

Merge branch 'winh-notes-error-handling-ee' into 'master'

Handle errors in successful notes reply (EE-port)

Closes gitlab-ce#61377

See merge request gitlab-org/gitlab-ee!12369
parents acd23dde 4ca49a16
......@@ -268,11 +268,20 @@ export const saveNote = ({ commit, dispatch }, noteData) => {
const { errors } = res;
const commandsChanges = res.commands_changes;
if (hasQuickActions && errors && Object.keys(errors).length) {
eTagPoll.makeRequest();
if (errors && Object.keys(errors).length) {
/*
The following reply means that quick actions have been successfully applied:
$('.js-gfm-input').trigger('clear-commands-cache.atwho');
Flash(__('Commands applied'), 'notice', noteData.flashContainer);
{"commands_changes":{},"valid":false,"errors":{"commands_only":["Commands applied"]}}
*/
if (hasQuickActions) {
eTagPoll.makeRequest();
$('.js-gfm-input').trigger('clear-commands-cache.atwho');
Flash(__('Commands applied'), 'notice', noteData.flashContainer);
} else {
throw new Error(__('Failed to save comment!'));
}
}
if (commandsChanges) {
......
......@@ -2,7 +2,8 @@ import AjaxCache from '~/lib/utils/ajax_cache';
import { trimFirstCharOfLineContent } from '~/diffs/store/utils';
import { sprintf, __ } from '~/locale';
const REGEX_QUICK_ACTIONS = /^\/\w+.*$/gm;
// factory function because global flag makes RegExp stateful
const createQuickActionsRegex = () => /^\/\w+.*$/gm;
export const findNoteObjectById = (notes, id) => notes.filter(n => n.id === id)[0];
......@@ -27,9 +28,9 @@ export const getQuickActionText = note => {
return text;
};
export const hasQuickActions = note => REGEX_QUICK_ACTIONS.test(note);
export const hasQuickActions = note => createQuickActionsRegex().test(note);
export const stripQuickActions = note => note.replace(REGEX_QUICK_ACTIONS, '').trim();
export const stripQuickActions = note => note.replace(createQuickActionsRegex(), '').trim();
export const prepareDiffLines = diffLines =>
diffLines.map(line => ({ ...trimFirstCharOfLineContent(line) }));
---
title: Handle errors in successful notes reply
merge_request: 28082
author:
type: fixed
......@@ -5084,6 +5084,9 @@ msgstr ""
msgid "Failed to reset key. Please try again."
msgstr ""
msgid "Failed to save comment!"
msgstr ""
msgid "Failed to save merge conflicts resolutions. Please try again!"
msgstr ""
......
import { hasQuickActions } from '~/notes/stores/utils';
describe('hasQuickActions', () => {
it.each`
input | expected
${'some comment'} | ${false}
${'/quickaction'} | ${true}
${'some comment with\n/quickaction'} | ${true}
`('returns $expected for $input', ({ input, expected }) => {
expect(hasQuickActions(input)).toBe(expected);
});
it('is stateless', () => {
expect(hasQuickActions('some comment')).toBe(hasQuickActions('some comment'));
expect(hasQuickActions('/quickaction')).toBe(hasQuickActions('/quickaction'));
});
});
......@@ -794,6 +794,51 @@ describe('Actions Notes Store', () => {
});
});
describe('saveNote', () => {
const payload = { endpoint: TEST_HOST, data: { 'note[note]': 'some text' } };
describe('if response contains errors', () => {
const res = { errors: { something: ['went wrong'] } };
it('throws an error', done => {
actions
.saveNote(
{
commit() {},
dispatch: () => Promise.resolve(res),
},
payload,
)
.then(() => done.fail('Expected error to be thrown!'))
.catch(error => {
expect(error.message).toBe('Failed to save comment!');
})
.then(done)
.catch(done.fail);
});
});
describe('if response contains no errors', () => {
const res = { valid: true };
it('returns the response', done => {
actions
.saveNote(
{
commit() {},
dispatch: () => Promise.resolve(res),
},
payload,
)
.then(data => {
expect(data).toBe(res);
})
.then(done)
.catch(done.fail);
});
});
});
describe('submitSuggestion', () => {
const discussionId = 'discussion-id';
const noteId = 'note-id';
......
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