All files / modules/70-pipeline/components/ExecutionStepDetails ExecutionStepDetails.tsx

94.64% Statements 53/56
83.58% Branches 56/67
100% Functions 8/8
96.23% Lines 51/53

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              3x 3x 3x 3x 3x   3x 3x   3x   3x 3x 3x 3x 3x     3x   3x   9x 9x 9x 9x 9x 9x                           9x 9x 9x 14x 9x   9x   9x 3x 3x           9x 8x     1x       1x     9x 6x 1x 1x         9x 9x   9x 1x 1x 8x 1x 1x 7x         9x   9x                                                 14x           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 from 'react'
import { Button, IconName } from '@wings-software/uicore'
import { Popover, Menu, Spinner } from '@blueprintjs/core'
import { has, defaultTo } from 'lodash-es'
import { useParams } from 'react-router-dom'
 
import { useExecutionContext } from '@pipeline/context/ExecutionContext'
import ExecutionLayoutToggle from '@pipeline/components/ExecutionLayout/ExecutionLayoutToggle'
import type { ExecutionPageQueryParams } from '@pipeline/utils/types'
import { useUpdateQueryParams } from '@common/hooks'
import type { ExecutionPathProps } from '@common/interfaces/RouteInterfaces'
import { useGetExecutionNode } from 'services/pipeline-ng'
import { useStrings } from 'framework/strings'
import factory from '@pipeline/factories/ExecutionFactory'
import { isCDStage, isCIStage, StageType } from '@pipeline/utils/stageHelpers'
import { isExecutionCompletedWithBadState, isExecutionRunning, isExecutionSuccess } from '@pipeline/utils/statusHelpers'
 
import type { StepType } from '../PipelineSteps/PipelineStepInterface'
import css from './ExecutionStepDetails.module.scss'
 
export default function ExecutionStepDetails(): React.ReactElement {
  const { allNodeMap, addNewNodeToMap, queryParams, selectedStepId, selectedStageId, pipelineStagesMap } =
    useExecutionContext()
  const { retryStep } = queryParams
  const { getString } = useStrings()
  const { updateQueryParams } = useUpdateQueryParams<ExecutionPageQueryParams>()
  const { accountId, projectIdentifier, orgIdentifier } = useParams<ExecutionPathProps>()
  const { data: executionNode, loading } = useGetExecutionNode({
    queryParams: {
      accountIdentifier: accountId,
      projectIdentifier,
      orgIdentifier,
      nodeExecutionId: defaultTo(retryStep, '')
    },
    /**
     * Do not fetch data:
     * 1. No retry step
     * 2. Already have data for it
     */
    lazy: !retryStep || has(allNodeMap, retryStep)
  })
  const originalStep = defaultTo(allNodeMap?.[selectedStepId], {})
  const selectedStep = defaultTo(retryStep ? allNodeMap[retryStep] : originalStep, {})
  const stepDetails = factory.getStepDetails(selectedStep.stepType as StepType)
  const interruptHistories = defaultTo(originalStep.interruptHistories, []).filter(row => row.interruptType === 'RETRY')
  const selectedStage = pipelineStagesMap.get(selectedStageId)
 
  let retryCount = interruptHistories.length
 
  if (retryStep) {
    retryCount = interruptHistories.findIndex(
      ({ interruptConfig }) => interruptConfig?.retryInterruptConfig?.retryId === retryStep
    )
  }
 
  let stageType: StageType | undefined
 
  if (isCDStage(selectedStage)) stageType = StageType.DEPLOY
  else Iif (isCIStage(selectedStage)) stageType = StageType.BUILD
 
  function goToRetryStepExecution(id: string): void {
    updateQueryParams({ retryStep: id })
  }
 
  function goToCurrentExecution(): void {
    updateQueryParams({ retryStep: [] as unknown as string /* this removes the param fro query */ })
  }
 
  React.useEffect(() => {
    if (executionNode?.data) {
      Object.assign(executionNode.data, { __isInterruptNode: true })
      addNewNodeToMap(defaultTo(executionNode.data.uuid, ''), executionNode.data)
    }
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [executionNode?.data])
 
  let leftRetryIcon: IconName | undefined = undefined
  let retryIconStatus = ''
 
  if (isExecutionSuccess(selectedStep?.status)) {
    leftRetryIcon = 'tick-circle'
    retryIconStatus = 'success'
  } else if (isExecutionRunning(selectedStep?.status)) {
    leftRetryIcon = 'steps-spinner'
    retryIconStatus = 'running'
  } else Iif (isExecutionCompletedWithBadState(selectedStep?.status)) {
    leftRetryIcon = 'circle-cross'
    retryIconStatus = 'failed'
  }
 
  const StepDetails = stepDetails.component
 
  return (
    <div className={css.main}>
      <div className={css.header}>
        <div className={css.title}>{selectedStep.name}</div>
        <div className={css.actions}>
          {interruptHistories.length > 0 ? (
            <Popover
              wrapperTagName="div"
              targetTagName="div"
              minimal
              position="bottom-left"
              popoverClassName={css.retryMenu}
            >
              <Button
                minimal
                className={css.retry}
                data-testid="retry-logs"
                data-status={retryIconStatus}
                icon={leftRetryIcon}
                iconProps={{ size: 12, className: css.retryStatusIcon }}
                rightIcon="chevron-down"
              >
                {getString('pipeline.execution.retryStepCount', { num: retryCount + 1 })}
              </Button>
              <Menu>
                {interruptHistories.map(({ interruptId, interruptConfig }, i) => (
                  <Menu.Item
                    active={retryStep === interruptConfig?.retryInterruptConfig?.retryId}
                    key={interruptId}
                    text={getString('pipeline.execution.retryStepCount', { num: i + 1 })}
                    onClick={() =>
                      goToRetryStepExecution(
                        interruptConfig.retryInterruptConfig?.retryId || /* istanbul ignore next */ ''
                      )
                    }
                  />
                ))}
                <Menu.Item
                  active={!retryStep}
                  text={getString('pipeline.execution.retryStepCount', { num: interruptHistories.length + 1 })}
                  onClick={goToCurrentExecution}
                />
              </Menu>
            </Popover>
          ) : null}
          <ExecutionLayoutToggle />
        </div>
      </div>
      {loading ? <Spinner /> : <StepDetails step={selectedStep} stageType={stageType} />}
    </div>
  )
}