debian-mirror-gitlab/app/assets/javascripts/ide/stores/actions/project.js

173 lines
4.9 KiB
JavaScript
Raw Normal View History

2020-05-24 23:13:21 +05:30
import { escape } from 'lodash';
2021-09-30 23:02:18 +05:30
import createFlash from '~/flash';
2018-11-08 19:23:39 +05:30
import { __, sprintf } from '~/locale';
import api from '../../../api';
2021-03-11 19:13:27 +05:30
import service from '../../services';
2018-05-09 12:01:36 +05:30
import * as types from '../mutation_types';
2018-11-08 19:23:39 +05:30
export const getProjectData = ({ commit, state }, { namespace, projectId, force = false } = {}) =>
2018-10-15 14:42:47 +05:30
new Promise((resolve, reject) => {
if (!state.projects[`${namespace}/${projectId}`] || force) {
2018-05-09 12:01:36 +05:30
commit(types.TOGGLE_LOADING, { entry: state });
2018-10-15 14:42:47 +05:30
service
.getProjectData(namespace, projectId)
2021-03-08 18:12:59 +05:30
.then((res) => res.data)
.then((data) => {
2018-10-15 14:42:47 +05:30
commit(types.TOGGLE_LOADING, { entry: state });
commit(types.SET_PROJECT, { projectPath: `${namespace}/${projectId}`, project: data });
2018-11-08 19:23:39 +05:30
commit(types.SET_CURRENT_PROJECT, `${namespace}/${projectId}`);
2018-10-15 14:42:47 +05:30
resolve(data);
})
.catch(() => {
2021-09-30 23:02:18 +05:30
createFlash({
message: __('Error loading project data. Please try again.'),
fadeTransition: false,
addBodyClass: true,
});
2018-10-15 14:42:47 +05:30
reject(new Error(`Project not loaded ${namespace}/${projectId}`));
});
} else {
resolve(state.projects[`${namespace}/${projectId}`]);
}
});
2018-05-09 12:01:36 +05:30
2018-11-08 19:23:39 +05:30
export const refreshLastCommitData = ({ commit }, { projectId, branchId } = {}) =>
service
.getBranchData(projectId, branchId)
.then(({ data }) => {
commit(types.SET_BRANCH_COMMIT, {
projectId,
branchId,
commit: data.commit,
});
})
2021-03-08 18:12:59 +05:30
.catch((e) => {
2021-09-30 23:02:18 +05:30
createFlash({
message: __('Error loading last commit.'),
fadeTransition: false,
addBodyClass: true,
});
2021-03-08 18:12:59 +05:30
throw e;
2018-10-15 14:42:47 +05:30
});
2018-11-08 19:23:39 +05:30
export const createNewBranchFromDefault = ({ state, dispatch, getters }, branch) =>
api
.createBranch(state.currentProjectId, {
ref: getters.currentProject.default_branch,
branch,
})
.then(() => {
dispatch('setErrorMessage', null);
2020-06-23 00:09:42 +05:30
window.location.reload();
2018-11-08 19:23:39 +05:30
})
.catch(() => {
dispatch('setErrorMessage', {
2019-02-15 15:39:39 +05:30
text: __('An error occurred creating the new branch.'),
2021-03-08 18:12:59 +05:30
action: (payload) => dispatch('createNewBranchFromDefault', payload),
2018-11-08 19:23:39 +05:30
actionText: __('Please try again'),
actionPayload: branch,
});
});
export const showBranchNotFoundError = ({ dispatch }, branchId) => {
dispatch('setErrorMessage', {
text: sprintf(
__("Branch %{branchName} was not found in this project's repository."),
{
2020-05-24 23:13:21 +05:30
branchName: `<strong>${escape(branchId)}</strong>`,
2018-11-08 19:23:39 +05:30
},
2018-10-15 14:42:47 +05:30
false,
2018-11-08 19:23:39 +05:30
),
2021-03-08 18:12:59 +05:30
action: (payload) => dispatch('createNewBranchFromDefault', payload),
2018-11-08 19:23:39 +05:30
actionText: __('Create branch'),
actionPayload: branchId,
2018-10-15 14:42:47 +05:30
});
2018-11-08 19:23:39 +05:30
};
2018-11-20 20:47:30 +05:30
2020-04-22 19:07:51 +05:30
export const loadEmptyBranch = ({ commit, state }, { projectId, branchId }) => {
2019-09-04 21:01:54 +05:30
const treePath = `${projectId}/${branchId}`;
2020-04-22 19:07:51 +05:30
const currentTree = state.trees[`${projectId}/${branchId}`];
2020-03-13 15:44:24 +05:30
2020-04-22 19:07:51 +05:30
// If we already have a tree, let's not recreate an empty one
if (currentTree) {
return;
}
2020-03-13 15:44:24 +05:30
2019-09-04 21:01:54 +05:30
commit(types.CREATE_TREE, { treePath });
commit(types.TOGGLE_LOADING, {
entry: state.trees[treePath],
forceValue: false,
2018-11-20 20:47:30 +05:30
});
2019-09-04 21:01:54 +05:30
};
2018-11-20 20:47:30 +05:30
2019-12-21 20:55:43 +05:30
export const loadFile = ({ dispatch, state }, { basePath }) => {
if (basePath) {
const path = basePath.slice(-1) === '/' ? basePath.slice(0, -1) : basePath;
const treeEntryKey = Object.keys(state.entries).find(
2021-03-08 18:12:59 +05:30
(key) => key === path && !state.entries[key].pending,
2019-12-21 20:55:43 +05:30
);
const treeEntry = state.entries[treeEntryKey];
2019-09-04 21:01:54 +05:30
2019-12-21 20:55:43 +05:30
if (treeEntry) {
dispatch('handleTreeEntryAction', treeEntry);
} else {
dispatch('createTempEntry', {
name: path,
type: 'blob',
});
}
2019-09-04 21:01:54 +05:30
}
2019-12-21 20:55:43 +05:30
};
2020-04-22 19:07:51 +05:30
export const loadBranch = ({ dispatch, getters, state }, { projectId, branchId }) => {
const currentProject = state.projects[projectId];
if (currentProject?.branches?.[branchId]) {
return Promise.resolve();
} else if (getters.emptyRepo) {
return dispatch('loadEmptyBranch', { projectId, branchId });
}
return dispatch('getBranchData', {
2018-12-13 13:39:08 +05:30
projectId,
branchId,
2019-07-07 11:18:12 +05:30
})
.then(() => {
dispatch('getMergeRequestsForBranch', {
projectId,
branchId,
});
2020-03-13 15:44:24 +05:30
const branch = getters.findBranch(projectId, branchId);
2019-12-21 20:55:43 +05:30
return dispatch('getFiles', {
2019-09-04 21:01:54 +05:30
projectId,
branchId,
2020-03-13 15:44:24 +05:30
ref: branch.commit.id,
2019-12-21 20:55:43 +05:30
});
2019-09-04 21:01:54 +05:30
})
2021-03-08 18:12:59 +05:30
.catch((err) => {
2019-09-04 21:01:54 +05:30
dispatch('showBranchNotFoundError', branchId);
2020-03-13 15:44:24 +05:30
throw err;
2019-07-07 11:18:12 +05:30
});
2020-04-22 19:07:51 +05:30
};
2019-12-21 20:55:43 +05:30
2020-04-22 19:07:51 +05:30
export const openBranch = ({ dispatch }, { projectId, branchId, basePath }) => {
dispatch('setCurrentBranchId', branchId);
return dispatch('loadBranch', { projectId, branchId })
.then(() => dispatch('loadFile', { basePath }))
.catch(
() =>
new Error(
sprintf(
__('An error occurred while getting files for - %{branchId}'),
{
2020-05-24 23:13:21 +05:30
branchId: `<strong>${escape(projectId)}/${escape(branchId)}</strong>`,
2020-04-22 19:07:51 +05:30
},
false,
2019-12-21 20:55:43 +05:30
),
2020-04-22 19:07:51 +05:30
),
);
2018-11-20 20:47:30 +05:30
};