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

runner_delete_action.vue « components « runner « ci « javascripts « assets « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: db8133c1ccb184fb3ea34d0d907a7bd7f5e7a5c4 (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
<script>
import runnerDeleteMutation from '~/ci/runner/graphql/shared/runner_delete.mutation.graphql';
import { createAlert } from '~/alert';
import { sprintf, s__ } from '~/locale';
import { captureException } from '~/ci/runner/sentry_utils';
import { getIdFromGraphQLId } from '~/graphql_shared/utils';
import { I18N_DELETED_TOAST } from '../constants';
import RunnerDeleteModal from './runner_delete_modal.vue';

/**
 * Component that wraps a delete GraphQL mutation for the
 * runner, given its id.
 *
 * You can use the slot to define a presentation for the
 * delete action, like a button or dropdown item.
 *
 * Usage:
 *
 * ```vue
 * <runner-delete-action
 *   #default="{ loading, onClick }"
 *   :runner="runner"
 *   @done="onDeleted"
 * >
 *   <button :disabled="loading" @click="onClick"> Delete! </button>
 * </runner-pause-action>
 * ```
 *
 */
export default {
  name: 'RunnerDeleteAction',
  components: {
    RunnerDeleteModal,
  },
  props: {
    runner: {
      type: Object,
      required: true,
      validator: (runner) => {
        return runner?.id && runner?.shortSha;
      },
    },
  },
  emits: ['done'],
  data() {
    return {
      loading: false,
    };
  },
  computed: {
    runnerId() {
      return getIdFromGraphQLId(this.runner.id);
    },
    runnerName() {
      return `#${this.runnerId} (${this.runner.shortSha})`;
    },
    runnerManagersCount() {
      return this.runner.managers?.count || 0;
    },
    runnerDeleteModalId() {
      return `delete-runner-modal-${this.runnerId}`;
    },
  },
  methods: {
    onClick() {
      this.$refs.modal.show();
    },
    async onDelete() {
      // "loading" stays "true" until this row is removed,
      // should only change back if the operation fails.
      this.loading = true;
      try {
        await this.$apollo.mutate({
          mutation: runnerDeleteMutation,
          variables: {
            input: {
              id: this.runner.id,
            },
          },
          update: (cache, { data }) => {
            const { errors } = data.runnerDelete;

            if (errors?.length) {
              this.onError(new Error(errors.join(' ')));
              return;
            }

            this.$emit('done', {
              message: sprintf(I18N_DELETED_TOAST, { name: this.runnerName }),
            });

            // Remove deleted runner from the cache
            const cacheId = cache.identify(this.runner);
            cache.evict({ id: cacheId });
            cache.gc();
          },
        });
      } catch (e) {
        this.onError(e);
      }
    },
    onError(error) {
      this.loading = false;
      const { message } = error;
      const title = sprintf(s__('Runners|Runner %{runnerName} failed to delete'), {
        runnerName: this.runnerName,
      });

      createAlert({ title, message });
      captureException({ error, component: this.$options.name });
    },
  },
};
</script>
<template>
  <div>
    <slot :loading="loading" :on-click="onClick"></slot>
    <runner-delete-modal
      ref="modal"
      :modal-id="runnerDeleteModalId"
      :runner-name="runnerName"
      :managers-count="runnerManagersCount"
      @primary="onDelete"
    />
  </div>
</template>