summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/group_settings/components/shared_runners_form.vue
blob: 3365f4aa76cd3cb6732d23451235542314cee1b8 (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
<script>
import { GlToggle, GlAlert } from '@gitlab/ui';
import axios from '~/lib/utils/axios_utils';
import { __ } from '~/locale';
import { ERROR_MESSAGE } from '../constants';

export default {
  components: {
    GlToggle,
    GlAlert,
  },
  inject: [
    'updatePath',
    'sharedRunnersSetting',
    'parentSharedRunnersSetting',
    'runnerEnabledValue',
    'runnerDisabledValue',
    'runnerAllowOverrideValue',
  ],
  data() {
    return {
      isLoading: false,
      value: this.sharedRunnersSetting,
      error: null,
    };
  },
  computed: {
    isSharedRunnersToggleDisabled() {
      return this.parentSharedRunnersSetting === this.runnerDisabledValue;
    },
    sharedRunnersToggleValue() {
      return this.value === this.runnerEnabledValue;
    },
    isOverrideToggleDisabled() {
      // cannot override when sharing is enabled
      return this.isSharedRunnersToggleDisabled || this.value === this.runnerEnabledValue;
    },
    overrideToggleValue() {
      return this.value === this.runnerAllowOverrideValue;
    },
  },
  methods: {
    onSharedRunnersToggle(value) {
      const newSetting = value ? this.runnerEnabledValue : this.runnerDisabledValue;
      this.updateSetting(newSetting);
    },
    onOverrideToggle(value) {
      const newSetting = value ? this.runnerAllowOverrideValue : this.runnerDisabledValue;
      this.updateSetting(newSetting);
    },
    updateSetting(setting) {
      if (this.isLoading) {
        return;
      }

      this.isLoading = true;

      axios
        .put(this.updatePath, { shared_runners_setting: setting })
        .then(() => {
          this.value = setting;
        })
        .catch((error) => {
          const message = [
            error.response?.data?.error || __('An error occurred while updating configuration.'),
            ERROR_MESSAGE,
          ].join(' ');

          this.error = message;
        })
        .finally(() => {
          this.isLoading = false;
        });
    },
  },
};
</script>

<template>
  <div>
    <gl-alert v-if="error" variant="danger" :dismissible="false" class="gl-mb-5">
      {{ error }}
    </gl-alert>

    <gl-alert
      v-if="isSharedRunnersToggleDisabled"
      variant="warning"
      :dismissible="false"
      class="gl-mb-5"
    >
      {{ __('Shared runners are disabled for the parent group') }}
    </gl-alert>

    <section class="gl-mb-5">
      <gl-toggle
        :value="sharedRunnersToggleValue"
        :is-loading="isLoading"
        :disabled="isSharedRunnersToggleDisabled"
        :label="__('Enable shared runners for this group')"
        :help="__('Enable shared runners for all projects and subgroups in this group.')"
        data-testid="shared-runners-toggle"
        @change="onSharedRunnersToggle"
      />
    </section>

    <section class="gl-mb-5">
      <gl-toggle
        :value="overrideToggleValue"
        :is-loading="isLoading"
        :disabled="isOverrideToggleDisabled"
        :label="__('Allow projects and subgroups to override the group setting')"
        :help="__('Allows projects or subgroups in this group to override the global setting.')"
        data-testid="override-runners-toggle"
        @change="onOverrideToggle"
      />
    </section>
  </div>
</template>