summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/ci_variable_list/store/utils.js
blob: f46a671ae7be9325d94c78b36a72bb837b10ddc9 (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
import { cloneDeep } from 'lodash';
import { displayText, types, allEnvironments } from '../constants';

const variableTypeHandler = (type) =>
  type === displayText.variableText ? types.variableType : types.fileType;

export const prepareDataForDisplay = (variables) => {
  const variablesToDisplay = [];
  variables.forEach((variable) => {
    const variableCopy = variable;
    if (variableCopy.variable_type === types.variableType) {
      variableCopy.variable_type = displayText.variableText;
    } else {
      variableCopy.variable_type = displayText.fileText;
    }
    variableCopy.secret_value = variableCopy.value;

    if (variableCopy.environment_scope === allEnvironments.type) {
      variableCopy.environment_scope = displayText.allEnvironmentsText;
    }
    variableCopy.protected_variable = variableCopy.protected;
    variablesToDisplay.push(variableCopy);
  });
  return variablesToDisplay;
};

export const prepareDataForApi = (variable, destroy = false) => {
  const variableCopy = cloneDeep(variable);
  variableCopy.protected = variableCopy.protected_variable.toString();
  delete variableCopy.protected_variable;
  variableCopy.masked = variableCopy.masked.toString();
  variableCopy.variable_type = variableTypeHandler(variableCopy.variable_type);
  if (variableCopy.environment_scope === displayText.allEnvironmentsText) {
    variableCopy.environment_scope = allEnvironments.type;
  }

  if (destroy) {
    // eslint-disable-next-line
    variableCopy._destroy = destroy;
  }

  return variableCopy;
};

export const prepareEnvironments = (environments) => environments.map((e) => e.name);