All files / modules/85-cv/components/PipelineSteps/ContinousVerification utils.ts

78.57% Statements 44/56
40% Branches 28/70
81.82% Functions 9/11
77.78% Lines 42/54

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              9x 9x 9x 9x   9x   9x                         9x 24x                       9x                                       9x                                                                     9x 10x 2x   1x 1x     1x 1x         2x   8x 2x       6x       2x               9x 7x 7x 7x 35x   7x 7x   7x 7x   7x 7x   7x 7x         7x                 9x   28x   4x 1x   3x     8x        
/*
 * Copyright 2021 Harness Inc. All rights reserved.
 * Use of this source code is governed by the PolyForm Shield 1.0.0 license
 * that can be found in the licenses directory at the root of this repository, also available at
 * https://polyformproject.org/wp-content/uploads/2020/06/PolyForm-Shield-1.0.0.txt.
 */
 
import { getMultiTypeFromValue, MultiTypeInputType, RUNTIME_INPUT_VALUE, SelectOption } from '@wings-software/uicore'
import { isEmpty, isNull, isUndefined, omit, omitBy, set } from 'lodash-es'
import { yupToFormErrors } from 'formik'
import * as Yup from 'yup'
import type { UseStringsReturn } from 'framework/strings'
import { getDurationValidationSchema } from '@common/components/MultiTypeDuration/MultiTypeDuration'
import type { ContinousVerificationData, spec } from './types'
import {
  VerificationSensitivityOptions,
  durationOptions,
  baseLineOptions,
  trafficSplitPercentageOptions,
  SensitivityTypes
} from './constants'
 
/**
 * checks if a field is a runtime input.
 * @param field
 * @returns boolean
 */
export function checkIfRunTimeInput(field: string | SelectOption | number | undefined): boolean {
  return getMultiTypeFromValue(field as string) === MultiTypeInputType.RUNTIME
}
 
/**
 * validates a particular field for errors.
 * @param fieldValue
 * @param fieldKey
 * @param data
 * @param errors
 * @param getString
 * @param isRequired
 */
export function validateField(
  fieldValue: string,
  fieldKey: string,
  data: ContinousVerificationData,
  errors: any,
  getString: UseStringsReturn['getString'],
  isRequired = true
): void {
  if (checkIfRunTimeInput(fieldValue) && isRequired && isEmpty(data?.spec?.spec && data?.spec?.spec[fieldKey])) {
    set(errors, `spec.spec.${fieldKey}`, getString('fieldRequired', { field: fieldKey }))
  }
}
 
/**
 * validates timeout field.
 * @param template
 * @param getString
 * @param data
 * @param errors
 */
export function validateTimeout(
  getString: UseStringsReturn['getString'],
  data: ContinousVerificationData,
  errors: any,
  template?: ContinousVerificationData,
  isRequired = true
): void {
  if (checkIfRunTimeInput(template?.timeout)) {
    let timeout: Yup.ObjectSchema
    if (isRequired) {
      timeout = Yup.object().shape({
        timeout: getDurationValidationSchema({ minimum: '10s' }).required(getString?.('validation.timeout10SecMinimum'))
      })
    } else {
      timeout = Yup.object().shape({
        timeout: getDurationValidationSchema({ minimum: '10s' })
      })
    }
 
    try {
      timeout.validateSync(data)
    } catch (e) {
      if (e instanceof Yup.ValidationError) {
        const err = yupToFormErrors(e)
        Object.assign(errors, err)
      }
    }
  }
}
 
/**
 * returns yaml data for spec field.
 * @param specInfo
 * @returns spec
 */
export function getSpecYamlData(specInfo?: spec, type?: string): spec {
  let validspec = omitBy(specInfo, v => isUndefined(v) || isNull(v) || v === '')
  switch (type) {
    case 'LoadTest':
      validspec = omit(validspec, ['trafficsplit'])
      break
    case 'Bluegreen':
    case 'Canary':
      validspec = omit(validspec, ['baseline'])
      break
    default:
      break
  }
 
  Object.keys(validspec).map((key: string) => {
    //TODO logic in if block will be removed once backend api is fixed : https://harness.atlassian.net/browse/CVNG-2481
    if (key === 'sensitivity') {
      validspec[key] = validspec[key].value
        ? SensitivityTypes[validspec[key].value as keyof typeof SensitivityTypes]
        : validspec[key]
    } else {
      validspec[key] = validspec[key].value ? validspec[key].value : validspec[key]
    }
  })
 
  return validspec
}
 
/**
 * returns forms data for spec field.
 * @param specInfo
 * @returns spec
 */
export function getSpecFormData(specInfo: spec | undefined): spec {
  const validspec: spec | undefined = { ...specInfo }
  Eif (specInfo) {
    Object.keys(specInfo).map((key: string) => {
      switch (key) {
        case 'sensitivity':
          setFieldData(validspec, 'sensitivity', VerificationSensitivityOptions)
          break
        case 'duration':
          setFieldData(validspec, 'duration', durationOptions)
          break
        case 'baseline':
          setFieldData(validspec, 'baseline', baseLineOptions)
          break
        case 'trafficsplit':
          setFieldData(validspec, 'trafficsplit', trafficSplitPercentageOptions)
          break
        default:
      }
    })
  }
  return validspec
}
 
/**
 * sets particular field data
 * @param validspec
 * @param field
 * @param options
 */
export function setFieldData(validspec: spec | undefined, field: string, fieldOptions: SelectOption[]): void {
  //finding the complete option if the field is fixed input
  if (validspec && validspec[field] && validspec[field] !== RUNTIME_INPUT_VALUE) {
    //TODO logic in if block will be removed once backend api is fixed : https://harness.atlassian.net/browse/CVNG-2481
    if (field === 'sensitivity') {
      validspec[field] = fieldOptions.find(
        (el: SelectOption) =>
          SensitivityTypes[el.value as keyof typeof SensitivityTypes] === (validspec && validspec[field])
      )
    } else {
      validspec[field] = fieldOptions.find((el: SelectOption) => el.value === (validspec && validspec[field]))
    }
  }
}