All files / modules/70-pipeline/components/ConditionalExecutionToolTip ConditionalExecutionTooltipWrapper.tsx

95.45% Statements 21/22
68.18% Branches 15/22
100% Functions 4/4
95.45% Lines 21/22

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              3x 3x               3x   3x                         3x             1x 1x 1x                         3x 1x 1x 1x 1x 1x             1x     3x           1x 1x 1x   1x       1x                  
/*
 * 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 from 'react'
import {
  PipelineOrStageStatus,
  WhenConditionStatus,
  statusToStatusMapping
} from '@pipeline/components/PipelineSteps/AdvancedSteps/ConditionalExecutionPanel/ConditionalExecutionPanelUtils'
import type { StepMode as Modes } from '@pipeline/utils/stepUtils'
import type { StringsMap } from 'stringTypes'
import type { ConditionalExecutionNodeRunInfo, ExpressionBlock } from '@pipeline/utils/types'
import ConditionalExecutionTooltipPreExecution from './ConditionalExecutionTooltip'
 
const statusToStringIdMapping: any = {
  OnPipelineSuccess: 'pipeline.conditionalExecution.statusOption.success',
  OnStageSuccess: 'pipeline.conditionalExecution.statusOption.success',
  OnPipelineFailure: 'pipeline.conditionalExecution.statusOption.failure',
  OnStageFailure: 'pipeline.conditionalExecution.statusOption.failure',
  Always: 'pipeline.conditionalExecution.statusOption.all'
}
 
export interface ConditionalExecutionToolTipWrapperProps {
  mode: Modes
  data: ConditionalExecutionNodeRunInfo
}
 
const processConditionData = (
  whenCondition: string
): {
  status: WhenConditionStatus
  statusStringId: keyof StringsMap
  condition: string | undefined
} => {
  const whenConditionInfo: string[] = whenCondition.split(' && ')
  const statusInfo: string = whenConditionInfo.shift()!.replace(/[^a-zA-Z]/g, '')
  return {
    status: statusToStatusMapping[statusInfo],
    statusStringId: statusToStringIdMapping[statusInfo],
    condition: whenConditionInfo.length > 0 ? whenConditionInfo.join(' && ').slice(1, -1) : undefined
  }
}
 
export interface ResolvedVariableInterface {
  fullExpression: string
  trimmedExpression: string
  expressionValue: string
}
 
const processResolvedVariables = (expressions?: ExpressionBlock[]): ResolvedVariableInterface[] => {
  const resolvedVariables: ResolvedVariableInterface[] = []
  expressions?.forEach(expression => {
    const expressionStr: string | undefined = expression.expression
    Eif (!!expressionStr && !statusToStatusMapping[expressionStr]) {
      resolvedVariables.push({
        fullExpression: expressionStr,
        trimmedExpression: expressionStr.split('.').pop() || '',
        expressionValue: expression.expressionValue || ''
      })
    }
  })
  return resolvedVariables
}
 
export default function ConditionalExecutionTooltipWrapper(
  props: ConditionalExecutionToolTipWrapperProps
): React.ReactElement {
  const {
    mode,
    data: { whenCondition, expressions }
  } = props
  const { status, condition } = processConditionData(whenCondition!)
  const resolvedVariables = processResolvedVariables(expressions)
 
  Iif (status === PipelineOrStageStatus.SUCCESS && !condition?.trim()) {
    return <></>
  }
 
  return (
    <ConditionalExecutionTooltipPreExecution
      mode={mode}
      status={status}
      condition={condition}
      resolvedVariables={resolvedVariables}
    />
  )
}