All files / modules/75-cd/components/PipelineSteps/PolicyStep PolicyStep.tsx

97.83% Statements 45/46
85.11% Branches 40/47
100% Functions 7/7
97.83% Lines 45/46

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              97x 97x 97x 97x   97x     97x   97x 97x 97x     97x 97x 97x   97x   105x 105x     105x 105x 105x 105x 105x   105x                                                   7x   7x 3x                   4x           4x     1x 7x                             2x 2x   2x 2x   2x 2x   2x       2x 2x     2x 2x 2x           2x           2x     2x       4x           8x          
/*
 * Copyright 2022 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 * as Yup from 'yup'
import { FormikErrors, yupToFormErrors } from 'formik'
import { isEmpty, set } from 'lodash-es'
 
import { IconName, Color, getMultiTypeFromValue, MultiTypeInputType } from '@harness/uicore'
import type { StringsMap } from 'stringTypes'
 
import { getDurationValidationSchema } from '@common/components/MultiTypeDuration/MultiTypeDuration'
 
import { StepProps, StepViewType, ValidateInputSetProps } from '@pipeline/components/AbstractSteps/Step'
import { StepType } from '@pipeline/components/PipelineSteps/PipelineStepInterface'
import { PipelineStep } from '@pipeline/components/PipelineSteps/PipelineStep'
 
import type { PolicyStepData, PolicyStepFormData } from './PolicyStepTypes'
import PolicyInputSetStep from './PolicyInputSetStep'
import { PolicyStepVariablesView, PolicyStepVariablesViewProps } from './PolicyStepVariablesView'
import { PolicyStepWidgetWithRef } from './PolicyStepWidget'
 
export class PolicyStep extends PipelineStep<PolicyStepData> {
  constructor() {
    super()
    this._hasStepVariables = true
  }
 
  protected type = StepType.Policy
  protected stepName = 'Policy Step'
  protected stepIcon: IconName = 'evaluate-policy'
  protected stepIconColor = Color.GREY_700
  protected stepDescription: keyof StringsMap = 'pipeline.stepDescription.Policy'
 
  protected defaultValues: PolicyStepData = {
    name: '',
    identifier: '',
    type: StepType.Policy,
    timeout: '10m',
    spec: {
      policySets: [],
      type: 'Custom',
      policySpec: {
        payload: ''
      }
    }
  }
 
  renderStep(props: StepProps<PolicyStepData>): JSX.Element {
    const {
      initialValues,
      onUpdate,
      onChange,
      stepViewType,
      formikRef,
      isNewStep,
      readonly,
      inputSetData,
      allowableTypes,
      customStepProps
    } = props
 
    if (stepViewType === StepViewType.InputSet || stepViewType === StepViewType.DeploymentForm) {
      return (
        <PolicyInputSetStep
          readonly={!!inputSetData?.readonly}
          template={inputSetData?.template}
          path={inputSetData?.path || ''}
          allowableTypes={allowableTypes}
        />
      )
    }
 
    Iif (stepViewType === StepViewType.InputVariable) {
      return (
        <PolicyStepVariablesView {...(customStepProps as PolicyStepVariablesViewProps)} originalData={initialValues} />
      )
    }
 
    return (
      <PolicyStepWidgetWithRef
        initialValues={this.getInitialValues(initialValues)}
        onUpdate={data => onUpdate?.(this.processFormData(data))}
        onChange={data => onChange?.(this.processFormData(data))}
        isNewStep={isNewStep}
        readonly={readonly}
        ref={formikRef}
        allowableTypes={allowableTypes}
      />
    )
  }
 
  validateInputSet({
    data,
    template,
    getString,
    viewType
  }: ValidateInputSetProps<PolicyStepData>): FormikErrors<PolicyStepData> {
    const errors: FormikErrors<PolicyStepData> = {}
    const isRequired = viewType === StepViewType.DeploymentForm
    /* istanbul ignore else */
    if (getMultiTypeFromValue(template?.timeout) === MultiTypeInputType.RUNTIME) {
      let timeoutSchema = getDurationValidationSchema({ minimum: '10s' })
      /* istanbul ignore else */
      if (isRequired) {
        timeoutSchema = timeoutSchema.required(getString?.('validation.timeout10SecMinimum'))
      }
      const timeout = Yup.object().shape({
        timeout: timeoutSchema
      })
 
      try {
        timeout.validateSync(data)
      } catch (e) {
        /* istanbul ignore else */
        if (e instanceof Yup.ValidationError) {
          const err = yupToFormErrors(e)
          Object.assign(errors, err)
        }
      }
    }
 
    /* istanbul ignore else */
    if (
      getMultiTypeFromValue(/* istanbul ignore next */ template?.spec?.policySpec?.payload) ===
        MultiTypeInputType.RUNTIME &&
      isRequired &&
      isEmpty(/* istanbul ignore next */ data?.spec?.policySpec?.payload?.trim())
    ) {
      set(errors, 'spec.policySpec.payload', getString?.('fieldRequired', { field: 'Payload' }))
    }
 
    return errors
  }
 
  private getInitialValues(initialValues: PolicyStepData): PolicyStepFormData {
    return {
      ...initialValues
    }
  }
 
  processFormData(data: PolicyStepFormData): PolicyStepData {
    return {
      ...data
    }
  }
}