2020-10-24 23:57:45 +05:30
|
|
|
import _ from 'lodash';
|
2021-03-11 19:13:27 +05:30
|
|
|
import Api from '~/api';
|
2021-09-04 01:27:46 +05:30
|
|
|
import createFlash from '~/flash';
|
2021-03-11 19:13:27 +05:30
|
|
|
import axios from '~/lib/utils/axios_utils';
|
2020-10-24 23:57:45 +05:30
|
|
|
import { s__ } from '~/locale';
|
|
|
|
import * as types from './mutation_types';
|
|
|
|
|
|
|
|
export const setBaseConfig = ({ commit }, options) => {
|
|
|
|
commit(types.SET_BASE_CONFIG, options);
|
|
|
|
};
|
|
|
|
|
|
|
|
export const setTabIndex = ({ commit }, tabIndex) => commit(types.SET_TABINDEX, tabIndex);
|
|
|
|
|
|
|
|
export const searchCommits = ({ dispatch, commit, state }, searchText) => {
|
|
|
|
commit(types.FETCH_COMMITS);
|
|
|
|
|
|
|
|
let params = {};
|
|
|
|
if (searchText) {
|
|
|
|
params = {
|
|
|
|
params: {
|
|
|
|
search: searchText,
|
|
|
|
per_page: 40,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return axios
|
|
|
|
.get(state.contextCommitsPath, params)
|
|
|
|
.then(({ data }) => {
|
2021-03-08 18:12:59 +05:30
|
|
|
let commits = data.map((o) => ({ ...o, isSelected: false }));
|
|
|
|
commits = commits.map((c) => {
|
2020-10-24 23:57:45 +05:30
|
|
|
const isPresent = state.selectedCommits.find(
|
2021-03-08 18:12:59 +05:30
|
|
|
(selectedCommit) => selectedCommit.short_id === c.short_id && selectedCommit.isSelected,
|
2020-10-24 23:57:45 +05:30
|
|
|
);
|
|
|
|
if (isPresent) {
|
|
|
|
return { ...c, isSelected: true };
|
|
|
|
}
|
|
|
|
return c;
|
|
|
|
});
|
|
|
|
if (!searchText) {
|
|
|
|
dispatch('setCommits', { commits: [...commits, ...state.contextCommits] });
|
|
|
|
} else {
|
|
|
|
dispatch('setCommits', { commits });
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(() => {
|
|
|
|
commit(types.FETCH_COMMITS_ERROR);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
export const setCommits = ({ commit }, { commits: data, silentAddition = false }) => {
|
|
|
|
let commits = _.uniqBy(data, 'short_id');
|
2021-03-08 18:12:59 +05:30
|
|
|
commits = _.orderBy(data, (c) => new Date(c.committed_date), ['desc']);
|
2020-10-24 23:57:45 +05:30
|
|
|
if (silentAddition) {
|
|
|
|
commit(types.SET_COMMITS_SILENT, commits);
|
|
|
|
} else {
|
|
|
|
commit(types.SET_COMMITS, commits);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
export const createContextCommits = ({ state }, { commits, forceReload = false }) =>
|
|
|
|
Api.createContextCommits(state.projectId, state.mergeRequestIid, {
|
2021-03-08 18:12:59 +05:30
|
|
|
commits: commits.map((commit) => commit.short_id),
|
2020-10-24 23:57:45 +05:30
|
|
|
})
|
|
|
|
.then(() => {
|
|
|
|
if (forceReload) {
|
|
|
|
window.location.reload();
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
})
|
|
|
|
.catch(() => {
|
|
|
|
if (forceReload) {
|
2021-09-04 01:27:46 +05:30
|
|
|
createFlash({
|
|
|
|
message: s__('ContextCommits|Failed to create context commits. Please try again.'),
|
|
|
|
});
|
2020-10-24 23:57:45 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
|
|
|
|
export const fetchContextCommits = ({ dispatch, commit, state }) => {
|
|
|
|
commit(types.FETCH_CONTEXT_COMMITS);
|
|
|
|
return Api.allContextCommits(state.projectId, state.mergeRequestIid)
|
|
|
|
.then(({ data }) => {
|
2021-03-08 18:12:59 +05:30
|
|
|
const contextCommits = data.map((o) => ({ ...o, isSelected: true }));
|
2020-10-24 23:57:45 +05:30
|
|
|
dispatch('setContextCommits', contextCommits);
|
|
|
|
dispatch('setCommits', {
|
|
|
|
commits: [...state.commits, ...contextCommits],
|
|
|
|
silentAddition: true,
|
|
|
|
});
|
|
|
|
dispatch('setSelectedCommits', contextCommits);
|
|
|
|
})
|
|
|
|
.catch(() => {
|
|
|
|
commit(types.FETCH_CONTEXT_COMMITS_ERROR);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
export const setContextCommits = ({ commit }, data) => {
|
|
|
|
commit(types.SET_CONTEXT_COMMITS, data);
|
|
|
|
};
|
|
|
|
|
|
|
|
export const removeContextCommits = ({ state }, forceReload = false) =>
|
|
|
|
Api.removeContextCommits(state.projectId, state.mergeRequestIid, {
|
|
|
|
commits: state.toRemoveCommits,
|
|
|
|
})
|
|
|
|
.then(() => {
|
|
|
|
if (forceReload) {
|
|
|
|
window.location.reload();
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
})
|
|
|
|
.catch(() => {
|
|
|
|
if (forceReload) {
|
2021-09-04 01:27:46 +05:30
|
|
|
createFlash({
|
|
|
|
message: s__('ContextCommits|Failed to delete context commits. Please try again.'),
|
|
|
|
});
|
2020-10-24 23:57:45 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
|
|
|
|
export const setSelectedCommits = ({ commit }, selected) => {
|
|
|
|
let selectedCommits = _.uniqBy(selected, 'short_id');
|
|
|
|
selectedCommits = _.orderBy(
|
|
|
|
selectedCommits,
|
2021-03-08 18:12:59 +05:30
|
|
|
(selectedCommit) => new Date(selectedCommit.committed_date),
|
2020-10-24 23:57:45 +05:30
|
|
|
['desc'],
|
|
|
|
);
|
|
|
|
commit(types.SET_SELECTED_COMMITS, selectedCommits);
|
|
|
|
};
|
|
|
|
|
|
|
|
export const setSearchText = ({ commit }, searchText) => commit(types.SET_SEARCH_TEXT, searchText);
|
|
|
|
|
|
|
|
export const setToRemoveCommits = ({ commit }, data) => commit(types.SET_TO_REMOVE_COMMITS, data);
|
|
|
|
|
|
|
|
export const resetModalState = ({ commit }) => commit(types.RESET_MODAL_STATE);
|