All files / app/assets/javascripts/ci_variable_list ci_variable_list.js

71.76% Statements 61/85
51.61% Branches 16/31
64.29% Functions 18/28
71.43% Lines 60/84

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266              2x                       11x 11x 11x 11x   11x                                                                                   11x               11x 11x       11x 23x     11x 1x 1x     11x 88x       11x 1x   1x 1x       11x   4x   4x 3x       4x         26x   26x     26x   26x 26x                                               3x 3x     3x 24x 24x       3x       3x   3x       2x 2x   2x             2x         2x       6x   34x   28x 28x 28x         4x   4x 4x 4x     4x     4x     4x       6x 6x                                                                                   2x 1x 1x            
import $ from 'jquery';
import SecretValues from '../behaviors/secret_values';
import CreateItemDropdown from '../create_item_dropdown';
import { parseBoolean } from '../lib/utils/common_utils';
import { s__ } from '../locale';
import setupToggleButtons from '../toggle_buttons';
 
const ALL_ENVIRONMENTS_STRING = s__('CiVariable|All environments');
 
function createEnvironmentItem(value) {
  return {
    title: value === '*' ? ALL_ENVIRONMENTS_STRING : value,
    id: value,
    text: value === '*' ? s__('CiVariable|* (All environments)') : value,
  };
}
 
export default class VariableList {
  constructor({ container, formField, maskableRegex }) {
    this.$container = $(container);
    this.formField = formField;
    this.maskableRegex = new RegExp(maskableRegex);
    this.environmentDropdownMap = new WeakMap();
 
    this.inputMap = {
      id: {
        selector: '.js-ci-variable-input-id',
        default: '',
      },
      variable_type: {
        selector: '.js-ci-variable-input-variable-type',
        default: 'env_var',
      },
      key: {
        selector: '.js-ci-variable-input-key',
        default: '',
      },
      secret_value: {
        selector: '.js-ci-variable-input-value',
        default: '',
      },
      protected: {
        selector: '.js-ci-variable-input-protected',
        // use `attr` instead of `data` as we don't want the value to be
        // converted. we need the value as a string.
        default: $('.js-ci-variable-input-protected').attr('data-default'),
      },
      masked: {
        selector: '.js-ci-variable-input-masked',
        // use `attr` instead of `data` as we don't want the value to be
        // converted. we need the value as a string.
        default: $('.js-ci-variable-input-masked').attr('data-default'),
      },
      environment_scope: {
        // We can't use a `.js-` class here because
        // deprecated_jquery_dropdown replaces the <input> and doesn't copy over the class
        // See https://gitlab.com/gitlab-org/gitlab-foss/issues/42458
        selector: `input[name="${this.formField}[variables_attributes][][environment_scope]"]`,
        default: '*',
      },
      _destroy: {
        selector: '.js-ci-variable-input-destroy',
        default: '',
      },
    };
 
    this.secretValues = new SecretValues({
      container: this.$container[0],
      valueSelector: '.js-row:not(:last-child) .js-secret-value',
      placeholderSelector: '.js-row:not(:last-child) .js-secret-value-placeholder',
    });
  }
 
  init() {
    this.bindEvents();
    this.secretValues.init();
  }
 
  bindEvents() {
    this.$container.find('.js-row').each((index, rowEl) => {
      this.initRow(rowEl);
    });
 
    this.$container.on('click', '.js-row-remove-button', (e) => {
      e.preventDefault();
      this.removeRow($(e.currentTarget).closest('.js-row'));
    });
 
    const inputSelector = Object.keys(this.inputMap)
      .map((name) => this.inputMap[name].selector)
      .join(',');
 
    // Remove any empty rows except the last row
    this.$container.on('blur', inputSelector, (e) => {
      const $row = $(e.currentTarget).closest('.js-row');
 
      Eif ($row.is(':not(:last-child)') && !this.checkIfRowTouched($row)) {
        this.removeRow($row);
      }
    });
 
    this.$container.on('input trigger-change', inputSelector, (e) => {
      // Always make sure there is an empty last row
      const $lastRow = this.$container.find('.js-row').last();
 
      if (this.checkIfRowTouched($lastRow)) {
        this.insertRow($lastRow);
      }
 
      // If masked, validate value against regex
      this.validateMaskability($(e.currentTarget).closest('.js-row'));
    });
  }
 
  initRow(rowEl) {
    const $row = $(rowEl);
 
    setupToggleButtons($row[0]);
 
    // Reset the resizable textarea
    $row.find(this.inputMap.secret_value.selector).css('height', '');
 
    const $environmentSelect = $row.find('.js-variable-environment-toggle');
    Iif ($environmentSelect.length) {
      const createItemDropdown = new CreateItemDropdown({
        $dropdown: $environmentSelect,
        defaultToggleLabel: ALL_ENVIRONMENTS_STRING,
        fieldName: `${this.formField}[variables_attributes][][environment_scope]`,
        getData: (term, callback) => callback(this.getEnvironmentValues()),
        createNewItemFromValue: createEnvironmentItem,
        onSelect: () => {
          // Refresh the other dropdowns in the variable list
          // so they have the new value we just picked
          this.refreshDropdownData();
 
          $row.find(this.inputMap.environment_scope.selector).trigger('trigger-change');
        },
      });
 
      // Clear out any data that might have been left-over from the row clone
      createItemDropdown.clearDropdown();
 
      this.environmentDropdownMap.set($row[0], createItemDropdown);
    }
  }
 
  insertRow($row) {
    const $rowClone = $row.clone();
    $rowClone.removeAttr('data-is-persisted');
 
    // Reset the inputs to their defaults
    Object.keys(this.inputMap).forEach((name) => {
      const entry = this.inputMap[name];
      $rowClone.find(entry.selector).val(entry.default);
    });
 
    // Close any dropdowns
    $rowClone.find('.dropdown-menu.show').each((index, $dropdown) => {
      $dropdown.classList.remove('show');
    });
 
    this.initRow($rowClone);
 
    $row.after($rowClone);
  }
 
  removeRow(row) {
    const $row = $(row);
    const isPersisted = parseBoolean($row.attr('data-is-persisted'));
 
    Iif (isPersisted) {
      $row.hide();
      $row
        // eslint-disable-next-line no-underscore-dangle
        .find(this.inputMap._destroy.selector)
        .val(true);
    } else {
      $row.remove();
    }
 
    // Refresh the other dropdowns in the variable list
    // so any value with the variable deleted is gone
    this.refreshDropdownData();
  }
 
  checkIfRowTouched($row) {
    return Object.keys(this.inputMap).some((name) => {
      // Row should not qualify as touched if only switches have been touched
      if (['protected', 'masked'].includes(name)) return false;
 
      const entry = this.inputMap[name];
      const $el = $row.find(entry.selector);
      return $el.length && $el.val() !== entry.default;
    });
  }
 
  validateMaskability($row) {
    const invalidInputClass = 'gl-field-error-outline';
 
    const variableValue = $row.find(this.inputMap.secret_value.selector).val();
    const isValueMaskable = this.maskableRegex.test(variableValue) || variableValue === '';
    const isMaskedChecked = $row.find(this.inputMap.masked.selector).val() === 'true';
 
    // Show a validation error if the user wants to mask an unmaskable variable value
    $row
      .find(this.inputMap.secret_value.selector)
      .toggleClass(invalidInputClass, isMaskedChecked && !isValueMaskable);
    $row
      .find('.js-secret-value-placeholder')
      .toggleClass(invalidInputClass, isMaskedChecked && !isValueMaskable);
    $row.find('.masking-validation-error').toggle(isMaskedChecked && !isValueMaskable);
  }
 
  toggleEnableRow(isEnabled = true) {
    this.$container.find(this.inputMap.key.selector).attr('disabled', !isEnabled);
    this.$container.find('.js-row-remove-button').attr('disabled', !isEnabled);
  }
 
  hideValues() {
    this.secretValues.updateDom(false);
  }
 
  getAllData() {
    // Ignore the last empty row because we don't want to try persist
    // a blank variable and run into validation problems.
    const validRows = this.$container.find('.js-row').toArray().slice(0, -1);
 
    return validRows.map((rowEl) => {
      const resultant = {};
      Object.keys(this.inputMap).forEach((name) => {
        const entry = this.inputMap[name];
        const $input = $(rowEl).find(entry.selector);
        if ($input.length) {
          resultant[name] = $input.val();
        }
      });
 
      return resultant;
    });
  }
 
  getEnvironmentValues() {
    const valueMap = this.$container
      .find(this.inputMap.environment_scope.selector)
      .toArray()
      .reduce(
        (prevValueMap, envInput) => ({
          ...prevValueMap,
          [envInput.value]: envInput.value,
        }),
        {},
      );
 
    return Object.keys(valueMap).map(createEnvironmentItem);
  }
 
  refreshDropdownData() {
    this.$container.find('.js-row').each((index, rowEl) => {
      const environmentDropdown = this.environmentDropdownMap.get(rowEl);
      Iif (environmentDropdown) {
        environmentDropdown.refreshData();
      }
    });
  }
}