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

71.43% Statements 25/35
45.95% Branches 17/37
33.33% Functions 3/9
73.53% Lines 25/34

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              3x 3x 3x 3x 3x   3x 3x   3x             3x 4x 4x   4x   4x           4x       4x         4x                                                         3x 1x 1x                                   3x 2x 1x   1x 1x             1x                                                                                                      
/*
 * 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, { useState } from 'react'
import { first } from 'lodash-es'
import { Text, Button, Icon, Utils, Container, Layout } from '@wings-software/uicore'
import { Collapse } from '@blueprintjs/core'
import { FontVariation, Color } from '@harness/design-system'
import type { CIBuildCommit } from 'services/ci'
import { String, useStrings } from 'framework/strings'
import { UserLabel, TimeAgoPopover } from '@common/exports'
 
import css from './CommitsInfo.module.scss'
 
export interface CommitIdProps {
  commitId: string
  commitLink?: string
}
 
export function CommitId({ commitId, commitLink }: CommitIdProps): React.ReactElement {
  const [isCommitIdCopied, setIsCommitIdCopied] = useState(false)
  const { getString } = useStrings()
 
  const slicedCommitId = commitId.slice(0, 7)
 
  const handleCopyButtonClick = (e: React.SyntheticEvent): void => {
    e.stopPropagation()
    Utils.copy(commitLink || commitId)
    setIsCommitIdCopied(true)
  }
 
  const handleCopyButtonTooltipClosed = (): void => {
    setIsCommitIdCopied(false)
  }
 
  const handleLinkClick = (event: any): void => {
    event.stopPropagation()
    window.open(event.target.href, '_blank')
  }
 
  return (
    <Text data-name="commitId" className={css.commitId} font={{ variation: FontVariation.SMALL }}>
      {commitLink ? (
        <a className={css.label} href={commitLink} rel="noreferrer" target="_blank" onClick={handleLinkClick}>
          {slicedCommitId}
        </a>
      ) : (
        <span className={css.label}>{slicedCommitId}</span>
      )}
      <Text
        tooltip={
          <Container padding="small">{getString(isCommitIdCopied ? 'copiedToClipboard' : 'clickToCopy')}</Container>
        }
        tooltipProps={{
          onClosed: handleCopyButtonTooltipClosed
        }}
        style={{ cursor: 'pointer' }}
        font={{ variation: FontVariation.SMALL }}
      >
        <Icon name="copy" size={14} onClick={handleCopyButtonClick} />
      </Text>
    </Text>
  )
}
 
export interface LastCommitProps {
  lastCommit: CIBuildCommit
}
 
export function LastCommit({ lastCommit }: LastCommitProps): React.ReactElement {
  const { message, id, link } = lastCommit
  return (
    <Layout.Horizontal flex={{ justifyContent: 'flex-start' }} spacing="small">
      <Icon className={css.icon} name="git-commit" size={14} color={Color.GREY_700} />
      {message ? (
        <Text className={css.message} title={message} font={{ variation: FontVariation.SMALL }} lineClamp={1}>
          {message}
        </Text>
      ) : null}
      {id ? <CommitId commitId={id} commitLink={link} /> : null}
    </Layout.Horizontal>
  )
}
 
export interface CommitsInfoProps {
  commits?: CIBuildCommit[]
  authorAvatar?: string
}
 
export function CommitsInfo(props: CommitsInfoProps): React.ReactElement | null {
  const [showCommits, setShowCommits] = React.useState(false)
  const { getString } = useStrings()
 
  const { commits = [], authorAvatar } = props
  const lastCommit = first(commits || [])
 
  function toggleCommits(e: React.SyntheticEvent): void {
    e.stopPropagation()
    setShowCommits(status => !status)
  }
 
  return (
    <div className={css.commitsInfo}>
      {lastCommit?.message && lastCommit?.id ? (
        <LastCommit lastCommit={lastCommit} />
      ) : (
        <Text font={{ variation: FontVariation.SMALL }}>{getString('common.noInfo')}</Text>
      )}
      {commits && commits.length > 1 ? (
        <React.Fragment>
          <div className={css.divider} data-show={showCommits} />
          <Button
            minimal
            icon={showCommits ? 'minus' : 'plus'}
            className={css.moreCommits}
            iconProps={{ size: 8 }}
            onClick={toggleCommits}
          >
            <String stringID="ci.moreCommitsLabel" />
          </Button>
 
          <Collapse isOpen={showCommits}>
            {commits.slice(1).map((commit, i) => {
              return (
                <div className={css.commit} key={i}>
                  <div style={{ fontSize: 0 }}>
                    <Text className={css.commitText} title={commit.message} font={{ variation: FontVariation.SMALL }}>
                      {commit.message}
                    </Text>
                  </div>
                  <Container flex>
                    {commit.ownerName && (
                      <UserLabel
                        className={css.user}
                        name={commit.ownerName}
                        email={commit.ownerEmail}
                        profilePictureUrl={authorAvatar}
                        iconProps={{ size: 16 }}
                      />
                    )}
                    <TimeAgoPopover time={commit.timeStamp || 0} inline={false} />
                  </Container>
                  {commit.id && <CommitId commitId={commit.id} commitLink={commit.link} />}
                </div>
              )
            })}
          </Collapse>
        </React.Fragment>
      ) : null}
    </div>
  )
}