summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/ide/stores/actions/project.js
blob: cece9154c8245ece4f4e3d4246673acc51d4e0d6 (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
import Visibility from 'visibilityjs';
import flash from '~/flash';
import { __ } from '~/locale';
import service from '../../services';
import * as types from '../mutation_types';
import Poll from '../../../lib/utils/poll';

let eTagPoll;

export const getProjectData = (
  { commit, state, dispatch },
  { namespace, projectId, force = false } = {},
) =>
  new Promise((resolve, reject) => {
    if (!state.projects[`${namespace}/${projectId}`] || force) {
      commit(types.TOGGLE_LOADING, { entry: state });
      service
        .getProjectData(namespace, projectId)
        .then(res => res.data)
        .then(data => {
          commit(types.TOGGLE_LOADING, { entry: state });
          commit(types.SET_PROJECT, { projectPath: `${namespace}/${projectId}`, project: data });
          if (!state.currentProjectId)
            commit(types.SET_CURRENT_PROJECT, `${namespace}/${projectId}`);
          resolve(data);
        })
        .catch(() => {
          flash(
            __('Error loading project data. Please try again.'),
            'alert',
            document,
            null,
            false,
            true,
          );
          reject(new Error(`Project not loaded ${namespace}/${projectId}`));
        });
    } else {
      resolve(state.projects[`${namespace}/${projectId}`]);
    }
  });

export const getBranchData = (
  { commit, state, dispatch },
  { projectId, branchId, force = false } = {},
) =>
  new Promise((resolve, reject) => {
    if (
      typeof state.projects[`${projectId}`] === 'undefined' ||
      !state.projects[`${projectId}`].branches[branchId] ||
      force
    ) {
      service
        .getBranchData(`${projectId}`, branchId)
        .then(({ data }) => {
          const { id } = data.commit;
          commit(types.SET_BRANCH, {
            projectPath: `${projectId}`,
            branchName: branchId,
            branch: data,
          });
          commit(types.SET_BRANCH_WORKING_REFERENCE, { projectId, branchId, reference: id });
          resolve(data);
        })
        .catch(() => {
          flash(
            __('Error loading branch data. Please try again.'),
            'alert',
            document,
            null,
            false,
            true,
          );
          reject(new Error(`Branch not loaded - ${projectId}/${branchId}`));
        });
    } else {
      resolve(state.projects[`${projectId}`].branches[branchId]);
    }
  });

export const refreshLastCommitData = ({ commit, state, dispatch }, { projectId, branchId } = {}) =>
  service
    .getBranchData(projectId, branchId)
    .then(({ data }) => {
      commit(types.SET_BRANCH_COMMIT, {
        projectId,
        branchId,
        commit: data.commit,
      });
    })
    .catch(() => {
      flash(__('Error loading last commit.'), 'alert', document, null, false, true);
    });

export const pollSuccessCallBack = ({ commit, state, dispatch }, { data }) => {
  if (data.pipelines && data.pipelines.length) {
    const lastCommitHash =
      state.projects[state.currentProjectId].branches[state.currentBranchId].commit.id;
    const lastCommitPipeline = data.pipelines.find(
      pipeline => pipeline.commit.id === lastCommitHash,
    );
    commit(types.SET_LAST_COMMIT_PIPELINE, {
      projectId: state.currentProjectId,
      branchId: state.currentBranchId,
      pipeline: lastCommitPipeline || {},
    });
  }

  return data;
};

export const pipelinePoll = ({ getters, dispatch }) => {
  eTagPoll = new Poll({
    resource: service,
    method: 'lastCommitPipelines',
    data: {
      getters,
    },
    successCallback: ({ data }) => dispatch('pollSuccessCallBack', { data }),
    errorCallback: () => {
      flash(
        __('Something went wrong while fetching the latest pipeline status.'),
        'alert',
        document,
        null,
        false,
        true,
      );
    },
  });

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

  Visibility.change(() => {
    if (!Visibility.hidden()) {
      eTagPoll.restart();
    } else {
      eTagPoll.stop();
    }
  });
};

export const stopPipelinePolling = () => {
  eTagPoll.stop();
};

export const restartPipelinePolling = () => {
  eTagPoll.restart();
};