All files / modules/35-connectors/components/CreateConnector CreateConnectorUtils.ts

92.86% Statements 13/14
44.44% Branches 4/9
83.33% Functions 5/6
92.31% Lines 12/13

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            220x 220x   220x 220x     220x         220x               220x     4x   28x         28x       28x           28x          
/*
 * 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 * as Yup from 'yup'
import { Scope } from '@common/interfaces/SecretsInterface'
import type { ConnectorConfigDTO } from 'services/cd-ng'
import { GitConnectionType } from '@connectors/pages/connectors/utils/ConnectorUtils'
import { GitAuthTypes } from '@connectors/pages/connectors/utils/ConnectorHelper'
import type { StringsMap } from 'stringTypes'
 
export const ConnectorSecretScope: { [scope: string]: string } = {
  [Scope.ORG]: 'org.',
  [Scope.ACCOUNT]: 'account.'
}
 
export function getScopingStringFromSecretRef(connecterConfig: ConnectorConfigDTO): string | undefined {
  return connecterConfig &&
    connecterConfig.passwordRefSecret &&
    connecterConfig.passwordRefSecret.scope !== Scope.PROJECT
    ? ConnectorSecretScope[connecterConfig.passwordRefSecret.scope]
    : undefined
}
 
export const getCommonConnectorsValidationSchema = (
  getString: (key: keyof StringsMap, vars?: Record<string, any> | undefined) => string
): Yup.ObjectSchema =>
  Yup.object().shape({
    username: Yup.string().when(['connectionType', 'authType'], {
      is: (connectionType, authType) => connectionType === GitConnectionType.HTTP && authType !== GitAuthTypes.KERBEROS,
      then: Yup.string().trim().required(getString('validation.username')),
      otherwise: Yup.string().nullable()
    }),
    authType: Yup.string().when('connectionType', {
      is: val => val === GitConnectionType.HTTP,
      then: Yup.string().trim().required(getString('validation.authType'))
    }),
    sshKey: Yup.object().when('connectionType', {
      is: val => val === GitConnectionType.SSH,
      then: Yup.object().required(getString('validation.sshKey')),
      otherwise: Yup.object().nullable()
    }),
    password: Yup.object().when(['connectionType', 'authType'], {
      is: (connectionType, authType) =>
        connectionType === GitConnectionType.HTTP && authType === GitAuthTypes.USER_PASSWORD,
      then: Yup.object().required(getString('validation.password')),
      otherwise: Yup.object().nullable()
    })
  })