import gitUrlParse from 'git-url-parse' ; import { Uri , window , workspace } from 'vscode' ; import { azureTools , isAzureProvider } from '../azure/azureTools' ; import { fluxTools } from '../flux/fluxTools' ; import { checkIfOpenedFolderGitRepositorySourceExists } from '../git/checkIfOpenedFolderGitRepositorySourceExists' ; import { checkGitVersion } from '../install' ; import { shell } from '../shell' ; import { sanitizeRFC1123 } from '../utils/stringUtils' ; import { getCurrentClusterInfo , refreshSourcesTreeView , refreshWorkloadsTreeView } from '../views/treeViews' ; import { makeSSHUrlFromGitUrl , showDeployKeyNotificationIfNeeded } from './createSource' ; param fileExplorerUri export async function createGitRepository ( fileExplorerUri ? , kustomizationName ? , kustomizationPath ? ) { const gitInstalled = await checkGitVersion ( ) ; if ( ! gitInstalled ) { return ; } const currentClusterInfo = await getCurrentClusterInfo ( ) ; if ( ! currentClusterInfo ) { return ; } let gitFolderFsPath = '' ; if ( fileExplorerUri ) { gitFolderFsPath = fileExplorerUri . fsPath ; } else { if ( ! workspace . workspaceFolders || workspace . workspaceFolders ?. length === 0 ) { const pickedFolder = await window . showOpenDialog ( { canSelectFiles : false , canSelectFolders : true , canSelectMany : false , } ) ; if ( ! pickedFolder ) { return ; } gitFolderFsPath = pickedFolder [ 0 ] . fsPath ; } else if ( workspace . workspaceFolders . length > 1 ) { const pickedFolder = await window . showQuickPick ( workspace . workspaceFolders . map ( folder => folder . uri . fsPath ) ) ; if ( ! pickedFolder ) { return ; } gitFolderFsPath = pickedFolder ; } else { gitFolderFsPath = workspace . workspaceFolders [ 0 ] . uri . fsPath ; } } if ( ! gitFolderFsPath ) { window . showErrorMessage ( 'No git repository folder is picked.' ) ; return ; } const gitRepositoryState = await getGitRepositoryState ( gitFolderFsPath , true ) ; if ( ! gitRepositoryState ) { return ; } let gitUrl = gitRepositoryState . url ; const gitBranch = gitRepositoryState . branch ; const newGitRepositorySourceName = nameGitRepositorySource ( gitUrl , gitBranch ) ; const parsedGitUrl = gitUrlParse ( gitUrl ) ; const isSSH = parsedGitUrl . protocol === 'ssh' ; if ( isSSH ) { gitUrl = makeSSHUrlFromGitUrl ( gitUrl ) ; } let deployKey ; if ( isAzureProvider ( currentClusterInfo . clusterProvider ) ) { const createGitRepoResult = await azureTools . createGitRepository ( newGitRepositorySourceName , gitUrl , gitBranch , isSSH , currentClusterInfo . clusterNode , currentClusterInfo . clusterProvider , kustomizationName , kustomizationPath ) ; deployKey = createGitRepoResult ?. deployKey ; refreshWorkloadsTreeView ( ) ; } else { const createGitRepoResult = await fluxTools . createSourceGit ( newGitRepositorySourceName , gitUrl , gitBranch , isSSH ) ; deployKey = createGitRepoResult ?. deployKey ; } showDeployKeyNotificationIfNeeded ( gitUrl , deployKey ) ; checkIfOpenedFolderGitRepositorySourceExists ( ) ; setTimeout ( async ( ) => { refreshSourcesTreeView ( ) ; } , 1000 ) ; return newGitRepositorySourceName ; } param url param branch export function nameGitRepositorySource ( url , branch ) { const parsedGitUrl = gitUrlParse ( url ) ; const organization = parsedGitUrl . organization ; const repository = parsedGitUrl . name ; return sanitizeRFC1123 ( `${ organization }-${ repository }-${ branch }` ) ; } param cwd param showErrorNotifications async function checkIfInsideGitRepository ( cwd , showErrorNotifications ) { const isInsideGitRepositoryShellResult = await shell . execWithOutput ( 'git rev-parse --is-inside-work-tree' , { cwd , revealOutputView : false , showProgress : false , } ) ; const isInsideGitRepository = isInsideGitRepositoryShellResult . code === 0 ; if ( ! isInsideGitRepository ) { if ( showErrorNotifications ) { window . showErrorMessage ( `Not a git repository ${ cwd }` ) ; } return false ; } return true ; } param cwd export async function getGitOriginUrl ( cwd ) { const gitRemoteUrlShellResult = await shell . execWithOutput ( 'git ls-remote --get-url origin' , { cwd , revealOutputView : false , showProgress : false , } ) ; if ( gitRemoteUrlShellResult . code !== 0 ) { window . showErrorMessage ( `Failed to get origin url ${ gitRemoteUrlShellResult . stderr }` ) ; return ; } const gitOriginUrl = gitRemoteUrlShellResult . stdout . trim ( ) ; return gitOriginUrl ; } param cwd export async function getGitBranch ( cwd ) { const gitCurrentBranchShellResult = await shell . execWithOutput ( 'git rev-parse --abbrev-ref HEAD' , { cwd , revealOutputView : false , showProgress : false , } ) ; if ( gitCurrentBranchShellResult . code !== 0 ) { window . showErrorMessage ( `Failed to get current git branch ${ gitCurrentBranchShellResult . stderr }` ) ; return ; } const gitBranch = gitCurrentBranchShellResult . stdout . trim ( ) ; return gitBranch ; } param cwd export async function getGitRepositoryState ( cwd , showErrorNotifications ) { const isInsideGitRepository = await checkIfInsideGitRepository ( cwd , showErrorNotifications ) ; if ( ! isInsideGitRepository ) { return ; } const gitUrl = await getGitOriginUrl ( cwd ) ; if ( ! gitUrl ) { return ; } const gitBranch = await getGitBranch ( cwd ) ; if ( ! gitBranch ) { return ; } return { url : gitUrl , branch : gitBranch , } ; }