All files / modules/10-common/components/YAMLBuilder YAMLBuilderUtils.tsx

68.97% Statements 40/58
58.43% Branches 52/89
45.45% Functions 5/11
68.42% Lines 39/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              1x     1x   1x                 1x 3x 3x 3x 3x 3x 3x 3x 3x 3x 2x   3x         3x 3x                     1x                 1x 1x 1x 1x 1x 2x 1x 1x                         1x 2x 2x 3x 3x   2x             1x                                   1x                                                         1x 1x 1x 1x 1x    
/*
 * 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 React from 'react'
import type { DetailedReactHTMLElement } from 'react'
import type { Diagnostic } from 'vscode-languageserver-types'
import { parse } from 'yaml'
 
import { findLeafToParentPath, getSchemaWithLanguageSettings, validateYAMLWithSchema } from '../../utils/YamlUtils'
import type { YamlBuilderProps } from '@common/interfaces/YAMLBuilderProps'
import type { ToasterProps } from '@wings-software/uicore/dist/hooks/useToaster/useToaster'
 
/**
 * Get YAML from editor with placeholder added at current position in editor
 * @param editor
 * @param shouldAddPlaceholder whether to add a placeholder at current position in editor during yaml->json conversion
 */
const getYAMLFromEditor = (editor: any, shouldAddPlaceholder: boolean): string | null => {
  const currentPositionInEditor = editor?.getPosition(),
    textInCurrentEditorLine = editor?.getValue(currentPositionInEditor)?.trim(),
    currentLineNumber = currentPositionInEditor?.lineNumber,
    splitedText = textInCurrentEditorLine?.split('\n').slice(0, currentLineNumber),
    currentLineContent = splitedText?.[currentLineNumber - 1]
  const lengthOfCurrentText = textInCurrentEditorLine?.length
  Eif (lengthOfCurrentText > 0) {
    let textToInsert = ''
    if (shouldAddPlaceholder) {
      textToInsert = textInCurrentEditorLine[lengthOfCurrentText - 1] === ':' ? '' : ': ' + 'placeholder'
    }
    splitedText[currentLineNumber - 1] = [
      currentLineContent?.slice(0, currentPositionInEditor.column - 1),
      textToInsert,
      currentLineContent?.slice(currentPositionInEditor.column - 1)
    ].join('')
    editor.setPosition(currentPositionInEditor)
    return splitedText.join('\n')
  }
  return null
}
 
/**
 * Get current property to parent json path
 * @param editor
 * @param onErrorCallback
 * @param shouldAddPlaceholder
 */
const getMetaDataForKeyboardEventProcessing = ({
  editor,
  onErrorCallback,
  shouldAddPlaceholder = false
}: {
  editor: any
  onErrorCallback?: YamlBuilderProps['onErrorCallback']
  shouldAddPlaceholder?: boolean
}): Record<string, string | undefined> | undefined => {
  const yamlInEditor = getYAMLFromEditor(editor, shouldAddPlaceholder)
  Eif (yamlInEditor) {
    try {
      const jsonEquivalentOfYAMLInEditor = parse(yamlInEditor)
      const textInCurrentEditorLine = editor.getModel()?.getLineContent(editor.getPosition().lineNumber)
      const currentProperty = textInCurrentEditorLine?.split(':').map((item: string) => item.trim())[0]
      const parentToCurrentPropertyPath = findLeafToParentPath(jsonEquivalentOfYAMLInEditor, currentProperty)
      return { currentProperty, yamlInEditor, parentToCurrentPropertyPath }
    } catch (e) {
      onErrorCallback?.(e)
    }
  }
}
 
/**
 * Get mapping of json path of a property to all errors on the value at that property
 * @param currentYaml
 * @param validationErrors
 * @param editor
 */
const getYAMLValidationErrors = (validationErrors: Diagnostic[]): Map<number, string> => {
  const errorMap = new Map<number, string>()
  validationErrors.forEach(valError => {
    const errorIndex = valError?.range?.end?.line
    errorMap.set(errorIndex, valError?.message)
  })
  return errorMap
}
 
/**
 * Get formatted HTML of list items
 * @param errorMap yaml path to validation error map
 */
const getValidationErrorMessagesForToaster = (
  errorMap: Map<string, string[]>
): DetailedReactHTMLElement<{ id: string }, HTMLElement> => {
  const errorRenderItemList: JSX.Element[] = []
  errorMap?.forEach((values: string[], key: string) => {
    errorRenderItemList.push(
      <li key={key}>
        In{' '}
        <b>
          <i>{key}</i>
        </b>
        , {values?.map((value: string) => value.charAt(0).toLowerCase() + value.slice(1))?.join(', ')}
      </li>
    )
  })
  return React.createElement('ul', { id: 'ul-errors' }, errorRenderItemList)
}
 
const verifyYAML = (args: {
  updatedYaml: string
  setYamlValidationErrors: (yamlValidationErrors: Map<number, string> | undefined) => void
  showError: ToasterProps['showError']
  errorMessage: string
  schema?: Record<string, any>
}): void => {
  const { updatedYaml, setYamlValidationErrors, showError, schema, errorMessage } = args
  if (!schema) {
    return
  }
  if (updatedYaml) {
    try {
      validateYAMLWithSchema(updatedYaml, getSchemaWithLanguageSettings(schema))
        .then((errors: Diagnostic[]) => {
          setYamlValidationErrors(getYAMLValidationErrors(errors))
        })
        .catch((error: string) => {
          showError(error, 5000)
        })
    } catch (err) {
      showError(errorMessage)
    }
    return
  }
  setYamlValidationErrors(undefined)
}
 
export {
  getYAMLFromEditor,
  getMetaDataForKeyboardEventProcessing,
  getYAMLValidationErrors,
  getValidationErrorMessagesForToaster,
  verifyYAML
}