All files / modules/75-cd/components/PipelineSteps PipelineStepsUtil.ts

96.3% Statements 52/54
76% Branches 19/25
100% Functions 13/13
96.3% Lines 52/54

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              102x 102x 102x 102x   102x 102x         102x 102x   102x 102x 102x 102x 102x 102x 102x 102x 102x 102x     102x 102x 102x     102x       27x 23x 9x   14x   27x 17x   10x   102x       25x 21x 7x   14x   25x 15x   10x     102x 13x     102x 95x     102x     52x     102x     5x         102x     5x         102x       5x                                       5x       5x             5x                            
/*
 * 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 { getMultiTypeFromValue, MultiTypeInputType } from '@wings-software/uicore'
import * as Yup from 'yup'
import isEmpty from 'lodash/isEmpty'
import { get } from 'lodash-es'
import type { UseStringsReturn } from 'framework/strings'
import { getNameAndIdentifierSchema } from '@pipeline/utils/tempates'
import {
  getFailureStrategiesValidationSchema,
  getVariablesValidationField
} from '@pipeline/components/PipelineSteps/AdvancedSteps/FailureStrategyPanel/validation'
 
const namespaceRegex = /^[a-z0-9]([-a-z0-9]*[a-z0-9])?$/
const releaseNameRegex = /^[a-z0-9]([-a-z0-9]*[a-z0-9])?(\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*$/
 
export enum ServiceDeploymentType {
  Kubernetes = 'Kubernetes',
  NativeHelm = 'NativeHelm',
  amazonEcs = 'amazonEcs',
  amazonAmi = 'amazonAmi',
  awsCodeDeploy = 'awsCodeDeploy',
  winrm = 'winrm',
  awsLambda = 'awsLambda',
  pcf = 'pcf',
  ssh = 'ssh'
}
 
export enum InfraDeploymentType {
  KubernetesDirect = 'KubernetesDirect',
  KubernetesGcp = 'KubernetesGcp'
}
 
export function getNameSpaceSchema(
  getString: UseStringsReturn['getString'],
  isRequired = true
): Yup.StringSchema<string | undefined> {
  const namespaceSchema = Yup.string().test('namespace', getString('cd.namespaceValidation'), function (value) {
    if (getMultiTypeFromValue(value) !== MultiTypeInputType.FIXED || isEmpty(value)) {
      return true
    }
    return namespaceRegex.test(value)
  })
  if (isRequired) {
    return namespaceSchema.required(getString('fieldRequired', { field: getString('common.namespace') }))
  }
  return namespaceSchema
}
export function getReleaseNameSchema(
  getString: UseStringsReturn['getString'],
  isRequired = true
): Yup.StringSchema<string | undefined> {
  const releaseNameSchema = Yup.string().test('releaseName', getString('cd.releaseNameValidation'), function (value) {
    if (getMultiTypeFromValue(value) !== MultiTypeInputType.FIXED || isEmpty(value)) {
      return true
    }
    return releaseNameRegex.test(value)
  })
  if (isRequired) {
    return releaseNameSchema.required(getString('fieldRequired', { field: getString('common.releaseName') }))
  }
  return releaseNameSchema
}
 
export function getConnectorSchema(getString: UseStringsReturn['getString']): Yup.StringSchema<string | undefined> {
  return Yup.string().required(getString('fieldRequired', { field: getString('connector') }))
}
 
export function getServiceRefSchema(getString: UseStringsReturn['getString']): Yup.StringSchema<string | undefined> {
  return Yup.string().trim().required(getString('cd.pipelineSteps.serviceTab.serviceIsRequired'))
}
 
export function getEnvironmentRefSchema(
  getString: UseStringsReturn['getString']
): Yup.StringSchema<string | undefined> {
  return Yup.string().trim().required(getString('cd.pipelineSteps.environmentTab.environmentIsRequired'))
}
 
export function getServiceDeploymentTypeSchema(
  getString: UseStringsReturn['getString']
): Yup.StringSchema<string | undefined> {
  return Yup.string()
    .oneOf(Object.values(ServiceDeploymentType))
    .required(getString('cd.pipelineSteps.serviceTab.deploymentTypeRequired'))
}
 
export function getInfraDeploymentTypeSchema(
  getString: UseStringsReturn['getString']
): Yup.StringSchema<string | undefined> {
  return Yup.string()
    .oneOf(Object.values(InfraDeploymentType))
    .required(getString('cd.pipelineSteps.infraTab.deploymentType'))
}
 
export function getCDStageValidationSchema(
  getString: UseStringsReturn['getString'],
  contextType?: string
): Yup.Schema<unknown> {
  return Yup.object().shape({
    ...getNameAndIdentifierSchema(getString, contextType),
    spec: Yup.object().shape({
      serviceConfig: Yup.object().shape({
        serviceRef: getServiceRefSchema(getString),
        serviceDefinition: Yup.object().shape({
          type: getServiceDeploymentTypeSchema(getString),
          spec: Yup.object().shape(getVariablesValidationField(getString))
        })
      }),
      infrastructure: Yup.object().shape({
        environmentRef: getEnvironmentRefSchema(getString),
        infrastructureDefinition: Yup.object().shape({
          type: getInfraDeploymentTypeSchema(getString),
          spec: Yup.object().shape({
            connectorRef: getConnectorSchema(getString),
            namespace: getNameSpaceSchema(getString),
            releaseName: getReleaseNameSchema(getString),
            cluster: Yup.mixed().test({
              test(val): boolean | Yup.ValidationError {
                const infraDeploymentType = get(
                  this.options.context,
                  'spec.infrastructure.infrastructureDefinition.type'
                )
                Iif (infraDeploymentType === InfraDeploymentType.KubernetesGcp) {
                  if (isEmpty(val) || (typeof val === 'object' && isEmpty(val.value))) {
                    return this.createError({
                      message: getString('fieldRequired', { field: getString('common.cluster') })
                    })
                  }
                }
                return true
              }
            })
          })
        })
      }),
      execution: Yup.object().shape({
        steps: Yup.array().required().min(1, getString('cd.pipelineSteps.executionTab.stepsCount'))
      })
    }),
    failureStrategies: getFailureStrategiesValidationSchema(getString),
    ...getVariablesValidationField(getString)
  })
}