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

92.86% Statements 26/28
70% Branches 42/60
100% Functions 6/6
92.31% Lines 24/26

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              110x     110x 15x   13x   3x       10x     2x     110x 4x 4x 3x   3x 3x 6x     5x             4x       110x 15x                     15x 10x   10x 10x   2x               15x    
/*
 * 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 { getMultiTypeFromValue, MultiTypeInputType } from '@wings-software/uicore'
import type { ApproverInputsSubmitCallInterface, HarnessApprovalData } from './types'
 
const getInitialValueForMinCount = (valueFromData: string | number): string | number => {
  if (getMultiTypeFromValue(valueFromData) === MultiTypeInputType.FIXED) {
    // type is FIXED
    if (valueFromData) {
      // type is FIXED and value exists i.e. user has typed some numbers, convert them to number and return
      return Number(valueFromData)
    }
    // If the type is FIXED but the value doesn't exist i.e. opening the form for the first time
    // return the default value of 1
    return 1
  }
  // if the type is not FIXED i.e. runtime or expression, return the string as it is
  return valueFromData
}
 
export const processFormData = (data: HarnessApprovalData): HarnessApprovalData => {
  const toReturn: HarnessApprovalData = { ...data }
  if (data.spec.approverInputs) {
    Iif (getMultiTypeFromValue(data.spec.approverInputs as string) === MultiTypeInputType.RUNTIME) {
      toReturn.spec.approverInputs = data.spec.approverInputs
    } else Eif (Array.isArray(data.spec.approverInputs)) {
      toReturn.spec.approverInputs = (data.spec.approverInputs as ApproverInputsSubmitCallInterface[])
        ?.filter(input => input.name)
        ?.map(
          (input: ApproverInputsSubmitCallInterface) =>
            ({
              name: input.name,
              defaultValue: input.defaultValue
            } as ApproverInputsSubmitCallInterface)
        )
    }
  }
  return toReturn
}
 
// Converting API call data for formik values, to populate while editing the step
export const processForInitialValues = (data: HarnessApprovalData): HarnessApprovalData => {
  const toReturn: HarnessApprovalData = {
    ...data,
    spec: {
      ...data.spec,
      approvers: {
        ...data.spec?.approvers,
        minimumCount: getInitialValueForMinCount(data.spec?.approvers?.minimumCount)
      }
    }
  }
 
  if (data.spec?.approverInputs) {
    Iif (getMultiTypeFromValue(data.spec?.approverInputs as string) === MultiTypeInputType.RUNTIME) {
      toReturn.spec.approverInputs = data.spec?.approverInputs
    } else Eif (Array.isArray(data.spec?.approverInputs)) {
      toReturn.spec.approverInputs = (data.spec?.approverInputs as ApproverInputsSubmitCallInterface[])?.map(
        (input: ApproverInputsSubmitCallInterface) =>
          ({
            name: input.name,
            defaultValue: input.defaultValue
          } as ApproverInputsSubmitCallInterface)
      )
    }
  }
 
  return toReturn
}