import { IBuildApi } from "azure-devops-node-api/BuildApi" ; import { AgentPoolQueue , Build , BuildDefinition , BuildDefinitionVariable , BuildRepository , ContinuousIntegrationTrigger , DefinitionQuality , DefinitionQueueStatus , DefinitionTriggerType , DefinitionType , YamlProcess , } from "azure-devops-node-api/interfaces/BuildInterfaces" ; import { logger } from "../../logger" ; import { getBuildApi } from "../azdoClient" ; import { build as buildError } from "../errorBuilder" ; import { errorStatusCode } from "../errorStatusCode" ; const hostedUbuntuPool = "Hosted Ubuntu 1604" ; const hostedUbuntuPoolId = 224 ; /**
 * Enum of Repository Provider types.
 */ export enum RepositoryTypes { Github = "github" , Azure = "tfsgit" } param org param token returns export const getBuildApiClient = async ( orgName , personalAccessToken ) : Promise < IBuildApi > => { return await getBuildApi ( { orgName , personalAccessToken , } ) ; } ; interface Pipeline { pipelineName : string ; repositoryUrl : string ; repositoryName : string ; yamlFileBranch : string ; yamlFilePath : string ; branchFilters : string [ ] ; maximumConcurrentBuilds : number ; variables ? : { [ key ] : BuildDefinitionVariable ; } ; } /**
 * Interface that describes a Pipeline Configuration for an Azure DevOps
 * backed git repository.
 */ export type IAzureRepoPipelineConfig = Pipeline ; /**
 * Interface that describes a Pipeline Configuration for a GitHub backed
 * git repository.
 */ export interface GithubRepoPipelineConfig extends Pipeline { serviceConnectionId : string ; } param pipelineConfig returns export const definitionForAzureRepoPipeline = ( pipelineConfig ) : BuildDefinition => { try { const pipelineDefinition = { } ; pipelineDefinition . badgeEnabled = true ; pipelineDefinition . triggers = [ { batchChanges : false , branchFilters : pipelineConfig . branchFilters , maxConcurrentBuildsPerBranch : pipelineConfig . maximumConcurrentBuilds , settingsSourceType : 2 , triggerType : DefinitionTriggerType . ContinuousIntegration , } as ContinuousIntegrationTrigger , ] ; pipelineDefinition . queue = { name : hostedUbuntuPool , pool : { id : hostedUbuntuPoolId , name : hostedUbuntuPool , } , } as AgentPoolQueue ; pipelineDefinition . queueStatus = DefinitionQueueStatus . Enabled ; pipelineDefinition . name = pipelineConfig . pipelineName ; pipelineDefinition . type = DefinitionType . Build ; pipelineDefinition . quality = DefinitionQuality . Definition ; pipelineDefinition . repository = { defaultBranch : pipelineConfig . yamlFileBranch , id : pipelineConfig . repositoryName , name : pipelineConfig . repositoryName , type : RepositoryTypes . Azure , url : pipelineConfig . repositoryUrl , } as BuildRepository ; pipelineDefinition . process = { yamlFilename : pipelineConfig . yamlFilePath , } as YamlProcess ; if ( pipelineConfig . variables ) { pipelineDefinition . variables = pipelineConfig . variables ; } return pipelineDefinition ; } catch ( err ) { throw buildError ( errorStatusCode . PIPELINE_ERR , "pipeline-err-create-def-azure-repo-pipeline" , err ) ; } } ; param pipelineConfig returns export const definitionForGithubRepoPipeline = ( pipelineConfig ) : BuildDefinition => { try { const pipelineDefinition = { } as BuildDefinition ; pipelineDefinition . badgeEnabled = true ; pipelineDefinition . triggers = [ { batchChanges : false , branchFilters : pipelineConfig . branchFilters , maxConcurrentBuildsPerBranch : pipelineConfig . maximumConcurrentBuilds , settingsSourceType : 2 , triggerType : DefinitionTriggerType . ContinuousIntegration , } as ContinuousIntegrationTrigger , ] ; pipelineDefinition . queue = { name : hostedUbuntuPool , pool : { id : hostedUbuntuPoolId , name : hostedUbuntuPool , } , } as AgentPoolQueue ; pipelineDefinition . queueStatus = DefinitionQueueStatus . Enabled ; pipelineDefinition . name = pipelineConfig . pipelineName ; pipelineDefinition . type = DefinitionType . Build ; pipelineDefinition . quality = DefinitionQuality . Definition ; pipelineDefinition . repository = { defaultBranch : pipelineConfig . yamlFileBranch , id : pipelineConfig . repositoryName , name : pipelineConfig . repositoryName , properties : { connectedServiceId : pipelineConfig . serviceConnectionId , } , type : RepositoryTypes . Github , url : pipelineConfig . repositoryUrl , } as BuildRepository ; pipelineDefinition . process = { yamlFilename : pipelineConfig . yamlFilePath , } as YamlProcess ; if ( pipelineConfig . variables ) { pipelineDefinition . variables = pipelineConfig . variables ; } return pipelineDefinition ; } catch ( err ) { throw buildError ( errorStatusCode . PIPELINE_ERR , "pipeline-err-create-def-githib-pipeline" , err ) ; } } ; param buildApi param azdoProject param definition returns export const createPipelineForDefinition = async ( buildApi , azdoProject , definition ) : Promise < BuildDefinition > => { logger . info ( "Creating pipeline for definition" ) ; try { const createdDefn = await buildApi . createDefinition ( definition , azdoProject ) ; return createdDefn ; } catch ( err ) { throw buildError ( errorStatusCode . PIPELINE_ERR , "pipeline-err-create-pipeline-for-defn" , err ) ; } } ; param buildApi param azdoProject param definitionId returns export const queueBuild = async ( buildApi , azdoProject , definitionId ) : Promise < Build > => { const buildReference = { definition : { id : definitionId , } , } ; try { return await buildApi . queueBuild ( buildReference , azdoProject ) ; } catch ( err ) { throw buildError ( errorStatusCode . PIPELINE_ERR , "pipeline-queue-build-err" , err ) ; } } ;