summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/ci_variable_list/components/ci_group_variables.vue
blob: 4af696b8dab13984ac7af20a44da0e3318b1247d (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
<script>
import { createAlert } from '~/flash';
import { __ } from '~/locale';
import { convertToGraphQLId } from '~/graphql_shared/utils';
import glFeatureFlagsMixin from '~/vue_shared/mixins/gl_feature_flags_mixin';
import { reportMessageToSentry } from '../utils';
import getGroupVariables from '../graphql/queries/group_variables.query.graphql';
import {
  ADD_MUTATION_ACTION,
  DELETE_MUTATION_ACTION,
  GRAPHQL_GROUP_TYPE,
  UPDATE_MUTATION_ACTION,
  genericMutationErrorText,
  variableFetchErrorText,
} from '../constants';
import addGroupVariable from '../graphql/mutations/group_add_variable.mutation.graphql';
import deleteGroupVariable from '../graphql/mutations/group_delete_variable.mutation.graphql';
import updateGroupVariable from '../graphql/mutations/group_update_variable.mutation.graphql';
import CiVariableSettings from './ci_variable_settings.vue';

export default {
  components: {
    CiVariableSettings,
  },
  mixins: [glFeatureFlagsMixin()],
  inject: ['endpoint', 'groupPath', 'groupId'],
  data() {
    return {
      groupVariables: [],
      hasNextPage: false,
      isLoadingMoreItems: false,
      loadingCounter: 0,
      pageInfo: {},
    };
  },
  apollo: {
    groupVariables: {
      query: getGroupVariables,
      variables() {
        return {
          fullPath: this.groupPath,
        };
      },
      update(data) {
        return data?.group?.ciVariables?.nodes || [];
      },
      result({ data }) {
        this.pageInfo = data?.group?.ciVariables?.pageInfo || this.pageInfo;
        this.hasNextPage = this.pageInfo?.hasNextPage || false;
        // Because graphQL has a limit of 100 items,
        // we batch load all the variables by making successive queries
        // to keep the same UX. As a safeguard, we make sure that we cannot go over
        // 20 consecutive API calls, which means 2000 variables loaded maximum.
        if (!this.hasNextPage) {
          this.isLoadingMoreItems = false;
        } else if (this.loadingCounter < 20) {
          this.hasNextPage = false;
          this.fetchMoreVariables();
          this.loadingCounter += 1;
        } else {
          createAlert({ message: this.$options.tooManyCallsError });
          reportMessageToSentry(this.$options.componentName, this.$options.tooManyCallsError, {});
        }
      },
      error() {
        this.isLoadingMoreItems = false;
        this.hasNextPage = false;
        createAlert({ message: variableFetchErrorText });
      },
    },
  },
  computed: {
    areScopedVariablesAvailable() {
      return this.glFeatures.groupScopedCiVariables;
    },
    isLoading() {
      return this.$apollo.queries.groupVariables.loading || this.isLoadingMoreItems;
    },
  },
  methods: {
    addVariable(variable) {
      this.variableMutation(ADD_MUTATION_ACTION, variable);
    },
    deleteVariable(variable) {
      this.variableMutation(DELETE_MUTATION_ACTION, variable);
    },
    fetchMoreVariables() {
      this.isLoadingMoreItems = true;

      this.$apollo.queries.groupVariables.fetchMore({
        variables: {
          fullPath: this.groupPath,
          after: this.pageInfo.endCursor,
        },
      });
    },
    updateVariable(variable) {
      this.variableMutation(UPDATE_MUTATION_ACTION, variable);
    },
    async variableMutation(mutationAction, variable) {
      try {
        const currentMutation = this.$options.mutationData[mutationAction];
        const { data } = await this.$apollo.mutate({
          mutation: currentMutation.action,
          variables: {
            endpoint: this.endpoint,
            fullPath: this.groupPath,
            groupId: convertToGraphQLId(GRAPHQL_GROUP_TYPE, this.groupId),
            variable,
          },
        });

        if (data[currentMutation.name]?.errors?.length) {
          const { errors } = data[currentMutation.name];
          createAlert({ message: errors[0] });
        }
      } catch {
        createAlert({ message: genericMutationErrorText });
      }
    },
  },
  componentName: 'GroupVariables',
  i18n: {
    tooManyCallsError: __('Maximum number of variables loaded (2000)'),
  },
  mutationData: {
    [ADD_MUTATION_ACTION]: { action: addGroupVariable, name: 'addGroupVariable' },
    [UPDATE_MUTATION_ACTION]: { action: updateGroupVariable, name: 'updateGroupVariable' },
    [DELETE_MUTATION_ACTION]: { action: deleteGroupVariable, name: 'deleteGroupVariable' },
  },
};
</script>

<template>
  <ci-variable-settings
    :are-scoped-variables-available="areScopedVariablesAvailable"
    :is-loading="isLoading"
    :variables="groupVariables"
    @add-variable="addVariable"
    @delete-variable="deleteVariable"
    @update-variable="updateVariable"
  />
</template>