All files / modules/75-cd/components/PipelineSteps/K8sServiceSpec/ArtifactSource artifactSourceUtils.ts

78.69% Statements 48/61
51.05% Branches 73/143
100% Functions 12/12
73.47% Lines 36/49

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              111x   111x   111x 111x                             111x   111x                 111x 1x 1x         111x 85x         111x 2x 111x 4x 2x   2x       111x             41x   7x 34x         1x   33x     111x 20x   111x 24x                         111x           1x                                         111x           1x                                             777x   111x       25x 111x 79x        
/*
 * 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, SelectOption } from '@harness/uicore'
import type { FormikValues } from 'formik'
import { cloneDeep, defaultTo, get, isEmpty, unset } from 'lodash-es'
import type { GetDataError } from 'restful-react'
import { parse } from 'yaml'
import {
  PRIMARY_ARTIFACT,
  TriggerDefaultFieldList,
  TriggerTypes
} from '@triggers/pages/triggers/utils/TriggersWizardPageUtils'
import type { K8SDirectServiceStep } from '@pipeline/factories/ArtifactTriggerInputFactory/types'
import type {
  ArtifactConfig,
  ArtifactoryBuildDetailsDTO,
  DockerBuildDetailsDTO,
  EcrBuildDetailsDTO,
  GcrBuildDetailsDTO,
  NexusBuildDetailsDTO,
  PipelineInfoConfig
} from 'services/cd-ng'
import { RegistryHostNames } from '@pipeline/components/ArtifactsSelection/ArtifactUtils'
import type { ArtifactType } from '@pipeline/components/ArtifactsSelection/ArtifactInterface'
import { clearRuntimeInputValue } from '../K8sServiceSpecHelper'
 
export type BuildDetailsDTO =
  | DockerBuildDetailsDTO[]
  | GcrBuildDetailsDTO[]
  | EcrBuildDetailsDTO[]
  | NexusBuildDetailsDTO[]
  | ArtifactoryBuildDetailsDTO[]
 
export const resetTags = (formik: FormikValues, tagPath: string): void => {
  const tagValue = get(formik.values, tagPath, '')
  Iif (getMultiTypeFromValue(tagValue) === MultiTypeInputType.FIXED && tagValue?.length) {
    formik.setFieldValue(tagPath, '')
  }
}
 
export const fromPipelineInputTriggerTab = (formik: FormikValues, fromTrigger = false): boolean => {
  return (
    formik?.values?.triggerType === TriggerTypes.ARTIFACT && !isEmpty(formik?.values?.selectedArtifact) && !fromTrigger
  )
}
 
export const isSelectedStage = (stageIdentifier: string, formikStageId: string): boolean =>
  stageIdentifier === formikStageId
export const isSelectedArtifact = (selectedArtifact: any, identifier?: string): boolean => {
  if (!isEmpty(identifier)) {
    return !isEmpty(selectedArtifact) && selectedArtifact.identifier === identifier
  }
  return (
    !isEmpty(selectedArtifact) && (!selectedArtifact.identifier || selectedArtifact.identifier === PRIMARY_ARTIFACT)
  )
}
export const isFieldfromTriggerTabDisabled = (
  fieldName: string,
  formik: FormikValues,
  stageIdentifier: string,
  fromTrigger = false,
  identifier?: string
): boolean => {
  if (fromTrigger) {
    // Trigger Configuration Tab
    return get(TriggerDefaultFieldList, fieldName) ? true : false
  } else if (
    fromPipelineInputTriggerTab(formik, fromTrigger) &&
    isSelectedArtifact(formik?.values?.selectedArtifact, identifier) &&
    isSelectedStage(stageIdentifier, formik?.values?.stageId)
  ) {
    return true
  }
  return false
}
 
export const getTagError = (fetchTagsError: GetDataError<any> | null): string =>
  get(fetchTagsError, 'data.message', null)
 
export const getYamlData = (formikValues: Record<string, any>): PipelineInfoConfig =>
  clearRuntimeInputValue(
    cloneDeep(
      parse(
        defaultTo(
          JSON.stringify({
            pipeline: formikValues
          }),
          ''
        )
      )
    )
  )
 
export const getPrimaryInitialValues = (
  initialValues: K8SDirectServiceStep,
  formik: FormikValues,
  stageIdentifier: string,
  artifactPath: string
): { type: ArtifactType; spec: ArtifactConfig } | undefined => {
  Iif (stageIdentifier === formik?.values?.stageId) {
    const initialArtifactValue = get(initialValues, `artifacts.${artifactPath}`)
    const { selectedArtifact } = formik?.values
 
    if (initialArtifactValue && isEmpty(selectedArtifact.identifier)) {
      /*
         backend requires eventConditions inside selectedArtifact but should not be added to inputYaml
        */
      if (selectedArtifact?.spec.eventConditions) {
        unset(selectedArtifact?.spec, 'eventConditions')
      }
 
      return {
        type: selectedArtifact?.type,
        spec: {
          ...selectedArtifact?.spec
        }
      }
    }
  }
}
export const getSidecarInitialValues = (
  initialValues: K8SDirectServiceStep,
  formik: FormikValues,
  stageIdentifier: string,
  artifactPath: string
): { identifier: string; type: ArtifactType; spec: ArtifactConfig } | undefined => {
  Iif (stageIdentifier === formik?.values?.stageId) {
    const initialArtifactValue = get(initialValues, `artifacts.${artifactPath}`)
    const { selectedArtifact } = formik?.values
 
    if (initialArtifactValue && selectedArtifact.identifier === initialArtifactValue.identifier) {
      /*
         backend requires eventConditions inside selectedArtifact but should not be added to inputYaml
        */
      if (selectedArtifact?.spec.eventConditions) {
        unset(selectedArtifact?.spec, 'eventConditions')
      }
 
      return {
        identifier: selectedArtifact?.identifier,
        type: selectedArtifact?.type,
        spec: {
          ...selectedArtifact?.spec
        }
      }
    }
  }
}
 
export const gcrUrlList: SelectOption[] = Object.values(RegistryHostNames).map(item => ({ label: item, value: item }))
 
export const isArtifactSourceRuntime = (
  isPrimaryArtifactsRuntime: boolean,
  isSidecarRuntime: boolean,
  isSidecar: boolean
): boolean => (!isSidecar && isPrimaryArtifactsRuntime) || (isSidecar && isSidecarRuntime)
export const getImagePath = (initialImagePath: string, formikImagePathValue: string): string => {
  return getMultiTypeFromValue(initialImagePath) !== MultiTypeInputType.RUNTIME
    ? initialImagePath
    : formikImagePathValue
}