All files / modules/70-pipeline/pages/execution/ExecutionPipelineView/ExecutionLogView ExecutionLogView.tsx

91.43% Statements 32/35
67.65% Branches 23/34
87.5% Functions 7/8
91.18% Lines 31/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              2x 2x 2x 2x   2x 2x 2x 2x   2x 2x   2x     2x 2x   2x                 3x 3x 3x 3x                             3x 2x     3x 8x               3x   3x 3x     1x             3x   3x 2x             3x                 18x                                              
/*
 * 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 { Container } from '@wings-software/uicore'
import { useParams } from 'react-router-dom'
import { defaultTo, get, has } from 'lodash-es'
 
import { useUpdateQueryParams } from '@common/hooks'
import { processExecutionData } from '@pipeline/utils/executionUtils'
import { useExecutionContext } from '@pipeline/context/ExecutionContext'
import { StageSelection, StageSelectOption } from '@pipeline/components/StageSelection/StageSelection'
import type { ExecutionPageQueryParams } from '@pipeline/utils/types'
import factory from '@pipeline/factories/ExecutionFactory'
import { isExecutionNotStarted, isExecutionSkipped } from '@pipeline/utils/statusHelpers'
import type { StepType } from '@pipeline/components/PipelineSteps/PipelineStepInterface'
import { useGetExecutionNode } from 'services/pipeline-ng'
import type { ExecutionPathProps } from '@common/interfaces/RouteInterfaces'
import type { ConsoleViewStepDetailProps } from '@pipeline/factories/ExecutionFactory/types'
import { StepsTree } from './StepsTree/StepsTree'
import css from './ExecutionLogView.module.scss'
 
export default function ExecutionLogView(): React.ReactElement {
  const {
    pipelineStagesMap,
    allNodeMap,
    pipelineExecutionDetail,
    selectedStageId,
    selectedStepId,
    queryParams,
    addNewNodeToMap
  } = useExecutionContext()
  const { accountId, projectIdentifier, orgIdentifier } = useParams<ExecutionPathProps>()
  const { updateQueryParams } = useUpdateQueryParams<ExecutionPageQueryParams>()
  const { data: executionNode, loading } = useGetExecutionNode({
    queryParams: {
      accountIdentifier: accountId,
      projectIdentifier,
      orgIdentifier,
      nodeExecutionId: defaultTo(queryParams.retryStep, '')
    },
    /**
     * Do not fetch data:
     * 1. No retry step
     * 2. Already have data for it
     */
    lazy: !queryParams.retryStep || has(allNodeMap, queryParams.retryStep)
  })
 
  const tree = React.useMemo(
    () => processExecutionData(pipelineExecutionDetail?.executionGraph),
    [pipelineExecutionDetail?.executionGraph]
  )
  const selectOptions: StageSelectOption[] = React.useMemo(() => {
    return [...pipelineStagesMap.entries()].map(([identifier, stage]) => ({
      label: defaultTo(stage?.name, ''),
      value: identifier,
      node: stage,
      type: defaultTo(stage.nodeType, '')
    }))
  }, [pipelineStagesMap])
 
  const selectedStep = allNodeMap[selectedStepId]
  const errorMessage =
    get(selectedStep, 'failureInfo.message') || get(selectedStep, 'executableResponses[0].skipTask.message')
  const isSkipped = isExecutionSkipped(selectedStep?.status)
 
  function handleStageChange(item: StageSelectOption): void {
    updateQueryParams({ stage: item.value as string })
  }
 
  function handleStepSelect(stepId: string, retryStep?: string): void {
    updateQueryParams({ step: stepId, stage: selectedStageId, retryStep })
  }
 
  const stepDetails = factory.getConsoleViewStepDetails(selectedStep?.stepType as StepType)
 
  React.useEffect(() => {
    Iif (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])
 
  return (
    <Container className={css.logsContainer}>
      <div className={css.stepsContainer}>
        <StageSelection
          selectOptions={selectOptions}
          onStageChange={handleStageChange}
          selectedStageId={selectedStageId}
          className={css.stageSelector}
          chevronIcon="chevron-down"
          itemDisabled={item => isExecutionNotStarted(item.node.status)}
          popoverProps={{ popoverClassName: css.stageSelectionMenu }}
        />
        <div className={css.stageTree}>
          <StepsTree
            nodes={tree}
            selectedStepId={selectedStepId}
            onStepSelect={handleStepSelect}
            retryStep={queryParams.retryStep}
            allNodeMap={allNodeMap}
            isRoot
          />
        </div>
      </div>
      {React.createElement<ConsoleViewStepDetailProps>(stepDetails.component, {
        step: selectedStep,
        errorMessage,
        isSkipped,
        loading
      })}
    </Container>
  )
}