2018-09-03 03:49:52 -04:00
|
|
|
import Visibility from 'visibilityjs';
|
2022-09-28 02:08:11 -04:00
|
|
|
import { createAlert } from '~/flash';
|
2018-10-17 06:34:19 -04:00
|
|
|
import axios from '~/lib/utils/axios_utils';
|
2021-01-08 04:10:50 -05:00
|
|
|
import { setFaviconOverlay, resetFavicon } from '~/lib/utils/favicon';
|
2021-02-14 13:09:20 -05:00
|
|
|
import httpStatusCodes from '~/lib/utils/http_status';
|
|
|
|
import Poll from '~/lib/utils/poll';
|
2018-10-17 06:34:19 -04:00
|
|
|
import {
|
|
|
|
canScroll,
|
|
|
|
isScrolledToBottom,
|
|
|
|
isScrolledToTop,
|
|
|
|
isScrolledToMiddle,
|
|
|
|
scrollDown,
|
|
|
|
scrollUp,
|
|
|
|
} from '~/lib/utils/scroll_utils';
|
2021-02-14 13:09:20 -05:00
|
|
|
import { __ } from '~/locale';
|
2021-08-05 11:09:46 -04:00
|
|
|
import { reportToSentry } from '../utils';
|
2021-02-01 10:08:56 -05:00
|
|
|
import * as types from './mutation_types';
|
2018-09-03 03:49:52 -04:00
|
|
|
|
2020-02-04 07:09:00 -05:00
|
|
|
export const init = ({ dispatch }, { endpoint, logState, pagePath }) => {
|
|
|
|
dispatch('setJobEndpoint', endpoint);
|
2021-09-22 20:12:17 -04:00
|
|
|
dispatch('setJobLogOptions', {
|
2020-02-04 07:09:00 -05:00
|
|
|
logState,
|
|
|
|
pagePath,
|
|
|
|
});
|
2021-01-05 22:10:22 -05:00
|
|
|
|
2021-09-22 20:12:17 -04:00
|
|
|
return Promise.all([dispatch('fetchJob'), dispatch('fetchJobLog')]);
|
2020-02-04 07:09:00 -05:00
|
|
|
};
|
|
|
|
|
2018-09-03 03:49:52 -04:00
|
|
|
export const setJobEndpoint = ({ commit }, endpoint) => commit(types.SET_JOB_ENDPOINT, endpoint);
|
2021-09-22 20:12:17 -04:00
|
|
|
export const setJobLogOptions = ({ commit }, options) => commit(types.SET_JOB_LOG_OPTIONS, options);
|
2018-10-17 06:34:19 -04:00
|
|
|
|
|
|
|
export const hideSidebar = ({ commit }) => commit(types.HIDE_SIDEBAR);
|
|
|
|
export const showSidebar = ({ commit }) => commit(types.SHOW_SIDEBAR);
|
|
|
|
|
|
|
|
export const toggleSidebar = ({ dispatch, state }) => {
|
|
|
|
if (state.isSidebarOpen) {
|
|
|
|
dispatch('hideSidebar');
|
|
|
|
} else {
|
|
|
|
dispatch('showSidebar');
|
|
|
|
}
|
|
|
|
};
|
2018-09-03 03:49:52 -04:00
|
|
|
|
|
|
|
let eTagPoll;
|
|
|
|
|
|
|
|
export const clearEtagPoll = () => {
|
|
|
|
eTagPoll = null;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const stopPolling = () => {
|
|
|
|
if (eTagPoll) eTagPoll.stop();
|
|
|
|
};
|
|
|
|
|
|
|
|
export const restartPolling = () => {
|
|
|
|
if (eTagPoll) eTagPoll.restart();
|
|
|
|
};
|
|
|
|
|
|
|
|
export const requestJob = ({ commit }) => commit(types.REQUEST_JOB);
|
|
|
|
|
|
|
|
export const fetchJob = ({ state, dispatch }) => {
|
|
|
|
dispatch('requestJob');
|
|
|
|
|
|
|
|
eTagPoll = new Poll({
|
|
|
|
resource: {
|
|
|
|
getJob(endpoint) {
|
|
|
|
return axios.get(endpoint);
|
|
|
|
},
|
|
|
|
},
|
|
|
|
data: state.jobEndpoint,
|
|
|
|
method: 'getJob',
|
|
|
|
successCallback: ({ data }) => dispatch('receiveJobSuccess', data),
|
|
|
|
errorCallback: () => dispatch('receiveJobError'),
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!Visibility.hidden()) {
|
2021-01-05 22:10:22 -05:00
|
|
|
eTagPoll.makeRequest();
|
2018-09-03 03:49:52 -04:00
|
|
|
} else {
|
|
|
|
axios
|
|
|
|
.get(state.jobEndpoint)
|
|
|
|
.then(({ data }) => dispatch('receiveJobSuccess', data))
|
|
|
|
.catch(() => dispatch('receiveJobError'));
|
|
|
|
}
|
|
|
|
|
|
|
|
Visibility.change(() => {
|
|
|
|
if (!Visibility.hidden()) {
|
|
|
|
dispatch('restartPolling');
|
|
|
|
} else {
|
|
|
|
dispatch('stopPolling');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2018-10-17 06:34:19 -04:00
|
|
|
export const receiveJobSuccess = ({ commit }, data = {}) => {
|
2018-10-03 10:13:20 -04:00
|
|
|
commit(types.RECEIVE_JOB_SUCCESS, data);
|
2018-10-17 06:34:19 -04:00
|
|
|
|
2018-11-12 16:59:20 -05:00
|
|
|
if (data.status && data.status.favicon) {
|
|
|
|
setFaviconOverlay(data.status.favicon);
|
2018-10-17 06:34:19 -04:00
|
|
|
} else {
|
|
|
|
resetFavicon();
|
|
|
|
}
|
2018-10-03 10:13:20 -04:00
|
|
|
};
|
2018-09-03 03:49:52 -04:00
|
|
|
export const receiveJobError = ({ commit }) => {
|
|
|
|
commit(types.RECEIVE_JOB_ERROR);
|
2022-09-28 02:08:11 -04:00
|
|
|
createAlert({
|
2021-06-18 02:10:20 -04:00
|
|
|
message: __('An error occurred while fetching the job.'),
|
|
|
|
});
|
2018-10-17 06:34:19 -04:00
|
|
|
resetFavicon();
|
2018-09-03 03:49:52 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2021-09-22 20:12:17 -04:00
|
|
|
* Job Log
|
2018-09-03 03:49:52 -04:00
|
|
|
*/
|
2018-10-17 06:34:19 -04:00
|
|
|
export const scrollTop = ({ dispatch }) => {
|
|
|
|
scrollUp();
|
|
|
|
dispatch('toggleScrollButtons');
|
2018-09-03 03:49:52 -04:00
|
|
|
};
|
|
|
|
|
2018-10-17 06:34:19 -04:00
|
|
|
export const scrollBottom = ({ dispatch }) => {
|
|
|
|
scrollDown();
|
|
|
|
dispatch('toggleScrollButtons');
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Responsible for toggling the disabled state of the scroll buttons
|
|
|
|
*/
|
|
|
|
export const toggleScrollButtons = ({ dispatch }) => {
|
|
|
|
if (canScroll()) {
|
|
|
|
if (isScrolledToMiddle()) {
|
|
|
|
dispatch('enableScrollTop');
|
|
|
|
dispatch('enableScrollBottom');
|
|
|
|
} else if (isScrolledToTop()) {
|
|
|
|
dispatch('disableScrollTop');
|
|
|
|
dispatch('enableScrollBottom');
|
|
|
|
} else if (isScrolledToBottom()) {
|
|
|
|
dispatch('disableScrollBottom');
|
|
|
|
dispatch('enableScrollTop');
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
dispatch('disableScrollBottom');
|
|
|
|
dispatch('disableScrollTop');
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
export const disableScrollBottom = ({ commit }) => commit(types.DISABLE_SCROLL_BOTTOM);
|
|
|
|
export const disableScrollTop = ({ commit }) => commit(types.DISABLE_SCROLL_TOP);
|
|
|
|
export const enableScrollBottom = ({ commit }) => commit(types.ENABLE_SCROLL_BOTTOM);
|
|
|
|
export const enableScrollTop = ({ commit }) => commit(types.ENABLE_SCROLL_TOP);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* While the automatic scroll down is active,
|
|
|
|
* we show the scroll down button with an animation
|
|
|
|
*/
|
|
|
|
export const toggleScrollAnimation = ({ commit }, toggle) =>
|
|
|
|
commit(types.TOGGLE_SCROLL_ANIMATION, toggle);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Responsible to handle automatic scroll
|
|
|
|
*/
|
|
|
|
export const toggleScrollisInBottom = ({ commit }, toggle) => {
|
2021-09-22 20:12:17 -04:00
|
|
|
commit(types.TOGGLE_IS_SCROLL_IN_BOTTOM_BEFORE_UPDATING_JOB_LOG, toggle);
|
2018-09-03 03:49:52 -04:00
|
|
|
};
|
|
|
|
|
2021-09-22 20:12:17 -04:00
|
|
|
export const requestJobLog = ({ commit }) => commit(types.REQUEST_JOB_LOG);
|
2018-09-03 03:49:52 -04:00
|
|
|
|
2021-09-22 20:12:17 -04:00
|
|
|
export const fetchJobLog = ({ dispatch, state }) =>
|
|
|
|
// update trace endpoint once BE compeletes trace re-naming in #340626
|
2018-09-03 03:49:52 -04:00
|
|
|
axios
|
2021-09-22 20:12:17 -04:00
|
|
|
.get(`${state.jobLogEndpoint}/trace.json`, {
|
|
|
|
params: { state: state.jobLogState },
|
2018-09-03 03:49:52 -04:00
|
|
|
})
|
|
|
|
.then(({ data }) => {
|
2018-10-17 06:34:19 -04:00
|
|
|
dispatch('toggleScrollisInBottom', isScrolledToBottom());
|
2021-09-22 20:12:17 -04:00
|
|
|
dispatch('receiveJobLogSuccess', data);
|
2018-09-03 03:49:52 -04:00
|
|
|
|
2020-02-04 07:09:00 -05:00
|
|
|
if (data.complete) {
|
2021-09-22 20:12:17 -04:00
|
|
|
dispatch('stopPollingJobLog');
|
|
|
|
} else if (!state.jobLogTimeout) {
|
|
|
|
dispatch('startPollingJobLog');
|
2018-09-03 03:49:52 -04:00
|
|
|
}
|
|
|
|
})
|
2021-08-05 11:09:46 -04:00
|
|
|
.catch((e) => {
|
|
|
|
if (e.response.status === httpStatusCodes.FORBIDDEN) {
|
2021-09-22 20:12:17 -04:00
|
|
|
dispatch('receiveJobLogUnauthorizedError');
|
2021-08-05 11:09:46 -04:00
|
|
|
} else {
|
|
|
|
reportToSentry('job_actions', e);
|
2021-09-22 20:12:17 -04:00
|
|
|
dispatch('receiveJobLogError');
|
2021-08-05 11:09:46 -04:00
|
|
|
}
|
|
|
|
});
|
2018-10-17 06:34:19 -04:00
|
|
|
|
2021-09-22 20:12:17 -04:00
|
|
|
export const startPollingJobLog = ({ dispatch, commit }) => {
|
|
|
|
const jobLogTimeout = setTimeout(() => {
|
|
|
|
commit(types.SET_JOB_LOG_TIMEOUT, 0);
|
|
|
|
dispatch('fetchJobLog');
|
2020-02-04 07:09:00 -05:00
|
|
|
}, 4000);
|
|
|
|
|
2021-09-22 20:12:17 -04:00
|
|
|
commit(types.SET_JOB_LOG_TIMEOUT, jobLogTimeout);
|
2020-02-04 07:09:00 -05:00
|
|
|
};
|
|
|
|
|
2021-09-22 20:12:17 -04:00
|
|
|
export const stopPollingJobLog = ({ state, commit }) => {
|
|
|
|
clearTimeout(state.jobLogTimeout);
|
|
|
|
commit(types.SET_JOB_LOG_TIMEOUT, 0);
|
|
|
|
commit(types.STOP_POLLING_JOB_LOG);
|
2018-09-03 03:49:52 -04:00
|
|
|
};
|
2020-02-04 07:09:00 -05:00
|
|
|
|
2021-09-22 20:12:17 -04:00
|
|
|
export const receiveJobLogSuccess = ({ commit }, log) => commit(types.RECEIVE_JOB_LOG_SUCCESS, log);
|
|
|
|
|
|
|
|
export const receiveJobLogError = ({ dispatch }) => {
|
|
|
|
dispatch('stopPollingJobLog');
|
2022-09-28 02:08:11 -04:00
|
|
|
createAlert({
|
2021-06-18 02:10:20 -04:00
|
|
|
message: __('An error occurred while fetching the job log.'),
|
|
|
|
});
|
2018-09-03 03:49:52 -04:00
|
|
|
};
|
2021-09-22 20:12:17 -04:00
|
|
|
|
|
|
|
export const receiveJobLogUnauthorizedError = ({ dispatch }) => {
|
|
|
|
dispatch('stopPollingJobLog');
|
2022-09-28 02:08:11 -04:00
|
|
|
createAlert({
|
2021-06-18 02:10:20 -04:00
|
|
|
message: __('The current user is not authorized to access the job log.'),
|
|
|
|
});
|
2020-12-10 10:10:12 -05:00
|
|
|
};
|
2019-09-12 05:34:12 -04:00
|
|
|
/**
|
2020-07-02 08:09:02 -04:00
|
|
|
* When the user clicks a collapsible line in the job
|
2019-09-12 05:34:12 -04:00
|
|
|
* log, we commit a mutation to update the state
|
|
|
|
*
|
|
|
|
* @param {Object} section
|
|
|
|
*/
|
|
|
|
export const toggleCollapsibleLine = ({ commit }, section) =>
|
|
|
|
commit(types.TOGGLE_COLLAPSIBLE_LINE, section);
|
2018-09-03 03:49:52 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Jobs list on sidebar - depend on stages dropdown
|
|
|
|
*/
|
2018-10-12 13:13:41 -04:00
|
|
|
export const requestJobsForStage = ({ commit }, stage) =>
|
|
|
|
commit(types.REQUEST_JOBS_FOR_STAGE, stage);
|
2018-09-03 03:49:52 -04:00
|
|
|
|
|
|
|
// On stage click, set selected stage + fetch job
|
2019-05-23 09:27:54 -04:00
|
|
|
export const fetchJobsForStage = ({ dispatch }, stage = {}) => {
|
2018-10-12 13:13:41 -04:00
|
|
|
dispatch('requestJobsForStage', stage);
|
2018-09-03 03:49:52 -04:00
|
|
|
|
|
|
|
axios
|
2018-10-03 10:13:20 -04:00
|
|
|
.get(stage.dropdown_path, {
|
|
|
|
params: {
|
|
|
|
retried: 1,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
.then(({ data }) => {
|
2020-12-23 19:10:25 -05:00
|
|
|
const retriedJobs = data.retried.map((job) => ({ ...job, retried: true }));
|
2018-10-03 10:13:20 -04:00
|
|
|
const jobs = data.latest_statuses.concat(retriedJobs);
|
|
|
|
|
|
|
|
dispatch('receiveJobsForStageSuccess', jobs);
|
|
|
|
})
|
2018-09-03 03:49:52 -04:00
|
|
|
.catch(() => dispatch('receiveJobsForStageError'));
|
|
|
|
};
|
|
|
|
export const receiveJobsForStageSuccess = ({ commit }, data) =>
|
|
|
|
commit(types.RECEIVE_JOBS_FOR_STAGE_SUCCESS, data);
|
2021-09-22 20:12:17 -04:00
|
|
|
|
2018-09-03 03:49:52 -04:00
|
|
|
export const receiveJobsForStageError = ({ commit }) => {
|
|
|
|
commit(types.RECEIVE_JOBS_FOR_STAGE_ERROR);
|
2022-09-28 02:08:11 -04:00
|
|
|
createAlert({
|
2021-06-18 02:10:20 -04:00
|
|
|
message: __('An error occurred while fetching the jobs.'),
|
|
|
|
});
|
2018-09-03 03:49:52 -04:00
|
|
|
};
|
|
|
|
|
2021-01-05 22:10:22 -05:00
|
|
|
export const triggerManualJob = ({ state }, variables) => {
|
2020-12-23 19:10:25 -05:00
|
|
|
const parsedVariables = variables.map((variable) => {
|
2020-05-07 17:09:26 -04:00
|
|
|
const copyVar = { ...variable };
|
2019-07-29 03:43:10 -04:00
|
|
|
delete copyVar.id;
|
|
|
|
return copyVar;
|
|
|
|
});
|
|
|
|
|
|
|
|
axios
|
|
|
|
.post(state.job.status.action.path, {
|
|
|
|
job_variables_attributes: parsedVariables,
|
|
|
|
})
|
2021-06-18 02:10:20 -04:00
|
|
|
.catch(() =>
|
2022-09-28 02:08:11 -04:00
|
|
|
createAlert({
|
2021-06-18 02:10:20 -04:00
|
|
|
message: __('An error occurred while triggering the job.'),
|
|
|
|
}),
|
|
|
|
);
|
2019-07-29 03:43:10 -04:00
|
|
|
};
|