import { GitCommit , GitPullRequestMergeStrategy , GitRef , } from 'azure-devops-node-api/interfaces/GitInterfaces.js' ; import { logger } from '../../logger' ; import * as azureApi from './azure-got-wrapper' ; import { getBranchNameWithoutRefsPrefix , getBranchNameWithoutRefsheadsPrefix , getNewBranchName , streamToString , } from './util' ; const mergePolicyGuid = 'fa4e907d-c16b-4a4c-9dfa-4916e5d171ab' ; export async function getRefs ( repoId , branchName ? ) { logger . debug ( `getRefs(${ repoId }, ${ branchName })` ) ; const azureApiGit = await azureApi . gitApi ( ) ; const refs = await azureApiGit . getRefs ( repoId , undefined , getBranchNameWithoutRefsPrefix ( branchName ) ) ; return refs ; } export interface AzureBranchObj { name : string ; oldObjectId : string ; } export async function getAzureBranchObj ( repoId , branchName , from ? ) { const fromBranchName = getNewBranchName ( from ) ; const refs = await getRefs ( repoId , fromBranchName ) ; if ( refs . length === 0 ) { logger . debug ( `getAzureBranchObj without a valid from, so initial commit.` ) ; return { name : getNewBranchName ( branchName ) , oldObjectId : '0000000000000000000000000000000000000000' , } ; } return { name : getNewBranchName ( branchName ) , oldObjectId : refs [ 0 ] . objectId , } ; } export async function getFile ( repoId , filePath , branchName ) { logger . trace ( `getFile(filePath=${ filePath }, branchName=${ branchName })` ) ; const azureApiGit = await azureApi . gitApi ( ) ; const item = await azureApiGit . getItemText ( repoId , filePath , undefined , undefined , 0 , false , false , true , { versionType : 0 , versionOptions : 0 , version : getBranchNameWithoutRefsheadsPrefix ( branchName ) , } ) ; if ( item ?. readable ) { const fileContent = await streamToString ( item ) ; try { const jTmp = JSON . parse ( fileContent ) ; if ( jTmp . typeKey === 'GitItemNotFoundException' ) { return null ; } if ( jTmp . typeKey === 'GitUnresolvableToCommitException' ) { return null ; } } catch ( error ) { } return fileContent ; } return null ; } export async function getCommitDetails ( commit , repoId ) { logger . debug ( `getCommitDetails(${ commit }, ${ repoId })` ) ; const azureApiGit = await azureApi . gitApi ( ) ; const results = await azureApiGit . getCommit ( commit , repoId ) ; return results ; } export async function getMergeMethod ( repoId , project , branchRef ? , defaultBranch ? ) { type Scope = { repositoryId : string ; refName ? : string ; matchKind : 'Prefix' | 'Exact' | 'DefaultBranch' ; } ; const isRelevantScope = ( scope ) : boolean => { if ( scope . matchKind === 'DefaultBranch' && ( ! branchRef || branchRef === `refs/heads/${ defaultBranch }` ) ) { return true ; } if ( scope . repositoryId !== repoId ) { return false ; } if ( ! branchRef ) { return true ; } return scope . matchKind === 'Exact' ? scope . refName === branchRef : branchRef . startsWith ( scope . refName ) ; } ; const policyConfigurations = ( await ( await azureApi . policyApi ( ) ) . getPolicyConfigurations ( project ) ) . filter ( ( p ) => p . settings . scope . some ( isRelevantScope ) && p . type . id === mergePolicyGuid ) . map ( ( p ) => p . settings ) [ 0 ] ; logger . trace ( `getMergeMethod(${ repoId }, ${ project }, ${ branchRef }) determining mergeMethod from matched policy:\n${ JSON . stringify ( policyConfigurations , null , 4 ) }` ) ; try { return Object . keys ( policyConfigurations ) . map ( ( p ) => GitPullRequestMergeStrategy [ p . slice ( 5 ) ] ) . find ( ( p ) => p ) ; } catch ( err ) { return GitPullRequestMergeStrategy . NoFastForward ; } }