All files / modules/40-gitsync/common gitSyncUtils.ts

80.33% Statements 49/61
76.92% Branches 50/65
100% Functions 9/9
77.36% Lines 41/53

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                66x   66x   66x 10x   10x                   66x                           66x                                 66x 22x 22x   22x 22x               22x     66x 180x 180x               66x 5x 5x     66x         187x 187x     66x       138x 138x 138x 35x       103x         66x 29x 29x 1x   28x   28x               66x 73x 73x 38x   35x       66x       74x              
/*
 * Copyright 2022 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 { Connectors } from '@connectors/constants'
import type { GitSyncConfig, ConnectorInfoDTO, GitSyncEntityDTO, EntityGitDetails } from 'services/cd-ng'
import { GitSuffixRegex } from '@common/utils/StringUtils'
 
export const getGitConnectorIcon = (type: GitSyncConfig['gitConnectorType']): IconName => {
  switch (type) {
    case Connectors.GITHUB:
      return 'github'
    case Connectors.GITLAB:
      return 'service-gotlab'
    case Connectors.BITBUCKET:
      return 'bitbucket-selected'
    default:
      return 'cog'
  }
}
 
export const modalTypes = {
  GIT_SYNC_REP: 'gitSyncRepo',
  GIT_SYNC_BRANCH: 'gitSyncBranch'
}
 
export interface ConnectorCardInterface {
  type: ConnectorInfoDTO['type']
  disabled?: boolean
  icon: {
    default: IconName
    selected: IconName
  }
}
 
export const gitCards: Array<ConnectorCardInterface> = [
  {
    type: Connectors.GITHUB,
    icon: {
      default: 'github-unselected',
      selected: 'github-selected'
    }
  },
  {
    type: Connectors.BITBUCKET,
    icon: {
      default: 'bitbucket-unselected',
      selected: 'bitbucket-selected'
    }
  }
]
 
export const getRepoPath = (gitRepo: GitSyncConfig): string => {
  let basePath = ''
  switch (gitRepo.gitConnectorType) {
    case Connectors.GITHUB:
      basePath = 'https://github.com/'
      break
    case Connectors.GITLAB:
      basePath = 'https://gitlab.com/'
      break
    case Connectors.BITBUCKET:
      basePath = 'https://bitbucket.com/'
  }
 
  return gitRepo.repo ? gitRepo.repo.replace(GitSuffixRegex, '').split(basePath)?.[1] ?? '' : ''
}
 
export const getRepoUrl = (baseUrl: string, repoName: string) => {
  Eif (!baseUrl) {
    return ''
  }
  if (baseUrl.endsWith('/')) {
    return baseUrl + repoName
  }
  return `${baseUrl}/${repoName}`
}
 
export const getHarnessFolderPathWithSuffix = (folderPath: string, suffix: string): string => {
  const sanitizedRootFolder = folderPath.split('/').reduce(rootFolderFormatter, '')
  return sanitizedRootFolder.concat(suffix)
}
 
const rootFolderFormatter = (previousValue: string, currentValue: string): string => {
  /* Convert folder paths like
    /* ///////a/////b////c///// to a/b/c/ */
  /* a/////b////c to a/b/c */
  /* ///////a/////b////c to a/b/c */
  const folderPart = currentValue.trim()
  return folderPart ? `${previousValue}/${folderPart}` : previousValue
}
 
export const getCompleteGitPath = (repo: string, folderPath: string, suffix: string): string => {
  // Convert repo url like
  /* https://github.com/wings-software/vb.git//// to https://github.com/wings-software/vb */
  /* https://github.com/wings-software/vb.git to https://github.com/wings-software/vb */
  const sanitizedRepo = repo.replace(GitSuffixRegex, '')
  const sanitizedRootFolder = folderPath.split('/').reduce(rootFolderFormatter, '')
  if (sanitizedRootFolder) {
    return sanitizedRepo.endsWith('/')
      ? `${sanitizedRepo}${sanitizedRootFolder.substring(1)}${suffix}`
      : `${sanitizedRepo}${sanitizedRootFolder}${suffix}`
  } else {
    return `${sanitizedRepo}${sanitizedRepo.endsWith('/') ? suffix.substring(1) : suffix}`
  }
  return ''
}
 
export const getExternalUrl = (config: GitSyncConfig, folderPath?: string): string => {
  const { repo, branch, gitConnectorType } = config
  if (!repo || !branch || !folderPath) {
    return ''
  }
  switch (gitConnectorType) {
    case Connectors.GITHUB:
      return `${repo}/tree/${branch}${folderPath.startsWith('/') ? '' : '/'}${folderPath}`
    case Connectors.BITBUCKET:
      return `${repo}/src/${branch}${folderPath.startsWith('/') ? '' : '/'}${folderPath}`
    default:
      return ''
  }
}
 
export const getEntityUrl = (entity: GitSyncEntityDTO): string => {
  const { repoUrl, folderPath, branch, entityGitPath } = entity
  if (repoUrl && branch && folderPath && entityGitPath) {
    return `${repoUrl}/blob/${branch}${folderPath.startsWith('/') ? '' : '/'}${folderPath}${entityGitPath}`
  } else {
    return ''
  }
}
 
export const getRepoEntityObject = (
  repo: GitSyncConfig | undefined,
  gitDetails: EntityGitDetails
): GitSyncEntityDTO => {
  return {
    repoUrl: repo?.repo,
    folderPath: gitDetails?.rootFolder,
    branch: repo?.branch,
    entityGitPath: gitDetails?.filePath
  }
}