import * as pkgLocationUtils from "../locationUtilities" ; import { ProvenanceHelper } from "../provenance" ; import * as tl from "azure-pipelines-task-lib" ; import { IExecOptions , IExecSyncResult } from "azure-pipelines-task-lib/toolrunner" ; import * as artifactToolRunner from "../ArtifactToolRunner" ; import * as artifactToolUtilities from "../ArtifactToolUtilities" ; import * as auth from "./Authentication" ; import { UniversalPackagesResult } from "./universalPackages" ; export async function run ( artifactToolPath , hash , targetFolder ) { const buildIdentityDisplayName = null ; const buildIdentityAccount = null ; try { const publishDir = targetFolder ; let serviceUri ; let feedId ; let packageName ; const version = `1.0.0-${ hash }` ; let accessToken ; let feedUri ; const publishedPackageVar = tl . getInput ( "publishedPackageVar" ) ; const versionRadio = 'custom' ; let internalAuthInfo ; const toolRunnerOptions = artifactToolRunner . getOptions ( ) ; let sessionId ; serviceUri = tl . getEndpointUrl ( "SYSTEMVSSCONNECTION" , false ) ; packageName = tl . getVariable ( 'Build.DefinitionName' ) . replace ( /\s/g , "" ) . substring ( 0 , 255 ) . toLowerCase ( ) ; feedId = tl . getInput ( "feedList" ) ; accessToken = pkgLocationUtils . getSystemAccessToken ( ) ; internalAuthInfo = new auth . InternalAuthInfo ( [ ] , accessToken ) ; toolRunnerOptions . env . UNIVERSAL_PUBLISH_PAT = internalAuthInfo . accessToken ; const useSessionEnabled = tl . getVariable ( "Packaging.SavePublishMetadata" ) ; if ( useSessionEnabled ) { let packagingLocation ; try { packagingLocation = await pkgLocationUtils . getNuGetUriFromBaseServiceUri ( serviceUri , accessToken ) ; } catch ( error ) { tl . debug ( JSON . stringify ( error ) ) ; packagingLocation = serviceUri ; } const pkgConn = pkgLocationUtils . getWebApiWithProxy ( packagingLocation , accessToken ) ; sessionId = await ProvenanceHelper . GetSessionId ( feedId , "upack" , pkgConn . serverUrl , [ pkgConn . authHandler ] , pkgConn . options ) ; } tl . debug ( tl . loc ( "Info_UsingArtifactToolPublish" ) ) ; if ( sessionId != null ) { feedId = sessionId ; } const publishOptions = { artifactToolPath , feedId , accountUrl : serviceUri , packageName , packageVersion : version , } as artifactToolRunner . IArtifactToolOptions ; publishPackageUsingArtifactTool ( publishDir , publishOptions , toolRunnerOptions ) ; if ( publishedPackageVar ) { tl . setVariable ( publishedPackageVar , `${ packageName } ${ version }` ) ; } return { toolRan : true , success : true , } ; } catch ( err ) { tl . warning ( `Issue saving package: ${ err }` ) ; return { toolRan : true , success : false , } ; } } function publishPackageUsingArtifactTool ( publishDir , options , execOptions ) { const command = new Array < string > ( ) ; command . push ( "universal" , "publish" , "--feed" , options . feedId , "--service" , options . accountUrl , "--package-name" , options . packageName , "--package-version" , options . packageVersion , "--path" , publishDir , "--patvar" , "UNIVERSAL_PUBLISH_PAT" , "--verbosity" , tl . getInput ( "verbosity" ) ) ; console . log ( tl . loc ( "Info_Publishing" , options . packageName , options . packageVersion , options . feedId ) ) ; const execResult = artifactToolRunner . runArtifactTool ( options . artifactToolPath , command , execOptions ) ; if ( execResult . code === 0 ) { return ; } throw new Error ( tl . loc ( "Error_UnexpectedErrorArtifactTool" , execResult . code , execResult . stderr ? execResult . stderr . trim ( ) : execResult . stderr ) ) ; }