2020-11-24 15:15:51 +05:30
|
|
|
import { escape } from 'lodash';
|
|
|
|
import { __ } from '~/locale';
|
2021-03-11 19:13:27 +05:30
|
|
|
import { COMMIT_TO_NEW_BRANCH } 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-03-08 18:12:59 +05:30
|
|
|
const createNewBranchAndCommit = (store) =>
|
2021-01-03 14:25:43 +05:30
|
|
|
store
|
2021-03-11 19:13:27 +05:30
|
|
|
.dispatch('commit/updateCommitAction', COMMIT_TO_NEW_BRANCH)
|
2021-01-03 14:25:43 +05:30
|
|
|
.then(() => store.dispatch('commit/commitChanges'));
|
|
|
|
|
2021-03-08 18:12:59 +05:30
|
|
|
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
|
|
|
});
|
|
|
|
|
2021-03-08 18:12:59 +05:30
|
|
|
export const createCodeownersCommitError = (message) => ({
|
2020-11-24 15:15:51 +05:30
|
|
|
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
|
|
|
});
|
|
|
|
|
2021-03-08 18:12:59 +05:30
|
|
|
export const createBranchChangedCommitError = (message) => ({
|
2020-11-24 15:15:51 +05:30
|
|
|
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,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2021-03-08 18:12:59 +05:30
|
|
|
export const branchAlreadyExistsCommitError = (message) => ({
|
2021-01-03 14:25:43 +05:30
|
|
|
title: __('Branch already exists'),
|
|
|
|
messageHTML: `${escape(message)}<br/><br/>${__(
|
|
|
|
'Would you like to try auto-generating a branch name?',
|
|
|
|
)}`,
|
|
|
|
primaryAction: {
|
|
|
|
text: __('Create new branch'),
|
2021-03-08 18:12:59 +05:30
|
|
|
callback: (store) =>
|
2021-01-03 14:25:43 +05:30
|
|
|
store.dispatch('commit/addSuffixToBranchName').then(() => createNewBranchAndCommit(store)),
|
|
|
|
},
|
2020-11-24 15:15:51 +05:30
|
|
|
});
|
|
|
|
|
2021-03-08 18:12:59 +05:30
|
|
|
export const parseCommitError = (e) => {
|
2020-11-24 15:15:51 +05:30
|
|
|
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
|
|
|
};
|