2020-11-24 15:15:51 +05:30
|
|
|
import { escape } from 'lodash';
|
|
|
|
import { __ } from '~/locale';
|
2021-01-03 14:25:43 +05:30
|
|
|
import consts from '../stores/modules/commit/constants';
|
2020-11-24 15:15:51 +05:30
|
|
|
|
|
|
|
const CODEOWNERS_REGEX = /Push.*protected branches.*CODEOWNERS/;
|
|
|
|
const BRANCH_CHANGED_REGEX = /changed.*since.*start.*edit/;
|
2021-01-03 14:25:43 +05:30
|
|
|
const BRANCH_ALREADY_EXISTS = /branch.*already.*exists/;
|
2020-11-24 15:15:51 +05:30
|
|
|
|
2021-01-03 14:25:43 +05:30
|
|
|
const createNewBranchAndCommit = store =>
|
|
|
|
store
|
|
|
|
.dispatch('commit/updateCommitAction', consts.COMMIT_TO_NEW_BRANCH)
|
|
|
|
.then(() => store.dispatch('commit/commitChanges'));
|
|
|
|
|
|
|
|
export const createUnexpectedCommitError = message => ({
|
2020-11-24 15:15:51 +05:30
|
|
|
title: __('Unexpected error'),
|
2021-01-03 14:25:43 +05:30
|
|
|
messageHTML: escape(message) || __('Could not commit. An unexpected error occurred.'),
|
2020-11-24 15:15:51 +05:30
|
|
|
});
|
|
|
|
|
|
|
|
export const createCodeownersCommitError = message => ({
|
|
|
|
title: __('CODEOWNERS rule violation'),
|
|
|
|
messageHTML: escape(message),
|
2021-01-03 14:25:43 +05:30
|
|
|
primaryAction: {
|
|
|
|
text: __('Create new branch'),
|
|
|
|
callback: createNewBranchAndCommit,
|
|
|
|
},
|
2020-11-24 15:15:51 +05:30
|
|
|
});
|
|
|
|
|
|
|
|
export const createBranchChangedCommitError = message => ({
|
|
|
|
title: __('Branch changed'),
|
|
|
|
messageHTML: `${escape(message)}<br/><br/>${__('Would you like to create a new branch?')}`,
|
2021-01-03 14:25:43 +05:30
|
|
|
primaryAction: {
|
|
|
|
text: __('Create new branch'),
|
|
|
|
callback: createNewBranchAndCommit,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
export const branchAlreadyExistsCommitError = message => ({
|
|
|
|
title: __('Branch already exists'),
|
|
|
|
messageHTML: `${escape(message)}<br/><br/>${__(
|
|
|
|
'Would you like to try auto-generating a branch name?',
|
|
|
|
)}`,
|
|
|
|
primaryAction: {
|
|
|
|
text: __('Create new branch'),
|
|
|
|
callback: store =>
|
|
|
|
store.dispatch('commit/addSuffixToBranchName').then(() => createNewBranchAndCommit(store)),
|
|
|
|
},
|
2020-11-24 15:15:51 +05:30
|
|
|
});
|
|
|
|
|
|
|
|
export const parseCommitError = e => {
|
|
|
|
const { message } = e?.response?.data || {};
|
|
|
|
|
|
|
|
if (!message) {
|
|
|
|
return createUnexpectedCommitError();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (CODEOWNERS_REGEX.test(message)) {
|
|
|
|
return createCodeownersCommitError(message);
|
|
|
|
} else if (BRANCH_CHANGED_REGEX.test(message)) {
|
|
|
|
return createBranchChangedCommitError(message);
|
2021-01-03 14:25:43 +05:30
|
|
|
} else if (BRANCH_ALREADY_EXISTS.test(message)) {
|
|
|
|
return branchAlreadyExistsCommitError(message);
|
2020-11-24 15:15:51 +05:30
|
|
|
}
|
|
|
|
|
2021-01-03 14:25:43 +05:30
|
|
|
return createUnexpectedCommitError(message);
|
2020-11-24 15:15:51 +05:30
|
|
|
};
|