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

83.72% Statements 36/43
52.17% Branches 36/69
85.71% Functions 6/7
83.72% Lines 36/43

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              7x 7x                     7x 7x 7x 7x 7x   7x 7x                   7x 13x 26x             7x 60x 60x 60x 60x             60x                       60x   60x 14x 3x   11x         60x       60x 14x     14x   14x                 14x 1x   13x 13x               60x 60x               2x                             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 {
  Dialog,
  FormInput,
  getErrorInfoFromErrorObject,
  Icon,
  Layout,
  ModalErrorHandlerBinding,
  SelectOption,
  Text,
  useToggleOpen
} from '@harness/uicore'
import { Color } from '@harness/design-system'
import { useParams } from 'react-router-dom'
import { defaultTo, isEmpty } from 'lodash-es'
import { useStrings } from 'framework/strings'
import { Error, useGetListOfBranchesByConnector } from 'services/cd-ng'
import type { ProjectPathProps } from '@common/interfaces/RouteInterfaces'
import { ErrorHandler } from '@common/components/ErrorHandler/ErrorHandler'
import css from './GitSyncRepoForm.module.scss'
 
export interface RepoBranchSelectProps {
  modalErrorHandler?: ModalErrorHandlerBinding
  connectorIdentifierRef?: string
  repoURL?: string
  selectedValue?: string
  onChange?: (selected: SelectOption, options?: SelectOption[]) => void
}
 
const getBranchSelectOptions = (data: string[] = []) => {
  return data.map((branch: string) => {
    return {
      label: defaultTo(branch, ''),
      value: defaultTo(branch, '')
    }
  })
}
 
const RepoBranchSelect: React.FC<RepoBranchSelectProps> = props => {
  const { modalErrorHandler, connectorIdentifierRef, repoURL, selectedValue } = props
  const { getString } = useStrings()
  const { accountId, projectIdentifier, orgIdentifier } = useParams<ProjectPathProps>()
  const [branchSelectOptions, setBranchSelectOptions] = useState<SelectOption[]>([])
 
  const {
    data: response,
    error,
    loading,
    refetch
  } = useGetListOfBranchesByConnector({
    queryParams: {
      connectorIdentifierRef,
      accountIdentifier: accountId,
      orgIdentifier,
      projectIdentifier,
      repoURL
    },
    debounce: 500,
    lazy: true
  })
 
  const { isOpen, open, close } = useToggleOpen()
 
  useEffect(() => {
    if (connectorIdentifierRef && repoURL) {
      refetch()
    } else {
      setBranchSelectOptions([])
    }
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [connectorIdentifierRef, repoURL])
 
  const handleError = (errorMessage: string): void => {
    modalErrorHandler?.showDanger(errorMessage)
  }
 
  useEffect(() => {
    Iif (loading) {
      return
    }
    modalErrorHandler?.hide()
 
    Iif (error) {
      if ((error?.data as Error)?.responseMessages?.length) {
        open()
      } else {
        handleError(getErrorInfoFromErrorObject(error))
      }
      return
    }
 
    if (response?.status !== 'SUCCESS') {
      response && handleError(getErrorInfoFromErrorObject(response))
    } else {
      Eif (!isEmpty(response?.data)) {
        setBranchSelectOptions(getBranchSelectOptions(response.data))
      } else {
        modalErrorHandler?.showDanger(getString('common.git.noBranchesFound'))
      }
    }
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [loading])
 
  const responseMessages = (error?.data as Error)?.responseMessages
  return (
    <Layout.Horizontal>
      <FormInput.Select
        name="branch"
        disabled={loading}
        items={branchSelectOptions}
        label={getString('gitsync.selectDefaultBranch')}
        value={{ label: selectedValue || '', value: selectedValue || '' }}
        onChange={selected => props.onChange?.(selected, branchSelectOptions)}
        selectProps={{ usePortal: true, popoverClassName: css.gitBranchSelectorPopover }}
      />
      {loading ? (
        <Layout.Horizontal spacing="small" flex padding={{ top: 'xsmall', left: 'xsmall' }}>
          <Icon name="steps-spinner" size={18} color={Color.PRIMARY_7} />
          <Text>{getString('gitsync.fetchingBranches').concat('...')}</Text>
        </Layout.Horizontal>
      ) : null}
      <Dialog isOpen={isOpen} enforceFocus={false} title={getString('gitsync.branchFetchFailed')} onClose={close}>
        {responseMessages ? <ErrorHandler responseMessages={responseMessages} /> : undefined}
      </Dialog>
    </Layout.Horizontal>
  )
}
export default RepoBranchSelect