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

10 11
let eTagPoll;

12 13 14
export const clearEtagPoll = () => {
  eTagPoll = null;
};
P
Phil Hughes 已提交
15 16
export const stopPipelinePolling = () => eTagPoll && eTagPoll.stop();
export const restartPipelinePolling = () => eTagPoll && eTagPoll.restart();
17

P
Phil Hughes 已提交
18
export const requestLatestPipeline = ({ commit }) => commit(types.REQUEST_LATEST_PIPELINE);
19
export const receiveLatestPipelineError = ({ commit, dispatch }) => {
P
Phil Hughes 已提交
20 21
  flash(__('There was an error loading latest pipeline'));
  commit(types.RECEIVE_LASTEST_PIPELINE_ERROR);
22
  dispatch('stopPipelinePolling');
P
Phil Hughes 已提交
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);
P
Phil Hughes 已提交
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();
  }
P
Phil Hughes 已提交
51

52 53 54 55 56 57 58
  Visibility.change(() => {
    if (!Visibility.hidden()) {
      eTagPoll.restart();
    } else {
      eTagPoll.stop();
    }
  });
59
};
P
Phil Hughes 已提交
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 });
P
Phil Hughes 已提交
68

69 70
export const fetchJobs = ({ dispatch }, stage) => {
  dispatch('requestJobs', stage.id);
P
Phil Hughes 已提交
71

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

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

P
Phil Hughes 已提交
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
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);
export const receiveJobTraceError = ({ commit }) => commit(types.RECEIVE_JOB_TRACE_ERROR);
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))
    .catch(() => dispatch('requestJobTraceError'));
};
P
Phil Hughes 已提交
101

P
Phil Hughes 已提交
102
export default () => {};