2021-06-16 18:10:35 +00:00
|
|
|
<script>
|
2022-01-14 12:18:55 +00:00
|
|
|
import { GlDropdownItem, GlLoadingIcon, GlModal, GlModalDirective } from '@gitlab/ui';
|
2022-01-12 09:15:13 +00:00
|
|
|
import { createAlert } from '~/flash';
|
2021-07-28 12:10:41 +00:00
|
|
|
import { TYPE_GROUP, TYPE_PROJECT } from '~/graphql_shared/constants';
|
|
|
|
import { convertToGraphQLId } from '~/graphql_shared/utils';
|
2021-06-16 18:10:35 +00:00
|
|
|
import { __, s__ } from '~/locale';
|
|
|
|
import runnersRegistrationTokenResetMutation from '~/runner/graphql/runners_registration_token_reset.mutation.graphql';
|
2021-07-02 12:08:31 +00:00
|
|
|
import { captureException } from '~/runner/sentry_utils';
|
2021-10-22 18:13:20 +00:00
|
|
|
import { INSTANCE_TYPE, GROUP_TYPE, PROJECT_TYPE } from '../../constants';
|
2021-06-16 18:10:35 +00:00
|
|
|
|
|
|
|
export default {
|
2021-07-02 12:08:31 +00:00
|
|
|
name: 'RunnerRegistrationTokenReset',
|
2022-01-14 12:18:55 +00:00
|
|
|
i18n: {
|
|
|
|
modalTitle: __('Reset registration token'),
|
|
|
|
modalCopy: __('Are you sure you want to reset the registration token?'),
|
|
|
|
},
|
2021-06-16 18:10:35 +00:00
|
|
|
components: {
|
2021-10-22 18:13:20 +00:00
|
|
|
GlDropdownItem,
|
|
|
|
GlLoadingIcon,
|
2022-01-14 12:18:55 +00:00
|
|
|
GlModal,
|
|
|
|
},
|
|
|
|
directives: {
|
|
|
|
GlModal: GlModalDirective,
|
2021-06-16 18:10:35 +00:00
|
|
|
},
|
2021-07-28 12:10:41 +00:00
|
|
|
inject: {
|
|
|
|
groupId: {
|
|
|
|
default: null,
|
|
|
|
},
|
|
|
|
projectId: {
|
|
|
|
default: null,
|
|
|
|
},
|
|
|
|
},
|
2022-01-14 12:18:55 +00:00
|
|
|
modalID: 'token-reset-modal',
|
2021-06-16 18:10:35 +00:00
|
|
|
props: {
|
|
|
|
type: {
|
|
|
|
type: String,
|
|
|
|
required: true,
|
|
|
|
validator(type) {
|
|
|
|
return [INSTANCE_TYPE, GROUP_TYPE, PROJECT_TYPE].includes(type);
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
data() {
|
|
|
|
return {
|
|
|
|
loading: false,
|
|
|
|
};
|
|
|
|
},
|
2021-07-28 12:10:41 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
2021-06-16 18:10:35 +00:00
|
|
|
methods: {
|
2022-01-14 12:18:55 +00:00
|
|
|
handleModalPrimary() {
|
|
|
|
this.resetToken();
|
|
|
|
},
|
2021-06-16 18:10:35 +00:00
|
|
|
async resetToken() {
|
|
|
|
this.loading = true;
|
|
|
|
try {
|
|
|
|
const {
|
|
|
|
data: {
|
|
|
|
runnersRegistrationTokenReset: { token, errors },
|
|
|
|
},
|
|
|
|
} = await this.$apollo.mutate({
|
|
|
|
mutation: runnersRegistrationTokenResetMutation,
|
|
|
|
variables: {
|
2021-07-28 12:10:41 +00:00
|
|
|
input: this.resetTokenInput,
|
2021-06-16 18:10:35 +00:00
|
|
|
},
|
|
|
|
});
|
|
|
|
if (errors && errors.length) {
|
2021-07-02 12:08:31 +00:00
|
|
|
throw new Error(errors.join(' '));
|
2021-06-16 18:10:35 +00:00
|
|
|
}
|
|
|
|
this.onSuccess(token);
|
|
|
|
} catch (e) {
|
|
|
|
this.onError(e);
|
|
|
|
} finally {
|
|
|
|
this.loading = false;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
onError(error) {
|
|
|
|
const { message } = error;
|
2022-01-12 09:15:13 +00:00
|
|
|
createAlert({ message });
|
2021-07-02 12:08:31 +00:00
|
|
|
|
|
|
|
this.reportToSentry(error);
|
2021-06-16 18:10:35 +00:00
|
|
|
},
|
|
|
|
onSuccess(token) {
|
2021-10-22 18:13:20 +00:00
|
|
|
this.$toast?.show(s__('Runners|New registration token generated!'));
|
2021-06-16 18:10:35 +00:00
|
|
|
this.$emit('tokenReset', token);
|
|
|
|
},
|
2021-07-02 12:08:31 +00:00
|
|
|
reportToSentry(error) {
|
|
|
|
captureException({ error, component: this.$options.name });
|
|
|
|
},
|
2021-06-16 18:10:35 +00:00
|
|
|
},
|
|
|
|
};
|
|
|
|
</script>
|
|
|
|
<template>
|
2022-01-14 12:18:55 +00:00
|
|
|
<gl-dropdown-item v-gl-modal="$options.modalID">
|
2021-06-16 18:10:35 +00:00
|
|
|
{{ __('Reset registration token') }}
|
2022-01-14 12:18:55 +00:00
|
|
|
<gl-modal
|
|
|
|
:modal-id="$options.modalID"
|
|
|
|
:title="$options.i18n.modalTitle"
|
|
|
|
@primary="handleModalPrimary"
|
|
|
|
>
|
|
|
|
<p>{{ $options.i18n.modalCopy }}</p>
|
|
|
|
</gl-modal>
|
2021-10-22 18:13:20 +00:00
|
|
|
<gl-loading-icon v-if="loading" inline />
|
|
|
|
</gl-dropdown-item>
|
2021-06-16 18:10:35 +00:00
|
|
|
</template>
|