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

34.62% Statements 9/26
0% Branches 0/51
0% Functions 0/5
34.62% Lines 9/26

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              8x 8x 8x 8x   8x 8x 8x 8x             8x                                                                                    
/*
 * 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 { PipelineInfoConfig } from 'services/cd-ng'
import { usePipelineContext } from '@pipeline/components/PipelineStudio/PipelineContext/PipelineContext'
import { useTemplateSelector } from '@pipeline/utils/useTemplateSelector'
import { createTemplate } from '@pipeline/utils/templateUtils'
import { getIdentifierFromValue } from '@common/components/EntityReference/EntityReference'
 
interface TemplateActionsReturnType {
  addOrUpdateTemplate: () => Promise<void>
  removeTemplate: () => Promise<void>
}
 
export function usePipelineTemplateActions(): TemplateActionsReturnType {
  const {
    state: { pipeline },
    updatePipeline
  } = usePipelineContext()
  const { getTemplate } = useTemplateSelector()
 
  const addOrUpdateTemplate = useCallback(async () => {
    const { template, isCopied } = await getTemplate({
      templateType: 'Pipeline',
      ...(pipeline.template && {
        selectedTemplateRef: getIdentifierFromValue(pipeline.template.templateRef),
        selectedVersionLabel: pipeline.template.versionLabel
      })
    })
    if (
      !isCopied &&
      isEqual(pipeline?.template?.templateRef, template.identifier) &&
      isEqual(pipeline?.template?.versionLabel, template.versionLabel)
    ) {
      return
    }
    const processNode = isCopied
      ? produce(defaultTo(parse(template?.yaml || '')?.template.spec, {}) as PipelineInfoConfig, draft => {
          draft.name = defaultTo(pipeline?.name, '')
          draft.identifier = defaultTo(pipeline?.identifier, '')
        })
      : createTemplate(pipeline, template)
    await updatePipeline(processNode)
  }, [pipeline.template, getTemplate, updatePipeline])
 
  const removeTemplate = useCallback(async () => {
    const node = pipeline
    const processNode = produce({} as PipelineInfoConfig, draft => {
      draft.name = defaultTo(node?.name, '')
      draft.identifier = defaultTo(node?.identifier, '')
    })
    await updatePipeline(processNode)
  }, [pipeline, updatePipeline])
 
  return { addOrUpdateTemplate, removeTemplate }
}