All files / modules/70-pipeline/components/ArtifactsSelection/ArtifactRepository ArtifactConnector.tsx

89.47% Statements 34/38
61.22% Branches 30/49
50% Functions 4/8
89.47% Lines 34/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 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              101x 101x                       101x 101x 101x 101x 101x 101x 101x       101x 101x 101x 101x 101x     101x                     101x                       2x   2x 2x 2x 2x 2x   2x   2x             2x           2x     2x 2x 2x   2x     2x                         2x                                                                                                         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, { useCallback } from 'react'
import {
  Button,
  ButtonVariation,
  Text,
  Formik,
  FormikForm,
  getMultiTypeFromValue,
  Layout,
  MultiTypeInputType,
  StepProps,
  ButtonSize
} from '@wings-software/uicore'
import * as Yup from 'yup'
import { FontVariation } from '@harness/design-system'
import { useParams } from 'react-router-dom'
import { set } from 'lodash-es'
import { ConfigureOptions } from '@common/components/ConfigureOptions/ConfigureOptions'
import { FormMultiTypeConnectorField } from '@connectors/components/ConnectorReferenceField/FormMultiTypeConnectorField'
import { useStrings } from 'framework/strings'
import type { ConnectorConfigDTO } from 'services/cd-ng'
import type { GitQueryParams, ProjectPathProps } from '@common/interfaces/RouteInterfaces'
import type { ConnectorSelectedValue } from '@connectors/components/ConnectorReferenceField/ConnectorReferenceField'
import { usePermission } from '@rbac/hooks/usePermission'
import { ResourceType } from '@rbac/interfaces/ResourceType'
import { PermissionIdentifier } from '@rbac/interfaces/PermissionIdentifier'
import { useQueryParams } from '@common/hooks'
import { ArtifactConnectorLabelMap, ArtifactToConnectorMap } from '../ArtifactHelper'
import type { ArtifactType, InitialArtifactDataType } from '../ArtifactInterface'
 
import css from './ArtifactConnector.module.scss'
interface ArtifactConnectorProps {
  handleViewChange: () => void
  expressions: string[]
  stepName: string
  isReadonly: boolean
  initialValues: InitialArtifactDataType
  selectedArtifact: ArtifactType | null
  allowableTypes: MultiTypeInputType[]
}
 
export function ArtifactConnector(props: StepProps<ConnectorConfigDTO> & ArtifactConnectorProps): React.ReactElement {
  const {
    previousStep,
    prevStepData,
    nextStep,
    initialValues,
    stepName,
    expressions,
    selectedArtifact,
    isReadonly,
    handleViewChange,
    allowableTypes
  } = props
 
  const { accountId, projectIdentifier, orgIdentifier } = useParams<ProjectPathProps>()
  const { repoIdentifier, branch } = useQueryParams<GitQueryParams>()
  const { getString } = useStrings()
  const connectorType = ArtifactToConnectorMap[selectedArtifact as ArtifactType]
  const selectedConnectorLabel = ArtifactConnectorLabelMap[selectedArtifact as ArtifactType]
 
  const newConnectorLabel = `${getString('newLabel')} ${selectedConnectorLabel} ${getString('connector')}`
 
  const [canCreate] = usePermission({
    resource: {
      resourceType: ResourceType.CONNECTOR
    },
    permissions: [PermissionIdentifier.UPDATE_CONNECTOR]
  })
 
  const primarySchema = Yup.object().shape({
    connectorId: Yup.string()
      .trim()
      .required(`${connectorType} ${getString('pipelineSteps.build.create.connectorRequiredError')}`)
  })
 
  const submitFirstStep = async (formData: any): Promise<void> => {
    nextStep?.({ ...formData })
  }
  const getInitialValues = useCallback((): InitialArtifactDataType => {
    Eif (prevStepData?.connectorId !== undefined) {
      set(initialValues, 'connectorId', prevStepData?.connectorId)
    }
    return initialValues
  }, [initialValues, prevStepData?.connectorId])
 
  return (
    <Layout.Vertical spacing="xxlarge" className={css.firstep}>
      <Text font={{ variation: FontVariation.H3 }} margin={{ bottom: 'medium' }}>
        {stepName}
      </Text>
      <Formik
        initialValues={getInitialValues()}
        validationSchema={primarySchema}
        formName="artifactConnForm"
        onSubmit={formData => {
          submitFirstStep(formData)
        }}
      >
        {formik => (
          <FormikForm>
            <div className={css.connectorForm}>
              <Layout.Horizontal
                spacing={'medium'}
                flex={{ alignItems: 'flex-start', justifyContent: 'flex-start' }}
                className={css.connectorContainer}
              >
                <FormMultiTypeConnectorField
                  name="connectorId"
                  label={`${selectedConnectorLabel} ${getString('connector')}`}
                  placeholder={`${getString('select')} ${selectedConnectorLabel} ${getString('connector')}`}
                  accountIdentifier={accountId}
                  projectIdentifier={projectIdentifier}
                  orgIdentifier={orgIdentifier}
                  width={400}
                  multiTypeProps={{ expressions, allowableTypes }}
                  isNewConnectorLabelVisible={
                    !(
                      getMultiTypeFromValue(formik.values.connectorId) === MultiTypeInputType.RUNTIME &&
                      (isReadonly || !canCreate)
                    )
                  }
                  createNewLabel={newConnectorLabel}
                  type={connectorType}
                  enableConfigureOptions={false}
                  selected={formik?.values?.connectorId}
                  gitScope={{ repo: repoIdentifier || '', branch, getDefaultFromOtherRepo: true }}
                />
                {getMultiTypeFromValue(formik.values.connectorId) === MultiTypeInputType.RUNTIME ? (
                  <ConfigureOptions
                    className={css.configureOptions}
                    value={formik.values.connectorId as unknown as string}
                    type={connectorType}
                    variableName="connectorRef"
                    showRequiredField={false}
                    showDefaultField={false}
                    showAdvanced={true}
                    onChange={value => {
                      formik.setFieldValue('connectorId', value)
                    }}
                    isReadonly={isReadonly}
                  />
                ) : (
                  <Button
                    variation={ButtonVariation.LINK}
                    size={ButtonSize.SMALL}
                    id="new-artifact-connector"
                    text={newConnectorLabel}
                    icon="plus"
                    iconProps={{ size: 12 }}
                    disabled={isReadonly || !canCreate}
                    onClick={() => {
                      handleViewChange()
                      nextStep?.()
                    }}
                    className={css.addNewArtifact}
                  />
                )}
              </Layout.Horizontal>
            </div>
            <Layout.Horizontal spacing="medium">
              <Button
                variation={ButtonVariation.SECONDARY}
                text={getString('back')}
                icon="chevron-left"
                onClick={() => previousStep?.(prevStepData)}
              />
              <Button
                variation={ButtonVariation.PRIMARY}
                type="submit"
                text={getString('continue')}
                rightIcon="chevron-right"
                disabled={
                  getMultiTypeFromValue(formik.values.connectorId) === MultiTypeInputType.FIXED &&
                  !(formik.values.connectorId as ConnectorSelectedValue)?.connector
                }
              />
            </Layout.Horizontal>
          </FormikForm>
        )}
      </Formik>
    </Layout.Vertical>
  )
}