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

75% Statements 21/28
37.04% Branches 173/467
100% Functions 2/2
74.07% Lines 20/27

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              1x 1x 1x 1x   1x 1x 1x 1x               2x                                                                                                               2x                                                                                                                         2x     2x     1x 2x   2x       2x 2x       2x 2x       2x              
/*
 * 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 cx from 'classnames'
import { Text, Icon, Layout } from '@wings-software/uicore'
import { useStrings, UseStringsReturn } from 'framework/strings'
import type { ExecutionSummaryProps } from '@pipeline/factories/ExecutionFactory/types'
import { getUIType, UIType } from '../common/getUIType'
import { CommitId } from '../CommitsInfo/CommitsInfo'
import { CICodebaseHeader } from '../CICodebaseHeader/CICodebaseHeader'
import css from './CIExecutionSummary.module.scss'
 
function getUIByType(
  uiType: UIType,
  { data, getString }: { data: ExecutionSummaryProps['data']; getString: UseStringsReturn['getString'] }
): React.ReactElement {
  let ui
 
  switch (uiType) {
    case UIType.Branch:
      ui = (
        <>
          <CICodebaseHeader repo={data.repoName} branch={data.branch} isDetailedView={true} />
          {data?.ciExecutionInfoDTO?.branch?.commits?.length > 0 && (
            <Layout.Horizontal flex spacing="small" margin={{ left: 'small' }}>
              <Icon name="git-branch-existing" size={14} />
              <div style={{ fontSize: 0 }}>
                <Text
                  font={{ size: 'small' }}
                  style={{ maxWidth: 150, whiteSpace: 'nowrap', textOverflow: 'ellipsis', overflow: 'hidden' }}
                  title={data?.ciExecutionInfoDTO?.branch?.commits[0]?.message}
                >
                  {data?.ciExecutionInfoDTO?.branch?.commits[0]?.message}
                </Text>
              </div>
              {data?.ciExecutionInfoDTO?.branch?.commits[0]?.id && (
                <CommitId
                  commitId={data?.ciExecutionInfoDTO?.branch?.commits[0]?.id}
                  commitLink={data?.ciExecutionInfoDTO?.branch?.commits[0]?.link}
                />
              )}
            </Layout.Horizontal>
          )}
        </>
      )
      break
    case UIType.Tag:
      ui = (
        <>
          <CICodebaseHeader repo={data.repoName} tag={data.tag} isDetailedView={true} />
          {data?.ciExecutionInfoDTO?.branch?.commits?.length > 0 && (
            <Layout.Horizontal flex spacing="small">
              <Icon name="git-branch-existing" size={14} />
              <div style={{ fontSize: 0 }}>
                <Text
                  font={{ size: 'small' }}
                  style={{ maxWidth: 150, whiteSpace: 'nowrap', textOverflow: 'ellipsis', overflow: 'hidden' }}
                  title={data?.ciExecutionInfoDTO?.branch?.commits[0]?.message}
                >
                  {data?.ciExecutionInfoDTO?.branch?.commits[0]?.message}
                </Text>
              </div>
              {data?.ciExecutionInfoDTO?.branch?.commits[0]?.id && (
                <CommitId
                  commitId={data?.ciExecutionInfoDTO?.branch?.commits[0]?.id}
                  commitLink={data?.ciExecutionInfoDTO?.branch?.commits[0]?.link}
                />
              )}
            </Layout.Horizontal>
          )}
        </>
      )
      break
    case UIType.PullRequest:
      ui = (
        <>
          <CICodebaseHeader
            repo={data.repoName}
            branch={data?.ciExecutionInfoDTO?.pullRequest?.sourceBranch}
            targetBranch={data?.ciExecutionInfoDTO?.pullRequest?.targetBranch}
            isDetailedView={true}
          />
          {data?.ciExecutionInfoDTO?.pullRequest && (
            <>
              {data?.ciExecutionInfoDTO?.pullRequest?.commits?.length > 0 && (
                <Layout.Horizontal flex spacing="small" margin={{ left: 'small' }}>
                  <Icon name="git-branch-existing" size={14} />
                  <div style={{ fontSize: 0 }}>
                    <Text
                      font={{ size: 'small' }}
                      style={{ maxWidth: 150, whiteSpace: 'nowrap', textOverflow: 'ellipsis', overflow: 'hidden' }}
                      title={data?.ciExecutionInfoDTO?.pullRequest?.commits[0]?.message}
                    >
                      {data?.ciExecutionInfoDTO?.pullRequest?.commits[0]?.message}
                    </Text>
                  </div>
                  {data?.ciExecutionInfoDTO?.pullRequest?.commits[0]?.id && (
                    <CommitId
                      commitId={data?.ciExecutionInfoDTO?.pullRequest?.commits[0]?.id}
                      commitLink={data?.ciExecutionInfoDTO?.pullRequest?.commits[0]?.link}
                    />
                  )}
                </Layout.Horizontal>
              )}
              <Layout.Horizontal flex spacing="small" margin={{ left: 'small' }}>
                <Icon name="git-pull" size={14} />
                <div style={{ fontSize: 0 }}>
                  <Text
                    font={{ size: 'small' }}
                    style={{ maxWidth: 150, 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"
                >
                  {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>
            </>
          )}
        </>
      )
      break
  }
 
  return ui
}
 
export const CIExecutionSummary = ({ data }: ExecutionSummaryProps): React.ReactElement => {
  const { getString } = useStrings()
 
  Iif (!data) {
    return <></>
  }
 
  const uiType = getUIType(data)
  Iif (!uiType) {
    return <></>
  }
 
  const ui = getUIByType(uiType, { data, getString })
  Iif (!ui) {
    return <></>
  }
 
  return (
    <div className={cx(css.main, { [css.pullRequest]: uiType === UIType.PullRequest })}>
      <Icon className={css.icon} size={18} name="ci-main" />
      {ui}
    </div>
  )
}