All files / modules/70-pipeline/components/execution/StepDetails/tabs/ServiceNowApprovalTab/ServiceNowCriteria ServiceNowCriteria.tsx

94.44% Statements 17/18
81.25% Branches 13/16
100% Functions 5/5
94.44% Lines 17/18

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              2x 2x   2x 2x               2x 2x   2x   2x         2x               6x             6x               2x 6x   6x           6x                           6x       9x                                      
/*
 * 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 { isEmpty } from 'lodash-es'
 
import { Text } from '@wings-software/uicore'
import { FontVariation } from '@harness/design-system'
import type {
  KeyValueCriteriaSpec,
  JexlCriteriaSpec,
  ConditionDTO,
  CriteriaSpecWrapperDTO,
  CriteriaSpecDTO
} from 'services/pipeline-ng'
import { String, StringKeys } from 'framework/strings'
import { Collapse } from '@pipeline/components/execution/StepDetails/common/Collapse/Collapse'
 
import css from './ServiceNowCriteria.module.scss'
 
const titles: Record<ServiceNowCriteriaProps['type'], StringKeys> = {
  approval: 'pipeline.approvalCriteria.approvalCriteria',
  rejection: 'pipeline.approvalCriteria.rejectionCriteria'
}
 
const conditionStr: Record<ConditionDTO['operator'], StringKeys> = {
  equals: 'pipeline.serviceNowApprovalStep.execution.conditions.equals',
  'not equals': 'pipeline.serviceNowApprovalStep.execution.conditions.not_equals',
  in: 'pipeline.serviceNowApprovalStep.execution.conditions.in',
  'not in': 'pipeline.serviceNowApprovalStep.execution.conditions.not_in'
}
 
function isJexlCriteria(type: CriteriaSpecWrapperDTO['type'], criteria: CriteriaSpecDTO): criteria is JexlCriteriaSpec {
  return criteria && type === 'Jexl'
}
 
function isKeyValuesCriteria(
  type: CriteriaSpecWrapperDTO['type'],
  criteria: CriteriaSpecDTO
): criteria is KeyValueCriteriaSpec {
  return criteria && type === 'KeyValues'
}
 
export interface ServiceNowCriteriaProps {
  type: 'approval' | 'rejection'
  criteria: CriteriaSpecWrapperDTO
}
 
export function ServiceNowCriteria(props: ServiceNowCriteriaProps): React.ReactElement | null {
  const { type, criteria } = props
 
  Iif (isEmpty(criteria.spec.conditions) && isEmpty(criteria.spec.expression)) {
    // Rejection criteria can be optional
    // So render nothing if we do not have conditions or expression
    return null
  }
 
  return (
    <Collapse className={css.serviceNowCriteria} title={<String stringID={titles[type]} />} isDefaultOpen>
      {isKeyValuesCriteria(criteria.type, criteria.spec) ? (
        <div className={css.collapseContainer}>
          <String
            tagName="div"
            stringID={
              criteria.spec.matchAnyCondition
                ? 'pipeline.jiraApprovalStep.execution.anyConditionsMsg'
                : 'pipeline.jiraApprovalStep.execution.allConditionsMsg'
            }
          />
          <ul className={css.conditions}>
            {(criteria.spec.conditions || []).map((condition: ConditionDTO, i: number) => (
              <li key={i}>
                <String stringID={conditionStr[condition.operator]} vars={condition} />
                <div className={css.keyWarper}>
                  {condition.value.split(',').map((key, j) => (
                    <Text lineClamp={1} font={{ variation: FontVariation.SMALL }} className={css.key} key={j}>
                      {key}
                    </Text>
                  ))}
                </div>
              </li>
            ))}
          </ul>
        </div>
      ) : null}
      {isJexlCriteria(criteria.type, criteria.spec) ? (
        <div className={css.collapseContainer}>
          <String stringID="common.jexlExpression" />
          <div className={css.jexl}>{criteria.spec.expression}</div>
        </div>
      ) : null}
    </Collapse>
  )
}