All files / modules/70-pipeline/components/PipelineStudio/PipelineVariables/Cards StageCard.tsx

70.45% Statements 31/44
64.1% Branches 25/39
27.27% Functions 3/11
70.45% Lines 31/44

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 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258              26x 26x 26x 26x 26x 26x           26x 26x 26x 26x 26x     26x   26x 26x 26x 26x   26x                           26x                     45x 43x 43x 43x   43x   1x         43x                                     43x                     43x                     43x                       43x                                                                                                               20x                                                                                                                 43x                                            
/*
 * 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 produce from 'immer'
import { defaultTo, isEmpty, lowerCase, set } from 'lodash-es'
import { Text, NestedAccordionPanel, MultiTypeInputType } from '@wings-software/uicore'
import { FontVariation, Color } from '@harness/design-system'
import cx from 'classnames'
import type { DeploymentStageConfig, ServiceSpec, StageElementConfig } from 'services/cd-ng'
import type {
  CustomVariablesData,
  CustomVariableEditableExtraProps
} from '@pipeline/components/PipelineSteps/Steps/CustomVariables/CustomVariableEditable'
import { StepType } from '@pipeline/components/PipelineSteps/PipelineStepInterface'
import { StepWidget } from '@pipeline/components/AbstractSteps/StepWidget'
import { StepViewType } from '@pipeline/components/AbstractSteps/Step'
import { useStrings } from 'framework/strings'
import { VariablesListTable } from '@pipeline/components/VariablesListTable/VariablesListTable'
import type { AllNGVariables } from '@pipeline/utils/types'
 
import VariableListTagRow from '@pipeline/components/VariablesListTable/VariableListTagRow'
import type { AbstractStepFactory } from '@pipeline/components/AbstractSteps/AbstractStepFactory'
import { ServiceCardPanel } from './ServiceCard'
import { ExecutionCardPanel } from './ExecutionCard'
import { EnvironmentCardPanel } from './EnvironmentCard'
import VariableAccordionSummary from '../VariableAccordionSummary'
import type { PipelineVariablesData } from '../types'
import css from '../PipelineVariables.module.scss'
 
export interface StageCardProps {
  stage: StageElementConfig
  originalStage: StageElementConfig
  unresolvedStage: StageElementConfig
  metadataMap: PipelineVariablesData['metadataMap']
  readonly?: boolean
  path?: string
  allowableTypes: MultiTypeInputType[]
  stepsFactory: AbstractStepFactory
  updateStage: (stage: StageElementConfig) => Promise<void>
}
 
export default function StageCard(props: StageCardProps): React.ReactElement {
  const {
    stage,
    originalStage,
    unresolvedStage,
    metadataMap,
    readonly,
    path,
    allowableTypes,
    updateStage,
    stepsFactory
  } = props
  const { getString } = useStrings()
  const stageSpec = stage.spec as DeploymentStageConfig
  const originalSpec = originalStage.spec as DeploymentStageConfig
 
  const onUpdateVariables = React.useCallback(
    ({ variables }: CustomVariablesData) => {
      updateStage({ ...unresolvedStage, variables })
    },
    [updateStage, unresolvedStage]
  )
 
  const onUpdateServiceConfig = React.useCallback(
    (serviceSpec: ServiceSpec) => {
      updateStage(
        produce(unresolvedStage, draft => {
          if (serviceSpec.artifacts) {
            set(draft, 'spec.serviceConfig.serviceDefinition.spec.artifacts', serviceSpec.artifacts)
          }
          if (serviceSpec.manifests) {
            set(draft, 'spec.serviceConfig.serviceDefinition.spec.manifest', serviceSpec.manifests)
          }
          if (serviceSpec.variables) {
            set(draft, 'spec.serviceConfig.serviceDefinition.spec.variables', serviceSpec.variables)
          }
        })
      )
    },
    [updateStage, unresolvedStage]
  )
 
  const onUpdateInfrastructure = React.useCallback(
    infrastructure => {
      updateStage(
        produce(unresolvedStage, draft => {
          set(draft, 'spec.infrastructure', infrastructure)
        })
      )
    },
    [updateStage, unresolvedStage]
  )
 
  const onUpdateInfrastructureProvisioner = React.useCallback(
    provisioner => {
      updateStage(
        produce(unresolvedStage, draft => {
          set(draft, 'spec.infrastructure.infrastructureDefinition.provisioner', provisioner)
        })
      )
    },
    [updateStage, unresolvedStage]
  )
 
  const onUpdateExecution = React.useCallback(
    execution => {
      updateStage(
        produce(unresolvedStage, draft => {
          set(draft, 'spec.execution', execution)
        })
      )
    },
    [updateStage, unresolvedStage]
  )
 
  const content = (
    <div className={css.variableCard}>
      <VariablesListTable
        data={stage}
        className={css.variablePaddingL0}
        originalData={originalStage}
        metadataMap={metadataMap}
      />
      {!isEmpty(originalStage?.tags) && (
        <VariableListTagRow
          metadataMap={metadataMap}
          name={lowerCase(getString('tagsLabel'))}
          tags={originalStage?.tags}
          fqn=""
          className={css.variablePaddingTagL2}
        />
      )}
      {originalSpec && (
        <React.Fragment>
          <NestedAccordionPanel
            noAutoScroll
            isDefaultOpen
            key={`${path}.${originalStage.identifier}.variables`}
            id={`${path}.${originalStage.identifier}.variables`}
            addDomId
            summary={
              <VariableAccordionSummary>
                <Text font={{ variation: FontVariation.SMALL_SEMI }} color={Color.BLACK}>
                  {getString('customVariables.title')}
                </Text>
              </VariableAccordionSummary>
            }
            collapseProps={{
              keepChildrenMounted: true
            }}
            summaryClassName={css.variableBorderBottom}
            details={
              <StepWidget<CustomVariablesData, CustomVariableEditableExtraProps>
                factory={stepsFactory}
                initialValues={{
                  variables: defaultTo(originalStage.variables, []) as AllNGVariables[],
                  canAddVariable: true
                }}
                allowableTypes={allowableTypes}
                readonly={readonly}
                type={StepType.CustomVariable}
                stepViewType={StepViewType.InputVariable}
                onUpdate={onUpdateVariables}
                customStepProps={{
                  formName: 'addEditStageCustomVariableForm',
                  variableNamePrefix: `${originalStage.identifier}.variables.`,
                  domId: `Stage.${originalStage.identifier}.Variables-panel`,
                  className: cx(css.customVariables, css.customVarPadL1, css.addVariableL1),
                  // heading: <b>{getString('customVariables.title')}</b>,
                  path: `${path}.customVariables`,
                  yamlProperties: (defaultTo(stage.variables, []) as AllNGVariables[]).map?.(
                    variable =>
                      metadataMap[variable.value || /* istanbul ignore next */ '']?.yamlProperties ||
                      /* istanbul ignore next */ {}
                  )
                }}
              />
            }
          />
          {/* TODO: Temporary disable for  CI (TBD)*/}
          {stage.type === 'Deployment' || stage.type === 'Approval' ? (
            <>
              {stageSpec.serviceConfig && originalSpec.serviceConfig ? (
                <ServiceCardPanel
                  serviceConfig={stageSpec.serviceConfig}
                  originalServiceConfig={originalSpec.serviceConfig}
                  metadataMap={metadataMap}
                  readonly={readonly}
                  stageIdentifier={originalStage.identifier}
                  path={`${path}.${originalStage.identifier}`}
                  allowableTypes={allowableTypes}
                  onUpdateServiceConfig={onUpdateServiceConfig}
                  stepsFactory={stepsFactory}
                  originalStage={originalStage}
                />
              ) : /* istanbul ignore next */ null}
              <EnvironmentCardPanel
                stage={stage}
                originalStage={originalStage}
                metadataMap={metadataMap}
                readonly={readonly}
                allowableTypes={allowableTypes}
                path={path}
                stepsFactory={stepsFactory}
                onUpdateInfrastructure={onUpdateInfrastructure}
                onUpdateInfrastructureProvisioner={onUpdateInfrastructureProvisioner}
              />
              {stageSpec.execution && originalSpec.execution ? (
                <ExecutionCardPanel
                  id={`${path}.${originalStage.identifier}.Execution`}
                  title={getString('executionText')}
                  execution={stageSpec.execution}
                  originalExecution={originalSpec.execution}
                  metadataMap={metadataMap}
                  stageIdentifier={originalStage.identifier}
                  allowableTypes={allowableTypes}
                  readonly={readonly}
                  path={`${path}.${originalStage.identifier}.Execution`}
                  onUpdateExecution={onUpdateExecution}
                  stepsFactory={stepsFactory}
                />
              ) : /* istanbul ignore next */ null}
            </>
          ) : /* istanbul ignore next */ null}
        </React.Fragment>
      )}
    </div>
  )
 
  return (
    <NestedAccordionPanel
      noAutoScroll
      isDefaultOpen
      collapseProps={{
        keepChildrenMounted: true
      }}
      key={`${path}.${originalStage.identifier}`}
      id={`${path}.${originalStage.identifier}`}
      addDomId
      summary={
        <VariableAccordionSummary>
          <Text font={{ variation: FontVariation.H6 }} color={Color.BLACK}>
            {originalStage.name ? `Stage: ${originalStage.name}` : 'Stage'}
          </Text>
        </VariableAccordionSummary>
      }
      summaryClassName={css.stageSummary}
      details={content}
    />
  )
}