All files / modules/35-user-profile/utils utils.ts

91.23% Statements 52/57
55.21% Branches 106/192
100% Functions 11/11
90.2% Lines 46/51

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                3x     3x   3x 3x 3x 3x     3x 3x 3x 3x 3x 3x                 3x 3x 3x 3x 3x 3x     3x               3x 60x   18x   20x   11x   11x               3x 2x   1x                         1x                                 3x       2x 2x                         3x       1x 1x                           3x       3x   2x   1x           3x       16x     3x 9x   4x       4x       1x      
/*
 * 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 { IconName } from '@wings-software/uicore'
import { ValueType } from '@secrets/components/TextReference/TextReference'
import type { SCMData } from '@user-profile/modals/SourceCodeManager/views/SourceCodeManagerForm'
import type { SourceCodeManagerAuthentication, SourceCodeManagerDTO } from 'services/cd-ng'
import { setSecretField } from '@secrets/utils/SecretField'
 
export enum ConnectionType {
  HTTPS = 'HTTPS',
  HTTP = 'Http',
  SSH = 'Ssh'
}
 
export enum AuthTypes {
  USERNAME_PASSWORD = 'UsernamePassword',
  USERNAME_TOKEN = 'UsernameToken',
  KERBEROS = 'Kerberos',
  SSH_KEY = 'SSH_KEY',
  AWSCredentials = 'AWSCredentials'
}
 
export interface SourceCodeType {
  text: string
  value: SourceCodeTypes
  icon: IconName
}
 
export enum SourceCodeTypes {
  BITBUCKET = 'BITBUCKET',
  GITHUB = 'GITHUB',
  GITLAB = 'GITLAB',
  AZURE_DEV_OPS = 'AZURE_DEV_OPS',
  AWS_CODE_COMMIT = 'AWS_CODE_COMMIT'
}
 
export const selectedValueToTypeMap: Record<SourceCodeTypes, SourceCodeTypes> = {
  [SourceCodeTypes.BITBUCKET]: SourceCodeTypes.BITBUCKET,
  [SourceCodeTypes.GITHUB]: SourceCodeTypes.GITHUB,
  [SourceCodeTypes.GITLAB]: SourceCodeTypes.GITLAB,
  [SourceCodeTypes.AWS_CODE_COMMIT]: SourceCodeTypes.AWS_CODE_COMMIT,
  [SourceCodeTypes.AZURE_DEV_OPS]: SourceCodeTypes.AZURE_DEV_OPS
}
 
export const getIconBySCM = (item: SourceCodeTypes): IconName => {
  switch (item) {
    case SourceCodeTypes.BITBUCKET:
      return 'bitbucket-blue'
    case SourceCodeTypes.GITHUB:
      return 'github'
    case SourceCodeTypes.GITLAB:
      return 'service-gotlab'
    case SourceCodeTypes.AWS_CODE_COMMIT:
      return 'service-aws-code-deploy'
    case SourceCodeTypes.AZURE_DEV_OPS:
      return 'service-azure'
    default:
      return 'bitbucket'
  }
}
 
export const getAuthentication = (values: SCMData): SourceCodeManagerAuthentication | undefined => {
  switch (values.authType) {
    case AuthTypes.USERNAME_PASSWORD:
      return {
        type: ConnectionType.HTTP,
        spec: {
          type: AuthTypes.USERNAME_PASSWORD,
          spec: {
            ...(values.username?.type === ValueType.TEXT
              ? { username: values.username.value }
              : { usernameRef: values.username?.value }),
            passwordRef: values.password?.referenceString
          }
        }
      }
    case AuthTypes.USERNAME_TOKEN:
      return {
        type: ConnectionType.HTTP,
        spec: {
          type: AuthTypes.USERNAME_TOKEN,
          spec: {
            ...(values.username?.type === ValueType.TEXT
              ? { username: values.username.value }
              : { usernameRef: values.username?.value }),
            tokenRef: values.accessToken?.referenceString
          }
        }
      }
    default:
      return undefined
  }
}
 
const getBitBucketFormData = async (
  sourceCodeManagerData: SourceCodeManagerDTO,
  accountIdentifier: string
): Promise<SCMData> => {
  const { name, authentication } = sourceCodeManagerData
  return {
    name: name,
    authType: authentication?.spec?.type,
    password: (await setSecretField(authentication?.spec?.spec?.passwordRef, {
      accountIdentifier
    })) as SCMData['password'],
    username: {
      value: authentication?.spec?.spec?.username || authentication?.spec?.spec?.usernameRef,
      type: authentication?.spec?.spec?.usernameRef ? ValueType.ENCRYPTED : ValueType.TEXT
    }
  }
}
 
const getGithubFormData = async (
  sourceCodeManagerData: SourceCodeManagerDTO,
  accountIdentifier: string
): Promise<SCMData> => {
  const { name, authentication } = sourceCodeManagerData
  return {
    name: name,
    authType: authentication?.spec?.type,
    accessToken: (await setSecretField(
      authentication?.spec?.spec?.tokenRef || authentication?.spec?.apiAccess?.spec?.tokenRef,
      { accountIdentifier }
    )) as SCMData['accessToken'],
    username: {
      value: authentication?.spec?.spec?.username || authentication?.spec?.spec?.usernameRef,
      type: authentication?.spec?.spec?.usernameRef ? ValueType.ENCRYPTED : ValueType.TEXT
    }
  }
}
 
export const getFormDataBasedOnSCMType = (
  sourceCodeManagerData: SourceCodeManagerDTO,
  accountIdentifier: string
): Promise<SCMData | undefined> => {
  switch (sourceCodeManagerData.type) {
    case SourceCodeTypes.BITBUCKET:
      return getBitBucketFormData(sourceCodeManagerData, accountIdentifier)
    case SourceCodeTypes.GITHUB:
      return getGithubFormData(sourceCodeManagerData, accountIdentifier)
    default:
      return Promise.resolve(undefined)
  }
}
 
export const getDefaultSCMType = (
  supportedSCMS: SourceCodeType[],
  currentSCM?: SourceCodeManagerDTO['type']
): SourceCodeType => {
  return supportedSCMS.find(item => item.value === currentSCM) || supportedSCMS[0]
}
 
export const getDefaultSelected = (type?: SourceCodeTypes): AuthTypes | undefined => {
  switch (type) {
    case SourceCodeTypes.GITHUB:
      return AuthTypes.USERNAME_TOKEN
    case SourceCodeTypes.BITBUCKET:
    case SourceCodeTypes.GITLAB:
    case SourceCodeTypes.AZURE_DEV_OPS:
      return AuthTypes.USERNAME_PASSWORD
    case SourceCodeTypes.AWS_CODE_COMMIT:
      return AuthTypes.AWSCredentials
    default:
      return undefined
  }
}