All files / modules/70-pipeline/components/PipelineStudio/PiplineHooks useVariablesExpression.ts

100% Statements 43/43
88% Branches 44/50
100% Functions 14/14
100% Lines 41/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              217x 217x   217x 217x 217x               1138x 1138x 433x 1124x   705x 275x   1138x           217x 2381x 2381x 2381x 2381x       2381x   2381x 879x 15x 15x 14x 14x         2381x 893x 28x     1282x     1282x       28x     1282x     1282x       28x       2381x 873x 1x   15x       1x   15x       1x       2381x    
/*
 * 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 { useEffect, useState } from 'react'
import { defaultTo, identity, isEmpty, map, sortBy, sortedUniq } from 'lodash-es'
import type { VariableMergeServiceResponse } from 'services/pipeline-ng'
import { useTemplateVariables } from '../../TemplateVariablesContext/TemplateVariablesContext'
import { usePipelineVariables } from '../../PipelineVariablesContext/PipelineVariablesContext'
import { usePipelineContext } from '../PipelineContext/PipelineContext'
/**
 * Traverse over stage and find out all local fqn
 */
function traverseStageObject(
  jsonObj: Record<string, any>,
  metadataMap: Required<VariableMergeServiceResponse>['metadataMap']
): string[] {
  const keys: string[] = []
  if (jsonObj !== null && typeof jsonObj == 'object') {
    Object.entries(jsonObj).forEach(([_key, value]) => {
      keys.push(...traverseStageObject(value, metadataMap))
    })
  } else if (metadataMap[jsonObj]) {
    keys.push(jsonObj)
  }
  return keys
}
 
/**
 * Hook to integrate and get expression for local stage and other stage
 */
export function useVariablesExpression(): { expressions: string[] } {
  const { variablesPipeline, metadataMap, initLoading } = usePipelineVariables()
  const { metadataMap: templateMetadataMap, initLoading: templateInitLoading } = useTemplateVariables()
  const [expressions, setExpressions] = useState<string[]>([])
  const [localStageKeys, setLocalStageKeys] = useState<string[]>([])
  const {
    state: { selectionState: { selectedStageId } = { selectedStageId: '' } },
    getStageFromPipeline
  } = usePipelineContext()
 
  useEffect(() => {
    if (!initLoading && selectedStageId && !isEmpty(selectedStageId)) {
      const stage = getStageFromPipeline(selectedStageId, variablesPipeline).stage
      if (stage) {
        const keys = traverseStageObject(stage, metadataMap)
        setLocalStageKeys(keys)
      }
    }
  }, [variablesPipeline, initLoading, selectedStageId, metadataMap, getStageFromPipeline])
 
  useEffect(() => {
    if (!initLoading && !isEmpty(metadataMap)) {
      const expression = sortedUniq(
        sortBy(
          map(metadataMap, (item, index) =>
            localStageKeys.indexOf(index) > -1
              ? defaultTo(item.yamlProperties?.localName, '')
              : defaultTo(item.yamlProperties?.fqn, '')
          ).filter(p => p),
          identity
        )
      )
      const outputExpression = sortedUniq(
        sortBy(
          map(metadataMap, (item, index) =>
            localStageKeys.indexOf(index) > -1
              ? defaultTo(item.yamlOutputProperties?.localName, '')
              : defaultTo(item.yamlOutputProperties?.fqn, '')
          ).filter(p => p),
          identity
        )
      )
      setExpressions([...expression, ...outputExpression])
    }
  }, [initLoading, metadataMap, localStageKeys])
 
  useEffect(() => {
    if (!templateInitLoading && !isEmpty(templateMetadataMap)) {
      const expression = sortedUniq(
        sortBy(
          map(templateMetadataMap, item => defaultTo(item.yamlProperties?.localName, '')).filter(p => p),
          identity
        )
      )
      const outputExpression = sortedUniq(
        sortBy(
          map(templateMetadataMap, item => defaultTo(item.yamlOutputProperties?.localName, '')).filter(p => p),
          identity
        )
      )
      setExpressions([...expression, ...outputExpression])
    }
  }, [templateInitLoading, templateMetadataMap])
 
  return { expressions }
}