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

85.19% Statements 46/54
54.22% Branches 45/83
60% Functions 9/15
84.44% Lines 38/45

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 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195                11x     11x   11x                                     11x                       11x                         1x 1x                                                 11x   21x             21x           11x 1x 21x 21x       11x                                                             11x 4x 4x 4x   2x           2x   1x     1x   1x     1x     4x     11x 11x 11x 11x     11x         46x 46x   46x                 11x 233x 96x       11x    
/*
 * 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, startCase } from 'lodash-es'
import type { PipelineExecutionFilterProperties, FilterDTO } from 'services/pipeline-ng'
 
import { EXECUTION_STATUS } from '@pipeline/utils/statusHelpers'
import type { FilterDataInterface, FilterInterface } from '@common/components/Filter/Constants'
import { StringUtils } from '@common/exports'
import type { CIWebhookInfoDTO } from 'services/ci'
 
export interface DeploymentTypeContext {
  deploymentType?: MultiSelectOption[]
  infrastructureType?: string
  services?: MultiSelectOption[]
  environments?: MultiSelectOption[]
}
 
export interface BuildTypeContext {
  buildType?: BUILD_TYPE
  repositoryName?: string
  sourceBranch?: string
  targetBranch?: string
  branch?: string
  tag?: string
}
 
const exclusionList = [
  'buildType',
  'repositoryName',
  'sourceBranch',
  'targetBranch',
  'tag',
  'branch',
  'services',
  'environments',
  'deploymentType'
]
 
export const getValidFilterArguments = (formData: Record<string, any>): PipelineExecutionFilterProperties => {
  const {
    status,
    buildType,
    repositoryName,
    sourceBranch,
    targetBranch,
    branch,
    tag,
    services,
    environments,
    deploymentType,
    infrastructureType
  } = formData
  return Object.assign(omit(formData, ...exclusionList), {
    status: status?.map((statusOption: MultiSelectOption) => statusOption?.value),
    moduleProperties: {
      ci: getCIModuleProperties(buildType as BUILD_TYPE, {
        repositoryName,
        sourceBranch,
        targetBranch,
        branch,
        tag
      }),
      cd: {
        serviceDefinitionTypes: deploymentType,
        infrastructureType: infrastructureType,
        serviceIdentifiers: services?.map((service: MultiSelectOption) => service?.value),
        envIdentifiers: environments?.map((env: MultiSelectOption) => env?.value)
      }
    }
  })
}
 
export type PipelineExecutionFormType = Omit<PipelineExecutionFilterProperties, 'status'> & {
  status?: MultiSelectOption[]
} & BuildTypeContext &
  DeploymentTypeContext
 
export const createOption = (label: string, value: string, count?: number): MultiSelectOption => {
  const labelWithCount =
    count && count > 0
      ? label
          .concat(' ')
          .concat('(')
          .concat((count || '').toString())
          .concat(')')
      : label
  return {
    label: labelWithCount,
    value: value
  } as MultiSelectOption
}
 
export const getOptionsForMultiSelect = (): MultiSelectOption[] => {
  return Object.keys(EXECUTION_STATUS).map(key => {
    const text = EXECUTION_STATUS[parseInt(key)] || ''
    return createOption(startCase(text), text)
  })
}
 
export const createRequestBodyPayload = ({
  isUpdate,
  data,
  projectIdentifier,
  orgIdentifier
}: {
  isUpdate: boolean
  data: FilterDataInterface<PipelineExecutionFormType, FilterInterface>
  projectIdentifier: string
  orgIdentifier: string
}): FilterDTO => {
  const {
    metadata: { name: _name, filterVisibility, identifier },
    formValues
  } = data
  const { pipelineName, status: _statuses, moduleProperties: _moduleProperties } = getValidFilterArguments(formValues)
  return {
    name: _name,
    identifier: isUpdate ? identifier : StringUtils.getIdentifierFromName(_name),
    filterVisibility: filterVisibility,
    projectIdentifier,
    orgIdentifier,
    filterProperties: {
      filterType: 'PipelineExecution',
      pipelineName: pipelineName || '',
      status: _statuses,
      moduleProperties: _moduleProperties as PipelineExecutionFilterProperties['moduleProperties']
    } as PipelineExecutionFilterProperties
  }
}
 
export const getCIModuleProperties = (buildType: BUILD_TYPE, contextInfo: BuildTypeContext): Record<string, any> => {
  const { repositoryName, sourceBranch, targetBranch, branch, tag } = contextInfo
  let moduleProperties = {}
  switch (buildType) {
    case BUILD_TYPE.PULL_OR_MERGE_REQUEST:
      moduleProperties = {
        ciExecutionInfoDTO: {
          event: 'pullRequest',
          pullRequest: { sourceBranch: sourceBranch, targetBranch: targetBranch }
        } as CIWebhookInfoDTO
      }
      break
    case BUILD_TYPE.BRANCH:
      moduleProperties = {
        branch: branch
      }
      break
    case BUILD_TYPE.TAG:
      moduleProperties = {
        tag: tag
      }
      break
  }
 
  return repositoryName ? Object.assign(moduleProperties, { repoName: repositoryName }) : moduleProperties
}
 
export const enum BUILD_TYPE {
  PULL_OR_MERGE_REQUEST = 'PULL_OR_MERGE_REQUEST',
  BRANCH = 'BRANCH',
  TAG = 'TAG'
}
 
export const getBuildType = (moduleProperties: {
  [key: string]: {
    [key: string]: any
  }
}): BUILD_TYPE | undefined => {
  const { branch, tag, ciExecutionInfoDTO } = moduleProperties?.ci || {}
  const { sourceBranch, targetBranch } = ciExecutionInfoDTO?.pullRequest || {}
 
  return sourceBranch && targetBranch
    ? BUILD_TYPE.PULL_OR_MERGE_REQUEST
    : branch
    ? BUILD_TYPE.BRANCH
    : tag
    ? BUILD_TYPE.TAG
    : undefined
}
 
export const getMultiSelectFormOptions = (values?: any[]): SelectOption[] | undefined => {
  return values?.map(item => {
    return { label: item.name ?? item, value: item.identifier ?? item }
  })
}
 
export const getFilterByIdentifier = (identifier: string, filters?: FilterDTO[]): FilterDTO | undefined =>
  filters?.find((filter: FilterDTO) => filter.identifier?.toLowerCase() === identifier.toLowerCase())