All files / modules/70-pipeline/components/execution/StepDetails/common/ExecutionContent/PolicyEvaluationContent PolicyEvaluationContent.tsx

100% Statements 49/49
88.46% Branches 46/52
100% Functions 8/8
100% Lines 49/49

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 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223              2x 2x 2x   2x 2x     2x 2x   2x   2x 2x   2x                               2x 9x 9x           9x 9x   9x 5x         5x 4x 4x 5x 5x 5x       5x     9x 7x   2x                   8x                 8x 8x 8x   8x   8x 2x   6x           6x   6x                                                                     10x                 2x 5x 5x 5x   5x   5x   5x           5x                                         5x                                           4x                          
/*
 * Copyright 2022 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, { useState } from 'react'
import { defaultTo, isEmpty } from 'lodash-es'
import { Link, useParams } from 'react-router-dom'
 
import { Collapse, Color, Container, Layout, Text } from '@harness/uicore'
import { useStrings } from 'framework/strings'
import type { ExecutionNode } from 'services/pipeline-ng'
 
import { useDeepCompareEffect } from '@common/hooks'
import { useModuleInfo } from '@common/hooks/useModuleInfo'
import type { ProjectPathProps } from '@common/interfaces/RouteInterfaces'
import routes from '@common/RouteDefinitions'
 
import EvaluationStatusLabel, { EvaluationStatus } from './EvaluationStatusLabel/EvaluationStatusLabel'
import { EvaluationCount } from './EvaluationCount/EvaluationCount'
 
import css from './PolicyEvaluationContent.module.scss'
 
interface EvaluationCounts {
  [EvaluationStatus.ERROR]: number
  [EvaluationStatus.WARNING]: number
  [EvaluationStatus.PASS]: number
}
 
export interface EvaluatedPolicy {
  name: string
  status: EvaluationStatus
  identifier?: string
  denyMessages?: []
  error?: string
}
 
export function PolicyEvaluationContent({ step }: { step: ExecutionNode }) {
  const { getString } = useStrings()
  const [evaluationCounts, setEvaluationCounts] = useState<EvaluationCounts>({
    error: 0,
    warning: 0,
    pass: 0
  })
 
  const policySetDetails = defaultTo(/* istanbul ignore next */ step?.outcomes?.output?.policySetDetails, {})
  const policySetIds = Object.keys(policySetDetails)
 
  useDeepCompareEffect(() => {
    const counts: EvaluationCounts = {
      error: 0,
      warning: 0,
      pass: 0
    }
    for (const policySetId in policySetDetails) {
      const policyDetails = defaultTo(policySetDetails[policySetId]?.policyDetails, [])
      for (const policyId in policyDetails) {
        const policy = policyDetails[policyId] as EvaluatedPolicy
        Eif (policy.status) {
          counts[policy.status] = defaultTo(counts[policy.status], 0) + 1
        }
      }
    }
    setEvaluationCounts(counts)
  }, [policySetDetails])
 
  if (isEmpty(policySetIds)) {
    return <Text margin={{ left: 'large' }}>{getString('common.policy.noPolicyEvalResult')}</Text>
  } else {
    return (
      <Container border={{ top: true }} padding="medium">
        <Layout.Horizontal>
          <Text margin={{ left: 'small', right: 'large' }}>{getString('pipeline.policyEvaluations.title')}</Text>
          <EvaluationCount status={EvaluationStatus.ERROR} count={evaluationCounts[EvaluationStatus.ERROR]} />
          <EvaluationCount status={EvaluationStatus.WARNING} count={evaluationCounts[EvaluationStatus.WARNING]} />
          <EvaluationCount status={EvaluationStatus.PASS} count={evaluationCounts[EvaluationStatus.PASS]} />
        </Layout.Horizontal>
        <Layout.Vertical padding={{ top: 'medium', bottom: 'medium' }}>
          {policySetIds.map(id => {
            return <PolicySetInfo key={id} policySet={policySetDetails[id]} />
          })}
        </Layout.Vertical>
      </Container>
    )
  }
}
 
function PolicySetInfo({ policySet }: { policySet: { [key: string]: any } }) {
  const { accountId, orgIdentifier, projectIdentifier } = useParams<ProjectPathProps>()
  const { getString } = useStrings()
  const { module } = useModuleInfo()
 
  const { name, identifier, status, policyDetails: policies } = policySet
 
  if (isEmpty(policies)) {
    return <Text margin={{ left: 'large' }}>{getString('common.policy.noPolicyResult')}</Text>
  } else {
    const scope = identifier.includes('account.')
      ? getString('account')
      : identifier.includes('org.')
      ? getString('orgLabel')
      : getString('projectLabel')
 
    const policyIds = Object.keys(policies)
 
    return (
      <Container>
        <Collapse
          collapseClassName={css.collapse}
          iconProps={
            {
              color: Color.PRIMARY_7,
              padding: { right: 'small' }
            } as any
          }
          collapsedIcon={'main-chevron-down'}
          expandedIcon={'main-chevron-up'}
          heading={
            <Layout.Horizontal flex={{ alignItems: 'center' }}>
              <Text lineClamp={1} width={'300px'} padding={{ right: 'small' }}>
                <Link
                  to={routes.toGovernancePolicySetDetail({
                    accountId,
                    policySetIdentifier: defaultTo(identifier, ''),
                    ...(scope === getString('orgLabel') && { orgIdentifier }),
                    ...(scope === getString('projectLabel') && { module, orgIdentifier, projectIdentifier })
                  })}
                >
                  {getString('pipeline.policyEvaluations.policySetName', { name })}
                </Link>
              </Text>
              <Text width={'100px'} font={{ size: 'small' }}>
                {scope}
              </Text>
              <EvaluationStatusLabel status={status} />
            </Layout.Horizontal>
          }
        >
          <Container margin={{ top: 'large' }} border={{ top: true }}>
            {policyIds.map((id, index) => {
              return <PolicyInfo key={id} policy={policies[id]} numberInList={index + 1} />
            })}
          </Container>
        </Collapse>
      </Container>
    )
  }
}
 
export function PolicyInfo({ policy, numberInList }: { policy: EvaluatedPolicy; numberInList: number }) {
  const { accountId, orgIdentifier, projectIdentifier } = useParams<ProjectPathProps>()
  const { getString } = useStrings()
  const { module } = useModuleInfo()
 
  const { name: policyName, identifier, status, denyMessages, error: errorMessage } = policy
  const statusColor =
    status === EvaluationStatus.PASS ? Color.SUCCESS : status === EvaluationStatus.ERROR ? Color.ERROR : Color.WARNING
 
  const scope = identifier?.includes('account.')
    ? getString('account')
    : identifier?.includes('org.')
    ? getString('orgLabel')
    : getString('projectLabel')
 
  return (
    <Layout.Horizontal flex={{ justifyContent: 'flex-start', alignItems: 'flex-start' }} padding={{ top: 'large' }}>
      <Layout.Vertical>
        <Text lineClamp={1} width={'400px'} padding={{ right: 'small' }}>
          <Link
            to={{
              pathname: routes.toGovernanceViewPolicy({
                accountId,
                policyIdentifier: defaultTo(identifier, ''),
                ...(scope === getString('orgLabel') && { orgIdentifier }),
                ...(scope === getString('projectLabel') && { module, orgIdentifier, projectIdentifier })
              })
            }}
          >
            {`Policy ${numberInList}: ${policyName}`}
          </Link>
        </Text>
        <Layout.Vertical>
          {!!denyMessages?.length && (
            <FailureInfo label={getString('details')}>
              {denyMessages.map((message: string, index: number) => (
                <Text key={index} color={statusColor}>
                  {denyMessages?.length > 1 ? '- ' : ''}
                  {message}
                </Text>
              ))}
            </FailureInfo>
          )}
          {errorMessage && (
            <FailureInfo label={getString('error')}>
              <Text key={errorMessage} color={statusColor} lineClamp={2}>
                {errorMessage}
              </Text>
            </FailureInfo>
          )}
        </Layout.Vertical>
      </Layout.Vertical>
      <EvaluationStatusLabel status={status} />
    </Layout.Horizontal>
  )
}
 
function FailureInfo({ label, children }: { label: string; children: React.ReactNode }) {
  return (
    <Layout.Horizontal
      spacing="xsmall"
      padding={{ left: 'xxxlarge', top: 'small' }}
      flex={{ alignItems: 'flex-start', justifyContent: 'flex-start' }}
    >
      <Text color={Color.BLACK}>{label.toUpperCase()}</Text>
      <Container padding={{ left: 'small' }} width={300}>
        {children}
      </Container>
    </Layout.Horizontal>
  )
}