All files / app/assets/javascripts/alerts_settings/utils mapping_transformations.js

100% Statements 25/25
100% Branches 6/6
100% Functions 9/9
100% Lines 24/24

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                        4x 31x   310x 950x       310x 299x   310x   310x                     4x 31x 82x     82x 82x 2x 2x   80x     82x                         4x 2x 4x 2x 1x 1x             2x      
import { isEqual } from 'lodash';
import { capitalizeFirstCharacter } from '~/lib/utils/text_utility';
 
/**
 * Given data for GitLab alert fields, parsed payload fields data and previously stored mapping (if any)
 * creates an object in a form convenient to build UI && interact with it
 * @param {Object} gitlabFields  - structure describing GitLab alert fields
 * @param {Object} payloadFields - parsed from sample JSON sample alert fields
 * @param {Object} savedMapping  - GitLab fields to parsed fields mapping
 *
 * @return {Object} mapping data for UI mapping builder
 */
export const getMappingData = (gitlabFields, payloadFields, savedMapping) => {
  return gitlabFields.map((gitlabField) => {
    // find fields from payload that match gitlab alert field by type
    const mappingFields = payloadFields.filter(({ type }) =>
      gitlabField.types.includes(type.toLowerCase()),
    );
 
    // find the mapping that was previously stored
    const foundMapping = savedMapping.find(
      ({ fieldName }) => fieldName.toLowerCase() === gitlabField.name,
    );
    const { path: mapping, fallbackPath: fallback } = foundMapping || {};
 
    return {
      mapping,
      fallback,
      searchTerm: '',
      fallbackSearchTerm: '',
      mappingFields,
      ...gitlabField,
    };
  });
};
 
export const setFieldsLabels = (fields) => {
  return fields.map((field) => {
    const { label } = field;
    let displayLabel;
    let tooltip;
    const labels = label.split('/');
    if (labels.length > 1) {
      tooltip = labels.join('.');
      displayLabel = `...${capitalizeFirstCharacter(labels.pop())}`;
    } else {
      displayLabel = capitalizeFirstCharacter(label);
    }
 
    return {
      ...field,
      displayLabel,
      tooltip,
    };
  });
};
/**
 * Based on mapping data configured by the user creates an object in a format suitable for save on BE
 * @param {Object} mappingData  - structure describing mapping between GitLab fields and parsed payload fields
 *
 * @return {Object} mapping data  to send to BE
 */
export const transformForSave = (mappingData) => {
  return mappingData.reduce((acc, field) => {
    const mapped = field.mappingFields.find(({ path }) => isEqual(path, field.mapping));
    if (mapped) {
      const { path, type, label } = mapped;
      acc.push({
        fieldName: field.name.toUpperCase(),
        path,
        type: type.toUpperCase(),
        label,
      });
    }
    return acc;
  }, []);
};