All files / modules/85-cv/components/PipelineSteps/ContinousVerification ContinousVerificationStep.tsx

68.29% Statements 28/41
37.84% Branches 28/74
57.14% Functions 4/7
68.29% Lines 28/41

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              9x   9x 9x 9x   9x 9x       9x 9x 9x 9x 9x   9x 9x   19x 19x     19x 19x 19x 19x 19x 19x                         9x   9x 2x                     7x               7x         28x                                                       7x                                      
/*
 * 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 React from 'react'
import type { IconName } from '@wings-software/uicore'
import { connect, FormikErrors } from 'formik'
import { omit } from 'lodash-es'
import { StepViewType, StepProps, ValidateInputSetProps } from '@pipeline/components/AbstractSteps/Step'
 
import { StepType } from '@pipeline/components/PipelineSteps/PipelineStepInterface'
import { PipelineStep } from '@pipeline/components/PipelineSteps/PipelineStep'
 
import type { StringsMap } from 'stringTypes'
import type { ContinousVerificationData, ContinousVerificationVariableStepProps, spec } from './types'
import { ContinousVerificationWidgetWithRef } from './components/ContinousVerificationWidget/ContinousVerificationWidget'
import { ContinousVerificationInputSetStep } from './components/ContinousVerificationInputSetStep/ContinousVerificationInputSetStep'
import { ContinousVerificationVariableStep } from './components/ContinousVerificationVariableStep/ContinousVerificationVariableStep'
import { getSpecFormData, getSpecYamlData, validateField, validateTimeout } from './utils'
import { cvDefaultValues } from './constants'
 
const ContinousVerificationInputSetStepFormik = connect(ContinousVerificationInputSetStep)
export class ContinousVerificationStep extends PipelineStep<ContinousVerificationData> {
  constructor() {
    super()
    this._hasStepVariables = true
  }
 
  protected type = StepType.Verify
  protected stepName = 'Verify'
  protected stepIcon: IconName = 'cv-main'
  protected stepDescription: keyof StringsMap = 'pipeline.stepDescription.Verify'
  protected isHarnessSpecific = true
  protected defaultValues: ContinousVerificationData = cvDefaultValues
 
  renderStep(props: StepProps<ContinousVerificationData>): JSX.Element {
    const {
      initialValues,
      onUpdate,
      stepViewType,
      inputSetData,
      formikRef,
      customStepProps,
      isNewStep,
      allowableTypes,
      onChange
    } = props
 
    if (stepViewType === StepViewType.InputSet || stepViewType === StepViewType.DeploymentForm) {
      return (
        <ContinousVerificationInputSetStepFormik
          initialValues={initialValues}
          onUpdate={onUpdate}
          stepViewType={stepViewType}
          readonly={!!inputSetData?.readonly}
          template={inputSetData?.template}
          path={inputSetData?.path || ''}
          allowableTypes={allowableTypes}
        />
      )
    } else Iif (stepViewType === StepViewType.InputVariable) {
      return (
        <ContinousVerificationVariableStep
          {...(customStepProps as ContinousVerificationVariableStepProps)}
          originalData={initialValues}
        />
      )
    }
    return (
      <ContinousVerificationWidgetWithRef
        initialValues={this.getInitialValues(initialValues)}
        onUpdate={data => onUpdate?.(this.processFormData(data))}
        isNewStep={isNewStep}
        onChange={data => onChange?.(this.processFormData(data))}
        allowableTypes={allowableTypes}
        stepViewType={stepViewType}
        ref={formikRef}
      />
    )
  }
 
  validateInputSet({
    data,
    template,
    getString,
    viewType
  }: ValidateInputSetProps<ContinousVerificationData>): FormikErrors<ContinousVerificationData> {
    const errors: FormikErrors<ContinousVerificationData> = {}
    const { sensitivity, duration, baseline, deploymentTag } = (template?.spec?.spec as spec) || {}
    const isRequired = viewType === StepViewType.DeploymentForm || viewType === StepViewType.TriggerForm
    if (getString) {
      validateField(sensitivity as string, 'sensitivity', data, errors, getString, isRequired)
      validateField(duration as string, 'duration', data, errors, getString, isRequired)
      validateField(baseline as string, 'baseline', data, errors, getString, isRequired)
      validateField(deploymentTag as string, 'deploymentTag', data, errors, getString, isRequired)
      validateTimeout(getString, data, errors, template, isRequired)
    }
    return errors
  }
 
  private getInitialValues(initialValues: ContinousVerificationData): ContinousVerificationData {
    return {
      ...initialValues,
      spec: {
        ...initialValues?.spec,
        spec: getSpecFormData(initialValues?.spec?.spec)
      }
    }
  }
 
  processFormData(data: ContinousVerificationData): ContinousVerificationData {
    return {
      ...data,
      spec: {
        ...omit(data?.spec, ['monitoredServiceRef', 'healthSources']),
        spec: getSpecYamlData(data?.spec?.spec, data?.spec?.type)
      }
    }
  }
}