All files / modules/70-pipeline/utils useStageTemplateActions.ts

90% Statements 27/30
47.5% Branches 38/80
80% Functions 4/5
90% Lines 27/30

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              3x 3x 3x 3x   3x 3x 3x 3x             3x               7x 7x 7x   7x 1x 1x               1x 1x             1x           1x           7x 1x 1x 1x 1x 1x   1x     7x    
/*
 * Copyright 2022 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 { defaultTo, isEqual } from 'lodash-es'
import { parse } from 'yaml'
import produce from 'immer'
import { useCallback } from 'react'
import type { StageElementConfig } from 'services/cd-ng'
import { usePipelineContext } from '@pipeline/components/PipelineStudio/PipelineContext/PipelineContext'
import { useTemplateSelector } from '@pipeline/utils/useTemplateSelector'
import { createTemplate, getStageType } from '@pipeline/utils/templateUtils'
import { getIdentifierFromValue } from '@common/components/EntityReference/EntityReference'
 
interface TemplateActionsReturnType {
  addOrUpdateTemplate: () => Promise<void>
  removeTemplate: () => Promise<void>
}
 
export function useStageTemplateActions(): TemplateActionsReturnType {
  const {
    state: {
      selectionState: { selectedStageId = '' },
      templateTypes
    },
    updateStage,
    getStageFromPipeline
  } = usePipelineContext()
  const { stage } = getStageFromPipeline(selectedStageId)
  const { getTemplate } = useTemplateSelector()
 
  const addOrUpdateTemplate = useCallback(async () => {
    try {
      const { template, isCopied } = await getTemplate({
        templateType: 'Stage',
        selectedChildType: getStageType(stage?.stage, templateTypes),
        ...(stage?.stage?.template && {
          selectedTemplateRef: getIdentifierFromValue(stage.stage.template.templateRef),
          selectedVersionLabel: stage.stage.template.versionLabel
        })
      })
      const node = stage?.stage
      Iif (
        !isCopied &&
        isEqual(node?.template?.templateRef, template.identifier) &&
        isEqual(node?.template?.versionLabel, template.versionLabel)
      ) {
        return
      }
      const processNode = isCopied
        ? produce(defaultTo(parse(template?.yaml || '')?.template.spec, {}) as StageElementConfig, draft => {
            draft.name = defaultTo(node?.name, '')
            draft.identifier = defaultTo(node?.identifier, '')
          })
        : createTemplate(node, template)
      await updateStage(processNode)
    } catch (_) {
      // Do nothing.. user cancelled template selection
    }
  }, [stage?.stage, templateTypes, getTemplate, updateStage])
 
  const removeTemplate = useCallback(async () => {
    const node = stage?.stage
    const processNode = produce({} as StageElementConfig, draft => {
      draft.name = defaultTo(node?.name, '')
      draft.identifier = defaultTo(node?.identifier, '')
      draft.type = getStageType(node, templateTypes)
    })
    await updateStage(processNode)
  }, [stage?.stage, templateTypes, updateStage])
 
  return { addOrUpdateTemplate, removeTemplate }
}