2019-07-07 11:18:12 +05:30
|
|
|
import Visibility from 'visibilityjs';
|
|
|
|
import * as types from './mutation_types';
|
|
|
|
import { convertObjectPropsToCamelCase } from '~/lib/utils/common_utils';
|
|
|
|
import Poll from '~/lib/utils/poll';
|
|
|
|
import createFlash from '~/flash';
|
|
|
|
import { s__, sprintf } from '~/locale';
|
|
|
|
import axios from '~/lib/utils/axios_utils';
|
2019-12-21 20:55:43 +05:30
|
|
|
import { jobsPathWithFilter, reposPathWithFilter } from './getters';
|
2019-07-07 11:18:12 +05:30
|
|
|
|
|
|
|
let eTagPoll;
|
|
|
|
|
|
|
|
export const clearJobsEtagPoll = () => {
|
|
|
|
eTagPoll = null;
|
|
|
|
};
|
|
|
|
export const stopJobsPolling = () => {
|
|
|
|
if (eTagPoll) eTagPoll.stop();
|
|
|
|
};
|
|
|
|
export const restartJobsPolling = () => {
|
|
|
|
if (eTagPoll) eTagPoll.restart();
|
|
|
|
};
|
|
|
|
|
|
|
|
export const setInitialData = ({ commit }, data) => commit(types.SET_INITIAL_DATA, data);
|
2019-12-21 20:55:43 +05:30
|
|
|
export const setFilter = ({ commit }, filter) => commit(types.SET_FILTER, filter);
|
2019-07-07 11:18:12 +05:30
|
|
|
|
|
|
|
export const requestRepos = ({ commit }, repos) => commit(types.REQUEST_REPOS, repos);
|
|
|
|
export const receiveReposSuccess = ({ commit }, repos) =>
|
|
|
|
commit(types.RECEIVE_REPOS_SUCCESS, repos);
|
|
|
|
export const receiveReposError = ({ commit }) => commit(types.RECEIVE_REPOS_ERROR);
|
|
|
|
export const fetchRepos = ({ state, dispatch }) => {
|
2019-12-21 20:55:43 +05:30
|
|
|
dispatch('stopJobsPolling');
|
2019-07-07 11:18:12 +05:30
|
|
|
dispatch('requestRepos');
|
|
|
|
|
2019-12-21 20:55:43 +05:30
|
|
|
const { provider } = state;
|
|
|
|
|
2019-07-07 11:18:12 +05:30
|
|
|
return axios
|
2019-12-21 20:55:43 +05:30
|
|
|
.get(reposPathWithFilter(state))
|
2019-07-07 11:18:12 +05:30
|
|
|
.then(({ data }) =>
|
|
|
|
dispatch('receiveReposSuccess', convertObjectPropsToCamelCase(data, { deep: true })),
|
|
|
|
)
|
|
|
|
.then(() => dispatch('fetchJobs'))
|
|
|
|
.catch(() => {
|
|
|
|
createFlash(
|
|
|
|
sprintf(s__('ImportProjects|Requesting your %{provider} repositories failed'), {
|
2019-12-21 20:55:43 +05:30
|
|
|
provider,
|
2019-07-07 11:18:12 +05:30
|
|
|
}),
|
|
|
|
);
|
|
|
|
|
|
|
|
dispatch('receiveReposError');
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
export const requestImport = ({ commit, state }, repoId) => {
|
|
|
|
if (!state.reposBeingImported.includes(repoId)) commit(types.REQUEST_IMPORT, repoId);
|
|
|
|
};
|
|
|
|
export const receiveImportSuccess = ({ commit }, { importedProject, repoId }) =>
|
|
|
|
commit(types.RECEIVE_IMPORT_SUCCESS, { importedProject, repoId });
|
|
|
|
export const receiveImportError = ({ commit }, repoId) =>
|
|
|
|
commit(types.RECEIVE_IMPORT_ERROR, repoId);
|
|
|
|
export const fetchImport = ({ state, dispatch }, { newName, targetNamespace, repo }) => {
|
|
|
|
dispatch('requestImport', repo.id);
|
|
|
|
|
|
|
|
return axios
|
|
|
|
.post(state.importPath, {
|
|
|
|
ci_cd_only: state.ciCdOnly,
|
|
|
|
new_name: newName,
|
|
|
|
repo_id: repo.id,
|
|
|
|
target_namespace: targetNamespace,
|
|
|
|
})
|
|
|
|
.then(({ data }) =>
|
|
|
|
dispatch('receiveImportSuccess', {
|
|
|
|
importedProject: convertObjectPropsToCamelCase(data, { deep: true }),
|
|
|
|
repoId: repo.id,
|
|
|
|
}),
|
|
|
|
)
|
|
|
|
.catch(() => {
|
|
|
|
createFlash(s__('ImportProjects|Importing the project failed'));
|
|
|
|
|
|
|
|
dispatch('receiveImportError', { repoId: repo.id });
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
export const receiveJobsSuccess = ({ commit }, updatedProjects) =>
|
|
|
|
commit(types.RECEIVE_JOBS_SUCCESS, updatedProjects);
|
|
|
|
export const fetchJobs = ({ state, dispatch }) => {
|
2019-12-21 20:55:43 +05:30
|
|
|
const { filter } = state;
|
|
|
|
|
|
|
|
if (eTagPoll) {
|
|
|
|
stopJobsPolling();
|
|
|
|
clearJobsEtagPoll();
|
|
|
|
}
|
2019-07-07 11:18:12 +05:30
|
|
|
|
|
|
|
eTagPoll = new Poll({
|
|
|
|
resource: {
|
2019-12-21 20:55:43 +05:30
|
|
|
fetchJobs: () => axios.get(jobsPathWithFilter(state)),
|
2019-07-07 11:18:12 +05:30
|
|
|
},
|
|
|
|
method: 'fetchJobs',
|
|
|
|
successCallback: ({ data }) =>
|
|
|
|
dispatch('receiveJobsSuccess', convertObjectPropsToCamelCase(data, { deep: true })),
|
2019-12-21 20:55:43 +05:30
|
|
|
errorCallback: () =>
|
|
|
|
createFlash(s__('ImportProjects|Update of imported projects with realtime changes failed')),
|
|
|
|
data: { filter },
|
2019-07-07 11:18:12 +05:30
|
|
|
});
|
|
|
|
|
|
|
|
if (!Visibility.hidden()) {
|
|
|
|
eTagPoll.makeRequest();
|
|
|
|
}
|
|
|
|
|
|
|
|
Visibility.change(() => {
|
|
|
|
if (!Visibility.hidden()) {
|
|
|
|
dispatch('restartJobsPolling');
|
|
|
|
} else {
|
|
|
|
dispatch('stopJobsPolling');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
// prevent babel-plugin-rewire from generating an invalid default during karma tests
|
|
|
|
export default () => {};
|