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

registration_token_reset_dropdown_item.vue « registration « components « runner « javascripts « assets « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 0e259807f98f8151e3a3dfb221d31d904acd7153 (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
<script>
import { GlDropdownItem, GlLoadingIcon, GlModal, GlModalDirective } from '@gitlab/ui';
import { createAlert } from '~/flash';
import { TYPE_GROUP, TYPE_PROJECT } from '~/graphql_shared/constants';
import { convertToGraphQLId } from '~/graphql_shared/utils';
import { __, s__ } from '~/locale';
import runnersRegistrationTokenResetMutation from '~/runner/graphql/runners_registration_token_reset.mutation.graphql';
import { captureException } from '~/runner/sentry_utils';
import { INSTANCE_TYPE, GROUP_TYPE, PROJECT_TYPE } from '../../constants';

export default {
  name: 'RunnerRegistrationTokenReset',
  i18n: {
    modalTitle: __('Reset registration token'),
    modalCopy: __('Are you sure you want to reset the registration token?'),
  },
  components: {
    GlDropdownItem,
    GlLoadingIcon,
    GlModal,
  },
  directives: {
    GlModal: GlModalDirective,
  },
  inject: {
    groupId: {
      default: null,
    },
    projectId: {
      default: null,
    },
  },
  modalID: 'token-reset-modal',
  props: {
    type: {
      type: String,
      required: true,
      validator(type) {
        return [INSTANCE_TYPE, GROUP_TYPE, PROJECT_TYPE].includes(type);
      },
    },
  },
  data() {
    return {
      loading: false,
    };
  },
  computed: {
    resetTokenInput() {
      switch (this.type) {
        case INSTANCE_TYPE:
          return {
            type: this.type,
          };
        case GROUP_TYPE:
          return {
            id: convertToGraphQLId(TYPE_GROUP, this.groupId),
            type: this.type,
          };
        case PROJECT_TYPE:
          return {
            id: convertToGraphQLId(TYPE_PROJECT, this.projectId),
            type: this.type,
          };
        default:
          return null;
      }
    },
  },
  methods: {
    handleModalPrimary() {
      this.resetToken();
    },
    async resetToken() {
      this.loading = true;
      try {
        const {
          data: {
            runnersRegistrationTokenReset: { token, errors },
          },
        } = await this.$apollo.mutate({
          mutation: runnersRegistrationTokenResetMutation,
          variables: {
            input: this.resetTokenInput,
          },
        });
        if (errors && errors.length) {
          throw new Error(errors.join(' '));
        }
        this.onSuccess(token);
      } catch (e) {
        this.onError(e);
      } finally {
        this.loading = false;
      }
    },
    onError(error) {
      const { message } = error;
      createAlert({ message });

      this.reportToSentry(error);
    },
    onSuccess(token) {
      this.$toast?.show(s__('Runners|New registration token generated!'));
      this.$emit('tokenReset', token);
    },
    reportToSentry(error) {
      captureException({ error, component: this.$options.name });
    },
  },
};
</script>
<template>
  <gl-dropdown-item v-gl-modal="$options.modalID">
    {{ __('Reset registration token') }}
    <gl-modal
      :modal-id="$options.modalID"
      :title="$options.i18n.modalTitle"
      @primary="handleModalPrimary"
    >
      <p>{{ $options.i18n.modalCopy }}</p>
    </gl-modal>
    <gl-loading-icon v-if="loading" inline />
  </gl-dropdown-item>
</template>