summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/ide/stores/modules/pipelines/actions.js
blob: 51872993f16ba42845fb922a7fc2873f31f4eecb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import axios from 'axios';
import Visibility from 'visibilityjs';
import httpStatus from '../../../../lib/utils/http_status';
import Poll from '../../../../lib/utils/poll';
import { __ } from '../../../../locale';
import { rightSidebarViews } from '../../../constants';
import service from '../../../services';
import * as types from './mutation_types';

let eTagPoll;

export const clearEtagPoll = () => {
  eTagPoll = null;
};
export const stopPipelinePolling = () => {
  if (eTagPoll) eTagPoll.stop();
};
export const restartPipelinePolling = () => {
  if (eTagPoll) eTagPoll.restart();
};
export const forcePipelineRequest = () => {
  if (eTagPoll) eTagPoll.makeRequest();
};

export const requestLatestPipeline = ({ commit }) => commit(types.REQUEST_LATEST_PIPELINE);
export const receiveLatestPipelineError = ({ commit, dispatch }, err) => {
  if (err.status !== httpStatus.NOT_FOUND) {
    dispatch(
      'setErrorMessage',
      {
        text: __('An error occurred while fetching the latest pipeline.'),
        action: () =>
          dispatch('forcePipelineRequest').then(() =>
            dispatch('setErrorMessage', null, { root: true }),
          ),
        actionText: __('Please try again'),
        actionPayload: null,
      },
      { root: true },
    );
  }
  commit(types.RECEIVE_LASTEST_PIPELINE_ERROR);
  dispatch('stopPipelinePolling');
};
export const receiveLatestPipelineSuccess = ({ rootGetters, commit }, { pipelines }) => {
  let lastCommitPipeline = false;

  if (pipelines && pipelines.length) {
    const lastCommitHash = rootGetters.lastCommit && rootGetters.lastCommit.id;
    lastCommitPipeline = pipelines.find((pipeline) => pipeline.commit.id === lastCommitHash);
  }

  commit(types.RECEIVE_LASTEST_PIPELINE_SUCCESS, lastCommitPipeline);
};

export const fetchLatestPipeline = ({ commit, dispatch, rootGetters }) => {
  if (eTagPoll) return;

  if (!rootGetters.lastCommit) {
    commit(types.RECEIVE_LASTEST_PIPELINE_SUCCESS, null);
    dispatch('stopPipelinePolling');
    return;
  }

  dispatch('requestLatestPipeline');

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

  if (!Visibility.hidden()) {
    eTagPoll.makeRequest();
  }

  Visibility.change(() => {
    if (!Visibility.hidden()) {
      dispatch('restartPipelinePolling');
    } else {
      dispatch('stopPipelinePolling');
    }
  });
};

export const requestJobs = ({ commit }, id) => commit(types.REQUEST_JOBS, id);
export const receiveJobsError = ({ commit, dispatch }, stage) => {
  dispatch(
    'setErrorMessage',
    {
      text: __('An error occurred while loading the pipelines jobs.'),
      action: (payload) =>
        dispatch('fetchJobs', payload).then(() =>
          dispatch('setErrorMessage', null, { root: true }),
        ),
      actionText: __('Please try again'),
      actionPayload: stage,
    },
    { root: true },
  );
  commit(types.RECEIVE_JOBS_ERROR, stage.id);
};
export const receiveJobsSuccess = ({ commit }, { id, data }) =>
  commit(types.RECEIVE_JOBS_SUCCESS, { id, data });

export const fetchJobs = ({ dispatch }, stage) => {
  dispatch('requestJobs', stage.id);

  return axios
    .get(stage.dropdownPath)
    .then(({ data }) => dispatch('receiveJobsSuccess', { id: stage.id, data }))
    .catch(() => dispatch('receiveJobsError', stage));
};

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

export const setDetailJob = ({ commit, dispatch }, job) => {
  commit(types.SET_DETAIL_JOB, job);
  dispatch('rightPane/open', job ? rightSidebarViews.jobsDetail : rightSidebarViews.pipelines, {
    root: true,
  });
};

export const requestJobLogs = ({ commit }) => commit(types.REQUEST_JOB_LOGS);
export const receiveJobLogsError = ({ commit, dispatch }) => {
  dispatch(
    'setErrorMessage',
    {
      text: __('An error occurred while fetching the job logs.'),
      action: () =>
        dispatch('fetchJobLogs').then(() => dispatch('setErrorMessage', null, { root: true })),
      actionText: __('Please try again'),
      actionPayload: null,
    },
    { root: true },
  );
  commit(types.RECEIVE_JOB_LOGS_ERROR);
};
export const receiveJobLogsSuccess = ({ commit }, data) =>
  commit(types.RECEIVE_JOB_LOGS_SUCCESS, data);

export const fetchJobLogs = ({ dispatch, state }) => {
  dispatch('requestJobLogs');

  // update trace endpoint once BE compeletes trace re-naming in #340626
  return axios
    .get(`${state.detailJob.path}/trace`, { params: { format: 'json' } })
    .then(({ data }) => dispatch('receiveJobLogsSuccess', data))
    .catch(() => dispatch('receiveJobLogsError'));
};

export const resetLatestPipeline = ({ commit }) => {
  commit(types.RECEIVE_LASTEST_PIPELINE_SUCCESS, null);
  commit(types.SET_DETAIL_JOB, null);
};