All files / modules/35-connectors/components/CreateConnector/DockerConnector/StepAuth StepDockerAuthentication.tsx

97.14% Statements 34/35
76.47% Branches 26/34
100% Functions 8/8
97.14% Lines 34/35

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              219x 219x                       219x 219x   219x       219x 219x 219x 219x 219x 219x                                                   219x                 219x 8x 7x 7x 7x   7x                     7x                                     7x 5x 2x 2x 2x 2x 2x                 7x 1x     7x                             14x         14x             12x                                                                                                 1x                                     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 {
  Layout,
  Button,
  Formik,
  FormInput,
  Text,
  StepProps,
  Container,
  SelectOption,
  ButtonVariation,
  PageSpinner
} from '@wings-software/uicore'
import * as Yup from 'yup'
import { FontVariation } from '@harness/design-system'
import type { IOptionProps } from '@blueprintjs/core'
import { setupDockerFormData, DockerProviderType } from '@connectors/pages/connectors/utils/ConnectorUtils'
import type { SecretReferenceInterface } from '@secrets/utils/SecretField'
import type { ConnectorConfigDTO, ConnectorRequestBody, ConnectorInfoDTO } from 'services/cd-ng'
 
import SecretInput from '@secrets/components/SecretInput/SecretInput'
import TextReference, { TextReferenceInterface, ValueType } from '@secrets/components/TextReference/TextReference'
import { useStrings } from 'framework/strings'
import { AuthTypes } from '@connectors/pages/connectors/utils/ConnectorHelper'
import commonStyles from '@connectors/components/CreateConnector/commonSteps/ConnectorCommonStyles.module.scss'
import css from '../CreateDockerConnector.module.scss'
 
interface StepDockerAuthenticationProps extends ConnectorInfoDTO {
  name: string
  isEditMode?: boolean
}
 
interface DockerAuthenticationProps {
  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 DockerFormInterface {
  dockerRegistryUrl: string
  authType: string
  dockerProviderType: string
  username: TextReferenceInterface | void
  password: SecretReferenceInterface | void
}
 
const defaultInitialFormData: DockerFormInterface = {
  dockerRegistryUrl: '',
  authType: AuthTypes.USER_PASSWORD,
  dockerProviderType: DockerProviderType.DOCKERHUB,
  username: undefined,
  password: undefined
}
 
const StepDockerAuthentication: React.FC<StepProps<StepDockerAuthenticationProps> & DockerAuthenticationProps> =
  props => {
    const { getString } = useStrings()
    const { prevStepData, nextStep, accountId } = props
    const [initialValues, setInitialValues] = useState(defaultInitialFormData)
    const [loadingConnectorSecrets, setLoadingConnectorSecrets] = useState(true && props.isEditMode)
 
    const authOptions: SelectOption[] = [
      {
        label: getString('usernamePassword'),
        value: AuthTypes.USER_PASSWORD
      },
      {
        label: getString('anonymous'),
        value: AuthTypes.ANNONYMOUS
      }
    ]
 
    const dockerProviderTypeOptions: IOptionProps[] = [
      {
        label: getString('connectors.docker.dockerHub'),
        value: DockerProviderType.DOCKERHUB
      },
      {
        label: getString('connectors.docker.harbour'),
        value: DockerProviderType.HARBOUR
      },
      {
        label: getString('connectors.docker.quay'),
        value: DockerProviderType.QUAY
      },
      {
        label: getString('connectors.docker.other'),
        value: DockerProviderType.OTHER
      }
    ]
 
    useEffect(() => {
      if (loadingConnectorSecrets) {
        Eif (props.isEditMode) {
          Eif (props.connectorInfo) {
            setupDockerFormData(props.connectorInfo, accountId).then(data => {
              setInitialValues(data as DockerFormInterface)
              setLoadingConnectorSecrets(false)
            })
          } else {
            setLoadingConnectorSecrets(false)
          }
        }
      }
    }, [loadingConnectorSecrets])
 
    const handleSubmit = (formData: ConnectorConfigDTO) => {
      nextStep?.({ ...props.connectorInfo, ...prevStepData, ...formData } as StepDockerAuthenticationProps)
    }
 
    return loadingConnectorSecrets ? (
      <PageSpinner />
    ) : (
      <Layout.Vertical className={css.stepDetails} spacing="small">
        <Text font={{ variation: FontVariation.H3 }}>{getString('details')}</Text>
        <Formik
          initialValues={{
            ...initialValues,
            ...prevStepData
          }}
          formName="dockerAuthForm"
          validationSchema={Yup.object().shape({
            dockerRegistryUrl: Yup.string().trim().required(getString('validation.dockerRegistryUrl')),
            authType: Yup.string().trim().required(getString('validation.authType')),
            username: Yup.string().when('authType', {
              is: val => val === AuthTypes.USER_PASSWORD,
              then: Yup.string().trim().required(getString('validation.username')),
              otherwise: Yup.string().nullable()
            }),
            password: Yup.object().when('authType', {
              is: val => val === AuthTypes.USER_PASSWORD,
              then: Yup.object().required(getString('validation.password')),
              otherwise: Yup.object().nullable()
            })
          })}
          onSubmit={handleSubmit}
        >
          {formikProps => (
            <>
              <Layout.Vertical padding={{ top: 'large', bottom: 'large' }} className={css.secondStep} width={'64%'}>
                <FormInput.Text
                  name="dockerRegistryUrl"
                  placeholder={getString('UrlLabel')}
                  label={getString('connectors.docker.dockerRegistryURL')}
                />
                <Text tooltipProps={{ dataTooltipId: 'dockerConnectorProviderType' }}>
                  {getString('connectors.docker.dockerProvideType')}
                </Text>
                <FormInput.RadioGroup
                  className={css.dockerProviderType}
                  inline
                  name="dockerProviderType"
                  radioGroup={{ inline: true }}
                  items={dockerProviderTypeOptions}
                  disabled={false}
                ></FormInput.RadioGroup>
                <Container className={css.authHeaderRow}>
                  <Text
                    font={{ variation: FontVariation.H6 }}
                    inline
                    tooltipProps={{ dataTooltipId: 'dockerConnectorAuthentication' }}
                  >
                    {getString('authentication')}
                  </Text>
                  <FormInput.Select
                    name="authType"
                    items={authOptions}
                    disabled={false}
                    className={commonStyles.authTypeSelectLarge}
                  />
                </Container>
                {formikProps.values.authType === AuthTypes.USER_PASSWORD ? (
                  <>
                    <TextReference
                      name="username"
                      stringId="username"
                      type={formikProps.values.username ? formikProps.values.username?.type : ValueType.TEXT}
                    />
                    <SecretInput name={'password'} label={getString('password')} />
                  </>
                ) : null}
              </Layout.Vertical>
              <Layout.Horizontal padding={{ top: 'small' }} spacing="medium">
                <Button
                  text={getString('back')}
                  icon="chevron-left"
                  onClick={() => props?.previousStep?.(props?.prevStepData)}
                  data-name="dockerBackButton"
                  variation={ButtonVariation.SECONDARY}
                />
                <Button
                  type="submit"
                  variation={ButtonVariation.PRIMARY}
                  onClick={formikProps.submitForm}
                  text={getString('continue')}
                  rightIcon="chevron-right"
                />
              </Layout.Horizontal>
            </>
          )}
        </Formik>
      </Layout.Vertical>
    )
  }
 
export default StepDockerAuthentication