All files / modules/75-ci/components/CIExecutionCardSummary CIExecutionCardSummary.tsx

76.67% Statements 23/30
48.76% Branches 138/283
66.67% Functions 2/3
76.67% Lines 23/30

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              1x 1x   1x 1x 1x 1x   1x               3x         3x   2x                     2x                             1x                                                                                       1x     3x     1x 4x   3x 3x       3x 3x       3x 3x       3x    
/*
 * 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 { Icon, Layout, Text } from '@wings-software/uicore'
import type { ExecutionSummaryProps, ExecutionCardInfoProps } from '@pipeline/factories/ExecutionFactory/types'
import { CommitsInfo } from '@ci/components/CommitsInfo/CommitsInfo'
import { useStrings, UseStringsReturn } from 'framework/strings'
import { getUIType, UIType } from '../common/getUIType'
import { CICodebaseHeader } from '../CICodebaseHeader/CICodebaseHeader'
 
import css from './CIExecutionCardSummary.module.scss'
 
function getUIByType(
  uiType: UIType,
  { data, getString }: { data: ExecutionSummaryProps['data']; getString: UseStringsReturn['getString'] }
): React.ReactElement {
  let ui
 
  const handleLinkClick = (event: any): void => {
    event.stopPropagation()
    window.open(event.target.href, '_blank')
  }
 
  switch (uiType) {
    case UIType.Branch:
      ui = (
        <>
          <CICodebaseHeader repo={data.repoName} branch={data.branch} />
          {data?.ciExecutionInfoDTO?.branch?.commits?.length > 0 && (
            <CommitsInfo
              commits={data?.ciExecutionInfoDTO?.branch?.commits}
              authorAvatar={data?.ciExecutionInfoDTO?.author?.avatar}
            />
          )}
        </>
      )
      break
    case UIType.Tag:
      ui = (
        <>
          <CICodebaseHeader repo={data.repoName} tag={data.tag} />
          {data?.ciExecutionInfoDTO?.branch?.commits?.length > 0 && (
            <CommitsInfo
              commits={data?.ciExecutionInfoDTO?.branch?.commits}
              authorAvatar={data?.ciExecutionInfoDTO?.author?.avatar}
            />
          )}
        </>
      )
      break
    case UIType.PullRequest:
      ui = (
        <>
          <CICodebaseHeader
            repo={data.repoName}
            branch={data?.ciExecutionInfoDTO?.pullRequest?.sourceBranch}
            targetBranch={data?.ciExecutionInfoDTO?.pullRequest?.targetBranch}
          />
          <Layout.Horizontal className={css.alignSelfStart} flex spacing="small">
            <Icon name="git-pull" size={14} />
            <div style={{ fontSize: 0 }}>
              <Text
                font={{ size: 'small' }}
                color="grey900"
                style={{ maxWidth: 500, whiteSpace: 'nowrap', textOverflow: 'ellipsis', overflow: 'hidden' }}
                title={data?.ciExecutionInfoDTO?.pullRequest?.title}
              >
                {data?.ciExecutionInfoDTO?.pullRequest?.title}
              </Text>
            </div>
            <a
              className={css.label}
              href={data?.ciExecutionInfoDTO?.pullRequest?.link || ''}
              target="_blank"
              rel="noopener noreferrer"
              onClick={handleLinkClick}
            >
              {getString('ci.prSymbol')}
              {typeof data?.ciExecutionInfoDTO?.pullRequest?.id === 'string' ||
              typeof data?.ciExecutionInfoDTO?.pullRequest?.id === 'number'
                ? data?.ciExecutionInfoDTO?.pullRequest?.id
                : data?.ciExecutionInfoDTO?.pullRequest?.id?.['$numberLong']
                ? data?.ciExecutionInfoDTO?.pullRequest?.id?.['$numberLong']
                : ''}
            </a>
            <div className={css.state}>{data?.ciExecutionInfoDTO?.pullRequest?.state}</div>
          </Layout.Horizontal>
          {data?.ciExecutionInfoDTO?.pullRequest?.commits?.length > 0 && (
            <CommitsInfo
              commits={data?.ciExecutionInfoDTO?.pullRequest?.commits}
              authorAvatar={data?.ciExecutionInfoDTO?.author?.avatar}
            />
          )}
        </>
      )
      break
  }
 
  return ui
}
 
export function CIExecutionCardSummary(props: ExecutionCardInfoProps): React.ReactElement {
  const { getString } = useStrings()
 
  const { data } = props
  Iif (!data) {
    return <></>
  }
 
  const uiType = getUIType(data)
  Iif (!uiType) {
    return <></>
  }
 
  const ui = getUIByType(uiType, { data, getString })
  Iif (!ui) {
    return <></>
  }
 
  return <div className={css.main}>{ui}</div>
}