Welcome to mirror list, hosted at ThFree Co, Russian Federation.

ci_variable_shared.vue « components « ci_variable_list « ci « javascripts « assets « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: ee2c0a771cf43d847e6d7ea0ddfafc671e4c6962 (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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
<script>
import { createAlert } from '~/alert';
import { __ } from '~/locale';
import glFeatureFlagsMixin from '~/vue_shared/mixins/gl_feature_flags_mixin';
import { mapEnvironmentNames, reportMessageToSentry } from '../utils';
import {
  ADD_MUTATION_ACTION,
  DELETE_MUTATION_ACTION,
  ENVIRONMENT_QUERY_LIMIT,
  SORT_DIRECTIONS,
  UPDATE_MUTATION_ACTION,
  mapMutationActionToToast,
  environmentFetchErrorText,
  genericMutationErrorText,
  variableFetchErrorText,
} from '../constants';
import CiVariableSettings from './ci_variable_settings.vue';

export default {
  components: {
    CiVariableSettings,
  },
  mixins: [glFeatureFlagsMixin()],
  inject: ['endpoint'],
  props: {
    areScopedVariablesAvailable: {
      required: true,
      type: Boolean,
    },
    componentName: {
      required: true,
      type: String,
    },
    entity: {
      required: false,
      type: String,
      default: '',
    },
    fullPath: {
      required: false,
      type: String,
      default: null,
    },
    hideEnvironmentScope: {
      type: Boolean,
      required: false,
      default: false,
    },
    id: {
      required: false,
      type: String,
      default: null,
    },
    mutationData: {
      required: true,
      type: Object,
      validator: (obj) => {
        const hasValidKeys = Object.keys(obj).includes(
          ADD_MUTATION_ACTION,
          UPDATE_MUTATION_ACTION,
          DELETE_MUTATION_ACTION,
        );

        const hasValidValues = Object.values(obj).reduce((acc, val) => {
          return acc && typeof val === 'object';
        }, true);

        return hasValidKeys && hasValidValues;
      },
    },
    refetchAfterMutation: {
      required: false,
      type: Boolean,
      default: false,
    },
    queryData: {
      required: true,
      type: Object,
      validator: (obj) => {
        const { ciVariables, environments } = obj;
        const hasCiVariablesKey = Boolean(ciVariables);
        let hasCorrectEnvData = true;

        const hasCorrectVariablesData =
          typeof ciVariables?.lookup === 'function' && typeof ciVariables.query === 'object';

        if (environments) {
          hasCorrectEnvData =
            typeof environments?.lookup === 'function' && typeof environments.query === 'object';
        }

        return hasCiVariablesKey && hasCorrectVariablesData && hasCorrectEnvData;
      },
    },
  },
  data() {
    return {
      ciVariables: [],
      hasNextPage: false,
      isInitialLoading: true,
      isLoadingMoreItems: false,
      loadingCounter: 0,
      maxVariableLimit: 0,
      pageInfo: {},
      sortDirection: SORT_DIRECTIONS.ASC,
    };
  },
  apollo: {
    ciVariables: {
      query() {
        return this.queryData.ciVariables.query;
      },
      variables() {
        return {
          fullPath: this.fullPath || undefined,
          first: this.pageSize,
          sort: this.sortDirection,
        };
      },
      update(data) {
        return this.queryData.ciVariables.lookup(data)?.nodes || [];
      },
      result({ data }) {
        this.maxVariableLimit = this.queryData.ciVariables.lookup(data)?.limit || 0;

        this.pageInfo = this.queryData.ciVariables.lookup(data)?.pageInfo || this.pageInfo;

        if (!this.glFeatures?.ciVariablesPages) {
          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.componentName, this.$options.tooManyCallsError, {});
          }
        }
      },
      error() {
        this.isLoadingMoreItems = false;
        this.hasNextPage = false;
        createAlert({ message: variableFetchErrorText });
      },
      watchLoading(flag) {
        if (!flag) {
          this.isInitialLoading = false;
        }
      },
    },
    environments: {
      query() {
        return this.queryData?.environments?.query || {};
      },
      skip() {
        return !this.queryData?.environments?.query;
      },
      variables() {
        return {
          fullPath: this.fullPath,
          ...this.environmentQueryVariables,
        };
      },
      update(data) {
        return mapEnvironmentNames(this.queryData.environments.lookup(data)?.nodes);
      },
      error() {
        createAlert({ message: environmentFetchErrorText });
      },
    },
  },
  computed: {
    areEnvironmentsLoading() {
      return this.$apollo.queries.environments.loading;
    },
    environmentQueryVariables() {
      if (this.glFeatures?.ciLimitEnvironmentScope) {
        return {
          first: ENVIRONMENT_QUERY_LIMIT,
          search: '',
        };
      }

      return {};
    },
    isLoading() {
      // TODO: Remove areEnvironmentsLoading and show loading icon in dropdown when
      // environment query is loading and FF is enabled
      // https://gitlab.com/gitlab-org/gitlab/-/issues/396990
      return (
        (this.$apollo.queries.ciVariables.loading && this.isInitialLoading) ||
        this.areEnvironmentsLoading ||
        this.isLoadingMoreItems
      );
    },
    pageSize() {
      return this.glFeatures?.ciVariablesPages ? 20 : 100;
    },
  },
  methods: {
    addVariable(variable) {
      this.variableMutation(ADD_MUTATION_ACTION, variable);
    },
    deleteVariable(variable) {
      this.variableMutation(DELETE_MUTATION_ACTION, variable);
    },
    fetchMoreVariables() {
      this.isLoadingMoreItems = true;

      this.$apollo.queries.ciVariables.fetchMore({
        variables: {
          after: this.pageInfo.endCursor,
        },
      });
    },
    handlePrevPage() {
      this.$apollo.queries.ciVariables.fetchMore({
        variables: {
          before: this.pageInfo.startCursor,
          first: null,
          last: this.pageSize,
        },
      });
    },
    handleNextPage() {
      this.$apollo.queries.ciVariables.fetchMore({
        variables: {
          after: this.pageInfo.endCursor,
          first: this.pageSize,
          last: null,
        },
      });
    },
    async handleSortChanged({ sortDesc }) {
      this.sortDirection = sortDesc ? SORT_DIRECTIONS.DESC : SORT_DIRECTIONS.ASC;

      // Wait for the new sort direction to be updated and then refetch
      await this.$nextTick();
      this.$apollo.queries.ciVariables.refetch();
    },
    updateVariable(variable) {
      this.variableMutation(UPDATE_MUTATION_ACTION, variable);
    },
    async searchEnvironmentScope(searchTerm) {
      if (this.glFeatures?.ciLimitEnvironmentScope) {
        this.$apollo.queries.environments.refetch({ search: searchTerm });
      }
    },
    async variableMutation(mutationAction, variable) {
      try {
        const currentMutation = this.mutationData[mutationAction];

        const { data } = await this.$apollo.mutate({
          mutation: currentMutation,
          variables: {
            endpoint: this.endpoint,
            fullPath: this.fullPath || undefined,
            id: this.id || undefined,
            variable,
          },
        });

        if (data.ciVariableMutation?.errors?.length) {
          const { errors } = data.ciVariableMutation;
          createAlert({ message: errors[0] });
        } else {
          this.$toast.show(mapMutationActionToToast[mutationAction](variable.key));

          if (this.refetchAfterMutation) {
            // The writing to cache for admin variable is not working
            // because there is no ID in the cache at the top level.
            // We therefore need to manually refetch.
            this.$apollo.queries.ciVariables.refetch();
          }
        }
      } catch (e) {
        createAlert({ message: genericMutationErrorText });
      }
    },
  },
  i18n: {
    tooManyCallsError: __('Maximum number of variables loaded (2000)'),
  },
};
</script>

<template>
  <ci-variable-settings
    :are-environments-loading="areEnvironmentsLoading"
    :are-scoped-variables-available="areScopedVariablesAvailable"
    :entity="entity"
    :environments="environments"
    :hide-environment-scope="hideEnvironmentScope"
    :is-loading="isLoading"
    :max-variable-limit="maxVariableLimit"
    :page-info="pageInfo"
    :variables="ciVariables"
    @add-variable="addVariable"
    @delete-variable="deleteVariable"
    @handle-prev-page="handlePrevPage"
    @handle-next-page="handleNextPage"
    @sort-changed="handleSortChanged"
    @search-environment-scope="searchEnvironmentScope"
    @update-variable="updateVariable"
  />
</template>