All files / modules/75-cd/components/PipelineStudio/DeployServiceSpecifications SelectDeploymentType.tsx

74.12% Statements 63/85
57.14% Branches 8/14
52.17% Functions 12/23
73.81% Lines 62/84

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 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362              4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x   4x 4x   4x     31x                                     4x 62x       62x       279x                   279x                   4x 31x 31x 31x 31x 31x 31x 31x     31x     31x 31x                     31x 31x                                                                                         31x 31x   31x                                             31x 9x                 9x             9x     9x 72x 72x                     72x   9x       31x 9x 9x     31x 31x   31x                                                                 31x                                                                                                                                                         31x                   31x 31x 31x                                
/*
 * 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 { Formik, FormikProps } from 'formik'
import { noop } from 'lodash-es'
import { Classes, PopoverInteractionKind } from '@blueprintjs/core'
import * as Yup from 'yup'
import { useParams } from 'react-router-dom'
import { Card, Dialog, HarnessDocTooltip, Icon, Layout, Popover, Text, Thumbnail, Utils } from '@wings-software/uicore'
import { useModalHook } from '@harness/use-modal'
import { Color, FontVariation } from '@harness/design-system'
import cx from 'classnames'
import { useStrings, UseStringsReturn } from 'framework/strings'
import { isCDCommunity, useLicenseStore } from 'framework/LicenseStore/LicenseStoreContext'
import { StageErrorContext } from '@pipeline/context/StageErrorContext'
import { DeployTabs } from '@cd/components/PipelineStudio/DeployStageSetupShell/DeployStageSetupShellUtils'
import { useFeatureFlags } from '@common/hooks/useFeatureFlag'
import { ServiceDeploymentType } from '@cd/components/PipelineSteps/PipelineStepsUtil'
import { CDFirstGenTrial } from './CDFirstGenTrial'
import type { DeploymentTypeItem } from './DeploymentInterface'
import stageCss from '../DeployStageSetupShell/DeployStage.module.scss'
import deployServiceCsss from './DeployServiceSpecifications.module.scss'
 
export function getServiceDeploymentTypeSchema(
  getString: UseStringsReturn['getString']
): Yup.StringSchema<string | undefined> {
  return Yup.string()
    .oneOf(Object.values(ServiceDeploymentType))
    .required(getString('cd.pipelineSteps.serviceTab.deploymentTypeRequired'))
}
 
interface SelectServiceDeploymentTypeProps {
  selectedDeploymentType: string
  isReadonly: boolean
  handleDeploymentTypeChange: (deploymentType: string) => void
}
 
interface CardListProps {
  items: DeploymentTypeItem[]
  isReadonly: boolean
  selectedValue: string
  onChange: (deploymentType: string) => void
  allowDisabledItemClick?: boolean
}
 
const CardList = ({ items, isReadonly, selectedValue, onChange, allowDisabledItemClick }: CardListProps) => {
  const handleChange = (e: React.ChangeEvent<HTMLInputElement>): void => {
    const { value } = e.target
    onChange(value)
  }
  return (
    <Layout.Horizontal spacing={'medium'} className={stageCss.cardListContainer}>
      {items.map(item => {
        const itemContent = (
          <Thumbnail
            key={item.value}
            label={item.label}
            value={item.value}
            icon={item.icon}
            disabled={item.disabled || isReadonly}
            selected={item.value === selectedValue}
            onClick={handleChange}
          />
        )
        return (
          <Utils.WrapOptionalTooltip key={item.value} tooltipProps={item.tooltipProps} tooltip={item.tooltip}>
            {allowDisabledItemClick ? <div onClick={() => onChange(item.value)}>{itemContent}</div> : itemContent}
          </Utils.WrapOptionalTooltip>
        )
      })}
    </Layout.Horizontal>
  )
}
 
export default function SelectDeploymentType(props: SelectServiceDeploymentTypeProps): JSX.Element {
  const { selectedDeploymentType, isReadonly } = props
  const { getString } = useStrings()
  const formikRef = React.useRef<FormikProps<unknown> | null>(null)
  const { subscribeForm, unSubscribeForm } = React.useContext(StageErrorContext)
  const { licenseInformation } = useLicenseStore()
  const { NG_NATIVE_HELM } = useFeatureFlags()
  const { accountId } = useParams<{
    accountId: string
  }>()
  const [selectedDeploymentTypeInCG, setSelectedDeploymentTypeInCG] = React.useState('')
 
  // Supported in NG
  const ngSupportedDeploymentTypes: DeploymentTypeItem[] = React.useMemo(
    () => [
      {
        label: getString('pipeline.serviceDeploymentTypes.kubernetes'),
        icon: 'service-kubernetes',
        value: ServiceDeploymentType.Kubernetes
      }
    ],
    [getString]
  )
 
  // Suppported in CG
  const cgSupportedDeploymentTypes: DeploymentTypeItem[] = React.useMemo(
    () => [
      {
        label: getString('pipeline.nativeHelm'),
        icon: 'service-helm',
        value: ServiceDeploymentType.NativeHelm
      },
      {
        label: getString('pipeline.serviceDeploymentTypes.amazonEcs'),
        icon: 'service-ecs',
        value: ServiceDeploymentType.amazonEcs
      },
      {
        label: getString('pipeline.serviceDeploymentTypes.amazonAmi'),
        icon: 'main-service-ami',
        value: ServiceDeploymentType.amazonAmi
      },
      {
        label: getString('pipeline.serviceDeploymentTypes.awsCodeDeploy'),
        icon: 'app-aws-code-deploy',
        value: ServiceDeploymentType.awsCodeDeploy
      },
      {
        label: getString('pipeline.serviceDeploymentTypes.winrm'),
        icon: 'command-winrm',
        value: ServiceDeploymentType.winrm
      },
      {
        label: getString('pipeline.serviceDeploymentTypes.awsLambda'),
        icon: 'app-aws-lambda',
        value: ServiceDeploymentType.awsLambda
      },
      {
        label: getString('pipeline.serviceDeploymentTypes.pcf'),
        icon: 'service-pivotal',
        value: ServiceDeploymentType.pcf
      },
      {
        label: getString('pipeline.serviceDeploymentTypes.ssh'),
        icon: 'secret-ssh',
        value: ServiceDeploymentType.ssh
      }
    ],
    [getString]
  )
 
  const [cgDeploymentTypes, setCgDeploymentTypes] = React.useState(cgSupportedDeploymentTypes)
  const [ngDeploymentTypes, setNgDeploymentTypes] = React.useState(ngSupportedDeploymentTypes)
 
  const [showCurrentGenSwitcherModal, hideCurrentGenSwitcherModal] = useModalHook(() => {
    return (
      <Dialog
        isOpen={true}
        enforceFocus={false}
        canEscapeKeyClose
        canOutsideClickClose
        onClose={hideCurrentGenSwitcherModal}
        isCloseButtonShown
        style={{
          width: 1200,
          height: 600,
          padding: 0
        }}
      >
        <CDFirstGenTrial
          selectedDeploymentType={cgDeploymentTypes.find(type => type.value === selectedDeploymentTypeInCG)}
          accountId={accountId}
        />
      </Dialog>
    )
  }, [selectedDeploymentTypeInCG])
 
  React.useEffect(() => {
    Iif (isCDCommunity(licenseInformation)) {
      cgSupportedDeploymentTypes.forEach(deploymentType => {
        deploymentType['disabled'] = true
        if (deploymentType.value === 'NativeHelm') {
          deploymentType['disabled'] = !NG_NATIVE_HELM
        }
      })
      setCgDeploymentTypes(cgSupportedDeploymentTypes)
    } else {
      Iif (NG_NATIVE_HELM) {
        // If FF enabled - Native Helm will be in NG - left section
        setNgDeploymentTypes([
          ...ngSupportedDeploymentTypes,
          ...cgSupportedDeploymentTypes.filter(deploymentType => deploymentType.value === 'NativeHelm')
        ])
      }
      const cgTypes = NG_NATIVE_HELM
        ? cgSupportedDeploymentTypes.filter(deploymentType => deploymentType.value !== 'NativeHelm')
        : cgSupportedDeploymentTypes
      cgTypes.forEach(deploymentType => {
        deploymentType['disabled'] = true
        deploymentType['tooltip'] = (
          <div
            className={cx(deployServiceCsss.tooltipContainer, deployServiceCsss.cursorPointer)}
            onClick={() => {
              setSelectedDeploymentTypeInCG(deploymentType.value)
              showCurrentGenSwitcherModal()
            }}
          >
            Use in Continuous Delivery First Generation
          </div>
        )
        deploymentType['tooltipProps'] = { isDark: true }
      })
      setCgDeploymentTypes(cgTypes)
    }
  }, [licenseInformation, NG_NATIVE_HELM])
 
  React.useEffect(() => {
    subscribeForm({ tab: DeployTabs.SERVICE, form: formikRef })
    return () => unSubscribeForm({ tab: DeployTabs.SERVICE, form: formikRef })
  }, [formikRef])
 
  const renderDeploymentTypes = React.useCallback((): JSX.Element => {
    Eif (!isCDCommunity(licenseInformation)) {
      const tooltipContent = (
        <article className={cx(deployServiceCsss.cdGenerationSelectionTooltip, deployServiceCsss.tooltipContainer)}>
          <section className={deployServiceCsss.cdGenerationSwitchContainer}>
            <div className={cx(deployServiceCsss.cdGenerationSwitcher, deployServiceCsss.cdGenerationSwitcherSelected)}>
              <div className={deployServiceCsss.newText}>NEW</div>
              <Icon className="infoCard.iconClassName" name="cd-solid" size={24} />
              {getString('common.purpose.cd.newGen.title')}
            </div>
            <div
              className={cx(deployServiceCsss.cdGenerationSwitcher, deployServiceCsss.cursorPointer)}
              onClick={() => {
                setSelectedDeploymentTypeInCG('')
                showCurrentGenSwitcherModal()
              }}
            >
              <Icon className={'infoCard.iconClassName'} name="command-approval" size={24} />
              {getString('common.purpose.cd.1stGen.title')}
            </div>
          </section>
          <section className={deployServiceCsss.cdGenerationContent}>
            <Text color={Color.GREY_0} font={{ variation: FontVariation.BODY }}>
              {getString('cd.cdSwitchToFirstGen.description4')}
            </Text>
            <a
              className={deployServiceCsss.learnMore}
              href="https://ngdocs.harness.io/article/1fjmm4by22"
              rel="noreferrer"
              target="_blank"
            >
              {getString('cd.cdSwitchToFirstGen.learnMoreAboutCD1stGen')}
            </a>
          </section>
        </article>
      )
      return (
        <Layout.Horizontal margin={{ top: 'medium' }}>
          <Layout.Vertical border={{ right: true }} margin={{ right: 'huge' }} padding={{ right: 'huge' }}>
            <div className={cx(stageCss.tabSubHeading, 'ng-tooltip-native')}>
              {getString('common.currentlyAvailable')}
            </div>
            <CardList
              items={ngDeploymentTypes}
              isReadonly={isReadonly}
              onChange={props.handleDeploymentTypeChange}
              selectedValue={selectedDeploymentType}
            />
          </Layout.Vertical>
 
          <Layout.Vertical>
            <Layout.Horizontal>
              <div className={deployServiceCsss.comingSoonBanner}>{getString('common.comingSoon')}</div>
              <div
                className={cx(stageCss.tabSubHeading, 'ng-tooltip-native')}
                data-tooltip-id="supportedInFirstGeneration"
              >
                {getString('common.currentlySupportedOn')}
                <a
                  target="_blank"
                  rel="noreferrer"
                  onClick={() => {
                    setSelectedDeploymentTypeInCG('')
                    showCurrentGenSwitcherModal()
                  }}
                  style={{ paddingLeft: '4px' }}
                >
                  {getString('common.firstGeneration')}
                </a>
                <HarnessDocTooltip tooltipId="supportedInFirstGeneration" useStandAlone={true} />
              </div>
              <Popover
                position="auto"
                interactionKind={PopoverInteractionKind.HOVER}
                content={tooltipContent}
                className={Classes.DARK}
              >
                <span className={deployServiceCsss.tooltipIcon}>
                  <Icon size={12} name="tooltip-icon" color={Color.PRIMARY_7} />
                </span>
              </Popover>
            </Layout.Horizontal>
            <CardList
              items={cgDeploymentTypes}
              isReadonly={isReadonly}
              onChange={(deploymentType: string) => {
                setSelectedDeploymentTypeInCG(deploymentType)
                showCurrentGenSwitcherModal()
              }}
              selectedValue={selectedDeploymentType}
              allowDisabledItemClick={true}
            />
          </Layout.Vertical>
        </Layout.Horizontal>
      )
    }
    return (
      <CardList
        items={[...ngSupportedDeploymentTypes, ...cgDeploymentTypes]}
        isReadonly={isReadonly}
        onChange={props.handleDeploymentTypeChange}
        selectedValue={selectedDeploymentType}
      />
    )
  }, [
    cgDeploymentTypes,
    ngSupportedDeploymentTypes,
    getString,
    isReadonly,
    licenseInformation,
    props.handleDeploymentTypeChange
  ])
 
  return (
    <Formik<{ deploymentType: string }>
      onSubmit={noop}
      enableReinitialize={true}
      initialValues={{ deploymentType: selectedDeploymentType }}
      validationSchema={Yup.object().shape({
        deploymentType: getServiceDeploymentTypeSchema(getString)
      })}
    >
      {formik => {
        window.dispatchEvent(new CustomEvent('UPDATE_ERRORS_STRIP', { detail: DeployTabs.SERVICE }))
        formikRef.current = formik
        return (
          <Card className={stageCss.sectionCard}>
            <div
              className={cx(stageCss.tabSubHeading, 'ng-tooltip-native')}
              data-tooltip-id="stageOverviewDeploymentType"
            >
              {getString('deploymentTypeText')}
              <HarnessDocTooltip tooltipId="stageOverviewDeploymentType" useStandAlone={true} />
            </div>
            {renderDeploymentTypes()}
          </Card>
        )
      }}
    </Formik>
  )
}