All files / modules/75-cd/components/PipelineStudio/DeployAdvancedSpecifications DeployAdvancedSpecifications.tsx

87.04% Statements 47/54
74.42% Branches 32/43
77.78% Functions 7/9
86.54% Lines 45/52

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              4x 4x 4x 4x 4x 4x 4x 4x 4x   4x 4x 4x 4x 4x 4x 4x         4x 11x                 11x 11x   11x 11x 11x 11x 11x   11x 11x 1x       11x                                                                                                         1x       1x 1x 1x     1x                                         1x       1x 1x 1x     1x 1x 1x   1x 1x       1x                                 4x  
/*
 * 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 { Card, Container, HarnessDocTooltip, Layout } from '@wings-software/uicore'
import { produce } from 'immer'
import { set, isEmpty } from 'lodash-es'
import { StepActions } from '@common/constants/TrackingConstants'
import { useTelemetry } from '@common/hooks/useTelemetry'
import { usePipelineContext } from '@pipeline/components/PipelineStudio/PipelineContext/PipelineContext'
import { FailureStrategyWithRef } from '@pipeline/components/PipelineStudio/FailureStrategy/FailureStrategy'
import { DelegateSelectorWithRef } from '@pipeline/components/PipelineStudio/DelegateSelector/DelegateSelector'
import type { StepFormikRef } from '@pipeline/components/PipelineStudio/StepCommands/StepCommands'
import ConditionalExecution from '@pipeline/components/PipelineStudio/ConditionalExecution/ConditionalExecution'
import { useStrings } from 'framework/strings'
import DeployServiceErrors from '@cd/components/PipelineStudio/DeployServiceSpecifications/DeployServiceErrors'
import { DeployTabs } from '@cd/components/PipelineStudio/DeployStageSetupShell/DeployStageSetupShellUtils'
import { StageErrorContext } from '@pipeline/context/StageErrorContext'
import { useValidationErrors } from '@pipeline/components/PipelineStudio/PiplineHooks/useValidationErrors'
import stageCss from '../DeployStageSetupShell/DeployStage.module.scss'
 
export interface AdvancedSpecifications {
  context?: string
}
const DeployAdvancedSpecifications: React.FC<AdvancedSpecifications> = ({ children }): JSX.Element => {
  const { getString } = useStrings()
 
  const {
    state: {
      selectionState: { selectedStageId }
    },
    isReadonly,
    getStageFromPipeline,
    updateStage
  } = usePipelineContext()
  const { stage } = getStageFromPipeline(selectedStageId || /* istanbul ignore next */ '')
 
  const formikRef = React.useRef<StepFormikRef | null>(null)
  const scrollRef = React.useRef<HTMLDivElement | null>(null)
  const { submitFormsForTab } = React.useContext(StageErrorContext)
  const { errorMap } = useValidationErrors()
  const { trackEvent } = useTelemetry()
 
  React.useEffect(() => {
    if (errorMap.size > 0) {
      submitFormsForTab(DeployTabs.ADVANCED)
    }
  }, [errorMap])
 
  return (
    <div className={stageCss.serviceOverrides}>
      <DeployServiceErrors domRef={scrollRef as React.MutableRefObject<HTMLElement | undefined>} />
      <div className={stageCss.contentSection} ref={scrollRef}>
        <div className={stageCss.tabHeading}>
          <span data-tooltip-id="delegateSelectorDeployStage">
            {getString('pipeline.delegate.DelegateSelectorOptional')}
          </span>
        </div>
        <Card className={stageCss.sectionCard} id="delegateSelector">
          <Layout.Horizontal>
            <div>
              <DelegateSelectorWithRef
                selectedStage={stage}
                isReadonly={isReadonly}
                ref={formikRef}
                onUpdate={delegateSelectors => {
                  const valuePassed = delegateSelectors.delegateSelectors
                  const { stage: pipelineStage } = getStageFromPipeline(
                    selectedStageId || /* istanbul ignore next */ ''
                  )
                  /* istanbul ignore else */
                  Iif (pipelineStage && pipelineStage.stage) {
                    const stageData = produce(pipelineStage, draft => {
                      set(draft, 'stage.delegateSelectors', valuePassed)
                    })
                    /* istanbul ignore else */
                    Iif (stageData.stage) {
                      updateStage(stageData.stage)
                    }
                  }
                }}
                tabName={DeployTabs.ADVANCED}
              />
            </div>
          </Layout.Horizontal>
        </Card>
        <Container margin={{ top: 'xxlarge' }}>{children}</Container>
 
        <div className={stageCss.tabHeading}>
          <span data-tooltip-id="conditionalExecutionDeployStage">
            {getString('pipeline.conditionalExecution.title')}
            <HarnessDocTooltip tooltipId="conditionalExecutionDeployStage" useStandAlone={true} />
          </span>
        </div>
 
        {!!stage && (
          <Card className={stageCss.sectionCard} id="conditionalExecution">
            <Layout.Horizontal>
              <ConditionalExecution
                isReadonly={isReadonly}
                selectedStage={stage}
                onUpdate={when => {
                  const { stage: pipelineStage } = getStageFromPipeline(
                    selectedStageId || /* istanbul ignore next */ ''
                  )
                  /* istanbul ignore else */
                  if (pipelineStage && pipelineStage.stage) {
                    const stageData = produce(pipelineStage, draft => {
                      set(draft, 'stage.when', when)
                    })
                    /* istanbul ignore else */
                    if (stageData.stage) updateStage(stageData.stage)
                  }
                }}
              />
            </Layout.Horizontal>
          </Card>
        )}
        <div className={stageCss.tabHeading}>
          <span data-tooltip-id="failureStrategyDeployStage">
            {getString('pipeline.failureStrategies.title')}
            <HarnessDocTooltip tooltipId="failureStrategyDeployStage" useStandAlone={true} />
          </span>
        </div>
        <Card className={stageCss.sectionCard} id="failureStrategy">
          <Layout.Horizontal>
            <div>
              <FailureStrategyWithRef
                selectedStage={stage}
                isReadonly={isReadonly}
                ref={formikRef}
                onUpdate={({ failureStrategies }) => {
                  const { stage: pipelineStage } = getStageFromPipeline(
                    selectedStageId || /* istanbul ignore next */ ''
                  )
                  /* istanbul ignore else */
                  if (pipelineStage && pipelineStage.stage) {
                    const stageData = produce(pipelineStage, draft => {
                      set(draft, 'stage.failureStrategies', failureStrategies)
                    })
                    /* istanbul ignore else */
                    if (stageData.stage) {
                      updateStage(stageData.stage)
                      const errors = formikRef.current?.getErrors()
                      /* istanbul ignore else */
                      if (isEmpty(errors)) {
                        const telemetryData = failureStrategies.map(strategy => ({
                          onError: strategy.onFailure?.errors?.join(', '),
                          action: strategy.onFailure?.action?.type
                        }))
                        telemetryData.length &&
                          trackEvent(StepActions.AddEditFailureStrategy, { data: JSON.stringify(telemetryData) })
                      }
                    }
                  }
                }}
                tabName={DeployTabs.ADVANCED}
              />
            </div>
          </Layout.Horizontal>
        </Card>
        <Container margin={{ top: 'xxlarge' }}>{children}</Container>
      </div>
    </div>
  )
}
 
export default DeployAdvancedSpecifications