import { WebApi } from "azure-devops-node-api" ; import { IGitApi } from "azure-devops-node-api/GitApi" ; import { GitRepository } from "azure-devops-node-api/interfaces/TfvcInterfaces" ; import { SimpleGit } from "simple-git/promise" ; import { completePullRequest as approvePR , getActivePullRequests , } from "../git/azure" ; import { build as buildError } from "../../lib/errorBuilder" ; import { errorStatusCode } from "../../lib/errorStatusCode" ; import { logger } from "../../logger" ; import { RequestContext , SP_USER_NAME } from "./constants" ; let gitAPI ; /**
 * Returns Git API client.
 */ export const getGitApi = async ( webAPI ) : Promise < IGitApi > => { if ( gitAPI ) { return gitAPI ; } gitAPI = await webAPI . getGitApi ( ) ; return gitAPI ; } ; param orgName param projectName param repoName export const getAzureRepoUrl = ( orgName , projectName , repoName ) : string => { return `https://dev.azure.com/${ orgName }/${ projectName }/_git/${ repoName }` ; } ; param gitApi param repoName param projectName export const createRepo = async ( gitApi , repoName , projectName ) : Promise < GitRepository > => { const createOptions = { name : repoName , } ; try { return await gitApi . createRepository ( createOptions , projectName ) ; } catch ( err ) { if ( err . statusCode === 401 ) { throw buildError ( errorStatusCode . GIT_OPS_ERR , "git-create-repo-no-permissions" , err ) ; } throw buildError ( errorStatusCode . GIT_OPS_ERR , "git-create-repo-err" , err ) ; } } ; param gitApi param repo param projectName export const deleteRepo = async ( gitApi , repo , projectName ) : Promise < void > => { logger . info ( "Deleting repository " + repo . name ) ; if ( repo . id ) { await gitApi . deleteRepository ( repo . id , projectName ) ; logger . info ( "Deleted repository " + repo . name ) ; } else { throw buildError ( errorStatusCode . GIT_OPS_ERR , "git-delete-repo-id-missing" ) ; } } ; param gitApi param repoName param projectName export const getRepoInAzureOrg = async ( gitApi , repoName , projectName ) : Promise < GitRepository | undefined > => { try { const respositories = await gitApi . getRepositories ( ) ; return ( respositories || [ ] ) . find ( ( repo ) => { return repo . project ?. name === projectName && repo . name === repoName ; } ) ; } catch ( err ) { if ( err . statusCode === 401 ) { throw buildError ( errorStatusCode . GIT_OPS_ERR , "git-get-repo-no-permissions" , err ) ; } throw buildError ( errorStatusCode . GIT_OPS_ERR , "git-get-repo-azure-err" , err ) ; } } ; param gitApi param repoName param projectName param removeIfExist export const createRepoInAzureOrg = async ( gitApi , repoName , projectName , removeIfExist = false ) : Promise < GitRepository > => { let repo = await getRepoInAzureOrg ( gitApi , repoName , projectName ) ; if ( repo ) { logger . info ( `Repo, ${ repoName } already exists.` ) ; if ( removeIfExist ) { await deleteRepo ( gitApi , repo , projectName ) ; } else { return repo ; } } repo = await createRepo ( gitApi , repoName , projectName ) ; logger . info ( `Repo, ${ repoName } is created.` ) ; return repo ; } ; param repo param orgName export const getRepoURL = ( repo , orgName ) : string => { return repo . remoteUrl ? repo . remoteUrl . replace ( `${ orgName }@` , "" ) : "" ; } ; param git param rc param repoName export const commitAndPushToRemote = async ( git , rc , repoName ) : Promise < void > => { logger . info ( `Pushing to ${ repoName } repo.` ) ; try { await git . commit ( `Initial commit for ${ repoName } repo` ) ; const resultLog = await git . log ( ) ; logger . info ( "Log Messages from Git:" ) ; resultLog . all . forEach ( ( f ) => logger . info ( "\t" + f . date + " --> " + f . message ) ) ; const remoteURL = `dev.azure.com/${ rc . orgName }/${ rc . projectName }/_git/${ repoName }` ; const remote = `https://${ SP_USER_NAME }:${ rc . accessToken }@${ remoteURL }` ; if ( ( await git . getRemotes ( false ) ) . length === 0 ) { await git . addRemote ( "origin" , remote ) ; } await git . push ( "origin" , "master" ) ; logger . info ( `Completed pushing to ${ repoName } repo.` ) ; } catch ( err ) { throw buildError ( errorStatusCode . GIT_OPS_ERR , "git-commit-Push-to_remote-err" , err ) ; } } ; export const completePullRequest = async ( gitApi , rc , repoName ) : Promise < void > => { const pullRequests = await getActivePullRequests ( gitApi , repoName , rc . projectName ) ; if ( pullRequests && pullRequests . length > 0 ) { const pr = pullRequests [ 0 ] ; if ( pr && pr . pullRequestId ) { await approvePR ( pr , rc ) ; return ; } } throw buildError ( errorStatusCode . GIT_OPS_ERR , "setup-cmd-cannot-locate-pr-for-approval" ) ; } ;