All files / modules/70-pipeline/components/ManifestSelection ManifestSelection.tsx

98.31% Statements 58/59
79.65% Branches 90/113
90.91% Functions 10/11
98.28% Lines 57/58

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              100x 100x   100x 100x 100x 100x     100x   100x     100x   100x 100x   100x                               14x   14x 14x 14x   14x             14x                 14x       14x 7x 1x 1x 1x 1x         1x     1x     6x 1x     5x 1x 1x   1x 1x     1x   4x 1x   3x         14x 7x     14x 7x 8x             14x 7x 8x 7x 7x 7x 7x       14x 14x 2x 2x 2x   12x     14x                                                    
/*
 * 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, { useMemo } from 'react'
import { Layout, Text } from '@wings-software/uicore'
 
import { useParams } from 'react-router-dom'
import { get } from 'lodash-es'
import { useGetConnectorListV2, PageConnectorResponse } from 'services/cd-ng'
import { usePipelineContext } from '@pipeline/components/PipelineStudio/PipelineContext/PipelineContext'
 
import type { PipelineType } from '@common/interfaces/RouteInterfaces'
import { getIdentifierFromValue, getScopeFromValue } from '@common/components/EntityReference/EntityReference'
 
import { useStrings } from 'framework/strings'
import type { Scope } from '@common/interfaces/SecretsInterface'
import type { DeploymentStageElementConfig } from '@pipeline/utils/pipelineTypes'
import { useDeepCompareEffect } from '@common/hooks'
import type { ManifestSelectionProps } from './ManifestInterface'
import ManifestListView from './ManifestListView'
import { getFlattenedStages, getStageIndexFromPipeline } from '../PipelineStudio/StageBuilder/StageBuilderUtil'
 
export default function ManifestSelection({
  isForOverrideSets = false,
  identifierName,
  isForPredefinedSets = false,
  isPropagating,
  overrideSetIdentifier
}: ManifestSelectionProps): JSX.Element {
  const {
    state: {
      pipeline,
      selectionState: { selectedStageId }
    },
    getStageFromPipeline,
    updateStage,
    isReadonly,
    allowableTypes
  } = usePipelineContext()
 
  const { stage } = getStageFromPipeline<DeploymentStageElementConfig>(selectedStageId || '')
  const [fetchedConnectorResponse, setFetchedConnectorResponse] = React.useState<PageConnectorResponse | undefined>()
  const { getString } = useStrings()
 
  const { accountId, orgIdentifier, projectIdentifier } = useParams<
    PipelineType<{
      orgIdentifier: string
      projectIdentifier: string
      accountId: string
    }>
  >()
  const defaultQueryParams = {
    pageIndex: 0,
    pageSize: 10,
    searchTerm: '',
    accountIdentifier: accountId,
    orgIdentifier,
    projectIdentifier,
    includeAllConnectorsAvailableAtScope: true
  }
  const { mutate: fetchConnectors } = useGetConnectorListV2({
    queryParams: defaultQueryParams
  })
 
  const listOfManifests = useMemo(() => {
    if (overrideSetIdentifier?.length) {
      const parentStageName = stage?.stage?.spec?.serviceConfig?.useFromStage?.stage
      const { index } = getStageIndexFromPipeline(pipeline, parentStageName)
      const { stages } = getFlattenedStages(pipeline)
      const overrideSets = get(
        stages[index],
        'stage.spec.serviceConfig.serviceDefinition.spec.manifestOverrideSets',
        []
      )
      const selectedOverrideSet = overrideSets.find(
        ({ overrideSet }: { overrideSet: { identifier: string } }) => overrideSet.identifier === overrideSetIdentifier
      )
      return get(selectedOverrideSet, 'overrideSet.manifests', [])
    }
 
    if (isPropagating) {
      return get(stage, 'stage.spec.serviceConfig.stageOverrides.manifests', [])
    }
 
    if (isForOverrideSets) {
      const listValue = get(stage, 'stage.spec.serviceConfig.serviceDefinition.spec.manifestOverrideSets', [])
      return listValue
        .map((overrideSets: { overrideSet: { identifier: string; manifests: [any] } }) => {
          Eif (overrideSets?.overrideSet?.identifier === identifierName) {
            return overrideSets.overrideSet?.manifests
          }
        })
        .filter((x: { overrideSet: { identifier: string; manifests: [any] } }) => x !== undefined)[0]
    } else {
      if (isForPredefinedSets) {
        return get(stage, 'stage.spec.serviceConfig.stageOverrides.manifests', [])
      } else {
        return get(stage, 'stage.spec.serviceConfig.serviceDefinition.spec.manifests', [])
      }
    }
  }, [overrideSetIdentifier, isPropagating, stage, isForOverrideSets, isForPredefinedSets, pipeline])
 
  useDeepCompareEffect(() => {
    refetchConnectorList()
  }, [stage, listOfManifests])
 
  const getConnectorList = (): Array<{ scope: Scope; identifier: string }> => {
    return listOfManifests?.length
      ? listOfManifests.map((data: any) => ({
          scope: getScopeFromValue(data?.manifest?.spec?.store?.spec?.connectorRef),
          identifier: getIdentifierFromValue(data?.manifest?.spec?.store?.spec?.connectorRef)
        }))
      : []
  }
 
  const refetchConnectorList = async (): Promise<void> => {
    const connectorList = getConnectorList()
    const connectorIdentifiers = connectorList.map((item: { scope: string; identifier: string }) => item.identifier)
    const response = await fetchConnectors({ filterType: 'Connector', connectorIdentifiers })
    Eif (response?.data) {
      const { data: connectorResponse } = response
      setFetchedConnectorResponse(connectorResponse)
    }
  }
 
  const getDeploymentType = (): string => {
    if (isPropagating) {
      const parentStageId = get(stage, 'stage.spec.serviceConfig.useFromStage.stage', null)
      const parentStage = getStageFromPipeline<DeploymentStageElementConfig>(parentStageId || '')
      return get(parentStage, 'stage.stage.spec.serviceConfig.serviceDefinition.type', null)
    }
    return get(stage, 'stage.spec.serviceConfig.serviceDefinition.type', null)
  }
 
  return (
    <Layout.Vertical>
      {/* {isForPredefinedSets && <PredefinedOverrideSets context="MANIFEST" currentStage={stage} />} //disabled for now */}
      {overrideSetIdentifier?.length === 0 && !isForOverrideSets && (
        <Text style={{ color: 'var(--grey-500)', lineHeight: '24px' }}>{getString('manifestSelectionInfo')}</Text>
      )}
 
      <ManifestListView
        isPropagating={isPropagating}
        pipeline={pipeline}
        updateStage={updateStage}
        stage={stage}
        isForOverrideSets={isForOverrideSets}
        identifierName={identifierName}
        isForPredefinedSets={isForPredefinedSets}
        overrideSetIdentifier={overrideSetIdentifier}
        connectors={fetchedConnectorResponse}
        refetchConnectors={refetchConnectorList}
        listOfManifests={listOfManifests}
        isReadonly={isReadonly}
        deploymentType={getDeploymentType()}
        allowableTypes={allowableTypes}
      />
    </Layout.Vertical>
  )
}