Commit e906be2f authored by Phil Hughes's avatar Phil Hughes

IDE sends last commit ID when committing changes

Closes #46192
parent f39582f7
...@@ -49,31 +49,6 @@ export const setLastCommitMessage = ({ rootState, commit }, data) => { ...@@ -49,31 +49,6 @@ export const setLastCommitMessage = ({ rootState, commit }, data) => {
commit(rootTypes.SET_LAST_COMMIT_MSG, commitMsg, { root: true }); commit(rootTypes.SET_LAST_COMMIT_MSG, commitMsg, { root: true });
}; };
export const checkCommitStatus = ({ rootState }) =>
service
.getBranchData(rootState.currentProjectId, rootState.currentBranchId)
.then(({ data }) => {
const { id } = data.commit;
const selectedBranch =
rootState.projects[rootState.currentProjectId].branches[rootState.currentBranchId];
if (selectedBranch.workingReference !== id) {
return true;
}
return false;
})
.catch(() =>
flash(
__('Error checking branch data. Please try again.'),
'alert',
document,
null,
false,
true,
),
);
export const updateFilesAfterCommit = ({ commit, dispatch, rootState }, { data }) => { export const updateFilesAfterCommit = ({ commit, dispatch, rootState }, { data }) => {
const selectedProject = rootState.projects[rootState.currentProjectId]; const selectedProject = rootState.projects[rootState.currentProjectId];
const lastCommit = { const lastCommit = {
...@@ -128,24 +103,17 @@ export const updateFilesAfterCommit = ({ commit, dispatch, rootState }, { data } ...@@ -128,24 +103,17 @@ export const updateFilesAfterCommit = ({ commit, dispatch, rootState }, { data }
export const commitChanges = ({ commit, state, getters, dispatch, rootState, rootGetters }) => { export const commitChanges = ({ commit, state, getters, dispatch, rootState, rootGetters }) => {
const newBranch = state.commitAction !== consts.COMMIT_TO_CURRENT_BRANCH; const newBranch = state.commitAction !== consts.COMMIT_TO_CURRENT_BRANCH;
const payload = createCommitPayload(getters.branchName, newBranch, state, rootState); const payload = createCommitPayload({
const getCommitStatus = newBranch ? Promise.resolve(false) : dispatch('checkCommitStatus'); branch: getters.branchName,
newBranch,
state,
rootState,
});
commit(types.UPDATE_LOADING, true); commit(types.UPDATE_LOADING, true);
return getCommitStatus return service
.then( .commit(rootState.currentProjectId, payload)
branchChanged =>
new Promise(resolve => {
if (branchChanged) {
// show the modal with a Bootstrap call
$('#ide-create-branch-modal').modal('show');
} else {
resolve();
}
}),
)
.then(() => service.commit(rootState.currentProjectId, payload))
.then(({ data }) => { .then(({ data }) => {
commit(types.UPDATE_LOADING, false); commit(types.UPDATE_LOADING, false);
...@@ -220,12 +188,16 @@ export const commitChanges = ({ commit, state, getters, dispatch, rootState, roo ...@@ -220,12 +188,16 @@ export const commitChanges = ({ commit, state, getters, dispatch, rootState, roo
); );
}) })
.catch(err => { .catch(err => {
if (err.response.status === 400) {
$('#ide-create-branch-modal').modal('show');
} else {
let errMsg = __('Error committing changes. Please try again.'); let errMsg = __('Error committing changes. Please try again.');
if (err.response.data && err.response.data.message) { if (err.response.data && err.response.data.message) {
errMsg += ` (${stripHtml(err.response.data.message)})`; errMsg += ` (${stripHtml(err.response.data.message)})`;
} }
flash(errMsg, 'alert', document, null, false, true); flash(errMsg, 'alert', document, null, false, true);
window.dispatchEvent(new Event('resize')); window.dispatchEvent(new Event('resize'));
}
commit(types.UPDATE_LOADING, false); commit(types.UPDATE_LOADING, false);
}); });
......
...@@ -47,6 +47,7 @@ export default { ...@@ -47,6 +47,7 @@ export default {
baseRaw: null, baseRaw: null,
html: data.html, html: data.html,
size: data.size, size: data.size,
lastCommit: data.last_commit,
}); });
}, },
[types.SET_FILE_RAW_DATA](state, { file, raw }) { [types.SET_FILE_RAW_DATA](state, { file, raw }) {
......
...@@ -104,7 +104,7 @@ export const setPageTitle = title => { ...@@ -104,7 +104,7 @@ export const setPageTitle = title => {
document.title = title; document.title = title;
}; };
export const createCommitPayload = (branch, newBranch, state, rootState) => ({ export const createCommitPayload = ({ branch, newBranch, state, rootState }) => ({
branch, branch,
commit_message: state.commitMessage, commit_message: state.commitMessage,
actions: rootState.stagedFiles.map(f => ({ actions: rootState.stagedFiles.map(f => ({
...@@ -112,6 +112,7 @@ export const createCommitPayload = (branch, newBranch, state, rootState) => ({ ...@@ -112,6 +112,7 @@ export const createCommitPayload = (branch, newBranch, state, rootState) => ({
file_path: f.path, file_path: f.path,
content: f.content, content: f.content,
encoding: f.base64 ? 'base64' : 'text', encoding: f.base64 ? 'base64' : 'text',
last_commit_id: newBranch ? undefined : f.lastCommit.id,
})), })),
start_branch: newBranch ? rootState.currentBranchId : undefined, start_branch: newBranch ? rootState.currentBranchId : undefined,
}); });
......
...@@ -200,6 +200,7 @@ class Projects::BlobController < Projects::ApplicationController ...@@ -200,6 +200,7 @@ class Projects::BlobController < Projects::ApplicationController
path_segments = @path.split('/') path_segments = @path.split('/')
path_segments.pop path_segments.pop
tree_path = path_segments.join('/') tree_path = path_segments.join('/')
last_commit = @repository.last_commit_for_path(@commit.id, @blob.path)
json = { json = {
id: @blob.id, id: @blob.id,
...@@ -217,6 +218,7 @@ class Projects::BlobController < Projects::ApplicationController ...@@ -217,6 +218,7 @@ class Projects::BlobController < Projects::ApplicationController
blame_path: project_blame_path(project, @id), blame_path: project_blame_path(project, @id),
commits_path: project_commits_path(project, @id), commits_path: project_commits_path(project, @id),
tree_path: project_tree_path(project, File.join(@ref, tree_path)), tree_path: project_tree_path(project, File.join(@ref, tree_path)),
last_commit: last_commit,
permalink: project_blob_path(project, File.join(@commit.id, @path)) permalink: project_blob_path(project, File.join(@commit.id, @path))
} }
......
...@@ -108,77 +108,6 @@ describe('IDE commit module actions', () => { ...@@ -108,77 +108,6 @@ describe('IDE commit module actions', () => {
}); });
}); });
describe('checkCommitStatus', () => {
beforeEach(() => {
store.state.currentProjectId = 'abcproject';
store.state.currentBranchId = 'master';
store.state.projects.abcproject = {
branches: {
master: {
workingReference: '1',
},
},
};
});
it('calls service', done => {
spyOn(service, 'getBranchData').and.returnValue(
Promise.resolve({
data: {
commit: { id: '123' },
},
}),
);
store
.dispatch('commit/checkCommitStatus')
.then(() => {
expect(service.getBranchData).toHaveBeenCalledWith('abcproject', 'master');
done();
})
.catch(done.fail);
});
it('returns true if current ref does not equal returned ID', done => {
spyOn(service, 'getBranchData').and.returnValue(
Promise.resolve({
data: {
commit: { id: '123' },
},
}),
);
store
.dispatch('commit/checkCommitStatus')
.then(val => {
expect(val).toBeTruthy();
done();
})
.catch(done.fail);
});
it('returns false if current ref equals returned ID', done => {
spyOn(service, 'getBranchData').and.returnValue(
Promise.resolve({
data: {
commit: { id: '1' },
},
}),
);
store
.dispatch('commit/checkCommitStatus')
.then(val => {
expect(val).toBeFalsy();
done();
})
.catch(done.fail);
});
});
describe('updateFilesAfterCommit', () => { describe('updateFilesAfterCommit', () => {
const data = { const data = {
id: '123', id: '123',
...@@ -314,6 +243,9 @@ describe('IDE commit module actions', () => { ...@@ -314,6 +243,9 @@ describe('IDE commit module actions', () => {
...file('changed'), ...file('changed'),
type: 'blob', type: 'blob',
active: true, active: true,
lastCommit: {
id: '123456789',
},
}; };
store.state.stagedFiles.push(f); store.state.stagedFiles.push(f);
store.state.changedFiles = [ store.state.changedFiles = [
...@@ -366,6 +298,7 @@ describe('IDE commit module actions', () => { ...@@ -366,6 +298,7 @@ describe('IDE commit module actions', () => {
file_path: jasmine.anything(), file_path: jasmine.anything(),
content: jasmine.anything(), content: jasmine.anything(),
encoding: jasmine.anything(), encoding: jasmine.anything(),
last_commit_id: undefined,
}, },
], ],
start_branch: 'master', start_branch: 'master',
...@@ -376,6 +309,32 @@ describe('IDE commit module actions', () => { ...@@ -376,6 +309,32 @@ describe('IDE commit module actions', () => {
.catch(done.fail); .catch(done.fail);
}); });
it('sends lastCommit ID when not creating new branch', done => {
store.state.commit.commitAction = '1';
store
.dispatch('commit/commitChanges')
.then(() => {
expect(service.commit).toHaveBeenCalledWith('abcproject', {
branch: jasmine.anything(),
commit_message: 'testing 123',
actions: [
{
action: 'update',
file_path: jasmine.anything(),
content: jasmine.anything(),
encoding: jasmine.anything(),
last_commit_id: '123456789',
},
],
start_branch: undefined,
});
done();
})
.catch(done.fail);
});
it('sets last Commit Msg', done => { it('sets last Commit Msg', done => {
store store
.dispatch('commit/commitChanges') .dispatch('commit/commitChanges')
......
import * as utils from '~/ide/stores/utils'; import * as utils from '~/ide/stores/utils';
import { file } from '../helpers';
describe('Multi-file store utils', () => { describe('Multi-file store utils', () => {
describe('setPageTitle', () => { describe('setPageTitle', () => {
...@@ -63,4 +64,63 @@ describe('Multi-file store utils', () => { ...@@ -63,4 +64,63 @@ describe('Multi-file store utils', () => {
expect(foundEntry).toBeUndefined(); expect(foundEntry).toBeUndefined();
}); });
}); });
describe('createCommitPayload', () => {
it('returns API payload', () => {
const state = {
commitMessage: 'commit message',
};
const rootState = {
stagedFiles: [
{
...file('staged'),
path: 'staged',
content: 'updated file content',
lastCommit: {
id: '123456789',
},
},
{
...file('newFile'),
path: 'added',
tempFile: true,
content: 'new file content',
base64: true,
lastCommit: {
id: '123456789',
},
},
],
currentBranchId: 'master',
};
const payload = utils.createCommitPayload({
branch: 'master',
newBranch: false,
state,
rootState,
});
expect(payload).toEqual({
branch: 'master',
commit_message: 'commit message',
actions: [
{
action: 'update',
file_path: 'staged',
content: 'updated file content',
encoding: 'text',
last_commit_id: '123456789',
},
{
action: 'create',
file_path: 'added',
content: 'new file content',
encoding: 'base64',
last_commit_id: '123456789',
},
],
start_branch: undefined,
});
});
});
}); });
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