All files / modules/70-pipeline/components/CommonPipelineStages/ApprovalStage ApprovalStageSetupShellMode.tsx

96.3% Statements 52/54
53.54% Branches 53/99
100% Functions 9/9
96.3% Lines 52/54

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              1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x   1x 1x 1x 1x 1x 1x 1x 1x 1x           1x 17x 16x 16x 16x 16x 16x                   16x   16x 16x   16x 3x 3x         8x             2x 2x             16x   13x 13x       16x             16x   3x   3x 3x   3x 3x 3x 3x       3x       3x                 16x       8x                                                                                                                                                            
/*
 * 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, useRef } from 'react'
import { unset } from 'lodash-es'
import YAML from 'yaml'
import produce from 'immer'
import { Button, Icon, Layout, Tab, Tabs } from '@wings-software/uicore'
import { Expander } from '@blueprintjs/core'
import { Color } from '@harness/design-system'
import { usePipelineContext } from '@pipeline/components/PipelineStudio/PipelineContext/PipelineContext'
import { PageSpinner } from '@common/components'
import { useStrings } from 'framework/strings'
import { GetInitialStageYamlSnippetQueryParams, useGetInitialStageYamlSnippet } from 'services/pipeline-ng'
import type { ApprovalStageConfig, StageElementConfig, StageElementWrapperConfig } from 'services/cd-ng'
import { StepType } from '@pipeline/components/PipelineSteps/PipelineStepInterface'
import { SaveTemplateButton } from '@pipeline/components/PipelineStudio/SaveTemplateButton/SaveTemplateButton'
import { useFeatureFlag } from '@common/hooks/useFeatureFlag'
import { FeatureFlag } from '@common/featureFlags'
import { isContextTypeNotStageTemplate } from '@pipeline/components/PipelineStudio/PipelineUtils'
import { ApprovalStageOverview } from './ApprovalStageOverview'
import { ApprovalStageExecution } from './ApprovalStageExecution'
import ApprovalAdvancedSpecifications from './ApprovalStageAdvanced'
import css from './ApprovalStageSetupShellMode.module.scss'
 
interface ApprovalStageElementConfig extends StageElementConfig {
  approvalType?: string
}
 
export function ApprovalStageSetupShellMode(): React.ReactElement {
  const { getString } = useStrings()
  const tabHeadings = [getString('overview'), getString('executionText'), getString('advancedTitle')]
  const isTemplatesEnabled = useFeatureFlag(FeatureFlag.NG_TEMPLATES)
  const layoutRef = useRef<HTMLDivElement>(null)
  const [selectedTabId, setSelectedTabId] = React.useState<string>(tabHeadings[1])
  const pipelineContext = usePipelineContext()
  const {
    state: {
      pipeline,
      selectionState: { selectedStageId = '', selectedStepId }
    },
    contextType,
    getStageFromPipeline,
    updatePipeline,
    updateStage
  } = pipelineContext
 
  const [loadGraph, setLoadGraph] = React.useState(false)
  const { stage: selectedStage } = getStageFromPipeline<ApprovalStageElementConfig>(selectedStageId)
 
  React.useEffect(() => {
    Eif (selectedStepId) {
      setSelectedTabId(tabHeadings[1])
    }
  }, [selectedStepId])
 
  function ActionButtons(): React.ReactElement {
    return (
      <Layout.Horizontal spacing="medium" padding="medium" className={css.footer}>
        <Button
          text={getString('next')}
          intent="primary"
          rightIcon="chevron-right"
          onClick={() => {
            updatePipeline(pipeline)
            setSelectedTabId(tabHeadings[1])
          }}
        />
      </Layout.Horizontal>
    )
  }
 
  React.useEffect(() => {
    // To drag and drop the canvas
    Eif (layoutRef.current) {
      layoutRef.current.scrollTo?.(0, 0)
    }
  }, [selectedTabId])
 
  const { data: yamlSnippet } = useGetInitialStageYamlSnippet({
    queryParams: {
      approvalType: (selectedStage?.stage?.approvalType ||
        StepType.HarnessApproval) as GetInitialStageYamlSnippetQueryParams['approvalType']
    }
  })
 
  useEffect(() => {
    // error handling if needed
    Eif (yamlSnippet?.data) {
      // The last part of condition is important, as we only need to add the YAML snippet the first time in the step.
      Eif (!selectedStage?.stage?.spec?.execution) {
        updateStage(
          produce(selectedStage as StageElementWrapperConfig, draft => {
            const jsonFromYaml = YAML.parse(yamlSnippet?.data || '') as ApprovalStageElementConfig
            Eif (draft?.stage && draft?.stage?.spec) {
              draft.stage.failureStrategies = jsonFromYaml.failureStrategies
              ;(draft.stage.spec as ApprovalStageConfig).execution =
                (jsonFromYaml.spec as ApprovalStageConfig)?.execution || {}
              // approvalType is just used in the UI, to populate the default steps for different approval types
              // For BE, the stage type is always 'Approval' and approval type is defined inside the step
              unset(draft.stage as ApprovalStageElementConfig, 'approvalType')
            }
          }).stage as ApprovalStageElementConfig
        ).then(() => {
          setLoadGraph(true)
        })
      } else if (selectedStage?.stage?.spec?.execution) {
        // We're opening an already added approval stage
        setLoadGraph(true)
      }
    }
  }, [yamlSnippet?.data])
 
  return (
    <section ref={layoutRef} key={selectedStageId} className={css.approvalStageSetupShellWrapper}>
      <Tabs
        id="approvalStageSetupShell"
        onChange={(tabId: string) => setSelectedTabId(tabId)}
        selectedTabId={selectedTabId}
        data-tabId={selectedTabId}
      >
        <Tab
          id={tabHeadings[0]}
          panel={
            <ApprovalStageOverview>
              <ActionButtons />
            </ApprovalStageOverview>
          }
          title={
            <span className={css.tab}>
              <Icon name="tick" height={20} size={20} color={Color.GREEN_800} />
              {tabHeadings[0]}
            </span>
          }
          data-testid={tabHeadings[0]}
        />
        <Icon
          name="chevron-right"
          height={20}
          size={20}
          margin={{ right: 'small', left: 'small' }}
          color={'grey400'}
          style={{ alignSelf: 'center' }}
        />
        <Tab
          id={tabHeadings[1]}
          title={
            <span className={css.tab}>
              <Icon name="deployment-success-legacy" height={20} size={20} />
              {tabHeadings[1]}
            </span>
          }
          panel={
            <>
              {loadGraph ? (
                <ApprovalStageExecution />
              ) : (
                <PageSpinner
                  className={css.graphLoadingSpinner}
                  message={getString('pipeline.approvalStage.settingUpStage')}
                />
              )}
            </>
          }
          data-testid={tabHeadings[1]}
        />
        <Icon
          name="chevron-right"
          height={20}
          size={20}
          margin={{ right: 'small', left: 'small' }}
          color={'grey400'}
          style={{ alignSelf: 'center' }}
        />
        <Tab
          id={tabHeadings[2]}
          title={
            <span className={css.tab}>
              <Icon name="advanced" height={20} size={20} />
              {tabHeadings[2]}
            </span>
          }
          panel={<ApprovalAdvancedSpecifications />}
          data-testid={tabHeadings[2]}
        />
        {isTemplatesEnabled && isContextTypeNotStageTemplate(contextType) && selectedStage?.stage && (
          <>
            <Expander />
            <SaveTemplateButton data={selectedStage?.stage} type={'Stage'} />
          </>
        )}
      </Tabs>
    </section>
  )
}