All files / modules/70-pipeline/components/RetryPipeline/RetryHistory RetryHistory.tsx

39.02% Statements 16/41
0% Branches 0/56
0% Functions 0/10
39.02% Lines 16/41

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 224 225 226 227 228 229 230 231              2x 2x                   2x 2x 2x 2x 2x 2x 2x   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, { useEffect } from 'react'
import {
  Text,
  Button,
  Card,
  Layout,
  PageSpinner,
  ButtonVariation,
  useToaster,
  ButtonSize
} from '@wings-software/uicore'
import { Classes, Popover, PopoverInteractionKind, Position } from '@blueprintjs/core'
import cx from 'classnames'
import { Color } from '@harness/design-system'
import { useHistory, useParams } from 'react-router-dom'
import { defaultTo } from 'lodash-es'
import { useStrings } from 'framework/strings'
import { ExecutionInfo, useLatestExecutionId, useRetryHistory } from 'services/pipeline-ng'
import type { ExecutionPathProps, PipelineType } from '@common/interfaces/RouteInterfaces'
import { formatDatetoLocale } from '@common/utils/dateUtils'
import { TimeAgoPopover } from '@common/components'
import ExecutionStatusLabel from '@pipeline/components/ExecutionStatusLabel/ExecutionStatusLabel'
import type { ExecutionStatus } from '@pipeline/utils/statusHelpers'
import routes from '@common/RouteDefinitions'
import RbacButton from '@rbac/components/Button/Button'
import css from './RetryHistory.module.scss'
 
interface RetryHistoryProps {
  canExecute: boolean
  showRetryHistory: boolean
  canRetry: boolean
}
 
function RetryHistory({ canExecute, showRetryHistory, canRetry }: RetryHistoryProps): React.ReactElement {
  const { getString } = useStrings()
  const { projectIdentifier, orgIdentifier, pipelineIdentifier, accountId, executionIdentifier, module } =
    useParams<PipelineType<ExecutionPathProps>>()
  const history = useHistory()
  const { clear, showPrimary } = useToaster()
 
  const { data: latestExecutionId, refetch: refetchLatestExecutionId } = useLatestExecutionId({
    planExecutionId: executionIdentifier,
    queryParams: {
      orgIdentifier,
      pipelineIdentifier: pipelineIdentifier,
      projectIdentifier,
      accountIdentifier: accountId
    },
    lazy: true
  })
  useEffect(() => {
    if (latestExecutionId?.data?.latestExecutionId) {
      clear()
      history.push(
        routes.toExecutionPipelineView({
          orgIdentifier,
          pipelineIdentifier: pipelineIdentifier,
          projectIdentifier,
          executionIdentifier: latestExecutionId.data.latestExecutionId || '',
          accountId,
          module
        })
      )
    }
  }, [latestExecutionId])
 
  useEffect(() => {
    if (showRetryHistory && !canRetry) {
      showPrimary(
        <Layout.Horizontal spacing="medium">
          <Text color={Color.WHITE} margin={{ left: 'small' }}>
            {getString('pipeline.viewLatestExecution')}
          </Text>
          <Text
            color={Color.WHITE}
            font={{ weight: 'bold' }}
            className={css.viewLatest}
            onClick={() => refetchLatestExecutionId()}
          >
            {getString('common.viewLatest')}
          </Text>
        </Layout.Horizontal>,
        0
      )
    }
  }, [showRetryHistory, canRetry])
 
  const {
    data: retryHistoryResponse,
    loading: loadingRetryHistory,
    refetch: refetchRetryHistory
  } = useRetryHistory({
    planExecutionId: executionIdentifier,
    queryParams: {
      pipelineIdentifier: pipelineIdentifier,
      accountIdentifier: accountId,
      orgIdentifier,
      projectIdentifier
    },
    lazy: true
  })
  const executionInfo = retryHistoryResponse?.data?.executionInfos
 
  const gotoExecutionDetails = (planExecutionId: string): void => {
    if (planExecutionId !== executionIdentifier) {
      history.push(
        routes.toExecutionPipelineView({
          orgIdentifier,
          pipelineIdentifier: pipelineIdentifier,
          projectIdentifier,
          executionIdentifier: planExecutionId || '',
          accountId,
          module
        })
      )
    }
  }
 
  const getExecutionDetail = (index: number): JSX.Element => {
    return (
      <Text color={Color.PRIMARY_7} font={{ size: 'normal', weight: 'semi-bold' }}>
        {index === 0
          ? `${getString('pipeline.recentExecutionText')} ${executionInfo?.length}/${executionInfo?.length}`
          : `${getString('executionText')} ${(executionInfo as ExecutionInfo[])?.length - index}/${
              executionInfo?.length
            }`}
      </Text>
    )
  }
 
  function RetryExecutionList(): JSX.Element {
    return (
      <div className={css.modalContent}>
        <Layout.Vertical>
          <div className={css.retryHeaderSection}>
            <div className={css.retryModalHeader}>
              <Text
                icon="execution-history"
                iconProps={{ size: 24 }}
                style={{ fontSize: 20 }}
                font={{ weight: 'bold' }}
                color={Color.GREY_700}
              >
                {getString('pipeline.retryHistory')}
              </Text>
            </div>
          </div>
          <div>
            {loadingRetryHistory ? (
              <PageSpinner />
            ) : (
              <>
                <Text color={Color.GREY_800} font={{ size: 'normal' }} margin="medium">
                  {getString('pipeline.retryHistoryDescription')}
                </Text>
                {executionInfo?.map((retryHistory, index) => {
                  return (
                    <Card
                      elevation={0}
                      className={cx(css.card, css.hoverCard, Classes.POPOVER_DISMISS)}
                      key={retryHistory.uuid}
                      onClick={() => gotoExecutionDetails(retryHistory?.uuid as string)}
                    >
                      <div className={css.content}>
                        <div className={cx(css.cardSection, css.executionDetail)}>
                          {getExecutionDetail(index)}
                          <ExecutionStatusLabel status={retryHistory.status as ExecutionStatus} />
                        </div>
                        <div className={cx(css.cardSection)}>
                          <Text
                            color={Color.GREY_400}
                            font={{ size: 'small', weight: 'light' }}
                            icon="calendar"
                            iconProps={{ size: 12 }}
                          >
                            {formatDatetoLocale(retryHistory.startTs as number)}
                          </Text>
                          <div>
                            <TimeAgoPopover
                              iconProps={{ size: 12, className: css.timerIcon }}
                              icon="time"
                              time={defaultTo(retryHistory.startTs, 0)}
                              inline={false}
                              className={css.timeAgo}
                            />
                          </div>
                        </div>
                      </div>
                    </Card>
                  )
                })}
              </>
            )}
          </div>
        </Layout.Vertical>
        <Button minimal icon="cross" className={cx(css.crossIcon, Classes.POPOVER_DISMISS)} />
      </div>
    )
  }
 
  return (
    <Popover
      interactionKind={PopoverInteractionKind.CLICK}
      position={Position.BOTTOM_RIGHT}
      content={<RetryExecutionList />}
      popoverClassName={css.retryPopover}
      autoFocus
    >
      <RbacButton
        icon="execution-history"
        text={getString('pipeline.retryHistory')}
        variation={ButtonVariation.SECONDARY}
        size={ButtonSize.SMALL}
        iconProps={{ size: 24, color: Color.PRIMARY_7 }}
        onClick={() => refetchRetryHistory()}
        disabled={!canExecute}
        className={cx(css.cardBtns, css.retryHistoryBtn)}
      />
    </Popover>
  )
}
 
export default RetryHistory