All files / modules/70-pipeline/components/RunPipelineModal useInputSets.tsx

100% Statements 25/25
100% Branches 61/61
100% Functions 3/3
100% Lines 25/25

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                32x 32x   32x 32x           32x             32x   32x                                                               32x                 216x   216x             216x                               216x     42x                     216x     216x 26x     190x 89x       89x 11x 11x   11x     78x     101x     216x                            
/*
 * 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 type { GetDataError } from 'restful-react'
import { parse } from 'yaml'
import { memoize } from 'lodash-es'
 
import { useMutateAsGet } from '@common/hooks/useMutateAsGet'
import {
  useGetTemplateFromPipeline,
  useGetMergeInputSetFromPipelineTemplateWithListInput,
  ResponseInputSetTemplateWithReplacedExpressionsResponse
} from 'services/pipeline-ng'
import type { Failure, PipelineInfoConfig } from 'services/cd-ng'
import {
  getStageIdentifierFromStageData,
  mergeTemplateWithInputSetData,
  StageSelectionData
} from '@pipeline/utils/runPipelineUtils'
 
import type { InputSetValue } from '../InputSetSelector/utils'
import { clearRuntimeInput } from '../PipelineStudio/StepUtil'
 
const memoizedParse = memoize(parse)
 
export interface Pipeline {
  pipeline?: PipelineInfoConfig
}
 
export interface UseInputSetsProps {
  accountId: string
  projectIdentifier: string
  orgIdentifier: string
  pipelineIdentifier: string
  pipelineExecutionId?: string
  branch?: string
  repoIdentifier?: string
  inputSetSelected?: InputSetValue[]
  rerunInputSetYaml?: string
  selectedStageData: StageSelectionData
}
 
export interface UseInputSetsReturn {
  inputSet: Pipeline
  parsedInputSetTemplateYaml: Pipeline
  inputSetYamlResponse: ResponseInputSetTemplateWithReplacedExpressionsResponse | null
  loading: boolean
  hasInputSets: boolean
  hasRuntimeInputs: boolean
  isInputSetApplied: boolean
  modules?: string[]
  error: GetDataError<Failure | Error> | null
  refetch(): Promise<void> | undefined
}
 
export function useInputSets(props: UseInputSetsProps): UseInputSetsReturn {
  const {
    inputSetSelected,
    rerunInputSetYaml,
    accountId,
    orgIdentifier,
    projectIdentifier,
    pipelineIdentifier,
    selectedStageData
  } = props
 
  const shouldFetchInputSets = !rerunInputSetYaml && Array.isArray(inputSetSelected) && inputSetSelected.length > 0
 
  const {
    data: inputSetYamlResponse,
    loading: loadingTemplate,
    error: templateError,
    refetch
  } = useMutateAsGet(useGetTemplateFromPipeline, {
    body: {
      stageIdentifiers: getStageIdentifierFromStageData(selectedStageData)
    },
    queryParams: {
      accountIdentifier: accountId,
      orgIdentifier,
      projectIdentifier,
      pipelineIdentifier
    }
  })
 
  const {
    data: inputSetData,
    loading: loadingInputSetsData,
    error: inputSetError
  } = useMutateAsGet(useGetMergeInputSetFromPipelineTemplateWithListInput, {
    lazy: !shouldFetchInputSets,
    body: {
      inputSetReferences: inputSetSelected?.map(row => row.value),
      stageIdentifiers: getStageIdentifierFromStageData(selectedStageData)
    },
    queryParams: {
      accountIdentifier: accountId,
      orgIdentifier,
      projectIdentifier,
      pipelineIdentifier
    }
  })
 
  let isInputSetApplied = false
 
  function getInputSet(): Pipeline {
    if (rerunInputSetYaml) {
      return memoizedParse(rerunInputSetYaml)
    }
 
    if (inputSetYamlResponse?.data?.inputSetTemplateYaml) {
      const parsedRunPipelineYaml = clearRuntimeInput(
        memoizedParse(inputSetYamlResponse.data.inputSetTemplateYaml).pipeline
      )
 
      if (shouldFetchInputSets && inputSetData?.data?.pipelineYaml) {
        isInputSetApplied = true
        const parsedInputSets = clearRuntimeInput(memoizedParse(inputSetData.data.pipelineYaml).pipeline)
 
        return mergeTemplateWithInputSetData({ pipeline: parsedRunPipelineYaml }, { pipeline: parsedInputSets })
      }
 
      return { pipeline: parsedRunPipelineYaml }
    }
 
    return {}
  }
 
  return {
    inputSet: getInputSet(),
    loading: loadingTemplate || loadingInputSetsData,
    error: templateError || inputSetError,
    parsedInputSetTemplateYaml: inputSetYamlResponse?.data?.inputSetTemplateYaml
      ? memoizedParse(inputSetYamlResponse.data.inputSetTemplateYaml)
      : {},
    hasRuntimeInputs: !!inputSetYamlResponse?.data?.inputSetTemplateYaml,
    hasInputSets: !!inputSetYamlResponse?.data?.hasInputSets,
    isInputSetApplied,
    inputSetYamlResponse,
    refetch
  }
}