All files / modules/70-pipeline/components/PredefinedOverrideSets PredefinedOverrideSets.tsx

64.58% Statements 31/48
50% Branches 23/46
62.5% Functions 5/8
65.96% Lines 31/47

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              100x 100x 100x 100x     100x                       100x       3x   3x 3x     3x 3x 3x 3x 1x   2x 1x   1x 1x         3x                                                   3x 3x   3x 3x 2x 2x 2x 4x       2x 1x 1x         3x                                  
/*
 * 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 from 'react'
import { Layout, Text, SelectOption } from '@wings-software/uicore'
import { isArray } from 'lodash-es'
import { usePipelineContext } from '@pipeline/components/PipelineStudio/PipelineContext/PipelineContext'
// This file needs to remove or moved
// eslint-disable-next-line no-restricted-imports
import {
  OverrideSetsInputSelector,
  InputSetSelectorProps
} from '@cd/components/OverrideSetsInputSelector/OverrideSetsInputSelector'
import type { DeploymentStageElementConfig, StageElementWrapper } from '@pipeline/utils/pipelineTypes'
import type { StageOverridesConfig } from 'services/cd-ng'
 
export interface PredefinedOverrideSetsProps {
  currentStage: StageElementWrapper<DeploymentStageElementConfig> | undefined
  context: string
}
 
export function PredefinedOverrideSets({ currentStage, context }: PredefinedOverrideSetsProps): React.ReactElement {
  const {
    state: { pipeline },
    updatePipeline
  } = usePipelineContext()
 
  const getCurrentSpec = (): StageOverridesConfig | undefined => {
    return currentStage?.stage?.spec?.serviceConfig?.stageOverrides
  }
 
  const getValuesByContext = () => {
    const path = getCurrentSpec()
    Eif (path) {
      if (context == 'ARTIFACT') {
        return path.useArtifactOverrideSets
      }
      if (context == 'MANIFEST') {
        return path.useManifestOverrideSets
      }
      Eif (context == 'VARIABLES') {
        return path.useVariableOverrideSets
      }
    }
  }
 
  const setOverrideSetsValue = (overrideSet: InputSetSelectorProps['value']): void => {
    let _stageOverridesValues: any[] = []
 
    if (!isArray(overrideSet) && overrideSet) {
      _stageOverridesValues = [overrideSet.value]
    } else if (overrideSet) {
      _stageOverridesValues = overrideSet.map(v => v.value)
    } else {
      _stageOverridesValues = []
    }
    const currentPath = getCurrentSpec()
    if (currentPath) {
      if (context == 'ARTIFACT') {
        currentPath.useArtifactOverrideSets = _stageOverridesValues
      }
      if (context == 'MANIFEST') {
        currentPath.useManifestOverrideSets = _stageOverridesValues
      }
      if (context == 'VARIABLES') {
        currentPath.useVariableOverrideSets = _stageOverridesValues
      }
 
      updatePipeline(pipeline)
    }
  }
 
  const getSelectedOverrideSetValues = (): InputSetSelectorProps['value'] => {
    const currentValues = getValuesByContext()
 
    Eif (currentValues) {
      if (currentValues.length > 1) {
        const _options: SelectOption[] = []
        Eif (_options) {
          currentValues.forEach((overrideSetName: string) => {
            _options.push({ label: overrideSetName, value: overrideSetName })
          })
        }
 
        return _options
      } else Eif (currentValues.length === 1) {
        return { label: currentValues[0], value: currentValues[0] }
      }
    }
  }
 
  return (
    <Layout.Horizontal
      flex={true}
      style={{ alignItems: 'center', justifyContent: 'end', marginBottom: 'var(--spacing-large)' }}
      spacing="medium"
    >
      <Text>Use Predefined Override Sets</Text>
      <OverrideSetsInputSelector
        context={context}
        value={getSelectedOverrideSetValues()}
        onChange={(value: InputSetSelectorProps['value']) => {
          setOverrideSetsValue(value)
        }}
      />
    </Layout.Horizontal>
  )
}