All files / modules/75-ci/components/PipelineSteps/StepCommonFields StepCommonFieldsInputSet.tsx

100% Statements 28/28
84% Branches 63/75
100% Functions 2/2
100% Lines 28/28

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              117x 117x 117x 117x 117x 117x 117x   117x 117x 117x 117x         117x 117x 117x 117x 117x                           80x 80x 80x 80x   80x 80x 80x   80x   80x                     70x                                       80x                                                                                                                                                                                                                                                                                                                                         117x  
/*
 * 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 { Text, getMultiTypeFromValue, MultiTypeInputType, Container, Layout } from '@wings-software/uicore'
import { Color } from '@harness/design-system'
import cx from 'classnames'
import { isEmpty } from 'lodash-es'
import { connect } from 'formik'
import { useStrings } from 'framework/strings'
import type { StringsMap } from 'stringTypes'
import { MultiTypeSelectField } from '@common/components/MultiTypeSelect/MultiTypeSelect'
import { MultiTypeTextField, MultiTypeTextProps } from '@common/components/MultiTypeText/MultiTypeText'
import { FormMultiTypeDurationField } from '@common/components/MultiTypeDuration/MultiTypeDuration'
import {
  GetShellOptions,
  GetImagePullPolicyOptions
} from '@ci/components/PipelineSteps/StepCommonFields/StepCommonFields'
import type { InputSetData } from '@pipeline/components/AbstractSteps/Step'
import { StepViewType } from '@pipeline/components/AbstractSteps/Step'
import { useVariablesExpression } from '@pipeline/components/PipelineStudio/PiplineHooks/useVariablesExpression'
import { getOptionalSubLabel } from '@ci/components/PipelineSteps/CIStep/CIStepOptionalConfig'
import { AllMultiTypeInputTypesForInputSet } from '../CIStep/StepUtils'
import css from '@pipeline/components/PipelineSteps/Steps/Steps.module.scss'
 
interface StepCommonFieldsInputSetProps<T> extends Omit<InputSetData<T>, 'path' | 'template'> {
  path?: string
  // @TODO: set up proper typing
  // eslint-disable-next-line
  template: any
  withoutTimeout?: boolean
  enableFields?: string[]
  stepViewType: StepViewType
  allowableTypes?: MultiTypeInputType[]
}
 
function StepCommonFieldsInputSet<T>(props: StepCommonFieldsInputSetProps<T>): JSX.Element | null {
  const { path, template, readonly, withoutTimeout, enableFields = [], stepViewType } = props
  const { getString } = useStrings()
  const { expressions } = useVariablesExpression()
  const isRunAsUserRuntime = getMultiTypeFromValue(template?.spec?.runAsUser) === MultiTypeInputType.RUNTIME
  const isLimitMemoryRuntime =
    getMultiTypeFromValue(template?.spec?.resources?.limits?.memory) === MultiTypeInputType.RUNTIME
  const isLimitCPURuntime = getMultiTypeFromValue(template?.spec?.resources?.limits?.cpu) === MultiTypeInputType.RUNTIME
  const isTimeoutRuntime = getMultiTypeFromValue(template?.timeout) === MultiTypeInputType.RUNTIME
 
  const stepCss = stepViewType === StepViewType.DeploymentForm ? css.sm : css.lg
 
  const renderMultiTypeTextField = ({
    name,
    tooltipId,
    labelKey,
    inputProps
  }: {
    name: string
    tooltipId: string
    labelKey: keyof StringsMap
    inputProps: MultiTypeTextProps['multiTextInputProps']
  }) => (
    <MultiTypeTextField
      name={name}
      label={
        <Layout.Horizontal flex={{ justifyContent: 'flex-start', alignItems: 'baseline' }}>
          <Text
            style={{ display: 'flex', alignItems: 'center' }}
            className={css.inpLabel}
            color={Color.GREY_800}
            font={{ size: 'small', weight: 'semi-bold' }}
          >
            {getString(labelKey)}
          </Text>
          &nbsp;
          {getOptionalSubLabel(getString, tooltipId)}
        </Layout.Horizontal>
      }
      multiTextInputProps={inputProps}
    />
  )
 
  return (
    <>
      {/* Currently not implemented due to no support for enum value fields */}
      {/* When ready, pass enableFields when <x>StepInputSet has field as runtime input */}
      {enableFields.includes('spec.imagePullPolicy') && (
        <Container className={cx(css.formGroup, stepCss, css.bottomMargin5)}>
          <MultiTypeSelectField
            name={`${isEmpty(path) ? '' : `${path}.`}spec.imagePullPolicy`}
            label={
              <Layout.Horizontal flex={{ justifyContent: 'flex-start', alignItems: 'baseline' }}>
                <Text className={css.inpLabel} color={Color.GREY_600} font={{ size: 'small', weight: 'semi-bold' }}>
                  {getString('pipelineSteps.pullLabel')}
                </Text>
                &nbsp;
                {getOptionalSubLabel(getString, 'imagePullPolicy')}
              </Layout.Horizontal>
            }
            multiTypeInputProps={{
              selectItems: GetImagePullPolicyOptions(),
              placeholder: getString('select'),
              multiTypeInputProps: {
                expressions,
                selectProps: { addClearBtn: true, items: GetImagePullPolicyOptions() },
                allowableTypes: AllMultiTypeInputTypesForInputSet
              },
              disabled: readonly
            }}
            disabled={readonly}
            configureOptionsProps={{ variableName: 'spec.imagePullPolicy' }}
          />
        </Container>
      )}
      {/* Currently not implemented due to no support for enum value fields */}
      {enableFields.includes('spec.shell') && (
        <Container className={cx(css.formGroup, stepCss, css.bottomMargin5)}>
          <MultiTypeSelectField
            name={`${isEmpty(path) ? '' : `${path}.`}spec.shell`}
            label={
              <Layout.Horizontal flex={{ justifyContent: 'flex-start', alignItems: 'baseline' }}>
                <Text className={css.inpLabel} color={Color.GREY_600} font={{ size: 'small', weight: 'semi-bold' }}>
                  {getString('common.shell')}
                </Text>
                &nbsp;
                {getOptionalSubLabel(getString, 'shell')}
              </Layout.Horizontal>
            }
            multiTypeInputProps={{
              selectItems: GetImagePullPolicyOptions(),
              placeholder: getString('select'),
              multiTypeInputProps: {
                expressions,
                selectProps: { addClearBtn: true, items: GetShellOptions(getString) },
                allowableTypes: AllMultiTypeInputTypesForInputSet
              },
              disabled: readonly
            }}
            disabled={readonly}
            configureOptionsProps={{ variableName: 'spec.shell' }}
          />
        </Container>
      )}
      {isRunAsUserRuntime && (
        <Container className={cx(css.formGroup, stepCss, css.topSpacingLarge, css.bottomMargin5)}>
          <MultiTypeTextField
            label={
              <Layout.Horizontal flex={{ justifyContent: 'flex-start', alignItems: 'baseline' }}>
                <Text className={css.inpLabel} color={Color.GREY_600} font={{ size: 'small', weight: 'semi-bold' }}>
                  {getString('pipeline.stepCommonFields.runAsUser')}
                </Text>
                &nbsp;
                {getOptionalSubLabel(getString, 'runAsUser')}
              </Layout.Horizontal>
            }
            name={`${isEmpty(path) ? '' : `${path}.`}spec.runAsUser`}
            multiTextInputProps={{
              multiTextInputProps: {
                expressions,
                allowableTypes: AllMultiTypeInputTypesForInputSet
              },
              disabled: readonly,
              placeholder: '1000'
            }}
          />
        </Container>
      )}
      {(isLimitMemoryRuntime || isLimitCPURuntime) && (
        <>
          <Container className={css.bottomMargin5}>
            <Text
              className={css.inpLabel}
              color={Color.GREY_600}
              font={{ size: 'small', weight: 'semi-bold' }}
              tooltipProps={{ dataTooltipId: 'setContainerResources' }}
            >
              {getString('pipelineSteps.setContainerResources')}
            </Text>
 
            <Layout.Horizontal
              className={cx(css.formGroup, css.lgOverride)}
              style={{ marginTop: 'small', marginBottom: 'small' }}
              spacing="medium"
            >
              {isLimitMemoryRuntime && (
                <Container style={{ flex: 1 }}>
                  {renderMultiTypeTextField({
                    name: `${isEmpty(path) ? '' : `${path}.`}spec.resources.limits.memory`,
                    tooltipId: 'limitMemory',
                    labelKey: 'pipelineSteps.limitMemoryLabel',
                    inputProps: {
                      multiTextInputProps: {
                        expressions,
                        allowableTypes: AllMultiTypeInputTypesForInputSet
                      },
                      disabled: readonly
                    }
                  })}
                </Container>
              )}
              {isLimitCPURuntime && (
                <Container style={{ flex: 1 }}>
                  {renderMultiTypeTextField({
                    name: `${isEmpty(path) ? '' : `${path}.`}spec.resources.limits.cpu`,
                    tooltipId: 'limitCPULabel',
                    labelKey: 'pipelineSteps.limitCPULabel',
                    inputProps: {
                      multiTextInputProps: {
                        expressions,
                        allowableTypes: AllMultiTypeInputTypesForInputSet
                      },
                      disabled: readonly
                    }
                  })}
                </Container>
              )}
            </Layout.Horizontal>
          </Container>
        </>
      )}
      {!withoutTimeout && isTimeoutRuntime && (
        <Container className={cx(css.formGroup, css.sm, css.bottomMargin5)}>
          <FormMultiTypeDurationField
            className={css.removeBpLabelMargin}
            label={
              <Layout.Horizontal flex={{ justifyContent: 'flex-start', alignItems: 'baseline' }}>
                <Text className={css.inpLabel} color={Color.GREY_600} font={{ size: 'small', weight: 'semi-bold' }}>
                  {getString('pipelineSteps.timeoutLabel')}
                </Text>
                &nbsp;
                {getOptionalSubLabel(getString, 'timeout')}
              </Layout.Horizontal>
            }
            name={`${isEmpty(path) ? '' : `${path}.`}timeout`}
            placeholder={getString('pipelineSteps.timeoutPlaceholder')}
            multiTypeDurationProps={{
              expressions,
              allowableTypes: AllMultiTypeInputTypesForInputSet
            }}
            disabled={readonly}
          />
        </Container>
      )}
    </>
  )
}
 
export default connect(StepCommonFieldsInputSet)