All files / modules/70-pipeline/components/CommonPipelineStages/ApprovalStage ApprovalStageOverview.tsx

89.74% Statements 35/39
67.15% Branches 92/137
57.14% Functions 4/7
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              1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x     1x 1x 1x     1x 1x   1x   1x                       8x 8x 8x 8x 8x 8x 8x   8x   4x         8x                                 4x 4x     4x 4x             4x         10x                                                                                                                                                          
/*
 * 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, { useCallback, useRef } from 'react'
import cx from 'classnames'
import * as Yup from 'yup'
import { Formik } from 'formik'
import { cloneDeep, debounce, noop } from 'lodash-es'
import { Accordion, Card, Container, FormikForm, HarnessDocTooltip, Layout, Text } from '@wings-software/uicore'
import { FontVariation } from '@harness/design-system'
import { NameIdDescriptionTags } from '@common/components/NameIdDescriptionTags/NameIdDescriptionTags'
import { useStrings } from 'framework/strings'
import { usePipelineContext } from '@pipeline/components/PipelineStudio/PipelineContext/PipelineContext'
import { isDuplicateStageId } from '@pipeline/components/PipelineStudio/StageBuilder/StageBuilderUtil'
import { StepWidget } from '@pipeline/components/AbstractSteps/StepWidget'
import type { CustomVariablesData } from '@pipeline/components/PipelineSteps/Steps/CustomVariables/CustomVariableEditable'
import type { StageElementConfig, StringNGVariable } from 'services/cd-ng'
import { StepViewType } from '@pipeline/components/AbstractSteps/Step'
import { StepType } from '@pipeline/components/PipelineSteps/PipelineStepInterface'
import { usePipelineVariables } from '@pipeline/components/PipelineVariablesContext/PipelineVariablesContext'
import type { AllNGVariables } from '@pipeline/utils/types'
import type { ApprovalStageElementConfig } from '@pipeline/utils/pipelineTypes'
import { getNameAndIdentifierSchema } from '@pipeline/utils/tempates'
import { isContextTypeNotStageTemplate } from '@pipeline/components/PipelineStudio/PipelineUtils'
import type { ApprovalStageOverviewProps } from './types'
import css from './ApprovalStageOverview.module.scss'
 
export function ApprovalStageOverview(props: ApprovalStageOverviewProps): React.ReactElement {
  const {
    state: {
      pipeline: { stages = [] },
      selectionState: { selectedStageId }
    },
    contextType,
    allowableTypes,
    stepsFactory,
    isReadonly,
    updateStage,
    getStageFromPipeline
  } = usePipelineContext()
  const { variablesPipeline, metadataMap } = usePipelineVariables()
  const { stage } = getStageFromPipeline<ApprovalStageElementConfig>(selectedStageId || '')
  const cloneOriginalData = cloneDeep(stage)!
  const allNGVariables = (cloneOriginalData?.stage?.variables || []) as AllNGVariables[]
  const { getString } = useStrings()
  const scrollRef = useRef<HTMLDivElement | null>(null)
  // eslint-disable-next-line react-hooks/exhaustive-deps
  const updateStageDebounced = useCallback(
    debounce((values: StageElementConfig): void => {
      updateStage({ ...stage?.stage, ...values })
    }, 300),
    [stage?.stage, updateStage]
  )
 
  return (
    <div className={cx(css.approvalStageOverviewWrapper, css.stageSection)}>
      <div className={css.content} ref={scrollRef}>
        <Text font={{ variation: FontVariation.H5 }} margin={{ bottom: 'small' }} id="stageOverview">
          {getString('stageOverview')}
        </Text>
        <Container id="stageOverview" className={css.basicOverviewDetails}>
          <Formik
            enableReinitialize
            initialValues={{
              identifier: cloneOriginalData?.stage?.identifier,
              name: cloneOriginalData?.stage?.name,
              description: cloneOriginalData?.stage?.description,
              tags: cloneOriginalData?.stage?.tags || {}
            }}
            validationSchema={Yup.object().shape(getNameAndIdentifierSchema(getString, contextType))}
            validate={values => {
              const errors: { name?: string } = {}
              Iif (isDuplicateStageId(values.identifier || '', stages, true)) {
                errors.name = getString('validation.identifierDuplicate')
              }
              Eif (cloneOriginalData) {
                updateStageDebounced({
                  ...(cloneOriginalData.stage as ApprovalStageElementConfig),
                  name: values?.name || '',
                  identifier: values?.identifier || '',
                  description: values?.description || ''
                })
              }
              return errors
            }}
            onSubmit={() => noop}
          >
            {formikProps => (
              <FormikForm>
                {isContextTypeNotStageTemplate(contextType) && (
                  <Card className={cx(css.sectionCard)}>
                    <NameIdDescriptionTags
                      formikProps={formikProps}
                      descriptionProps={{
                        disabled: isReadonly
                      }}
                      identifierProps={{
                        isIdentifierEditable: false,
                        inputGroupProps: { disabled: isReadonly }
                      }}
                      tagsProps={{
                        disabled: isReadonly
                      }}
                    />
                  </Card>
                )}
              </FormikForm>
            )}
          </Formik>
        </Container>
 
        <Accordion activeId={allNGVariables.length > 0 ? 'variables' : ''}>
          <Accordion.Panel
            id="variables"
            summary={
              <Text margin={{ left: 'small' }} font={{ variation: FontVariation.H5 }}>
                {getString('advancedTitle')}
              </Text>
            }
            addDomId={true}
            details={
              <Card className={css.sectionCard} id="variables">
                <div className={cx(css.tabSubHeading, 'ng-tooltip-native')} data-tooltip-id="overviewStageVariables">
                  {getString('pipeline.stageVariables')}
                  <HarnessDocTooltip tooltipId="overviewStageVariables" useStandAlone={true} />
                </div>
                <Layout.Horizontal>
                  <StepWidget<CustomVariablesData>
                    factory={stepsFactory}
                    readonly={isReadonly}
                    initialValues={{
                      variables: ((cloneOriginalData?.stage as StageElementConfig)?.variables ||
                        []) as AllNGVariables[],
                      canAddVariable: true
                    }}
                    allowableTypes={allowableTypes}
                    type={StepType.CustomVariable}
                    stepViewType={StepViewType.StageVariable}
                    onUpdate={({ variables }: CustomVariablesData) => {
                      updateStageDebounced({
                        ...(cloneOriginalData?.stage as ApprovalStageElementConfig),
                        variables
                      })
                    }}
                    customStepProps={{
                      yamlProperties:
                        getStageFromPipeline(
                          cloneOriginalData?.stage?.identifier || '',
                          variablesPipeline
                        )?.stage?.stage?.variables?.map?.(
                          variable => metadataMap[(variable as StringNGVariable).value || '']?.yamlProperties || {}
                        ) || []
                    }}
                  />
                </Layout.Horizontal>
              </Card>
            }
          />
        </Accordion>
 
        {props.children}
      </div>
    </div>
  )
}