All files / modules/70-pipeline/utils stepUtils.ts

71.93% Statements 41/57
45% Branches 18/40
88.89% Functions 8/9
71.93% Lines 41/57

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              127x 127x 127x 127x     127x         127x   127x 127x 127x 127x     127x 82x     127x 82x     127x 82x     127x 82x     127x                                 127x         79x 79x 79x   18x 18x         79x   3x                                 76x                           127x       1x 1x 1x   1x     1x 1x       1x 1x                         1x   1x       1x 1x    
/*
 * 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 produce from 'immer'
import { isEmpty, set, get } from 'lodash-es'
import { StepType } from '@pipeline/components/PipelineSteps/PipelineStepInterface'
import { StageType } from '@pipeline/utils/stageHelpers'
import type { StageElementConfig, StepElementConfig } from 'services/cd-ng'
import type { StepPalleteModuleInfo } from 'services/pipeline-ng'
import {
  StepOrStepGroupOrTemplateStepData,
  TabTypes,
  Values
} from '@pipeline/components/PipelineStudio/StepCommands/StepCommandTypes'
import { sanitize } from '@common/utils/JSONUtils'
 
export enum StepMode {
  STAGE = 'STAGE',
  STEP_GROUP = 'STEP_GROUP',
  STEP = 'STEP'
}
 
export function isHarnessApproval(stepType?: string): boolean {
  return stepType === StepType.HarnessApproval
}
 
export function isJiraApproval(stepType?: string): boolean {
  return stepType === StepType.JiraApproval
}
 
export function isApprovalStep(stepType?: string): boolean {
  return isHarnessApproval(stepType) || isJiraApproval(stepType) || isServiceNowApproval(stepType)
}
 
export function isServiceNowApproval(stepType?: string): boolean {
  return stepType === StepType.ServiceNowApproval
}
 
export function getAllStepPaletteModuleInfos(): StepPalleteModuleInfo[] {
  return [
    {
      module: 'cd',
      shouldShowCommonSteps: false
    },
    {
      module: 'ci',
      shouldShowCommonSteps: true
    },
    {
      module: 'cv',
      shouldShowCommonSteps: false
    }
  ]
}
 
export function getStepPaletteModuleInfosFromStage(
  stageType?: string,
  stage?: StageElementConfig,
  initialCategory?: string
): StepPalleteModuleInfo[] {
  const deploymentType = get(stage, 'spec.serviceConfig.serviceDefinition.type', undefined)
  let category = initialCategory
  switch (deploymentType) {
    case 'Kubernetes':
      category = 'Kubernetes'
      break
    case 'NativeHelm':
      category = 'Helm'
      break
  }
  switch (stageType) {
    case StageType.BUILD:
      return [
        {
          module: 'ci',
          shouldShowCommonSteps: false
        }
      ]
 
    case StageType.FEATURE:
      return [
        {
          module: 'pms',
          category: 'FeatureFlag',
          shouldShowCommonSteps: true
        }
      ]
 
    default:
      return [
        {
          module: 'cd',
          category: stageType === StageType.APPROVAL ? 'Approval' : category,
          shouldShowCommonSteps: true
        },
        {
          module: 'cv',
          shouldShowCommonSteps: false
        }
      ]
  }
}
 
export function getStepDataFromValues(
  item: Partial<Values>,
  initialValues: StepOrStepGroupOrTemplateStepData
): StepElementConfig {
  const processNode = produce(initialValues as StepElementConfig, node => {
    Eif (item.tab !== TabTypes.Advanced) {
      Iif ((item as StepElementConfig).description) {
        node.description = (item as StepElementConfig).description
      } else Iif (node.description) {
        delete node.description
      }
      Eif ((item as StepElementConfig).timeout) {
        node.timeout = (item as StepElementConfig).timeout
      } else if (node.timeout) {
        delete node.timeout
      }
      Eif ((item as StepElementConfig).spec) {
        node.spec = { ...(item as StepElementConfig).spec }
      }
    } else {
      if (item.when) {
        node.when = item.when
      }
      if (!isEmpty(item.delegateSelectors)) {
        set(node, 'spec.delegateSelectors', item.delegateSelectors)
      } else if (node.spec?.delegateSelectors) {
        delete node.spec.delegateSelectors
      }
    }
    // default strategies can be present without having the need to click on Advanced Tab. For eg. in CV step.
    Iif (Array.isArray(item.failureStrategies) && !isEmpty(item.failureStrategies)) {
      node.failureStrategies = item.failureStrategies
    } else Iif (node.failureStrategies) {
      delete node.failureStrategies
    }
  })
  sanitize(processNode, { removeEmptyArray: false, removeEmptyObject: false, removeEmptyString: false })
  return processNode
}