summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/ci_variable_list/graphql/resolvers.js
blob: c041531ae308ee88336e58d23733a488c789e8cc (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
import axios from 'axios';
import {
  convertObjectPropsToCamelCase,
  convertObjectPropsToSnakeCase,
} from '../../lib/utils/common_utils';
import { getIdFromGraphQLId } from '../../graphql_shared/utils';
import {
  GRAPHQL_GROUP_TYPE,
  GRAPHQL_PROJECT_TYPE,
  groupString,
  instanceString,
  projectString,
} from '../constants';
import getProjectVariables from './queries/project_variables.query.graphql';
import getGroupVariables from './queries/group_variables.query.graphql';
import getAdminVariables from './queries/variables.query.graphql';

const prepareVariableForApi = ({ variable, destroy = false }) => {
  return {
    ...convertObjectPropsToSnakeCase(variable),
    id: getIdFromGraphQLId(variable?.id),
    variable_type: variable.variableType.toLowerCase(),
    secret_value: variable.value,
    _destroy: destroy,
  };
};

const mapVariableTypes = (variables = [], kind) => {
  return variables.map((ciVar) => {
    return {
      __typename: `Ci${kind}Variable`,
      ...convertObjectPropsToCamelCase(ciVar),
      variableType: ciVar.variable_type ? ciVar.variable_type.toUpperCase() : ciVar.variableType,
    };
  });
};

const prepareProjectGraphQLResponse = ({ data, projectId, errors = [] }) => {
  return {
    errors,
    project: {
      __typename: GRAPHQL_PROJECT_TYPE,
      id: projectId,
      ciVariables: {
        __typename: 'CiVariableConnection',
        nodes: mapVariableTypes(data.variables, projectString),
      },
    },
  };
};

const prepareGroupGraphQLResponse = ({ data, groupId, errors = [] }) => {
  return {
    errors,
    group: {
      __typename: GRAPHQL_GROUP_TYPE,
      id: groupId,
      ciVariables: {
        __typename: 'CiVariableConnection',
        nodes: mapVariableTypes(data.variables, groupString),
      },
    },
  };
};

const prepareAdminGraphQLResponse = ({ data, errors = [] }) => {
  return {
    errors,
    ciVariables: {
      __typename: `Ci${instanceString}VariableConnection`,
      nodes: mapVariableTypes(data.variables, instanceString),
    },
  };
};

const callProjectEndpoint = async ({
  endpoint,
  fullPath,
  variable,
  projectId,
  cache,
  destroy = false,
}) => {
  try {
    const { data } = await axios.patch(endpoint, {
      variables_attributes: [prepareVariableForApi({ variable, destroy })],
    });
    return prepareProjectGraphQLResponse({ data, projectId });
  } catch (e) {
    return prepareProjectGraphQLResponse({
      data: cache.readQuery({ query: getProjectVariables, variables: { fullPath } }),
      projectId,
      errors: [...e.response.data],
    });
  }
};

const callGroupEndpoint = async ({
  endpoint,
  fullPath,
  variable,
  groupId,
  cache,
  destroy = false,
}) => {
  try {
    const { data } = await axios.patch(endpoint, {
      variables_attributes: [prepareVariableForApi({ variable, destroy })],
    });
    return prepareGroupGraphQLResponse({ data, groupId });
  } catch (e) {
    return prepareGroupGraphQLResponse({
      data: cache.readQuery({ query: getGroupVariables, variables: { fullPath } }),
      groupId,
      errors: [...e.response.data],
    });
  }
};

const callAdminEndpoint = async ({ endpoint, variable, cache, destroy = false }) => {
  try {
    const { data } = await axios.patch(endpoint, {
      variables_attributes: [prepareVariableForApi({ variable, destroy })],
    });

    return prepareAdminGraphQLResponse({ data });
  } catch (e) {
    return prepareAdminGraphQLResponse({
      data: cache.readQuery({ query: getAdminVariables }),
      errors: [...e.response.data],
    });
  }
};

export const resolvers = {
  Mutation: {
    addProjectVariable: async (_, { endpoint, fullPath, variable, projectId }, { cache }) => {
      return callProjectEndpoint({ endpoint, fullPath, variable, projectId, cache });
    },
    updateProjectVariable: async (_, { endpoint, fullPath, variable, projectId }, { cache }) => {
      return callProjectEndpoint({ endpoint, fullPath, variable, projectId, cache });
    },
    deleteProjectVariable: async (_, { endpoint, fullPath, variable, projectId }, { cache }) => {
      return callProjectEndpoint({ endpoint, fullPath, variable, projectId, cache, destroy: true });
    },
    addGroupVariable: async (_, { endpoint, fullPath, variable, groupId }, { cache }) => {
      return callGroupEndpoint({ endpoint, fullPath, variable, groupId, cache });
    },
    updateGroupVariable: async (_, { endpoint, fullPath, variable, groupId }, { cache }) => {
      return callGroupEndpoint({ endpoint, fullPath, variable, groupId, cache });
    },
    deleteGroupVariable: async (_, { endpoint, fullPath, variable, groupId }, { cache }) => {
      return callGroupEndpoint({ endpoint, fullPath, variable, groupId, cache, destroy: true });
    },
    addAdminVariable: async (_, { endpoint, variable }, { cache }) => {
      return callAdminEndpoint({ endpoint, variable, cache });
    },
    updateAdminVariable: async (_, { endpoint, variable }, { cache }) => {
      return callAdminEndpoint({ endpoint, variable, cache });
    },
    deleteAdminVariable: async (_, { endpoint, variable }, { cache }) => {
      return callAdminEndpoint({ endpoint, variable, cache, destroy: true });
    },
  },
};