summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/ci_variable_list/components/ci_variable_modal.vue
blob: 3e58fc407555dae2df7adb0745cffc43de3a6f6a (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
<script>
import { __ } from '~/locale';
import { mapActions, mapState } from 'vuex';
import { ADD_CI_VARIABLE_MODAL_ID } from '../constants';
import {
  GlModal,
  GlFormSelect,
  GlFormGroup,
  GlFormInput,
  GlFormCheckbox,
  GlLink,
  GlIcon,
} from '@gitlab/ui';

export default {
  modalId: ADD_CI_VARIABLE_MODAL_ID,
  components: {
    GlModal,
    GlFormSelect,
    GlFormGroup,
    GlFormInput,
    GlFormCheckbox,
    GlLink,
    GlIcon,
  },
  computed: {
    ...mapState([
      'projectId',
      'environments',
      'typeOptions',
      'variable',
      'variableBeingEdited',
      'isGroup',
      'maskableRegex',
    ]),
    canSubmit() {
      return this.variableData.key !== '' && this.variableData.secret_value !== '';
    },
    canMask() {
      const regex = RegExp(this.maskableRegex);
      return regex.test(this.variableData.secret_value);
    },
    variableData() {
      return this.variableBeingEdited || this.variable;
    },
    modalActionText() {
      return this.variableBeingEdited ? __('Update Variable') : __('Add variable');
    },
    primaryAction() {
      return {
        text: this.modalActionText,
        attributes: { variant: 'success', disabled: !this.canSubmit },
      };
    },
    cancelAction() {
      return {
        text: __('Cancel'),
      };
    },
  },
  methods: {
    ...mapActions([
      'addVariable',
      'updateVariable',
      'resetEditing',
      'displayInputValue',
      'clearModal',
    ]),
    updateOrAddVariable() {
      if (this.variableBeingEdited) {
        this.updateVariable(this.variableBeingEdited);
      } else {
        this.addVariable();
      }
    },
    resetModalHandler() {
      if (this.variableBeingEdited) {
        this.resetEditing();
      } else {
        this.clearModal();
      }
    },
  },
};
</script>

<template>
  <gl-modal
    :modal-id="$options.modalId"
    :title="modalActionText"
    :action-primary="primaryAction"
    :action-cancel="cancelAction"
    @ok="updateOrAddVariable"
    @hidden="resetModalHandler"
  >
    <form>
      <gl-form-group label="Type" label-for="ci-variable-type">
        <gl-form-select
          id="ci-variable-type"
          v-model="variableData.variable_type"
          :options="typeOptions"
        />
      </gl-form-group>

      <div class="d-flex">
        <gl-form-group label="Key" label-for="ci-variable-key" class="w-50 append-right-15">
          <gl-form-input
            id="ci-variable-key"
            v-model="variableData.key"
            type="text"
            data-qa-selector="variable_key"
          />
        </gl-form-group>

        <gl-form-group label="Value" label-for="ci-variable-value" class="w-50">
          <gl-form-input
            id="ci-variable-value"
            v-model="variableData.secret_value"
            type="text"
            data-qa-selector="variable_value"
          />
        </gl-form-group>
      </div>

      <gl-form-group v-if="!isGroup" label="Environment scope" label-for="ci-variable-env">
        <gl-form-select
          id="ci-variable-env"
          v-model="variableData.environment_scope"
          :options="environments"
        />
      </gl-form-group>

      <gl-form-group label="Flags" label-for="ci-variable-flags">
        <gl-form-checkbox v-model="variableData.protected" class="mb-0">
          {{ __('Protect variable') }}
          <gl-link href="/help/ci/variables/README#protected-environment-variables">
            <gl-icon name="question" :size="12" />
          </gl-link>
          <p class="prepend-top-4 clgray">
            {{ __('Allow variables to run on protected branches and tags.') }}
          </p>
        </gl-form-checkbox>

        <gl-form-checkbox
          ref="masked-ci-variable"
          v-model="variableData.masked"
          :disabled="!canMask"
          data-qa-selector="variable_masked"
        >
          {{ __('Mask variable') }}
          <gl-link href="/help/ci/variables/README#masked-variables">
            <gl-icon name="question" :size="12" />
          </gl-link>
          <p class="prepend-top-4 append-bottom-0 clgray">
            {{
              __(
                'Variables will be masked in job logs. Requires values to meet regular expression requirements.',
              )
            }}
            <gl-link href="/help/ci/variables/README#masked-variables">{{
              __('More information')
            }}</gl-link>
          </p>
        </gl-form-checkbox>
      </gl-form-group>
    </form>
  </gl-modal>
</template>