All files / modules/75-cd/components/PipelineSteps/ShellScriptStep OptionalConfiguration.tsx

83.87% Statements 26/31
58.33% Branches 14/24
58.33% Functions 7/12
83.87% Lines 26/31

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 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296              97x 97x 97x                   97x 97x     97x 97x 97x 97x 97x   97x               97x 97x   97x                     97x         39x 39x 39x 39x   39x                             39x               37x                                                                             2x                                             39x               50x                                                                 4x                                                                                                                                                                                                                        
/*
 * Copyright 2022 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 { FormikProps, FieldArray } from 'formik'
import {
  Button,
  ButtonVariation,
  FormikForm,
  FormInput,
  getMultiTypeFromValue,
  Layout,
  MultiTypeInputType,
  Text
} from '@wings-software/uicore'
import { v4 as uuid } from 'uuid'
import cx from 'classnames'
import type { IOptionProps } from '@blueprintjs/core'
 
import { useStrings } from 'framework/strings'
import { useVariablesExpression } from '@pipeline/components/PipelineStudio/PiplineHooks/useVariablesExpression'
import MultiTypeFieldSelector from '@common/components/MultiTypeFieldSelector/MultiTypeFieldSelector'
import { ConfigureOptions } from '@common/components/ConfigureOptions/ConfigureOptions'
import MultiTypeSecretInput from '@secrets/components/MutiTypeSecretInput/MultiTypeSecretInput'
 
import {
  scriptInputType,
  scriptOutputType,
  ShellScriptFormData,
  ShellScriptOutputStepVariable,
  ShellScriptStepVariable
} from './shellScriptTypes'
 
import stepCss from '@pipeline/components/PipelineSteps/Steps/Steps.module.scss'
import css from './ShellScript.module.scss'
 
export const targetTypeOptions: IOptionProps[] = [
  {
    label: 'Specify Target Host',
    value: 'targethost'
  },
  {
    label: 'On Delegate',
    value: 'delegate'
  }
]
 
export default function OptionalConfiguration(props: {
  formik: FormikProps<ShellScriptFormData>
  readonly?: boolean
  allowableTypes: MultiTypeInputType[]
}): React.ReactElement {
  const { formik, readonly, allowableTypes } = props
  const { values: formValues, setFieldValue } = formik
  const { getString } = useStrings()
  const { expressions } = useVariablesExpression()
 
  return (
    <FormikForm>
      <div className={stepCss.stepPanel}>
        <div className={stepCss.formGroup}>
          <MultiTypeFieldSelector
            name="spec.environmentVariables"
            label={getString('pipeline.scriptInputVariables')}
            isOptional
            optionalLabel={getString('common.optionalLabel')}
            defaultValueToReset={[]}
            disableTypeSelection
          >
            <FieldArray
              name="spec.environmentVariables"
              render={({ push, remove }) => {
                return (
                  <div className={css.panel}>
                    <div className={css.environmentVarHeader}>
                      <span className={css.label}>Name</span>
                      <span className={css.label}>Type</span>
                      <span className={css.label}>Value</span>
                    </div>
                    {formValues.spec.environmentVariables?.map(({ id }: ShellScriptStepVariable, i: number) => {
                      return (
                        <div className={css.environmentVarHeader} key={id}>
                          <FormInput.Text
                            name={`spec.environmentVariables[${i}].name`}
                            placeholder={getString('name')}
                            disabled={readonly}
                          />
                          <FormInput.Select
                            items={scriptInputType}
                            name={`spec.environmentVariables[${i}].type`}
                            placeholder={getString('typeLabel')}
                            disabled={readonly}
                          />
                          <FormInput.MultiTextInput
                            name={`spec.environmentVariables[${i}].value`}
                            placeholder={getString('valueLabel')}
                            multiTextInputProps={{
                              allowableTypes,
                              expressions,
                              disabled: readonly
                            }}
                            label=""
                            disabled={readonly}
                          />
                          <Button
                            variation={ButtonVariation.ICON}
                            icon="main-trash"
                            data-testid={`remove-environmentVar-${i}`}
                            onClick={() => remove(i)}
                            disabled={readonly}
                          />
                        </div>
                      )
                    })}
                    <Button
                      icon="plus"
                      variation={ButtonVariation.LINK}
                      data-testid="add-environmentVar"
                      disabled={readonly}
                      onClick={() => push({ name: '', type: 'String', value: '', id: uuid() })}
                      className={css.addButton}
                    >
                      {getString('addInputVar')}
                    </Button>
                  </div>
                )
              }}
            />
          </MultiTypeFieldSelector>
        </div>
        <div className={stepCss.formGroup}>
          <MultiTypeFieldSelector
            name="spec.outputVariables"
            label={getString('pipeline.scriptOutputVariables')}
            isOptional
            optionalLabel={getString('common.optionalLabel')}
            defaultValueToReset={[]}
            disableTypeSelection
          >
            <FieldArray
              name="spec.outputVariables"
              render={({ push, remove }) => {
                return (
                  <div className={css.panel}>
                    <div className={css.outputVarHeader}>
                      <span className={css.label}>Name</span>
                      <span className={css.label}>Type</span>
                      <span className={css.label}>Value</span>
                    </div>
                    {formValues.spec.outputVariables?.map(({ id }: ShellScriptOutputStepVariable, i: number) => {
                      return (
                        <div className={css.outputVarHeader} key={id}>
                          <FormInput.Text
                            name={`spec.outputVariables[${i}].name`}
                            placeholder={getString('name')}
                            disabled={readonly}
                          />
                          <FormInput.Select
                            items={scriptOutputType}
                            name={`spec.outputVariables[${i}].type`}
                            placeholder={getString('typeLabel')}
                            disabled={readonly}
                          />
 
                          <FormInput.MultiTextInput
                            name={`spec.outputVariables[${i}].value`}
                            placeholder={getString('valueLabel')}
                            multiTextInputProps={{
                              allowableTypes,
                              expressions,
                              disabled: readonly
                            }}
                            label=""
                            disabled={readonly}
                          />
 
                          <Button minimal icon="main-trash" onClick={() => remove(i)} disabled={readonly} />
                        </div>
                      )
                    })}
                    <Button
                      icon="plus"
                      variation={ButtonVariation.LINK}
                      onClick={() => push({ name: '', type: 'String', value: '', id: uuid() })}
                      disabled={readonly}
                      className={css.addButton}
                    >
                      {getString('addOutputVar')}
                    </Button>
                  </div>
                )
              }}
            />
          </MultiTypeFieldSelector>
        </div>
        <div className={stepCss.formGroup}>
          <FormInput.RadioGroup
            name="spec.onDelegate"
            label={getString('pipeline.executionTarget')}
            isOptional
            optionalLabel={getString('common.optionalLabel')}
            radioGroup={{ inline: true }}
            items={targetTypeOptions}
            className={css.radioGroup}
            disabled={readonly}
          />
        </div>
        {formValues.spec.onDelegate === 'targethost' ? (
          <div>
            <div className={cx(stepCss.formGroup, stepCss.md)}>
              <FormInput.MultiTextInput
                name="spec.executionTarget.host"
                placeholder={getString('cd.specifyTargetHost')}
                label={getString('targetHost')}
                style={{ marginTop: 'var(--spacing-small)' }}
                multiTextInputProps={{ expressions, disabled: readonly, allowableTypes }}
                disabled={readonly}
              />
              {getMultiTypeFromValue(formValues.spec.executionTarget.host) === MultiTypeInputType.RUNTIME && (
                <ConfigureOptions
                  value={formValues.spec.executionTarget.host}
                  type="String"
                  variableName="spec.executionTarget.host"
                  showRequiredField={false}
                  showDefaultField={false}
                  showAdvanced={true}
                  onChange={value => setFieldValue('spec.executionTarget.host', value)}
                  style={{ marginTop: 12 }}
                  isReadonly={readonly}
                />
              )}
            </div>
            <div className={cx(stepCss.formGroup, stepCss.md)}>
              <MultiTypeSecretInput
                type="SSHKey"
                name="spec.executionTarget.connectorRef"
                label={getString('sshConnector')}
                expressions={expressions}
                allowableTypes={allowableTypes}
                disabled={readonly}
              />
              {getMultiTypeFromValue(formValues?.spec.executionTarget.connectorRef) === MultiTypeInputType.RUNTIME && (
                <ConfigureOptions
                  value={formValues?.spec.executionTarget.connectorRef as string}
                  type={
                    <Layout.Horizontal spacing="medium" style={{ alignItems: 'center' }}>
                      <Text>{getString('pipelineSteps.connectorLabel')}</Text>
                    </Layout.Horizontal>
                  }
                  variableName="spec.executionTarget.connectorRef"
                  showRequiredField={false}
                  showDefaultField={false}
                  showAdvanced={true}
                  onChange={value => {
                    setFieldValue('spec.executionTarget.connectorRef', value)
                  }}
                  style={{ marginTop: 4 }}
                  isReadonly={readonly}
                />
              )}
            </div>
            <div className={cx(stepCss.formGroup, stepCss.md)}>
              <FormInput.MultiTextInput
                name="spec.executionTarget.workingDirectory"
                placeholder={getString('cd.enterWorkDirectory')}
                label={getString('workingDirectory')}
                style={{ marginTop: 'var(--spacing-medium)' }}
                disabled={readonly}
                multiTextInputProps={{ expressions, disabled: readonly, allowableTypes }}
              />
              {getMultiTypeFromValue(formValues.spec.executionTarget.workingDirectory) ===
                MultiTypeInputType.RUNTIME && (
                <ConfigureOptions
                  value={formValues.spec.executionTarget.workingDirectory}
                  type="String"
                  variableName="spec.executionTarget.workingDirectory"
                  showRequiredField={false}
                  showDefaultField={false}
                  showAdvanced={true}
                  onChange={value => setFieldValue('spec.executionTarget.workingDirectory', value)}
                  style={{ marginTop: 12 }}
                  isReadonly={readonly}
                />
              )}
            </div>
          </div>
        ) : null}
      </div>
    </FormikForm>
  )
}