All files / modules/70-pipeline/components/execution/StepDetails/tabs/JiraApprovalTab/JiraCriteria JiraCriteria.tsx

93.75% Statements 15/16
81.25% Branches 13/16
100% Functions 5/5
93.75% Lines 15/16

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              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 type {
  KeyValueCriteriaSpec,
  JexlCriteriaSpec,
  ConditionDTO,
  CriteriaSpecWrapperDTO,
  CriteriaSpecDTO
} from 'services/pipeline-ng'
import { String } from 'framework/strings'
import type { StringKeys } from 'framework/strings'
import { Collapse } from '@pipeline/components/execution/StepDetails/common/Collapse/Collapse'
 
import css from './JiraCriteria.module.scss'
 
const titles: Record<JiraCriteriaProps['type'], StringKeys> = {
  approval: 'pipeline.approvalCriteria.approvalCriteria',
  rejection: 'pipeline.approvalCriteria.rejectionCriteria'
}
 
const conditionStr: Record<ConditionDTO['operator'], StringKeys> = {
  equals: 'pipeline.jiraApprovalStep.execution.conditions.equals',
  'not equals': 'pipeline.jiraApprovalStep.execution.conditions.not_equals',
  in: 'pipeline.jiraApprovalStep.execution.conditions.in',
  'not in': 'pipeline.jiraApprovalStep.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 JiraCriteriaProps {
  type: 'approval' | 'rejection'
  criteria: CriteriaSpecWrapperDTO
}
 
export function JiraCriteria(props: JiraCriteriaProps): 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.jiraCriteria} 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} />
                {condition.value.split(',').map((key, j) => (
                  <span className={css.key} key={j}>
                    {key}
                  </span>
                ))}
              </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>
  )
}