All files / modules/70-pipeline/components/PipelineSteps/AdvancedSteps AdvancedSteps.tsx

89.74% Statements 35/39
73.77% Branches 90/122
83.33% Functions 5/6
89.74% Lines 35/39

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              26x   26x 26x 26x   26x 26x           26x 26x     26x 26x   26x   26x 26x                   26x 2x 2x   2x               2x       2x       2x     2x               1x                 4x   4x                   26x                   4x   4x 4x   4x 3x 1x       1x       1x           4x                                                                                                                   26x  
/*
 * 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 type { FormikProps } from 'formik'
import { Formik, FormikForm, Accordion, AccordionHandle } from '@wings-software/uicore'
import * as Yup from 'yup'
import { debounce, defaultTo, isEmpty } from 'lodash-es'
 
import { useStrings } from 'framework/strings'
import {
  AdvancedPanels,
  StepCommandsProps,
  Values,
  TabTypes
} from '@pipeline/components/PipelineStudio/StepCommands/StepCommandTypes'
import { StepFormikFowardRef, setFormikRef } from '@pipeline/components/AbstractSteps/Step'
import { StepMode as Modes } from '@pipeline/utils/stepUtils'
import type { StepElementConfig, StepGroupElementConfig } from 'services/cd-ng'
import type { TemplateStepNode } from 'services/pipeline-ng'
import DelegateSelectorPanel from './DelegateSelectorPanel/DelegateSelectorPanel'
import FailureStrategyPanel from './FailureStrategyPanel/FailureStrategyPanel'
import type { AllFailureStrategyConfig } from './FailureStrategyPanel/utils'
import { getFailureStrategiesValidationSchema } from './FailureStrategyPanel/validation'
import type { StepType } from '../PipelineStepInterface'
import ConditionalExecutionPanel from './ConditionalExecutionPanel/ConditionalExecutionPanel'
import css from './AdvancedSteps.module.scss'
 
export type FormValues = Pick<Values, 'delegateSelectors' | 'when'> & {
  failureStrategies: AllFailureStrategyConfig[]
}
 
export interface AdvancedStepsProps extends Omit<StepCommandsProps, 'onUseTemplate' | 'onRemoveTemplate'> {
  stepType?: StepType
}
 
export default function AdvancedSteps(props: AdvancedStepsProps, formikRef: StepFormikFowardRef): React.ReactElement {
  const { step, onChange, onUpdate } = props
  const { getString } = useStrings()
 
  const debouncedUpdate = React.useCallback(
    debounce((data: FormValues): void => {
      onChange?.({ ...data, tab: TabTypes.Advanced })
    }, 300),
    [onUpdate]
  )
 
  const failureStrategies =
    ((step as TemplateStepNode)?.template?.templateInputs as StepElementConfig)?.failureStrategies ||
    (step as StepElementConfig | StepGroupElementConfig)?.failureStrategies
 
  const delegateSelectors =
    ((step as TemplateStepNode)?.template?.templateInputs as StepElementConfig)?.spec?.delegateSelectors ||
    (step as StepElementConfig)?.spec?.delegateSelectors
 
  const when =
    ((step as TemplateStepNode)?.template?.templateInputs as StepElementConfig)?.when ||
    (step as StepElementConfig | StepGroupElementConfig)?.when
 
  return (
    <Formik<FormValues>
      initialValues={{
        failureStrategies: defaultTo(failureStrategies, []) as AllFailureStrategyConfig[],
        delegateSelectors: defaultTo(delegateSelectors, []),
        when
      }}
      onSubmit={data => {
        onUpdate({ ...data, tab: TabTypes.Advanced })
      }}
      validate={debouncedUpdate}
      formName="pipelineAdvancedSteps"
      validationSchema={Yup.object().shape({
        failureStrategies: getFailureStrategiesValidationSchema(getString)
      })}
    >
      {(formikProps: FormikProps<FormValues>) => {
        setFormikRef(formikRef, formikProps)
 
        return <AdvancedTabForm {...props} formikProps={formikProps} />
      }}
    </Formik>
  )
}
 
export interface AdvancedTabFormProps extends Omit<AdvancedStepsProps, 'onChange'> {
  formikProps: FormikProps<FormValues>
}
 
export function AdvancedTabForm(props: AdvancedTabFormProps): React.ReactElement {
  const {
    formikProps,
    hiddenPanels = [],
    hasStepGroupAncestor,
    isStepGroup,
    stepsFactory,
    isReadonly,
    stageType,
    stepType
  } = props
 
  const accordionRef = React.useRef<AccordionHandle>({} as AccordionHandle)
  const { getString } = useStrings()
 
  React.useEffect(() => {
    if (formikProps.isSubmitting) {
      Iif (!isEmpty(formikProps.errors?.failureStrategies) && accordionRef.current) {
        accordionRef.current.open(AdvancedPanels.FailureStrategy)
      }
 
      Iif (!isEmpty(formikProps.errors?.when) && accordionRef.current) {
        accordionRef.current.open(AdvancedPanels.ConditionalExecution)
      }
 
      Iif (!isEmpty(formikProps.errors?.delegateSelectors) && accordionRef.current) {
        accordionRef.current.open(AdvancedPanels.DelegateSelectors)
      }
    }
  }, [formikProps.isSubmitting, formikProps.errors])
 
  return (
    <FormikForm className={css.form}>
      <div>
        <Accordion
          ref={accordionRef}
          allowMultiOpen
          activeId={
            hiddenPanels.indexOf(AdvancedPanels.DelegateSelectors) === -1 &&
            stepsFactory.getStep(stepType)?.hasDelegateSelectionVisible
              ? AdvancedPanels.DelegateSelectors
              : hiddenPanels.indexOf(AdvancedPanels.ConditionalExecution) === -1
              ? AdvancedPanels.ConditionalExecution
              : hiddenPanels.indexOf(AdvancedPanels.FailureStrategy) === -1
              ? AdvancedPanels.FailureStrategy
              : ''
          }
        >
          {hiddenPanels.indexOf(AdvancedPanels.DelegateSelectors) === -1 &&
            stepsFactory.getStep(stepType)?.hasDelegateSelectionVisible && (
              <Accordion.Panel
                id={AdvancedPanels.DelegateSelectors}
                summary={getString('delegate.DelegateSelector')}
                details={<DelegateSelectorPanel isReadonly={isReadonly} formikProps={formikProps} />}
              />
            )}
          {hiddenPanels.indexOf(AdvancedPanels.ConditionalExecution) === -1 && (
            <Accordion.Panel
              id={AdvancedPanels.ConditionalExecution}
              summary={getString('pipeline.conditionalExecution.title')}
              details={
                <ConditionalExecutionPanel
                  formikProps={formikProps}
                  mode={isStepGroup ? Modes.STEP_GROUP : Modes.STEP}
                  isReadonly={isReadonly}
                />
              }
            />
          )}
          {hiddenPanels.indexOf(AdvancedPanels.FailureStrategy) === -1 && (
            <Accordion.Panel
              id={AdvancedPanels.FailureStrategy}
              summary={getString('pipeline.failureStrategies.title')}
              details={
                <FailureStrategyPanel
                  mode={hasStepGroupAncestor || isStepGroup ? Modes.STEP_GROUP : Modes.STEP}
                  stageType={stageType}
                  formikProps={formikProps}
                  isReadonly={isReadonly}
                />
              }
            />
          )}
        </Accordion>
      </div>
    </FormikForm>
  )
}
 
export const AdvancedStepsWithRef = React.forwardRef(AdvancedSteps)