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

95.4% Statements 83/87
82.03% Branches 105/128
92.31% Functions 24/26
95.65% Lines 66/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              112x   112x         112x 3x 1x 1x 1x   3x 1x 1x   3x 1x       112x 5x   112x 25x                   25x 7x   7x     2x         7x   25x 157x 45x 44x     25x     112x       15x 7x 1x 6x 6x         2x     1x 2x       1x     4x         112x 25x                                         112x       112x 4x                                         4x 5x           112x         2x 2x 2x 6x 2x 2x         2x     112x         3x 3x 7x 7x 7x 7x     3x     112x 18x 18x 10x 10x     18x    
/*
 * Copyright 2021 Harness Inc. All rights reserved.
 * Use of this source code is governed by the PolyForm Free Trial 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/05/PolyForm-Free-Trial-1.0.0.txt.
 */
 
import { getMultiTypeFromValue, MultiSelectOption, MultiTypeInputType, SelectOption } from '@wings-software/uicore'
import type { FormikProps } from 'formik'
import { isEmpty } from 'lodash-es'
import type { JiraFieldNG } from 'services/cd-ng'
import type { JiraProjectSelectOption } from '../JiraApproval/types'
import type { JiraCreateData, JiraCreateFieldType, JiraFieldNGWithValue } from './types'
 
export const resetForm = (formik: FormikProps<JiraCreateData>, parent: string) => {
  if (parent === 'connectorRef') {
    formik.setFieldValue('spec.projectKey', '')
    formik.setFieldValue('spec.issueType', '')
    formik.setFieldValue('spec.fields', [])
  }
  if (parent === 'projectKey') {
    formik.setFieldValue('spec.issueType', '')
    formik.setFieldValue('spec.fields', [])
  }
  if (parent === 'issueType') {
    formik.setFieldValue('spec.fields', [])
  }
}
 
export const omitSummaryDescription = (fields: JiraCreateFieldType[]): JiraCreateFieldType[] =>
  fields?.filter(field => field.name !== 'Summary' && field.name !== 'Description')
 
export const processFieldsForSubmit = (values: JiraCreateData): JiraCreateFieldType[] => {
  const toReturn: JiraCreateFieldType[] = [
    {
      name: 'Summary',
      value: values.spec.summary || ''
    },
    {
      name: 'Description',
      value: values.spec.description || ''
    }
  ]
  values.spec.selectedFields?.forEach((field: JiraFieldNGWithValue) => {
    const name = field.name
    const value =
      typeof field.value === 'string' || typeof field.value === 'number'
        ? field.value
        : Array.isArray(field.value)
        ? (field.value as MultiSelectOption[]).map(opt => opt.value.toString()).join(',')
        : typeof field.value === 'object'
        ? (field.value as SelectOption).value?.toString()
        : ''
    // The return value should be comma separated string or a number
    toReturn.push({ name, value })
  })
  values.spec.fields?.forEach((kvField: JiraCreateFieldType) => {
    const alreadyExists = toReturn.find(ff => ff.name === kvField.name)
    if (!alreadyExists) {
      toReturn.push(kvField)
    }
  })
  return toReturn
}
 
export const getInitialValueForSelectedField = (
  savedFields: JiraCreateFieldType[],
  field: JiraFieldNG
): string | number | SelectOption | MultiSelectOption[] => {
  const savedValue = savedFields.find(sf => sf.name === field.name)?.value
  if (typeof savedValue === 'number') {
    return savedValue as number
  } else Eif (typeof savedValue === 'string') {
    if (
      getMultiTypeFromValue(savedValue) === MultiTypeInputType.FIXED &&
      field.allowedValues &&
      field.schema.type === 'option'
    ) {
      if (field.schema.array) {
        // multiselect
        // return multiselectoption[]
        const splitValues = (savedValue as string).split(',')
        return splitValues.map(splitvalue => ({ label: splitvalue, value: splitvalue })) as MultiSelectOption[]
      } else {
        // singleselect
        // return selectoption
        return { label: savedValue, value: savedValue } as SelectOption
      }
    }
    return savedValue as string
  }
  return ''
}
 
export const processFormData = (values: JiraCreateData): JiraCreateData => {
  return {
    ...values,
    spec: {
      delegateSelectors: values.spec.delegateSelectors,
      connectorRef:
        getMultiTypeFromValue(values.spec.connectorRef as SelectOption) === MultiTypeInputType.FIXED
          ? (values.spec.connectorRef as SelectOption)?.value?.toString()
          : values.spec.connectorRef,
      projectKey:
        getMultiTypeFromValue(values.spec.projectKey as JiraProjectSelectOption) === MultiTypeInputType.FIXED
          ? (values.spec.projectKey as JiraProjectSelectOption)?.key?.toString()
          : values.spec.projectKey,
      issueType:
        getMultiTypeFromValue(values.spec.issueType as JiraProjectSelectOption) === MultiTypeInputType.FIXED
          ? (values.spec.issueType as JiraProjectSelectOption)?.key?.toString()
          : values.spec.issueType,
      fields: processFieldsForSubmit(values)
    }
  }
}
 
export const getKVFields = (values: JiraCreateData): JiraCreateFieldType[] => {
  return omitSummaryDescription(processFieldsForSubmit(values))
}
 
export const processInitialValues = (values: JiraCreateData): JiraCreateData => {
  return {
    ...values,
    spec: {
      delegateSelectors: values.spec.delegateSelectors,
      connectorRef: values.spec.connectorRef,
      projectKey:
        values.spec.projectKey && getMultiTypeFromValue(values.spec.projectKey) === MultiTypeInputType.FIXED
          ? {
              label: values.spec.projectKey.toString(),
              value: values.spec.projectKey.toString(),
              key: values.spec.projectKey.toString()
            }
          : values.spec.projectKey,
      issueType:
        values.spec.issueType && getMultiTypeFromValue(values.spec.issueType) === MultiTypeInputType.FIXED
          ? {
              label: values.spec.issueType.toString(),
              value: values.spec.issueType.toString(),
              key: values.spec.issueType.toString()
            }
          : values.spec.issueType,
      summary: values.spec.fields?.find(field => field.name === 'Summary')?.value.toString(),
      description: values.spec.fields?.find(field => field.name === 'Description')?.value.toString(),
      fields: omitSummaryDescription(values.spec.fields)
    }
  }
}
 
export const getSelectedFieldsToBeAddedInForm = (
  newFields: JiraFieldNG[],
  existingFields: JiraFieldNGWithValue[] = [],
  existingKVFields: JiraCreateFieldType[]
): JiraFieldNGWithValue[] => {
  const toReturn: JiraFieldNGWithValue[] = []
  newFields.forEach(field => {
    const alreadyPresent = existingFields.find(existing => existing.name === field.name)
    const alreadyPresentKVField = existingKVFields.find(kv => kv.name === field.name)
    Eif (!alreadyPresent && !alreadyPresentKVField) {
      toReturn.push({ ...field, value: !isEmpty(field.allowedValues) ? [] : '' })
    } else {
      toReturn.push({ ...field, value: alreadyPresent !== undefined ? alreadyPresent?.value : '' })
    }
  })
  return toReturn
}
 
export const getKVFieldsToBeAddedInForm = (
  newFields: JiraCreateFieldType[],
  existingFields: JiraCreateFieldType[] = [],
  existingSelectedFields: JiraFieldNGWithValue[] = []
): JiraCreateFieldType[] => {
  const toReturn: JiraCreateFieldType[] = [...existingFields]
  newFields.forEach(field => {
    const alreadyPresent = existingFields.find(existing => existing.name === field.name)
    const alreadyPresentSelectedField = existingSelectedFields.find(existing => existing.name === field.name)
    Eif (!alreadyPresent && !alreadyPresentSelectedField) {
      toReturn.push(field)
    }
  })
  return toReturn
}
 
export const updateMap = (alreadySelectedFields: JiraFieldNG[]): Record<string, boolean> => {
  const map: Record<string, boolean> = {}
  if (!isEmpty(alreadySelectedFields)) {
    alreadySelectedFields.forEach(field => {
      map[field.name] = true
    })
  }
  return map
}