Commit 210fbf5b authored by Paul Slaughter's avatar Paul Slaughter

Merge branch '36523-get-files-code-smell' into 'master'

Resolve code smells in `getFiles` in Web IDE `ide/stores/actions/tree.js`

See merge request gitlab-org/gitlab!21805
parents 7dc386cc d673dfdf
...@@ -141,7 +141,7 @@ export const getMergeRequestVersions = ( ...@@ -141,7 +141,7 @@ export const getMergeRequestVersions = (
}); });
export const openMergeRequest = ( export const openMergeRequest = (
{ dispatch, state }, { dispatch, state, getters },
{ projectId, targetProjectId, mergeRequestId } = {}, { projectId, targetProjectId, mergeRequestId } = {},
) => ) =>
dispatch('getMergeRequestData', { dispatch('getMergeRequestData', {
...@@ -152,17 +152,18 @@ export const openMergeRequest = ( ...@@ -152,17 +152,18 @@ export const openMergeRequest = (
.then(mr => { .then(mr => {
dispatch('setCurrentBranchId', mr.source_branch); dispatch('setCurrentBranchId', mr.source_branch);
// getFiles needs to be called after getting the branch data
// since files are fetched using the last commit sha of the branch
return dispatch('getBranchData', { return dispatch('getBranchData', {
projectId, projectId,
branchId: mr.source_branch, branchId: mr.source_branch,
}).then(() => }).then(() => {
dispatch('getFiles', { const branch = getters.findBranch(projectId, mr.source_branch);
return dispatch('getFiles', {
projectId, projectId,
branchId: mr.source_branch, branchId: mr.source_branch,
}), ref: branch.commit.id,
); });
});
}) })
.then(() => .then(() =>
dispatch('getMergeRequestVersions', { dispatch('getMergeRequestVersions', {
......
...@@ -111,7 +111,7 @@ export const loadFile = ({ dispatch, state }, { basePath }) => { ...@@ -111,7 +111,7 @@ export const loadFile = ({ dispatch, state }, { basePath }) => {
} }
}; };
export const loadBranch = ({ dispatch }, { projectId, branchId }) => export const loadBranch = ({ dispatch, getters }, { projectId, branchId }) =>
dispatch('getBranchData', { dispatch('getBranchData', {
projectId, projectId,
branchId, branchId,
...@@ -121,9 +121,13 @@ export const loadBranch = ({ dispatch }, { projectId, branchId }) => ...@@ -121,9 +121,13 @@ export const loadBranch = ({ dispatch }, { projectId, branchId }) =>
projectId, projectId,
branchId, branchId,
}); });
const branch = getters.findBranch(projectId, branchId);
return dispatch('getFiles', { return dispatch('getFiles', {
projectId, projectId,
branchId, branchId,
ref: branch.commit.id,
}); });
}) })
.catch(() => { .catch(() => {
......
...@@ -46,19 +46,20 @@ export const setDirectoryData = ({ state, commit }, { projectId, branchId, treeL ...@@ -46,19 +46,20 @@ export const setDirectoryData = ({ state, commit }, { projectId, branchId, treeL
}); });
}; };
export const getFiles = ({ state, commit, dispatch, getters }, { projectId, branchId } = {}) => export const getFiles = ({ state, commit, dispatch }, payload = {}) =>
new Promise((resolve, reject) => { new Promise((resolve, reject) => {
const { projectId, branchId, ref = branchId } = payload;
if ( if (
!state.trees[`${projectId}/${branchId}`] || !state.trees[`${projectId}/${branchId}`] ||
(state.trees[`${projectId}/${branchId}`].tree && (state.trees[`${projectId}/${branchId}`].tree &&
state.trees[`${projectId}/${branchId}`].tree.length === 0) state.trees[`${projectId}/${branchId}`].tree.length === 0)
) { ) {
const selectedProject = state.projects[projectId]; const selectedProject = state.projects[projectId];
const selectedBranch = getters.findBranch(projectId, branchId);
commit(types.CREATE_TREE, { treePath: `${projectId}/${branchId}` }); commit(types.CREATE_TREE, { treePath: `${projectId}/${branchId}` });
service service
.getFiles(selectedProject.web_url, selectedBranch.commit.id) .getFiles(selectedProject.web_url, ref)
.then(({ data }) => { .then(({ data }) => {
const { entries, treeList } = decorateFiles({ const { entries, treeList } = decorateFiles({
data, data,
...@@ -77,8 +78,8 @@ export const getFiles = ({ state, commit, dispatch, getters }, { projectId, bran ...@@ -77,8 +78,8 @@ export const getFiles = ({ state, commit, dispatch, getters }, { projectId, bran
.catch(e => { .catch(e => {
dispatch('setErrorMessage', { dispatch('setErrorMessage', {
text: __('An error occurred whilst loading all the files.'), text: __('An error occurred whilst loading all the files.'),
action: payload => action: actionPayload =>
dispatch('getFiles', payload).then(() => dispatch('setErrorMessage', null)), dispatch('getFiles', actionPayload).then(() => dispatch('setErrorMessage', null)),
actionText: __('Please try again'), actionText: __('Please try again'),
actionPayload: { projectId, branchId }, actionPayload: { projectId, branchId },
}); });
......
...@@ -348,6 +348,8 @@ describe('IDE store merge request actions', () => { ...@@ -348,6 +348,8 @@ describe('IDE store merge request actions', () => {
let testMergeRequest; let testMergeRequest;
let testMergeRequestChanges; let testMergeRequestChanges;
const mockGetters = { findBranch: () => ({ commit: { id: 'abcd2322' } }) };
beforeEach(() => { beforeEach(() => {
testMergeRequest = { testMergeRequest = {
source_branch: 'abcbranch', source_branch: 'abcbranch',
...@@ -406,8 +408,8 @@ describe('IDE store merge request actions', () => { ...@@ -406,8 +408,8 @@ describe('IDE store merge request actions', () => {
); );
}); });
it('dispatch actions for merge request data', done => { it('dispatches actions for merge request data', done => {
openMergeRequest(store, mr) openMergeRequest({ state: store.state, dispatch: store.dispatch, getters: mockGetters }, mr)
.then(() => { .then(() => {
expect(store.dispatch.calls.allArgs()).toEqual([ expect(store.dispatch.calls.allArgs()).toEqual([
['getMergeRequestData', mr], ['getMergeRequestData', mr],
...@@ -424,6 +426,7 @@ describe('IDE store merge request actions', () => { ...@@ -424,6 +426,7 @@ describe('IDE store merge request actions', () => {
{ {
projectId: mr.projectId, projectId: mr.projectId,
branchId: testMergeRequest.source_branch, branchId: testMergeRequest.source_branch,
ref: 'abcd2322',
}, },
], ],
['getMergeRequestVersions', mr], ['getMergeRequestVersions', mr],
...@@ -449,7 +452,7 @@ describe('IDE store merge request actions', () => { ...@@ -449,7 +452,7 @@ describe('IDE store merge request actions', () => {
{ new_path: 'bar', path: 'bar' }, { new_path: 'bar', path: 'bar' },
]; ];
openMergeRequest(store, mr) openMergeRequest({ state: store.state, dispatch: store.dispatch, getters: mockGetters }, mr)
.then(() => { .then(() => {
expect(store.dispatch).toHaveBeenCalledWith( expect(store.dispatch).toHaveBeenCalledWith(
'updateActivityBarView', 'updateActivityBarView',
......
...@@ -285,16 +285,21 @@ describe('IDE store project actions', () => { ...@@ -285,16 +285,21 @@ describe('IDE store project actions', () => {
describe('loadBranch', () => { describe('loadBranch', () => {
const projectId = 'abc/def'; const projectId = 'abc/def';
const branchId = '123-lorem'; const branchId = '123-lorem';
const ref = 'abcd2322';
it('fetches branch data', done => { it('fetches branch data', done => {
const mockGetters = { findBranch: () => ({ commit: { id: ref } }) };
spyOn(store, 'dispatch').and.returnValue(Promise.resolve()); spyOn(store, 'dispatch').and.returnValue(Promise.resolve());
loadBranch(store, { projectId, branchId }) loadBranch(
{ getters: mockGetters, state: store.state, dispatch: store.dispatch },
{ projectId, branchId },
)
.then(() => { .then(() => {
expect(store.dispatch.calls.allArgs()).toEqual([ expect(store.dispatch.calls.allArgs()).toEqual([
['getBranchData', { projectId, branchId }], ['getBranchData', { projectId, branchId }],
['getMergeRequestsForBranch', { projectId, branchId }], ['getMergeRequestsForBranch', { projectId, branchId }],
['getFiles', { projectId, branchId }], ['getFiles', { projectId, branchId, ref }],
]); ]);
}) })
.then(done) .then(done)
......
...@@ -17,6 +17,7 @@ describe('Multi-file store tree actions', () => { ...@@ -17,6 +17,7 @@ describe('Multi-file store tree actions', () => {
projectId: 'abcproject', projectId: 'abcproject',
branch: 'master', branch: 'master',
branchId: 'master', branchId: 'master',
ref: '12345678',
}; };
beforeEach(() => { beforeEach(() => {
...@@ -29,14 +30,6 @@ describe('Multi-file store tree actions', () => { ...@@ -29,14 +30,6 @@ describe('Multi-file store tree actions', () => {
store.state.currentBranchId = 'master'; store.state.currentBranchId = 'master';
store.state.projects.abcproject = { store.state.projects.abcproject = {
web_url: '', web_url: '',
branches: {
master: {
workingReference: '12345678',
commit: {
id: '12345678',
},
},
},
}; };
}); });
......
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