All files / modules/75-cd/components/PipelineSteps/K8sServiceSpec K8sServiceSpecVariablesForm.tsx

100% Statements 24/24
79.47% Branches 120/151
100% Functions 4/4
100% Lines 24/24

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              98x 98x 98x 98x 98x   98x     98x 98x 98x 98x   98x             98x 98x                                   98x 4x 2x 2x   2x 2x 2x 2x                                                                                                                         2x                                                                       2x                                                                                           1x                  
/*
 * 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 { NestedAccordionPanel, Text } from '@wings-software/uicore'
import cx from 'classnames'
import { FontVariation, Color } from '@harness/design-system'
import { isEmpty, omit } from 'lodash-es'
 
import { useStrings } from 'framework/strings'
import type { ServiceSpec } from 'services/cd-ng'
import type { VariableMergeServiceResponse, YamlProperties } from 'services/pipeline-ng'
import { StepType } from '@pipeline/components/PipelineSteps/PipelineStepInterface'
import { StepViewType } from '@pipeline/components/AbstractSteps/Step'
import { StepWidget } from '@pipeline/components/AbstractSteps/StepWidget'
import VariableAccordionSummary from '@pipeline/components/PipelineStudio/PipelineVariables/VariableAccordionSummary'
import type { AbstractStepFactory } from '@pipeline/components/AbstractSteps/AbstractStepFactory'
import { VariablesListTable } from '@pipeline/components/VariablesListTable/VariablesListTable'
import type {
  CustomVariablesData,
  CustomVariableEditableExtraProps
} from '@pipeline/components/PipelineSteps/Steps/CustomVariables/CustomVariableEditable'
import type { AllNGVariables } from '@pipeline/utils/types'
 
import css from './K8sServiceSpec.module.scss'
import pipelineVariableCss from '@pipeline/components/PipelineStudio/PipelineVariables/PipelineVariables.module.scss'
export interface K8sServiceSpecVariablesFormProps {
  initialValues: ServiceSpec
  stepsFactory: AbstractStepFactory
  stageIdentifier: string
  onUpdate?(data: ServiceSpec): void
  metadataMap: Required<VariableMergeServiceResponse>['metadataMap']
  variablesData: ServiceSpec
  readonly?: boolean
  path?: string
}
 
export interface VariableRowProps {
  data?: YamlProperties | undefined
  valueType?: string
  value: string
}
 
export function K8sServiceSpecVariablesForm(props: K8sServiceSpecVariablesFormProps): React.ReactElement {
  const { initialValues, stepsFactory, onUpdate, variablesData, metadataMap, readonly, path } = props
  const { manifests, artifacts, variables } = initialValues
  const { getString } = useStrings()
 
  const primaryArtifactVariables = variablesData?.artifacts?.primary?.spec
  const sidecarArtifactVariables = variablesData?.artifacts?.sidecars
  const manifestsVariables = variablesData.manifests
  return (
    <React.Fragment>
      {artifacts && !isEmpty(omit(variablesData?.artifacts, 'uuid')) ? (
        <NestedAccordionPanel
          isDefaultOpen
          addDomId
          noAutoScroll
          id={`${path}.Artifacts`}
          summary={
            <VariableAccordionSummary>
              <Text font={{ variation: FontVariation.SMALL_SEMI }} color={Color.BLACK}>
                {getString('artifacts')}
              </Text>
            </VariableAccordionSummary>
          }
          summaryClassName={cx(css.variableBorderBottom, pipelineVariableCss.accordianSummaryL3)}
          details={
            variablesData?.artifacts && (
              <>
                <NestedAccordionPanel
                  isDefaultOpen
                  noAutoScroll
                  addDomId
                  collapseProps={{ keepChildrenMounted: true }}
                  id={`${path}.Artifacts.Primary`}
                  summary={
                    <VariableAccordionSummary>
                      <Text font={{ variation: FontVariation.SMALL_SEMI }} color={Color.BLACK}>
                        {getString('primaryArtifactText')}
                      </Text>
                    </VariableAccordionSummary>
                  }
                  summaryClassName={cx(css.variableBorderBottom, pipelineVariableCss.accordianSummaryL3)}
                  details={
                    <VariablesListTable
                      className={pipelineVariableCss.variablePaddingL3}
                      data={primaryArtifactVariables}
                      originalData={initialValues?.artifacts?.primary?.spec}
                      metadataMap={metadataMap}
                    />
                  }
                />
 
                {!!sidecarArtifactVariables?.length && (
                  <>
                    <NestedAccordionPanel
                      isDefaultOpen
                      addDomId
                      noAutoScroll
                      id={`${path}..Artifacts.Sidecars`}
                      summary={
                        <VariableAccordionSummary>
                          <Text font={{ variation: FontVariation.SMALL_SEMI }} color={Color.BLACK}>
                            {getString('common.sidecarArtifactsText')}
                          </Text>
                        </VariableAccordionSummary>
                      }
                      summaryClassName={cx(css.variableBorderBottom, pipelineVariableCss.accordianSummaryL3)}
                      details={
                        Array.isArray(sidecarArtifactVariables) &&
                        sidecarArtifactVariables.map(({ sidecar }, index) => {
                          return (
                            <VariablesListTable
                              className={pipelineVariableCss.variablePaddingL3}
                              key={index}
                              data={sidecar?.spec}
                              originalData={initialValues?.artifacts?.sidecars?.[index]?.sidecar?.spec}
                              metadataMap={metadataMap}
                            />
                          )
                        })
                      }
                    />
                  </>
                )}
              </>
            )
          }
        />
      ) : null}
      {manifests && typeof manifestsVariables !== 'string' && !isEmpty(omit(manifestsVariables, 'uuid')) ? (
        <NestedAccordionPanel
          isDefaultOpen
          noAutoScroll
          addDomId
          id={`${path}.Manifests`}
          summary={
            <VariableAccordionSummary>
              <Text font={{ variation: FontVariation.SMALL_SEMI }} color={Color.BLACK}>
                {getString('manifests')}
              </Text>
            </VariableAccordionSummary>
          }
          summaryClassName={cx(css.variableBorderBottom, pipelineVariableCss.accordianSummaryL3)}
          details={
            !!manifestsVariables?.length && (
              <>
                {manifestsVariables?.map(({ manifest }, index) => (
                  <VariablesListTable
                    key={index}
                    className={cx(css.manifestVariablesTable, pipelineVariableCss.variablePaddingL3)}
                    data={manifest?.spec?.store?.spec}
                    originalData={initialValues?.manifests?.[index]?.manifest?.spec?.store?.spec}
                    metadataMap={metadataMap}
                  />
                ))}
              </>
            )
          }
        />
      ) : null}
      <NestedAccordionPanel
        isDefaultOpen
        noAutoScroll
        addDomId
        id={`${path}.Variables`}
        summary={
          <VariableAccordionSummary>
            <Text font={{ variation: FontVariation.SMALL_SEMI }} color={Color.BLACK}>
              {getString('variablesText')}
            </Text>
          </VariableAccordionSummary>
        }
        summaryClassName={cx(css.variableBorderBottom, pipelineVariableCss.accordianSummaryL2)}
        details={
          <StepWidget<CustomVariablesData, CustomVariableEditableExtraProps>
            factory={stepsFactory}
            initialValues={{
              variables: (variables || []) as AllNGVariables[],
              canAddVariable: true
            }}
            type={StepType.CustomVariable}
            stepViewType={StepViewType.InputVariable}
            onUpdate={onUpdate}
            allowableTypes={[]}
            readonly={readonly}
            customStepProps={{
              formName: 'addEditServiceCustomVariableForm',
              variableNamePrefix: 'serviceConfig.variables.',
              className: cx(css.customVariables, pipelineVariableCss.customVarPadL2, pipelineVariableCss.addVariableL2),
              path: path,
              // heading: <b>{getString('customVariables.title')}</b>,
              yamlProperties: (variablesData?.variables as AllNGVariables[])?.map(
                variable => metadataMap?.[variable.value || '']?.yamlProperties || {}
              )
            }}
          />
        }
      />
    </React.Fragment>
  )
}