actions.js 3.39 KB
Newer Older
1
import Visibility from 'visibilityjs';
2
import axios from 'axios';
Phil Hughes's avatar
Phil Hughes committed
3 4
import { __ } from '../../../../locale';
import flash from '../../../../flash';
5 6
import Poll from '../../../../lib/utils/poll';
import service from '../../../services';
7
import { rightSidebarViews } from '../../../constants';
Phil Hughes's avatar
Phil Hughes committed
8 9
import * as types from './mutation_types';

10 11
let eTagPoll;

12 13 14
export const clearEtagPoll = () => {
  eTagPoll = null;
};
Phil Hughes's avatar
Phil Hughes committed
15 16
export const stopPipelinePolling = () => eTagPoll && eTagPoll.stop();
export const restartPipelinePolling = () => eTagPoll && eTagPoll.restart();
17

Phil Hughes's avatar
Phil Hughes committed
18
export const requestLatestPipeline = ({ commit }) => commit(types.REQUEST_LATEST_PIPELINE);
19
export const receiveLatestPipelineError = ({ commit, dispatch }) => {
Phil Hughes's avatar
Phil Hughes committed
20 21
  flash(__('There was an error loading latest pipeline'));
  commit(types.RECEIVE_LASTEST_PIPELINE_ERROR);
22
  dispatch('stopPipelinePolling');
Phil Hughes's avatar
Phil Hughes committed
23
};
24
export const receiveLatestPipelineSuccess = ({ rootGetters, commit }, { pipelines }) => {
25 26
  let lastCommitPipeline = false;

27 28
  if (pipelines && pipelines.length) {
    const lastCommitHash = rootGetters.lastCommit && rootGetters.lastCommit.id;
29
    lastCommitPipeline = pipelines.find(pipeline => pipeline.commit.id === lastCommitHash);
30
  }
31 32

  commit(types.RECEIVE_LASTEST_PIPELINE_SUCCESS, lastCommitPipeline);
Phil Hughes's avatar
Phil Hughes committed
33 34
};

35 36 37 38 39 40 41 42 43 44 45 46
export const fetchLatestPipeline = ({ dispatch, rootGetters }) => {
  if (eTagPoll) return;

  dispatch('requestLatestPipeline');

  eTagPoll = new Poll({
    resource: service,
    method: 'lastCommitPipelines',
    data: { getters: rootGetters },
    successCallback: ({ data }) => dispatch('receiveLatestPipelineSuccess', data),
    errorCallback: () => dispatch('receiveLatestPipelineError'),
  });
47

48 49 50
  if (!Visibility.hidden()) {
    eTagPoll.makeRequest();
  }
Phil Hughes's avatar
Phil Hughes committed
51

52 53 54 55 56 57 58
  Visibility.change(() => {
    if (!Visibility.hidden()) {
      eTagPoll.restart();
    } else {
      eTagPoll.stop();
    }
  });
59
};
Phil Hughes's avatar
Phil Hughes committed
60

61 62 63 64 65 66 67
export const requestJobs = ({ commit }, id) => commit(types.REQUEST_JOBS, id);
export const receiveJobsError = ({ commit }, id) => {
  flash(__('There was an error loading jobs'));
  commit(types.RECEIVE_JOBS_ERROR, id);
};
export const receiveJobsSuccess = ({ commit }, { id, data }) =>
  commit(types.RECEIVE_JOBS_SUCCESS, { id, data });
Phil Hughes's avatar
Phil Hughes committed
68

69 70
export const fetchJobs = ({ dispatch }, stage) => {
  dispatch('requestJobs', stage.id);
Phil Hughes's avatar
Phil Hughes committed
71

72
  axios
73
    .get(stage.dropdownPath)
74
    .then(({ data }) => dispatch('receiveJobsSuccess', { id: stage.id, data }))
75
    .catch(() => dispatch('receiveJobsError', stage.id));
Phil Hughes's avatar
Phil Hughes committed
76 77
};

78 79 80
export const toggleStageCollapsed = ({ commit }, stageId) =>
  commit(types.TOGGLE_STAGE_COLLAPSE, stageId);

81 82 83 84 85 86 87 88
export const setDetailJob = ({ commit, dispatch }, job) => {
  commit(types.SET_DETAIL_JOB, job);
  dispatch('setRightPane', job ? rightSidebarViews.jobsDetail : rightSidebarViews.pipelines, {
    root: true,
  });
};

export const requestJobTrace = ({ commit }) => commit(types.REQUEST_JOB_TRACE);
89 90 91 92
export const receiveJobTraceError = ({ commit }) => {
  flash(__('Error fetching job trace'));
  commit(types.RECEIVE_JOB_TRACE_ERROR);
};
93 94 95 96 97 98 99 100 101
export const receiveJobTraceSuccess = ({ commit }, data) =>
  commit(types.RECEIVE_JOB_TRACE_SUCCESS, data);

export const fetchJobTrace = ({ dispatch, state }) => {
  dispatch('requestJobTrace');

  return axios
    .get(`${state.detailJob.path}/trace`, { params: { format: 'json' } })
    .then(({ data }) => dispatch('receiveJobTraceSuccess', data))
Phil Hughes's avatar
Phil Hughes committed
102
    .catch(() => dispatch('receiveJobTraceError'));
103
};
Phil Hughes's avatar
Phil Hughes committed
104

Phil Hughes's avatar
Phil Hughes committed
105
export default () => {};