import { getPersonalAccessTokenHandler , WebApi } from "azure-devops-node-api" ; import { IBuildApi } from "azure-devops-node-api/BuildApi" ; import { ITaskAgentApi } from "azure-devops-node-api/TaskAgentApi" ; import { RestClient } from "typed-rest-client" ; import { Config } from "../config" ; import { logger } from "../logger" ; import { AzureDevOpsOpts } from "./git" ; import { build as buildError } from "./errorBuilder" ; import { errorStatusCode } from "./errorStatusCode" ; let connection ; let restApi ; let buildApi ; let taskAgentApi ; param orgName returns export const azdoUrl = ( orgName ) : string => `https://dev.azure.com/${ orgName }` ; param opts returns export const getWebApi = async ( opts = { } ) : Promise < WebApi > => { if ( connection ) { return connection ; } const config = Config ( ) ; const { personalAccessToken = config . azure_devops ?. access_token , orgName = config . azure_devops ?. org , } = opts ; if ( ! personalAccessToken ) { throw buildError ( errorStatusCode . AZURE_CLIENT , "azure-client-get-web-api-err-missing-access-token" ) ; } if ( ! orgName ) { throw buildError ( errorStatusCode . AZURE_CLIENT , "azure-client-get-web-api-err-missing-org" ) ; } const orgUrl = azdoUrl ( orgName ) ; logger . debug ( `getWebApi with org url: ${ orgUrl } and token: ${ personalAccessToken }` ) ; const authHandler = getPersonalAccessTokenHandler ( personalAccessToken ) ; connection = new WebApi ( orgUrl , authHandler ) ; return connection ; } ; export const invalidateWebApi = ( ) : void => { connection = undefined ; } ; param opts returns export const getRestClient = async ( opts = { } ) : Promise < RestClient > => { if ( restApi ) { return restApi ; } try { const webApi = await getWebApi ( opts ) ; restApi = webApi . rest ; return restApi ; } catch ( err ) { throw buildError ( errorStatusCode . AZURE_CLIENT , "azure-client-get-rest-client-err" , err ) ; } } ; param opts returns export const getBuildApi = async ( opts = { } ) : Promise < IBuildApi > => { if ( buildApi ) { return buildApi ; } try { const webApi = await getWebApi ( opts ) ; buildApi = await webApi . getBuildApi ( ) ; return buildApi ; } catch ( err ) { throw buildError ( errorStatusCode . AZURE_CLIENT , "azure-client-get-build-client-err" , err ) ; } } ; param opts returns export const getTaskAgentApi = async ( opts = { } ) : Promise < ITaskAgentApi > => { if ( taskAgentApi ) { return taskAgentApi ; } try { const webApi = await getWebApi ( opts ) ; taskAgentApi = await webApi . getTaskAgentApi ( ) ; return taskAgentApi ; } catch ( err ) { throw buildError ( errorStatusCode . AZURE_CLIENT , "azure-client-get-task-agent-client-err" , err ) ; } } ;