All files / modules/70-pipeline/components/ArtifactsSelection ArtifactUtils.ts

83.75% Statements 67/80
48.85% Branches 106/217
80% Functions 12/15
81.16% Lines 56/69

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              121x   121x   121x 121x               121x 121x 121x 121x 121x 121x 121x 121x     121x 121x 16x           121x 163x     163x     163x     121x                                                                                 121x 185x 222x 222x         121x                       121x 3x       5x       121x         6x       6x             6x 6x   6x     121x         5x       5x             5x 5x   5x     121x         39x   39x 31x     8x       8x     8x 2x   8x     121x 31x   14x               4x                 13x                 121x 44x    
/*
 * 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 { getMultiTypeFromValue, MultiTypeInputType, RUNTIME_INPUT_VALUE } from '@harness/uicore'
import type { FormikValues } from 'formik'
import { defaultTo, get, isEmpty, merge } from 'lodash-es'
import type { ArtifactConfig, ConnectorConfigDTO } from 'services/cd-ng'
import { ENABLED_ARTIFACT_TYPES } from './ArtifactHelper'
import {
  ArtifactTagHelperText,
  ArtifactType,
  ImagePathTypes,
  RepositoryPortOrServer,
  TagTypes
} from './ArtifactInterface'
 
export enum RegistryHostNames {
  GCR_URL = 'gcr.io',
  US_GCR_URL = 'us.gcr.io',
  ASIA_GCR_URL = 'asia.gcr.io',
  EU_GCR_URL = 'eu.gcr.io',
  MIRROR_GCR_URL = 'mirror.gcr.io',
  K8S_GCR_URL = 'k8s.gcr.io',
  LAUNCHER_GCR_URL = 'launcher.gcr.io'
}
 
export const repositoryFormat = 'docker'
export const resetTag = (formik: FormikValues): void => {
  formik.values.tagType === 'value' &&
    getMultiTypeFromValue(formik.values.tag?.value) === MultiTypeInputType.FIXED &&
    formik.values.tag?.value?.length &&
    formik.setFieldValue('tag', '')
}
 
export const getConnectorIdValue = (prevStepData: ConnectorConfigDTO | undefined): string => {
  Iif (getMultiTypeFromValue(prevStepData?.connectorId) !== MultiTypeInputType.FIXED) {
    return prevStepData?.connectorId
  }
  Iif (prevStepData?.connectorId?.value) {
    return prevStepData?.connectorId?.value
  }
  return prevStepData?.identifier || ''
}
 
export const helperTextData = (
  selectedArtifact: ArtifactType,
  formik: FormikValues,
  connectorIdValue: string
): ArtifactTagHelperText => {
  switch (selectedArtifact) {
    case ENABLED_ARTIFACT_TYPES.DockerRegistry:
      return {
        imagePath: formik.values?.imagePath,
        connectorRef: connectorIdValue
      }
    case ENABLED_ARTIFACT_TYPES.Ecr:
      return {
        imagePath: formik.values?.imagePath,
        region: formik.values?.region || '',
        connectorRef: connectorIdValue
      }
    case ENABLED_ARTIFACT_TYPES.Gcr:
      return {
        imagePath: formik.values?.imagePath,
        registryHostname: formik.values?.registryHostname || '',
        connectorRef: connectorIdValue
      }
    case ENABLED_ARTIFACT_TYPES.Nexus3Registry:
      return {
        artifactPath: formik.values?.artifactPath,
        repository: formik.values?.repository,
        repositoryPort: formik.values?.repositoryPort,
        connectorRef: connectorIdValue
      }
    case ENABLED_ARTIFACT_TYPES.ArtifactoryRegistry:
      return {
        artifactPath: formik.values?.artifactPath,
        repository: formik.values?.repository,
        connectorRef: connectorIdValue
      }
    default:
      return {} as ArtifactTagHelperText
  }
}
 
export const checkIfQueryParamsisNotEmpty = (queryParamList: Array<string | number | undefined>): boolean => {
  return queryParamList.every(querydata => {
    Eif (typeof querydata !== 'number') {
      return !isEmpty(querydata)
    }
    return querydata !== undefined
  })
}
export const shouldFetchTags = (
  prevStepData: ConnectorConfigDTO | undefined,
  queryParamList: Array<string | number>
): boolean => {
  return (
    !isEmpty(getConnectorIdValue(prevStepData)) &&
    getMultiTypeFromValue(getConnectorIdValue(prevStepData)) === MultiTypeInputType.FIXED &&
    checkIfQueryParamsisNotEmpty(queryParamList) &&
    queryParamList.every(query => getMultiTypeFromValue(query) === MultiTypeInputType.FIXED)
  )
}
 
export const shouldFetchTagsSource = (connectorRefValue: any, queryParamList: Array<string>): boolean => {
  return (
    !isEmpty(connectorRefValue) &&
    getMultiTypeFromValue(connectorRefValue) === MultiTypeInputType.FIXED &&
    checkIfQueryParamsisNotEmpty(queryParamList) &&
    queryParamList.every(query => getMultiTypeFromValue(query) === MultiTypeInputType.FIXED)
  )
}
 
export const getFinalArtifactObj = (
  formData: ImagePathTypes & { connectorId?: string },
  isSideCar: boolean
): ArtifactConfig => {
  const tagData =
    formData?.tagType === TagTypes.Value
      ? { tag: defaultTo(formData.tag?.value, formData.tag) }
      : { tagRegex: defaultTo(formData.tagRegex?.value, formData.tagRegex) }
 
  const artifactObj: ArtifactConfig = {
    spec: {
      connectorRef: formData?.connectorId,
      imagePath: formData?.imagePath,
      ...tagData
    }
  }
  Eif (isSideCar) {
    merge(artifactObj, { identifier: formData?.identifier })
  }
  return artifactObj
}
 
export const getFinalArtifactFormObj = (
  formData: ImagePathTypes & { connectorId?: string },
  isSideCar: boolean
): ArtifactConfig => {
  const tagData =
    formData?.tagType === TagTypes.Value
      ? { tag: defaultTo(formData.tag?.value, formData.tag) }
      : { tagRegex: defaultTo(formData.tagRegex?.value, formData.tagRegex) }
 
  const artifactObj: ArtifactConfig = {
    spec: {
      connectorRef: formData?.connectorId,
      artifactPath: formData?.artifactPath,
      ...tagData
    }
  }
  Eif (isSideCar) {
    merge(artifactObj, { identifier: formData?.identifier })
  }
  return artifactObj
}
 
export const getArtifactFormData = (
  initialValues: ImagePathTypes,
  selectedArtifact: ArtifactType,
  isSideCar: boolean
): ImagePathTypes => {
  const specValues = get(initialValues, 'spec', null)
 
  if (selectedArtifact !== (initialValues as any)?.type || !specValues) {
    return defaultArtifactInitialValues(selectedArtifact)
  }
 
  const values = {
    ...specValues,
    tagType: specValues.tag ? TagTypes.Value : TagTypes.Regex
  }
  Iif (specValues?.tag && getMultiTypeFromValue(specValues?.tag) === MultiTypeInputType.FIXED) {
    values.tag = { label: specValues?.tag, value: specValues?.tag }
  }
  if (isSideCar && initialValues?.identifier) {
    merge(values, { identifier: initialValues?.identifier })
  }
  return values
}
 
export const defaultArtifactInitialValues = (selectedArtifact: ArtifactType): any => {
  switch (selectedArtifact) {
    case ENABLED_ARTIFACT_TYPES.Nexus3Registry:
      return {
        identifier: '',
        tagType: TagTypes.Value,
        tag: RUNTIME_INPUT_VALUE,
        tagRegex: RUNTIME_INPUT_VALUE,
        repositoryPortorRepositoryURL: RepositoryPortOrServer.RepositoryUrl
      }
    case ENABLED_ARTIFACT_TYPES.CustomArtifact:
      return {
        identifier: '',
        version: RUNTIME_INPUT_VALUE
      }
    case ENABLED_ARTIFACT_TYPES.DockerRegistry:
    case ENABLED_ARTIFACT_TYPES.Gcr:
    case ENABLED_ARTIFACT_TYPES.Ecr:
    case ENABLED_ARTIFACT_TYPES.ArtifactoryRegistry:
    default:
      return {
        identifier: '',
        tag: RUNTIME_INPUT_VALUE,
        tagType: TagTypes.Value,
        tagRegex: RUNTIME_INPUT_VALUE
      }
  }
}
 
export const showConnectorStep = (selectedArtifact: ArtifactType): boolean => {
  return selectedArtifact !== ENABLED_ARTIFACT_TYPES.CustomArtifact
}