All files / modules/85-cv/pages/ChangeSource/ChangeSourceDrawer ChangeSourceDrawer.utils.ts

96.2% Statements 76/79
60.54% Branches 112/185
100% Functions 13/13
95.71% Lines 67/70

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                19x         19x 19x 19x                 19x       2x 2x       2x     1x 1x   1x 1x       2x     19x       55x   220x   80x 80x 80x         19x           13x             13x   13x 1x   13x 6x   13x 1x   13x 2x     13x 7x         13x   13x 7x     13x     19x           13x 13x   5x       1x 1x         1x 1x   1x 1x   1x   2x     5x       19x       20x 20x 60x       5x     55x   20x     19x 2x   1x                             1x       19x 6x             19x 8x   1x   3x   4x      
/*
 * 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 type { SelectOption } from '@wings-software/uicore'
import { isEmpty, cloneDeep } from 'lodash-es'
import type { Item } from '@wings-software/uicore/dist/components/ThumbnailSelect/ThumbnailSelect'
import type { StringsMap } from 'stringTypes'
import type { UseStringsReturn } from 'framework/strings'
import type { ChangeSourceDTO, MonitoredServiceDTO } from 'services/cv'
import { Connectors } from '@connectors/constants'
import { getIconBySource } from '../ChangeSource.utils'
import {
  ChangeSourceConnectorOptions,
  ChangeSourceFieldNames,
  ChangeSourceCategoryOptions,
  ChangeSourceCategoryName,
  ChangeSourceTypes
} from './ChangeSourceDrawer.constants'
import type { UpdatedChangeSourceDTO } from './ChangeSourceDrawer.types'
 
export const createChangesourceList = (
  changeSource: ChangeSourceDTO[],
  healthSourcesPayload: ChangeSourceDTO
): ChangeSourceDTO[] => {
  let updatedHealthSources = []
  if (
    changeSource &&
    !isEmpty(changeSource) &&
    changeSource.some(
      (el: any) => el?.identifier === healthSourcesPayload?.identifier && el?.type === healthSourcesPayload?.type
    )
  ) {
    updatedHealthSources = changeSource?.map((el: any) =>
      el?.identifier === healthSourcesPayload?.identifier ? healthSourcesPayload : el
    )
  } else Eif (changeSource && !isEmpty(changeSource)) {
    updatedHealthSources = [...changeSource, healthSourcesPayload]
  } else {
    updatedHealthSources = [healthSourcesPayload]
  }
  return updatedHealthSources
}
 
export const createCardOptions = (
  category: ChangeSourceDTO['category'],
  getString: UseStringsReturn['getString']
): Item[] => {
  return (
    cloneDeep(ChangeSourceConnectorOptions)
      .filter(item => item.category === category)
      .map(item => {
        item.icon = getIconBySource(item.value as ChangeSourceDTO['type'])
        item.label = getString(item.label.toString() as keyof StringsMap)
        return item
      }) || []
  )
}
 
export const validateChangeSource = (
  values: ChangeSourceDTO,
  tableData: ChangeSourceDTO[],
  isEdit: boolean,
  getString: UseStringsReturn['getString']
): any => {
  const { identifier, name, category, type, spec } = values
  const errors: {
    identifier?: string
    name?: string
    category?: string
    type?: string
    spec?: { [key: string]: any }
  } = {}
 
  if (!isEdit && tableData?.some(item => item.identifier === identifier)) {
    errors.name = getString('cv.changeSource.duplicateIdentifier')
  }
  if (!name) {
    errors.name = getString('cv.changeSource.selectChangeSourceName')
  }
  if (!category) {
    errors.category = getString('cv.changeSource.selectChangeSourceProvider')
  }
  if (!type) {
    errors.type = getString('cv.changeSource.selectChangeSourceType')
  }
 
  if (!spec?.connectorRef && type !== ChangeSourceTypes.HarnessCDNextGen && type !== ChangeSourceTypes.HarnessCD) {
    errors.spec = {
      connectorRef: getString('cv.onboarding.selectProductScreen.validationText.connectorRef')
    }
  }
 
  const specError = validateChangeSourceSpec(type, spec, errors?.spec || {}, getString)
 
  if (!isEmpty(specError)) {
    errors.spec = specError
  }
 
  return errors
}
 
export const validateChangeSourceSpec = (
  type: ChangeSourceDTO['type'],
  spec: ChangeSourceDTO['spec'],
  errorSpec: { [key: string]: string },
  getString: UseStringsReturn['getString']
): { [key: string]: string } => {
  let errors = { ...errorSpec }
  switch (type) {
    case ChangeSourceTypes.PagerDuty:
      return spec?.pagerDutyServiceId
        ? {}
        : { ...errors, pagerDutyServiceId: getString('cv.changeSource.PageDuty.selectPagerDutyService') }
    case ChangeSourceTypes.HarnessCD:
      Eif (!spec?.harnessApplicationId) {
        errors = {
          ...errors,
          harnessApplicationId: getString('cv.changeSource.HarnessCDCurrentGen.selectHarnessAppId')
        }
      }
      Eif (!spec?.harnessEnvironmentId) {
        errors = { ...errors, harnessEnvironmentId: getString('cv.changeSource.HarnessCDCurrentGen.selectHarnessEnv') }
      }
      Eif (!spec?.harnessServiceId) {
        errors = { ...errors, harnessServiceId: getString('cv.changeSource.HarnessCDCurrentGen.selectHarnessService') }
      }
      return errors
    case ChangeSourceTypes.K8sCluster:
      return spec?.isConnectorInvalid === true ? { ...errors, isConnectorInvalid: getString('invalidText') } : errors
 
    default:
      return {}
  }
}
 
export const getChangeSourceOptions = (
  getString: UseStringsReturn['getString'],
  type?: MonitoredServiceDTO['type']
): SelectOption[] => {
  const options: SelectOption[] = []
  for (const category of ChangeSourceCategoryOptions) {
    if (
      (type === 'Application' && category.value === ChangeSourceCategoryName.INFRASTRUCTURE) ||
      (type === 'Infrastructure' && category.value === ChangeSourceCategoryName.DEPLOYMENT)
    ) {
      continue
    }
 
    options.push({ label: getString(category.label as keyof StringsMap), value: category.value })
  }
  return options
}
 
export const updateSpecByType = (data: ChangeSourceDTO): ChangeSourceDTO['spec'] => {
  switch (data?.type) {
    case ChangeSourceTypes.PagerDuty:
      return {
        connectorRef: data?.spec?.connectorRef,
        pagerDutyServiceId: data?.spec?.pagerDutyServiceId
      }
    case ChangeSourceTypes.K8sCluster:
      return {
        connectorRef: data?.spec?.connectorRef
      }
    case ChangeSourceTypes.HarnessCD:
      return {
        harnessApplicationId: data?.spec?.harnessApplicationId?.value,
        harnessServiceId: data?.spec?.harnessServiceId?.value,
        harnessEnvironmentId: data?.spec?.harnessEnvironmentId?.value
      }
    default:
      return {}
  }
}
 
export const buildInitialData = (categoryOptions: SelectOption[]): UpdatedChangeSourceDTO => {
  return {
    [ChangeSourceFieldNames.CATEGORY]: categoryOptions[0].value,
    [ChangeSourceFieldNames.TYPE]: preSelectChangeSourceConnectorOnCategoryChange(categoryOptions[0].value as string),
    spec: {},
    enabled: true
  }
}
export const preSelectChangeSourceConnectorOnCategoryChange = (categoryName: string): string => {
  switch (categoryName) {
    case ChangeSourceCategoryName.ALERT:
      return Connectors.PAGER_DUTY
    case ChangeSourceCategoryName.INFRASTRUCTURE:
      return Connectors.KUBERNETES_CLUSTER
    default:
      return ''
  }
}