All files / modules/70-pipeline/components/execution/StepDetails/views/HarnessApprovalView HarnessApprovalView.tsx

100% Statements 34/34
87.5% Branches 21/24
100% Functions 5/5
100% Lines 34/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 158 159 160 161 162 163              1x 1x 1x 1x   1x   1x           1x 1x   1x       1x 1x 1x 1x                         1x 11x 11x 11x 11x       11x             11x                   11x           11x 5x 1x       11x 5x     11x 2x             9x 4x             5x                           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, { useEffect } from 'react'
import { Spinner, Tabs } from '@blueprintjs/core'
import { Button, Layout, PageError } from '@wings-software/uicore'
import { get, merge } from 'lodash-es'
 
import { useStrings } from 'framework/strings'
import type { StepDetailProps } from '@pipeline/factories/ExecutionFactory/types'
import {
  ResponseApprovalInstanceResponse,
  ResponseHarnessApprovalInstanceAuthorization,
  useGetApprovalInstance,
  useGetHarnessApprovalInstanceAuthorization
} from 'services/pipeline-ng'
import { useDeepCompareEffect } from '@common/hooks'
import { isExecutionWaiting } from '@pipeline/utils/statusHelpers'
 
import {
  HarnessApprovalTab,
  ApprovalData
} from '@pipeline/components/execution/StepDetails/tabs/HarnessApprovalTab/HarnessApprovalTab'
import { PipelineDetailsTab } from '@pipeline/components/execution/StepDetails/tabs/PipelineDetailsTab/PipelineDetailsTab'
import { InputOutputTab } from '@pipeline/components/execution/StepDetails/tabs/InputOutputTab/InputOutputTab'
import { NoApprovalInstance } from '../NoApprovalInstanceCreated'
import tabCss from '../DefaultView/DefaultView.module.scss'
 
export interface HarnessApprovalViewProps extends StepDetailProps {
  mock?: {
    data?: ResponseApprovalInstanceResponse
    loading?: boolean
  }
  getApprovalAuthorizationMock?: {
    loading: boolean
    data: ResponseHarnessApprovalInstanceAuthorization
  }
}
 
export function HarnessApprovalView(props: HarnessApprovalViewProps): React.ReactElement {
  const { step, mock, getApprovalAuthorizationMock } = props
  const approvalInstanceId = get(step, 'executableResponses[0].async.callbackIds[0]') || ''
  const isWaiting = isExecutionWaiting(step.status)
  const { getString } = useStrings()
 
  // store the data in state because the approve/reject call returns the updated state
  // hence we can save one additional call to the server
  const [approvalData, setApprovalData] = React.useState<ApprovalData>(null)
 
  const {
    data,
    refetch,
    loading: loadingApprovalData,
    error
  } = useGetApprovalInstance({
    approvalInstanceId,
    mock,
    lazy: true
  })
 
  const {
    data: authData,
    refetch: refetchAuthData,
    loading: loadingAuthData
  } = useGetHarnessApprovalInstanceAuthorization({
    approvalInstanceId,
    lazy: !isWaiting,
    mock: getApprovalAuthorizationMock
  })
 
  useEffect(() => {
    if (approvalInstanceId) {
      refetch()
    }
  }, [approvalInstanceId])
 
  useDeepCompareEffect(() => {
    setApprovalData(data?.data as ApprovalData)
  }, [data])
 
  if (error) {
    return (
      <Layout.Vertical height="100%">
        <PageError message={(error.data as Error)?.message || error.message} />
      </Layout.Vertical>
    )
  }
 
  if (loadingApprovalData || loadingAuthData) {
    return (
      <Layout.Vertical height="100%" flex={{ alignItems: 'center', justifyContent: 'center' }}>
        <Spinner />
      </Layout.Vertical>
    )
  }
 
  return (
    <Tabs id="step-details" className={tabCss.tabs} renderActiveTabPanelOnly>
      <Tabs.Tab
        key="Approval"
        id="Approval"
        title={getString('approvalStage.title')}
        panel={
          approvalInstanceId ? (
            <HarnessApprovalTab
              approvalInstanceId={approvalInstanceId}
              approvalData={approvalData}
              isWaiting={isWaiting}
              authData={authData}
              updateState={updatedData => {
                setApprovalData(updatedData)
                refetchAuthData()
              }}
              stepParameters={step.stepParameters}
            />
          ) : (
            <NoApprovalInstance />
          )
        }
      />
      <Tabs.Tab
        id="PipelineDetails"
        key="PipelineDetails"
        title={getString('common.pipelineDetails')}
        panel={<PipelineDetailsTab />}
      />
      <Tabs.Tab
        id="Input"
        key="Input"
        title={getString('common.input')}
        panel={<InputOutputTab baseFqn={step.baseFqn} mode="input" data={step.stepParameters} />}
      />
      <Tabs.Tab
        id="Output"
        key="Output"
        title={getString('outputLabel')}
        panel={
          <InputOutputTab
            baseFqn={step.baseFqn}
            mode="output"
            data={Array.isArray(step.outcomes) ? { output: merge({}, ...step.outcomes) } : step.outcomes}
          />
        }
      />
      <Tabs.Expander />
      <Button
        minimal
        intent="primary"
        icon="refresh"
        iconProps={{ size: 12, style: { marginRight: 'var(--spacing-2)' } }}
        style={{ transform: 'translateY(-5px)' }}
        onClick={() => refetch()}
      >
        {getString('common.refresh')}
      </Button>
    </Tabs>
  )
}