debian-mirror-gitlab/app/assets/javascripts/import_projects/store/actions.js

123 lines
3.4 KiB
JavaScript
Raw Normal View History

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';
2020-06-23 00:09:42 +05:30
import { visitUrl } from '~/lib/utils/url_utility';
2019-07-07 11:18:12 +05:30
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;
2020-06-23 00:09:42 +05:30
const hasRedirectInError = e => e?.response?.data?.error?.redirect;
const redirectToUrlInError = e => visitUrl(e.response.data.error.redirect);
2019-07-07 11:18:12 +05:30
export const clearJobsEtagPoll = () => {
eTagPoll = null;
};
export const stopJobsPolling = () => {
if (eTagPoll) eTagPoll.stop();
};
export const restartJobsPolling = () => {
if (eTagPoll) eTagPoll.restart();
};
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
2020-06-23 00:09:42 +05:30
export const fetchRepos = ({ state, dispatch, commit }) => {
2019-12-21 20:55:43 +05:30
dispatch('stopJobsPolling');
2020-06-23 00:09:42 +05:30
commit(types.REQUEST_REPOS);
2019-07-07 11:18:12 +05:30
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 }) =>
2020-06-23 00:09:42 +05:30
commit(types.RECEIVE_REPOS_SUCCESS, convertObjectPropsToCamelCase(data, { deep: true })),
2019-07-07 11:18:12 +05:30
)
.then(() => dispatch('fetchJobs'))
2020-06-23 00:09:42 +05:30
.catch(e => {
if (hasRedirectInError(e)) {
redirectToUrlInError(e);
} else {
createFlash(
sprintf(s__('ImportProjects|Requesting your %{provider} repositories failed'), {
provider,
}),
);
commit(types.RECEIVE_REPOS_ERROR);
}
2019-07-07 11:18:12 +05:30
});
};
2020-06-23 00:09:42 +05:30
export const fetchImport = ({ state, commit }, { newName, targetNamespace, repo }) => {
if (!state.reposBeingImported.includes(repo.id)) {
commit(types.REQUEST_IMPORT, repo.id);
}
2019-07-07 11:18:12 +05:30
return axios
.post(state.importPath, {
ci_cd_only: state.ciCdOnly,
new_name: newName,
repo_id: repo.id,
target_namespace: targetNamespace,
})
.then(({ data }) =>
2020-06-23 00:09:42 +05:30
commit(types.RECEIVE_IMPORT_SUCCESS, {
2019-07-07 11:18:12 +05:30
importedProject: convertObjectPropsToCamelCase(data, { deep: true }),
repoId: repo.id,
}),
)
.catch(() => {
createFlash(s__('ImportProjects|Importing the project failed'));
2020-06-23 00:09:42 +05:30
commit(types.RECEIVE_IMPORT_ERROR, repo.id);
2019-07-07 11:18:12 +05:30
});
};
export const receiveJobsSuccess = ({ commit }, updatedProjects) =>
commit(types.RECEIVE_JOBS_SUCCESS, updatedProjects);
2020-06-23 00:09:42 +05:30
export const fetchJobs = ({ state, commit, 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 }) =>
2020-06-23 00:09:42 +05:30
commit(types.RECEIVE_JOBS_SUCCESS, convertObjectPropsToCamelCase(data, { deep: true })),
errorCallback: e => {
if (hasRedirectInError(e)) {
redirectToUrlInError(e);
} else {
createFlash(s__('ImportProjects|Update of imported projects with realtime changes failed'));
}
},
2019-12-21 20:55:43 +05:30
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 () => {};