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

50% Statements 10/20
0% Branches 0/40
0% Functions 0/7
41.18% Lines 7/17

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                2x       2x 2x     2x                         2x                                                                                 2x                                                                         2x          
/*
 * 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 type { MultiSelectOption, SelectOption } from '@wings-software/uicore'
import { omit } from 'lodash-es'
import type { PipelineFilterProperties, FilterDTO, NGTag } from 'services/pipeline-ng'
 
import type { FilterDataInterface, FilterInterface } from '@common/components/Filter/Constants'
import { StringUtils } from '@common/exports'
import { BUILD_TYPE, getCIModuleProperties } from './PipelineExecutionFilterRequestUtils'
import type { DeploymentTypeContext, BuildTypeContext } from './PipelineExecutionFilterRequestUtils'
 
const exclusionList = [
  'buildType',
  'repositoryName',
  'sourceBranch',
  'targetBranch',
  'tag',
  'branch',
  'services',
  'environments',
  'deploymentType',
  'infrastructureType'
]
 
export const getValidFilterArguments = (formData: Record<string, any>): PipelineFilterProperties => {
  const {
    buildType,
    repositoryName,
    sourceBranch,
    targetBranch,
    branch,
    tag,
    services,
    environments,
    deploymentType,
    infrastructureType,
    pipelineTags
  } = formData
  return Object.assign(omit(formData, ...exclusionList), {
    pipelineTags: Object.keys(pipelineTags || {})?.map((key: string) => {
      return { key, value: pipelineTags[key] } as NGTag
    }),
    moduleProperties: {
      ci: getCIModuleProperties(buildType as BUILD_TYPE, {
        repositoryName,
        sourceBranch,
        targetBranch,
        branch,
        tag
      }),
      cd: {
        deploymentTypes: deploymentType,
        infrastructureTypes: infrastructureType ? [infrastructureType] : undefined,
        serviceNames: services?.map((service: MultiSelectOption) => service?.value),
        environmentNames: environments?.map((env: MultiSelectOption) => env?.value)
      }
    }
  })
}
 
export type PipelineFormType = Omit<PipelineFilterProperties, 'pipelineTags'> & {
  pipelineTags?: Record<string, any>
} & BuildTypeContext &
  DeploymentTypeContext
 
export const createRequestBodyPayload = ({
  isUpdate,
  data,
  projectIdentifier,
  orgIdentifier
}: {
  isUpdate: boolean
  data: FilterDataInterface<PipelineFormType, FilterInterface>
  projectIdentifier: string
  orgIdentifier: string
}): FilterDTO => {
  const {
    metadata: { name: _name, filterVisibility, identifier },
    formValues
  } = data
  const {
    name: _pipelineName,
    pipelineTags: _pipelineTags,
    moduleProperties: _moduleProperties,
    description: _description
  } = getValidFilterArguments(formValues)
  return {
    name: _name,
    identifier: isUpdate ? identifier : StringUtils.getIdentifierFromName(_name),
    filterVisibility: filterVisibility,
    projectIdentifier,
    orgIdentifier,
    filterProperties: {
      filterType: 'PipelineSetup',
      pipelineTags: _pipelineTags || [],
      description: _description,
      name: _pipelineName,
      moduleProperties: _moduleProperties as PipelineFilterProperties['moduleProperties']
    } as PipelineFilterProperties
  }
}
 
export const getMultiSelectFormOptions = (values?: any[]): SelectOption[] | undefined => {
  return values?.map(item => {
    return { label: item.name ?? item, value: item.name ?? item }
  })
}