summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/ci_variable_list/store/actions.js
blob: ac31e845b0d6f25a725d5d36fc5c59c4f1aa6276 (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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
import Api from '~/api';
import { createAlert } from '~/flash';
import axios from '~/lib/utils/axios_utils';
import { __ } from '~/locale';
import * as types from './mutation_types';
import { prepareDataForApi, prepareDataForDisplay, prepareEnvironments } from './utils';

export const toggleValues = ({ commit }, valueState) => {
  commit(types.TOGGLE_VALUES, valueState);
};

export const clearModal = ({ commit }) => {
  commit(types.CLEAR_MODAL);
};

export const resetEditing = ({ commit, dispatch }) => {
  // fetch variables again if modal is being edited and then hidden
  // without saving changes, to cover use case of reactivity in the table
  dispatch('fetchVariables');
  commit(types.RESET_EDITING);
};

export const setVariableProtected = ({ commit }) => {
  commit(types.SET_VARIABLE_PROTECTED);
};

export const requestAddVariable = ({ commit }) => {
  commit(types.REQUEST_ADD_VARIABLE);
};

export const receiveAddVariableSuccess = ({ commit }) => {
  commit(types.RECEIVE_ADD_VARIABLE_SUCCESS);
};

export const receiveAddVariableError = ({ commit }, error) => {
  commit(types.RECEIVE_ADD_VARIABLE_ERROR, error);
};

export const addVariable = ({ state, dispatch }) => {
  dispatch('requestAddVariable');

  return axios
    .patch(state.endpoint, {
      variables_attributes: [prepareDataForApi(state.variable)],
    })
    .then(() => {
      dispatch('receiveAddVariableSuccess');
      dispatch('fetchVariables');
    })
    .catch((error) => {
      createAlert({
        message: error.response.data[0],
      });
      dispatch('receiveAddVariableError', error);
    });
};

export const requestUpdateVariable = ({ commit }) => {
  commit(types.REQUEST_UPDATE_VARIABLE);
};

export const receiveUpdateVariableSuccess = ({ commit }) => {
  commit(types.RECEIVE_UPDATE_VARIABLE_SUCCESS);
};

export const receiveUpdateVariableError = ({ commit }, error) => {
  commit(types.RECEIVE_UPDATE_VARIABLE_ERROR, error);
};

export const updateVariable = ({ state, dispatch }) => {
  dispatch('requestUpdateVariable');

  const updatedVariable = prepareDataForApi(state.variable);
  updatedVariable.secrect_value = updateVariable.value;

  return axios
    .patch(state.endpoint, { variables_attributes: [updatedVariable] })
    .then(() => {
      dispatch('receiveUpdateVariableSuccess');
      dispatch('fetchVariables');
    })
    .catch((error) => {
      createAlert({
        message: error.response.data[0],
      });
      dispatch('receiveUpdateVariableError', error);
    });
};

export const editVariable = ({ commit }, variable) => {
  const variableToEdit = variable;
  variableToEdit.secret_value = variableToEdit.value;
  commit(types.VARIABLE_BEING_EDITED, variableToEdit);
};

export const requestVariables = ({ commit }) => {
  commit(types.REQUEST_VARIABLES);
};
export const receiveVariablesSuccess = ({ commit }, variables) => {
  commit(types.RECEIVE_VARIABLES_SUCCESS, variables);
};

export const fetchVariables = ({ dispatch, state }) => {
  dispatch('requestVariables');

  return axios
    .get(state.endpoint)
    .then(({ data }) => {
      dispatch('receiveVariablesSuccess', prepareDataForDisplay(data.variables));
    })
    .catch(() => {
      createAlert({
        message: __('There was an error fetching the variables.'),
      });
    });
};

export const requestDeleteVariable = ({ commit }) => {
  commit(types.REQUEST_DELETE_VARIABLE);
};

export const receiveDeleteVariableSuccess = ({ commit }) => {
  commit(types.RECEIVE_DELETE_VARIABLE_SUCCESS);
};

export const receiveDeleteVariableError = ({ commit }, error) => {
  commit(types.RECEIVE_DELETE_VARIABLE_ERROR, error);
};

export const deleteVariable = ({ dispatch, state }) => {
  dispatch('requestDeleteVariable');

  const destroy = true;

  return axios
    .patch(state.endpoint, { variables_attributes: [prepareDataForApi(state.variable, destroy)] })
    .then(() => {
      dispatch('receiveDeleteVariableSuccess');
      dispatch('fetchVariables');
    })
    .catch((error) => {
      createAlert({
        message: error.response.data[0],
      });
      dispatch('receiveDeleteVariableError', error);
    });
};

export const requestEnvironments = ({ commit }) => {
  commit(types.REQUEST_ENVIRONMENTS);
};

export const receiveEnvironmentsSuccess = ({ commit }, environments) => {
  commit(types.RECEIVE_ENVIRONMENTS_SUCCESS, environments);
};

export const fetchEnvironments = ({ dispatch, state }) => {
  dispatch('requestEnvironments');

  return Api.environments(state.projectId)
    .then((res) => {
      dispatch('receiveEnvironmentsSuccess', prepareEnvironments(res.data));
    })
    .catch(() => {
      createAlert({
        message: __('There was an error fetching the environments information.'),
      });
    });
};

export const setEnvironmentScope = ({ commit, dispatch }, environment) => {
  commit(types.SET_ENVIRONMENT_SCOPE, environment);
  dispatch('setSelectedEnvironment', environment);
};

export const addWildCardScope = ({ commit, dispatch }, environment) => {
  commit(types.ADD_WILD_CARD_SCOPE, environment);
  commit(types.SET_ENVIRONMENT_SCOPE, environment);
  dispatch('setSelectedEnvironment', environment);
};

export const resetSelectedEnvironment = ({ commit }) => {
  commit(types.RESET_SELECTED_ENVIRONMENT);
};

export const setSelectedEnvironment = ({ commit }, environment) => {
  commit(types.SET_SELECTED_ENVIRONMENT, environment);
};

export const updateVariableKey = ({ commit }, { key }) => {
  commit(types.UPDATE_VARIABLE_KEY, key);
};

export const updateVariableValue = ({ commit }, { secret_value }) => {
  commit(types.UPDATE_VARIABLE_VALUE, secret_value);
};

export const updateVariableType = ({ commit }, { variable_type }) => {
  commit(types.UPDATE_VARIABLE_TYPE, variable_type);
};

export const updateVariableProtected = ({ commit }, { protected_variable }) => {
  commit(types.UPDATE_VARIABLE_PROTECTED, protected_variable);
};

export const updateVariableMasked = ({ commit }, { masked }) => {
  commit(types.UPDATE_VARIABLE_MASKED, masked);
};