All files / modules/40-gitsync/components/gitSyncRepoForm RepoTestConnection.tsx

66.67% Statements 20/30
25.71% Branches 9/35
40% Functions 2/5
66.67% Lines 20/30

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              7x 7x 7x 7x 7x   7x   7x 7x                 7x 57x 57x 57x   57x 57x   57x                   57x 57x     57x                                                             57x             7x  
/*
 * 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, { useState, useEffect } from 'react'
import { Container, ModalErrorHandlerBinding, shouldShowError } from '@harness/uicore'
import { useParams } from 'react-router-dom'
import { noop } from 'lodash-es'
import { TestConnectionWidget, TestStatus } from '@common/components/TestConnectionWidget/TestConnectionWidget'
import type { ProjectPathProps } from '@common/interfaces/RouteInterfaces'
import { ResponseConnectorValidationResult, useGetTestGitRepoConnectionResult } from 'services/cd-ng'
import type { ConnectorSelectedValue } from '@connectors/components/ConnectorReferenceField/ConnectorReferenceField'
import useRBACError from '@rbac/utils/useRBACError/useRBACError'
import css from './GitSyncRepoForm.module.scss'
 
export interface RepoTestConnectionProps {
  gitConnector?: ConnectorSelectedValue
  repoURL?: string
  onTestStatusChange?: (testStatus: TestStatus) => void // Can be used in parent
  modalErrorHandler?: ModalErrorHandlerBinding
}
 
const RepoTestConnection: React.FC<RepoTestConnectionProps> = props => {
  const { gitConnector, repoURL = '', onTestStatusChange = noop, modalErrorHandler } = props
  const { identifier = '', orgIdentifier, projectIdentifier } = gitConnector?.connector || {}
  const { getRBACErrorMessage } = useRBACError()
 
  const { accountId } = useParams<ProjectPathProps>()
  const [testStatus, setTestStatus] = useState<TestStatus>(TestStatus.NOT_INITIATED)
 
  const { mutate: testRepo } = useGetTestGitRepoConnectionResult({
    identifier,
    pathParams: {
      identifier
    },
    queryParams: {
      repoURL
    }
  })
 
  useEffect(() => {
    onTestStatusChange(testStatus)
  }, [onTestStatusChange, testStatus])
 
  const testConnection = async (): Promise<void> => {
    modalErrorHandler?.hide()
    if (identifier && repoURL) {
      setTestStatus(TestStatus.IN_PROGRESS)
      testRepo(undefined, {
        pathParams: {
          identifier
        },
        queryParams: {
          accountIdentifier: accountId,
          orgIdentifier,
          projectIdentifier,
          repoURL
        }
      })
        .then((response: ResponseConnectorValidationResult) => {
          if (response?.data?.status !== 'SUCCESS') {
            setTestStatus(TestStatus.FAILED)
          } else {
            setTestStatus(TestStatus.SUCCESS)
          }
        })
        .catch(e => {
          setTestStatus(TestStatus.FAILED)
          if (shouldShowError(e)) {
            modalErrorHandler?.showDanger(getRBACErrorMessage(e))
          }
        })
    }
  }
 
  return identifier ? (
    <Container flex className={css.testConnection}>
      <TestConnectionWidget testStatus={testStatus} onTest={testConnection} />
    </Container>
  ) : null
}
 
export default RepoTestConnection