All files / modules/70-pipeline/components/PipelineSteps/Steps/CustomVariables CustomVariables.tsx

92.06% Statements 58/63
49.54% Branches 108/218
80% Functions 12/15
92.06% Lines 58/63

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 194 195 196 197 198 199 200 201              113x 113x 113x 113x 113x   113x 113x 113x 113x 113x   113x   113x 113x 113x       113x   113x 1x               113x 1x               113x 113x       247x   247x 153x                       94x 33x                         61x     1x                             61x 61x 61x 52x   52x           39x       61x 22x   61x                 1x 1x       1x         1x 1x 1x 1x                 1x 1x             1x                   118x 118x     118x 118x 118x 118x 118x 118x       118x     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 { IconName, getMultiTypeFromValue, MultiTypeInputType } from '@wings-software/uicore'
import { get, set, isEmpty, isNil } from 'lodash-es'
import { parse } from 'yaml'
import { CompletionItemKind } from 'vscode-languageserver-types'
import type { FormikErrors } from 'formik'
import { loggerFor } from 'framework/logging/logging'
import { ModuleName } from 'framework/types/ModuleName'
import { listSecretsV2Promise, SecretResponseWrapper } from 'services/cd-ng'
import { StepViewType, ValidateInputSetProps, Step } from '@pipeline/components/AbstractSteps/Step'
import { StepType } from '@pipeline/components/PipelineSteps/PipelineStepInterface'
import type { CompletionItemInterface } from '@common/interfaces/YAMLBuilderProps'
import { Scope } from '@common/interfaces/SecretsInterface'
import type { AllNGVariables } from '@pipeline/utils/types'
import { CustomVariableEditable, CustomVariableEditableExtraProps } from './CustomVariableEditable'
import { CustomVariableInputSet, CustomVariableInputSetExtraProps } from './CustomVariableInputSet'
import { CustomVariablesEditableStage } from './CustomVariablesEditableStage'
import type { CustomVariablesData } from './CustomVariableEditable'
import type { StepProps } from '../../PipelineStep'
 
const logger = loggerFor(ModuleName.COMMON)
 
const getConnectorValue = (connector?: SecretResponseWrapper): string =>
  `${
    connector?.secret?.orgIdentifier && connector?.secret?.projectIdentifier
      ? connector?.secret?.identifier
      : connector?.secret?.orgIdentifier
      ? `${Scope.ORG}.${connector?.secret?.identifier}`
      : `${Scope.ACCOUNT}.${connector?.secret?.identifier}`
  }` || ''
 
const getConnectorName = (connector?: SecretResponseWrapper): string =>
  `${
    connector?.secret?.orgIdentifier && connector?.secret?.projectIdentifier
      ? `${connector?.secret?.type}: ${connector?.secret?.name}`
      : connector?.secret?.orgIdentifier
      ? `${connector?.secret?.type}[Org]: ${connector?.secret?.name}`
      : `${connector?.secret?.type}[Account]: ${connector?.secret?.name}`
  }` || ''
 
const SecretRefRegex = /^.+variables\.\d+\.value$/
export class CustomVariables extends Step<CustomVariablesData> {
  renderStep(
    props: StepProps<CustomVariablesData, CustomVariableEditableExtraProps | CustomVariableInputSetExtraProps>
  ): JSX.Element {
    const { initialValues, onUpdate, stepViewType, customStepProps, inputSetData, readonly, allowableTypes } = props
 
    if (stepViewType === StepViewType.InputSet || stepViewType === StepViewType.DeploymentForm) {
      return (
        <CustomVariableInputSet
          initialValues={initialValues}
          onUpdate={data => onUpdate?.(this.processFormData(data))}
          stepViewType={stepViewType}
          {...customStepProps}
          inputSetData={inputSetData}
          allowableTypes={allowableTypes}
        />
      )
    }
 
    if (stepViewType === StepViewType.StageVariable) {
      return (
        <CustomVariablesEditableStage
          initialValues={initialValues}
          onUpdate={data => onUpdate?.(this.processFormData(data))}
          stepViewType={stepViewType}
          enableValidation
          readonly={readonly}
          allowableTypes={allowableTypes}
          {...customStepProps}
        />
      )
    }
 
    return (
      <CustomVariableEditable
        initialValues={initialValues}
        onUpdate={data => onUpdate?.(this.processFormData(data))}
        stepViewType={stepViewType}
        readonly={readonly}
        allowableTypes={allowableTypes}
        {...customStepProps}
      />
    )
  }
 
  validateInputSet({
    data,
    template,
    getString,
    viewType
  }: ValidateInputSetProps<CustomVariablesData>): FormikErrors<CustomVariablesData> {
    const errors: FormikErrors<CustomVariablesData> = { variables: [] }
    const isRequired = viewType === StepViewType.DeploymentForm || viewType === StepViewType.TriggerForm
    data?.variables?.forEach((variable: AllNGVariables, index: number) => {
      const currentVariableTemplate = get(template, `variables[${index}].value`, '')
 
      if (
        isRequired &&
        ((isEmpty(variable.value) && variable.type !== 'Number') ||
          (variable.type === 'Number' && (typeof variable.value !== 'number' || isNaN(variable.value)))) &&
        getMultiTypeFromValue(currentVariableTemplate) === MultiTypeInputType.RUNTIME
      ) {
        set(errors, `variables[${index}].value`, getString?.('fieldRequired', { field: 'Value' }))
      }
    })
 
    if (!errors?.variables?.length) {
      delete errors.variables
    }
    return errors
  }
 
  async getSecretsListForYaml(
    path: string,
    yaml: string,
    params: Record<string, unknown>
  ): Promise<CompletionItemInterface[]> {
    let pipelineObj
    try {
      pipelineObj = parse(yaml)
    } catch (err) {
      logger.error('Error while parsing the yaml', err)
    }
    const { accountId } = params as {
      accountId: string
      orgIdentifier: string
      projectIdentifier: string
    }
    Eif (pipelineObj) {
      const obj = get(pipelineObj, path.replace('.value', ''))
      Eif (obj?.type === 'Secret') {
        const listOfSecrets = await listSecretsV2Promise({
          queryParams: {
            accountIdentifier: accountId,
            includeSecretsFromEverySubScope: true,
            types: ['SecretText', 'SecretFile'],
            pageIndex: 0,
            pageSize: 100
          }
        }).then(response =>
          response?.data?.content?.map(connector => {
            return {
              label: getConnectorName(connector),
              insertText: getConnectorValue(connector),
              kind: CompletionItemKind.Field
            }
          })
        )
        return listOfSecrets || []
      }
    }
 
    return new Promise(resolve => {
      resolve([])
    })
  }
 
  constructor() {
    super()
    this.invocationMap.set(SecretRefRegex, this.getSecretsListForYaml.bind(this))
  }
 
  protected type = StepType.CustomVariable
  protected stepName = 'Custom Variables'
  protected stepIcon: IconName = 'variable'
  protected stepPaletteVisible = false
  protected _hasStepVariables = true
  protected invocationMap: Map<
    RegExp,
    (path: string, yaml: string, params: Record<string, unknown>) => Promise<CompletionItemInterface[]>
  > = new Map()
  protected defaultValues: CustomVariablesData = { variables: [] }
 
  processFormData(data: CustomVariablesData): CustomVariablesData {
    return {
      ...data,
      variables: data.variables.map(row => ({
        name: row.name,
        type: row.type,
        ...(!isNil(row.default)
          ? { default: row.type === 'Number' ? parseFloat(row.default as unknown as string) : row.default }
          : {}),
        value:
          row.type === 'Number' &&
          getMultiTypeFromValue(row.value as unknown as string) === MultiTypeInputType.FIXED &&
          row.value
            ? parseFloat(row.value as unknown as string)
            : row.value
      })) as AllNGVariables[]
    }
  }
}