All files / modules/70-pipeline/components/PipelineSteps/Steps StepsTransformValuesUtils.ts

100% Statements 139/139
88.2% Branches 157/178
100% Functions 24/24
100% Lines 137/137

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                120x 120x 120x                 120x 120x 120x 120x 120x 120x 120x 120x 120x 120x 120x 120x 120x 120x 120x 120x 120x                   120x 297x   1159x 105x 105x     1054x 233x   233x 8x         297x     120x                       62x   62x 813x   813x 544x     813x   50x   43x           50x     813x   29x   31x         29x     813x   10x   6x         10x     813x   12x 13x     12x     813x   1x 2x     1x     813x   5x 2x     5x     813x   20x 60x     20x     813x   9x 33x     9x     813x   5x 8x     5x     813x 54x 54x     813x 12x     12x   5x         12x     813x 54x 54x     813x 8x 8x       62x     120x 63x   63x 862x 522x 522x     862x 53x   53x 53x 31x 73x 73x         53x     862x 35x     35x   54x 35x     862x 7x     7x     10x 10x     7x     862x 62x   62x 62x     862x               48x   48x 48x     862x 10x     10x   10x   10x 6x     10x     862x 62x 62x     862x 62x 62x     862x 1x 1x       63x    
/*
 * 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 { v4 as nameSpace, v5 as uuid } from 'uuid'
import { get, set, isEmpty, isObjectLike, isPlainObject, isBoolean } from 'lodash-es'
import { getMultiTypeFromValue, MultiTypeInputType } from '@wings-software/uicore'
import type {
  MapType,
  MultiTypeConnectorRef,
  MultiTypeSelectOption,
  MultiTypeListUIType,
  MultiTypeMapUIType
} from './StepsTypes'
 
export enum Types {
  Text,
  Map,
  List,
  OutputVariables,
  ArchiveFormat,
  Pull,
  BuildTool,
  Language,
  ConnectorRef,
  ReportPaths,
  LimitMemory,
  LimitCPU,
  Provisioner,
  Boolean,
  ImagePullPolicy,
  Shell
}
 
interface Field {
  name: string
  type: Types
}
 
type Dependencies = { [key: string]: any }
 
export function removeEmptyKeys<T>(object: { [key: string]: any }): T {
  Object.keys(object).forEach(key => {
    // We should skip this check for boolean in order not to remove a key with a value "false"
    if (((isObjectLike(object[key]) && isEmpty(object[key])) || !object[key]) && !isBoolean(object[key])) {
      delete object[key]
      return
    }
 
    if (isPlainObject(object[key])) {
      removeEmptyKeys(object[key])
 
      if (isEmpty(object[key])) {
        delete object[key]
      }
    }
  })
 
  return object as T
}
 
export function getInitialValuesInCorrectFormat<T, U>(
  initialValues: T,
  fields: Field[],
  {
    archiveFormatOptions,
    pullOptions,
    buildToolOptions,
    languageOptions,
    imagePullPolicyOptions,
    shellOptions
  }: Dependencies = {}
): U {
  const values = {}
 
  fields.forEach(({ name, type }) => {
    const value = get(initialValues, name)
 
    if (type === Types.Text || type === Types.ConnectorRef || type === Types.Boolean) {
      set(values, name, value)
    }
 
    if (type === Types.Map) {
      const map =
        typeof value === 'string'
          ? value
          : Object.keys(value || {}).map(key => ({
              id: uuid('', nameSpace()),
              key: key,
              value: value[key]
            }))
 
      set(values, name, map)
    }
 
    if (type === Types.List) {
      const list =
        typeof value === 'string'
          ? value
          : value?.map((_value: string) => ({
              id: uuid('', nameSpace()),
              value: _value
            })) || []
 
      set(values, name, list)
    }
 
    if (type === Types.OutputVariables) {
      const list =
        typeof value === 'string'
          ? value
          : value?.map((_value: { name: string }) => ({
              id: uuid('', nameSpace()),
              value: _value.name
            })) || []
 
      set(values, name, list)
    }
 
    if (type === Types.ArchiveFormat) {
      const archiveFormat =
        getMultiTypeFromValue(value) === MultiTypeInputType.FIXED
          ? archiveFormatOptions?.find((option: SelectOption) => option.value === value) || archiveFormatOptions[0]
          : value
 
      set(values, name, archiveFormat)
    }
 
    if (type === Types.Pull) {
      const pull =
        getMultiTypeFromValue(value) === MultiTypeInputType.FIXED
          ? pullOptions?.find((option: SelectOption) => option.value === value) || pullOptions[0]
          : value
 
      set(values, name, pull)
    }
 
    if (type === Types.BuildTool) {
      const buildTool =
        getMultiTypeFromValue(value) === MultiTypeInputType.FIXED
          ? buildToolOptions?.find((option: SelectOption) => option.value === value)
          : value
 
      set(values, name, buildTool)
    }
 
    if (type === Types.ImagePullPolicy) {
      const imagePullPolicy =
        getMultiTypeFromValue(value) === MultiTypeInputType.FIXED
          ? imagePullPolicyOptions?.find((option: SelectOption) => option.value === value)
          : value
 
      set(values, name, imagePullPolicy)
    }
 
    if (type === Types.Shell) {
      const shell =
        getMultiTypeFromValue(value) === MultiTypeInputType.FIXED
          ? shellOptions?.find((option: SelectOption) => option.value === value)
          : value
 
      set(values, name, shell)
    }
 
    if (type === Types.Language) {
      const language =
        getMultiTypeFromValue(value) === MultiTypeInputType.FIXED
          ? languageOptions?.find((option: SelectOption) => option.value === value)
          : value
 
      set(values, name, language)
    }
 
    if (type === Types.LimitMemory) {
      const _value = get(initialValues, 'spec.resources.limits.memory')
      set(values, 'spec.limitMemory', _value)
    }
 
    if (type === Types.ReportPaths) {
      const _value = get(initialValues, 'spec.reports.spec.paths')
 
      const list =
        typeof _value === 'string'
          ? _value
          : _value?.map((val: string) => ({
              id: uuid('', nameSpace()),
              value: val
            })) || []
 
      set(values, 'spec.reportPaths', list)
    }
 
    if (type === Types.LimitCPU) {
      const _value = get(initialValues, 'spec.resources.limits.cpu')
      set(values, 'spec.limitCPU', _value)
    }
 
    if (type === Types.Provisioner) {
      const provisioner = get(initialValues, 'provisioner')
      set(values, 'provisioner.stage.spec.execution', provisioner)
    }
  })
 
  return values as U
}
 
export function getFormValuesInCorrectFormat<T, U>(formValues: T, fields: Field[]): U {
  const values = {}
 
  fields.forEach(({ name, type }) => {
    if (type === Types.Text || type === Types.Boolean) {
      const value = get(formValues, name)
      set(values, name, value)
    }
 
    if (type === Types.Map) {
      const value = get(formValues, name) as MultiTypeMapUIType
 
      const map: MapType = {}
      if (Array.isArray(value)) {
        value.forEach(mapValue => {
          Eif (mapValue.key) {
            map[mapValue.key] = mapValue.value
          }
        })
      }
 
      set(values, name, typeof value === 'string' ? value : map)
    }
 
    if (type === Types.List) {
      const value = get(formValues, name) as MultiTypeListUIType
 
      const list =
        typeof value === 'string'
          ? value
          : value?.filter(listValue => !!listValue.value).map(listValue => listValue.value)
      set(values, name, list)
    }
 
    if (type === Types.OutputVariables) {
      const value = get(formValues, name) as MultiTypeListUIType
 
      const list =
        typeof value === 'string'
          ? value
          : value
              ?.filter(listValue => !!listValue.value)
              .map(listValue => ({
                name: listValue.value
              }))
      set(values, name, list)
    }
 
    if (type === Types.ConnectorRef) {
      const value = get(formValues, name) as MultiTypeConnectorRef
 
      const connectorRef = typeof value === 'string' ? value : value?.value
      set(values, name, connectorRef)
    }
    // Set Select field values
    if (
      type === Types.ArchiveFormat ||
      type === Types.Pull ||
      type === Types.BuildTool ||
      type === Types.Language ||
      type === Types.Shell ||
      type === Types.ImagePullPolicy
    ) {
      const value = get(formValues, name) as MultiTypeSelectOption
 
      const pull = typeof value === 'string' ? value : value?.value
      set(values, name, pull)
    }
 
    if (type === Types.ReportPaths) {
      const _value = get(formValues, 'spec.reportPaths') as MultiTypeListUIType
 
      const list =
        typeof _value === 'string'
          ? _value
          : _value?.filter(listValue => !!listValue.value).map(listValue => listValue.value)
 
      if (!isEmpty(list)) {
        set(values, 'spec.reports.type', 'JUnit')
      }
 
      set(values, 'spec.reports.spec.paths', list)
    }
 
    if (type === Types.LimitMemory) {
      const _value = get(formValues, 'spec.limitMemory')
      set(values, 'spec.resources.limits.memory', _value)
    }
 
    if (type === Types.LimitCPU) {
      const _value = get(formValues, 'spec.limitCPU')
      set(values, 'spec.resources.limits.cpu', _value)
    }
 
    if (type === Types.Provisioner) {
      const _value = get(formValues, 'provisioner.stage.spec.execution')
      set(values, 'provisioner', _value)
    }
  })
 
  return removeEmptyKeys<U>(values)
}