All files / modules/35-connectors/components/CreateConnector/GitConnector/StepAuth StepGitAuthentication.tsx

94.74% Statements 36/38
47.06% Branches 16/34
90% Functions 9/10
94.74% Lines 36/38

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              219x 219x 219x                     219x 219x   219x     219x 219x 219x 219x 219x 219x                                                 219x             219x 5x 4x                       219x 4x 3x 3x 3x   3x 2x 1x 1x 1x 1x 1x                 3x 1x     3x                           6x       6x         6x               4x                                                               219x  
/*
 * 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, { useState, useEffect } from 'react'
import cx from 'classnames'
import {
  Layout,
  Button,
  Formik,
  Text,
  FormikForm as Form,
  StepProps,
  Container,
  ButtonVariation,
  PageSpinner
} from '@wings-software/uicore'
import * as Yup from 'yup'
import { FontVariation } from '@harness/design-system'
import type { FormikProps } from 'formik'
import { setupGitFormData, GitConnectionType } from '@connectors/pages/connectors/utils/ConnectorUtils'
import type { SecretReferenceInterface } from '@secrets/utils/SecretField'
import type { ConnectorConfigDTO, ConnectorRequestBody, ConnectorInfoDTO } from 'services/cd-ng'
import SSHSecretInput from '@secrets/components/SSHSecretInput/SSHSecretInput'
import SecretInput from '@secrets/components/SecretInput/SecretInput'
import TextReference, { TextReferenceInterface, ValueType } from '@secrets/components/TextReference/TextReference'
import { useStrings } from 'framework/strings'
import css from './StepGitAuthentication.module.scss'
import commonCss from '../../commonSteps/ConnectorCommonStyles.module.scss'
 
interface StepGitAuthenticationProps extends ConnectorInfoDTO {
  name: string
  isEditMode?: boolean
}
 
interface GitAuthenticationProps {
  onConnectorCreated: (data?: ConnectorRequestBody) => void | Promise<void>
  isEditMode: boolean
  setIsEditMode: (val: boolean) => void
  setFormData?: (formData: ConnectorConfigDTO) => void
  connectorInfo: ConnectorInfoDTO | void
  accountId: string
  orgIdentifier: string
  projectIdentifier: string
}
 
interface GitFormInterface {
  connectionType: string
  username: TextReferenceInterface | void
  password: SecretReferenceInterface | void
  sshKey: SecretReferenceInterface | void
}
 
const defaultInitialFormData: GitFormInterface = {
  connectionType: GitConnectionType.HTTP,
  username: undefined,
  password: undefined,
  sshKey: undefined
}
 
const RenderGitAuthForm: React.FC<FormikProps<GitFormInterface>> = props => {
  const { getString } = useStrings()
  return (
    <>
      <TextReference
        name="username"
        stringId="username"
        type={props.values.username ? props.values.username?.type : ValueType.TEXT}
      />
      <SecretInput name="password" label={getString('password')} />
    </>
  )
}
 
const StepGitAuthentication: React.FC<StepProps<StepGitAuthenticationProps> & GitAuthenticationProps> = props => {
  const { getString } = useStrings()
  const { prevStepData, nextStep, accountId } = props
  const [initialValues, setInitialValues] = useState(defaultInitialFormData)
  const [loadingConnectorSecrets, setLoadingConnectorSecrets] = useState(true && props.isEditMode)
 
  useEffect(() => {
    if (loadingConnectorSecrets) {
      Eif (props.isEditMode) {
        Eif (props.connectorInfo) {
          setupGitFormData(props.connectorInfo, accountId).then(data => {
            setInitialValues(data as GitFormInterface)
            setLoadingConnectorSecrets(false)
          })
        } else {
          setLoadingConnectorSecrets(false)
        }
      }
    }
  }, [loadingConnectorSecrets])
 
  const handleSubmit = (formData: ConnectorConfigDTO) => {
    nextStep?.({ ...props.connectorInfo, ...prevStepData, ...formData } as StepGitAuthenticationProps)
  }
 
  return loadingConnectorSecrets ? (
    <PageSpinner />
  ) : (
    <Layout.Vertical width="60%" style={{ minHeight: 460 }} className={cx(css.secondStep, commonCss.stepContainer)}>
      <Text font={{ variation: FontVariation.H3 }}>{getString('credentials')}</Text>
 
      <Formik
        initialValues={{
          ...initialValues,
          ...prevStepData
        }}
        formName="stepGitAuthForm"
        validationSchema={Yup.object().shape({
          username: Yup.string().when('connectionType', {
            is: val => val === GitConnectionType.HTTP,
            then: Yup.string().trim().required(getString('validation.username'))
          }),
          sshKey: Yup.object().when('connectionType', {
            is: val => val === GitConnectionType.SSH,
            then: Yup.object().required(getString('validation.sshKey')),
            otherwise: Yup.object().nullable()
          }),
          password: Yup.object().when('connectionType', {
            is: val => val === GitConnectionType.HTTP,
            then: Yup.object().required(getString('validation.password')),
            otherwise: Yup.object().nullable()
          })
        })}
        onSubmit={handleSubmit}
      >
        {formikProps => (
          <Form className={cx(commonCss.fullHeight, commonCss.fullHeightDivsWithFlex)}>
            <Container className={cx(css.stepFormWrapper, commonCss.paddingTop8)}>
              {formikProps.values.connectionType === GitConnectionType.SSH ? (
                <SSHSecretInput name="sshKey" label={getString('SSH_KEY')} />
              ) : (
                <RenderGitAuthForm {...formikProps} />
              )}
            </Container>
 
            <Layout.Horizontal padding={{ top: 'small' }} spacing="medium">
              <Button
                text={getString('back')}
                icon="chevron-left"
                onClick={() => props?.previousStep?.(props?.prevStepData)}
                data-name="gitBackButton"
                variation={ButtonVariation.SECONDARY}
              />
              <Button
                type="submit"
                intent="primary"
                text={getString('continue')}
                rightIcon="chevron-right"
                variation={ButtonVariation.PRIMARY}
              />
            </Layout.Horizontal>
          </Form>
        )}
      </Formik>
    </Layout.Vertical>
  )
}
 
export default StepGitAuthentication