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

100% Statements 21/21
83.93% Branches 47/56
100% Functions 7/7
100% Lines 17/17

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              110x       110x 19x 19x 6x   6x     2x         6x   19x 34x 24x 23x     19x     110x 19x                                               110x 4x                                              
/*
 * 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 { JiraCreateFieldType, JiraFieldNGWithValue } from '../JiraCreate/types'
import type { JiraUpdateData } from './types'
 
export const processFieldsForSubmit = (values: JiraUpdateData): JiraCreateFieldType[] => {
  const toReturn: JiraCreateFieldType[] = []
  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 alreadyPresent = toReturn.find(field => field.name === kvField.name)
    if (!alreadyPresent) {
      toReturn.push(kvField)
    }
  })
  return toReturn
}
 
export const processFormData = (values: JiraUpdateData): JiraUpdateData => {
  return {
    ...values,
    spec: {
      connectorRef:
        getMultiTypeFromValue(values.spec.connectorRef as SelectOption) === MultiTypeInputType.FIXED
          ? (values.spec.connectorRef as SelectOption)?.value?.toString()
          : values.spec.connectorRef,
      issueKey: values.spec.issueKey,
      transitionTo:
        values.spec.transitionTo?.transitionName || values.spec.transitionTo?.status
          ? {
              transitionName: values.spec.transitionTo.transitionName,
              status:
                getMultiTypeFromValue(values.spec.transitionTo.status as SelectOption) === MultiTypeInputType.FIXED
                  ? (values.spec.transitionTo.status as SelectOption).value?.toString()
                  : values.spec.transitionTo.status
            }
          : undefined,
      fields: processFieldsForSubmit(values),
      delegateSelectors: values.spec.delegateSelectors
    }
  }
}
 
export const processInitialValues = (values: JiraUpdateData): JiraUpdateData => {
  return {
    ...values,
    spec: {
      delegateSelectors: values.spec.delegateSelectors,
      connectorRef: values.spec.connectorRef,
      issueKey: values.spec.issueKey,
      transitionTo: values.spec.transitionTo
        ? {
            status:
              getMultiTypeFromValue(values.spec.transitionTo.status as string) === MultiTypeInputType.FIXED &&
              values.spec.transitionTo.status
                ? {
                    label: values.spec.transitionTo.status.toString(),
                    value: values.spec.transitionTo.status.toString()
                  }
                : values.spec.transitionTo.status,
            transitionName: values.spec.transitionTo.transitionName
          }
        : undefined,
      fields: values.spec.fields
    }
  }
}