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

153 lines
4.6 KiB
JavaScript
Raw Normal View History

2018-11-08 19:23:39 +05:30
import _ from 'underscore';
2018-05-09 12:01:36 +05:30
import flash from '~/flash';
2018-11-08 19:23:39 +05:30
import { __, sprintf } from '~/locale';
2018-05-09 12:01:36 +05:30
import service from '../../services';
2018-11-08 19:23:39 +05:30
import api from '../../../api';
2018-05-09 12:01:36 +05:30
import * as types from '../mutation_types';
2018-11-08 19:23:39 +05:30
import router from '../../ide_router';
2018-05-09 12:01:36 +05:30
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)
.then(res => res.data)
.then(data => {
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(() => {
flash(
2018-11-08 19:23:39 +05:30
__('Error loading project data. Please try again.'),
2018-10-15 14:42:47 +05:30
'alert',
document,
null,
false,
true,
);
reject(new Error(`Project not loaded ${namespace}/${projectId}`));
});
} else {
resolve(state.projects[`${namespace}/${projectId}`]);
}
});
2018-05-09 12:01:36 +05:30
export const getBranchData = (
2018-11-08 19:23:39 +05:30
{ commit, dispatch, state },
2018-05-09 12:01:36 +05:30
{ projectId, branchId, force = false } = {},
2018-10-15 14:42:47 +05:30
) =>
new Promise((resolve, reject) => {
if (
typeof state.projects[`${projectId}`] === 'undefined' ||
!state.projects[`${projectId}`].branches[branchId] ||
force
) {
service
.getBranchData(`${projectId}`, branchId)
.then(({ data }) => {
const { id } = data.commit;
commit(types.SET_BRANCH, {
projectPath: `${projectId}`,
branchName: branchId,
branch: data,
});
commit(types.SET_BRANCH_WORKING_REFERENCE, { projectId, branchId, reference: id });
resolve(data);
})
2018-11-08 19:23:39 +05:30
.catch(e => {
if (e.response.status === 404) {
dispatch('showBranchNotFoundError', branchId);
} else {
flash(
__('Error loading branch data. Please try again.'),
'alert',
document,
null,
false,
true,
);
}
2018-10-15 14:42:47 +05:30
reject(new Error(`Branch not loaded - ${projectId}/${branchId}`));
});
} else {
resolve(state.projects[`${projectId}`].branches[branchId]);
}
});
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,
});
})
.catch(() => {
flash(__('Error loading last commit.'), 'alert', document, null, false, true);
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);
router.push(`${router.currentRoute.path}?${Date.now()}`);
})
.catch(() => {
dispatch('setErrorMessage', {
text: __('An error occured creating the new branch.'),
action: payload => dispatch('createNewBranchFromDefault', payload),
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."),
{
branchName: `<strong>${_.escape(branchId)}</strong>`,
},
2018-10-15 14:42:47 +05:30
false,
2018-11-08 19:23:39 +05:30
),
action: payload => dispatch('createNewBranchFromDefault', payload),
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
2018-12-13 13:39:08 +05:30
export const openBranch = ({ dispatch, state }, { projectId, branchId, basePath }) => {
2018-11-20 20:47:30 +05:30
dispatch('setCurrentBranchId', branchId);
dispatch('getBranchData', {
projectId,
branchId,
});
2018-12-13 13:39:08 +05:30
return dispatch('getFiles', {
projectId,
branchId,
}).then(() => {
if (basePath) {
const path = basePath.slice(-1) === '/' ? basePath.slice(0, -1) : basePath;
const treeEntryKey = Object.keys(state.entries).find(
key => key === path && !state.entries[key].pending,
);
const treeEntry = state.entries[treeEntryKey];
2018-11-20 20:47:30 +05:30
2018-12-13 13:39:08 +05:30
if (treeEntry) {
dispatch('handleTreeEntryAction', treeEntry);
2018-11-20 20:47:30 +05:30
}
2018-12-13 13:39:08 +05:30
}
});
2018-11-20 20:47:30 +05:30
};