All files / modules/72-triggers/pages/triggers/views ConnectorSection.tsx

80.95% Statements 34/42
62.32% Branches 43/69
28.57% Functions 2/7
80.49% Lines 33/41

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              5x 5x 5x 5x   5x 5x 5x       5x 5x   5x 5x 5x 5x         5x 76x       76x 76x         76x     76x 76x                           76x             76x 76x 76x 76x 76x 76x 7x                                     69x 45x                             24x     76x                                                                           5x  
/*
 * 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 { FormInput, Text, TextInput, HarnessDocTooltip } from '@wings-software/uicore'
import { Color, FontVariation } from '@harness/design-system'
import { useParams } from 'react-router-dom'
import type { ConnectorConfigDTO } from 'services/cd-ng'
import { Connectors, connectorUrlType } from '@connectors/constants'
import { useStrings } from 'framework/strings'
import {
  ConnectorReferenceField,
  ConnectorReferenceDTO
} from '@connectors/components/ConnectorReferenceField/ConnectorReferenceField'
import { Scope } from '@common/interfaces/SecretsInterface'
import useCreateConnectorModal from '@connectors/modals/ConnectorModal/useCreateConnectorModal'
import type { GitQueryParams } from '@common/interfaces/RouteInterfaces'
import { useQueryParams } from '@common/hooks'
import { AWS_CODECOMMIT } from '../utils/TriggersWizardPageUtils'
import { GitSourceProviders } from '../utils/TriggersListUtils'
import css from './ConnectorSection.module.scss'
interface ConnectorSectionInterface {
  formikProps?: any
}
 
export const ConnectorSection: React.FC<ConnectorSectionInterface> = ({ formikProps }) => {
  const { getString } = useStrings()
  const {
    values: { sourceRepo, repoName, connectorRef },
    values
  } = formikProps
  const { accountId, projectIdentifier, orgIdentifier } = useParams<{
    projectIdentifier: string
    orgIdentifier: string
    accountId: string
  }>()
  const { repoIdentifier, branch } = useQueryParams<GitQueryParams>()
 
  // undefined scope means added from + Add
  const [liveRepoName, setLiveRepoName] = React.useState(repoName || '')
  const setSelectedConnector = (value: ConnectorReferenceDTO, scope: Scope = Scope.PROJECT): void => {
    formikProps.setValues({
      ...values,
      connectorRef: {
        label: value.name || '',
        value: `${scope !== Scope.PROJECT ? `${scope}.` : ''}${value.identifier}`,
        connector: value,
        live: value?.status?.status === 'SUCCESS'
      },
      repoName: ''
    })
    setLiveRepoName('')
  }
 
  const { openConnectorModal } = useCreateConnectorModal({
    onSuccess: (data?: ConnectorConfigDTO) => {
      if (data?.connector) {
        setSelectedConnector(data.connector)
      }
    }
  })
  const connectorUrl = connectorRef?.connector?.spec?.url
  const constructRepoUrl = `${connectorUrl}${connectorUrl?.endsWith('/') ? '' : '/'}`
  const updatedSourceRepo = sourceRepo === GitSourceProviders.AWS_CODECOMMIT.value ? AWS_CODECOMMIT : sourceRepo
  const renderRepoUrl = (): JSX.Element | null => {
    const connectorURLType = connectorRef?.connector?.spec?.type
    if (connectorURLType === connectorUrlType.REPO) {
      return (
        <>
          <Text
            font={{ variation: FontVariation.FORM_INPUT_TEXT, weight: 'semi-bold' }}
            color={Color.GREY_600}
            margin={{ bottom: 'xsmall' }}
            data-tooltip-id="repoUrl"
          >
            {getString('repositoryUrlLabel')}
          </Text>
          <HarnessDocTooltip tooltipId="repoUrl" useStandAlone={true} />
          <TextInput
            style={{ marginBottom: 'var(--spacing-xsmall)', borderColor: 'var(--bp3-intent-color, #dddddd)' }}
            value={connectorUrl}
            placeholder={getString('pipeline.repositoryUrlPlaceholder')}
            disabled
          />
        </>
      )
    } else if (connectorURLType === connectorUrlType.ACCOUNT || connectorURLType === connectorUrlType.REGION) {
      return (
        <>
          <FormInput.Text
            style={{ marginBottom: 'var(--spacing-xsmall)' }}
            label={getString('common.repositoryName')}
            placeholder={getString('pipeline.manifestType.repoNamePlaceholder')}
            name="repoName"
            onChange={(e: React.ChangeEvent<HTMLInputElement>) => setLiveRepoName(e.target.value)}
          />
          <Text data-name="" style={{ marginBottom: 'var(--spacing-medium)' }} color={Color.GREY_400}>
            {`${constructRepoUrl}${liveRepoName}`}
          </Text>
        </>
      )
    }
    return null
  }
 
  return (
    <section className={css.main}>
      <ConnectorReferenceField
        error={formikProps.errors.connectorRef}
        name="connectorRef"
        style={{ display: 'inline-block' }}
        width={324}
        type={Connectors[updatedSourceRepo?.toUpperCase()]}
        selected={formikProps.values.connectorRef}
        label={getString('connector')}
        placeholder={getString('connectors.selectConnector')}
        accountIdentifier={accountId}
        projectIdentifier={projectIdentifier}
        orgIdentifier={orgIdentifier}
        onChange={(value, scope) => {
          setSelectedConnector(value, scope)
          formikProps.validateForm()
        }}
        gitScope={{ repo: repoIdentifier || '', branch, getDefaultFromOtherRepo: true }}
      />
      <Text
        className={css.addButton}
        width={43}
        color={Color.PRIMARY_7}
        data-name="plusAdd"
        onClick={() => {
          openConnectorModal(false, Connectors[sourceRepo?.toUpperCase()], {
            gitDetails: { repoIdentifier, branch, getDefaultFromOtherRepo: true }
          }) // isEditMode, type, and connectorInfo
        }}
      >
        {getString('plusAdd')}
      </Text>
      {renderRepoUrl()}
    </section>
  )
}
 
export default ConnectorSection