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

96.97% Statements 32/33
86.21% Branches 50/58
100% Functions 5/5
96.97% Lines 32/33

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              107x 107x 107x 107x 107x   107x 107x   107x     107x 107x 107x         107x 35x     35x   35x 35x   35x 35x 53x     35x   22x 4x           4x           35x     35x                                       107x 46x   46x               59x 24x   35x   35x                        
/*
 * 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, { useEffect } from 'react'
import { get, isEmpty } from 'lodash-es'
import { useParams } from 'react-router-dom'
import cx from 'classnames'
import { Text } from '@harness/uicore'
 
import { useStrings } from 'framework/strings'
import manifestSourceBaseFactory from '@cd/factory/ManifestSourceFactory/ManifestSourceBaseFactory'
import type { GitQueryParams, InputSetPathProps, PipelineType } from '@common/interfaces/RouteInterfaces'
import { useQueryParams } from '@common/hooks'
import type { ManifestConfig } from 'services/cd-ng'
import type { KubernetesManifestsProps } from '../K8sServiceSpecInterface'
import { isRuntimeMode } from '../K8sServiceSpecHelper'
import { fromPipelineInputTriggerTab, getManifestTriggerSetValues } from '../ManifestSource/ManifestSourceUtils'
import css from './KubernetesManifests.module.scss'
 
interface ManifestInputFieldProps extends KubernetesManifestsProps {
  manifest: ManifestConfig
}
const ManifestInputField = (props: ManifestInputFieldProps): React.ReactElement | null => {
  const { projectIdentifier, orgIdentifier, accountId, pipelineIdentifier } = useParams<
    PipelineType<InputSetPathProps> & { accountId: string }
  >()
  const { repoIdentifier, branch } = useQueryParams<GitQueryParams>()
 
  const runtimeMode = isRuntimeMode(props.stepViewType)
  const isManifestsRuntime = runtimeMode && !!get(props.template, 'manifests', false)
 
  const manifestSource = manifestSourceBaseFactory.getManifestSource(props.manifest.type)
  const manifestDefaultValue = props.manifests?.find(
    manifestData => manifestData?.manifest?.identifier === props.manifest?.identifier
  )?.manifest as ManifestConfig
 
  useEffect(() => {
    /* instanbul ignore else */
    if (fromPipelineInputTriggerTab(props.formik, props.fromTrigger)) {
      const manifestTriggerData = getManifestTriggerSetValues(
        props.initialValues,
        props.formik,
        props.stageIdentifier,
        props.manifestPath as string
      )
      !isEmpty(manifestTriggerData) &&
        props.formik.setFieldValue(`${props.path}.${props.manifestPath}`, manifestTriggerData)
    }
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [])
 
  Iif (!manifestSource) {
    return null
  }
  return (
    <div key={props.manifest?.identifier}>
      <Text className={css.inputheader} margin={{ top: 'medium' }}>
        {!props.fromTrigger && get(props.manifest, 'identifier', '')}
      </Text>
      {manifestSource &&
        manifestSource.renderContent({
          ...props,
          isManifestsRuntime,
          projectIdentifier,
          orgIdentifier,
          accountId,
          pipelineIdentifier,
          repoIdentifier,
          branch,
          manifest: manifestDefaultValue
        })}
    </div>
  )
}
export function KubernetesManifests(props: KubernetesManifestsProps): React.ReactElement {
  const { getString } = useStrings()
 
  return (
    <div className={cx(css.nopadLeft, css.accordionSummary)} id={`Stage.${props.stageIdentifier}.Service.Manifests`}>
      {!props.fromTrigger && (
        <div className={css.subheading}>
          {getString('pipelineSteps.deploy.serviceSpecifications.deploymentTypes.manifests')}
        </div>
      )}
      {props.template.manifests?.map((manifestObj, index) => {
        if (!manifestObj?.manifest || !props.manifests?.length) {
          return null
        }
        const manifestPath = `manifests[${index}].manifest`
 
        return (
          <ManifestInputField
            {...props}
            manifest={manifestObj.manifest}
            manifestPath={manifestPath}
            key={manifestObj.manifest?.identifier}
          />
        )
      })}
    </div>
  )
}