import AdmZip = require ( 'adm-zip' ) ; import os = require ( "os" ) ; import * as path from "path" ; import * as semver from "semver" ; import * as pkgLocationUtils from "./locationUtilities" ; import * as tl from "azure-pipelines-task-lib" ; import * as toollib from "azure-pipelines-tool-lib/tool" ; export function getArtifactToolLocation ( dirName ) { let toolPath = path . join ( dirName , "ArtifactTool.exe" ) ; if ( tl . osType ( ) !== "Windows_NT" ) { toolPath = path . join ( dirName , "artifacttool" ) ; } return toolPath ; } function _createExtractFolder ( dest ? ) { if ( ! dest ) { dest = path . join ( tl . getVariable ( "Agent.TempDirectory" ) , "artifactTool" ) ; } tl . mkdirP ( dest ) ; return dest ; } export async function extractZip ( file ) { if ( ! file ) { throw new Error ( "parameter 'file' is required" ) ; } const dest = _createExtractFolder ( ) ; const zip = new AdmZip ( file ) ; zip . extractAllTo ( dest , true ) ; return dest ; } export async function getArtifactToolFromService ( serviceUri , accessToken , toolName ) { const overrideArtifactToolPath = tl . getVariable ( "UPack.OverrideArtifactToolPath" ) ; if ( overrideArtifactToolPath != null ) { return getArtifactToolLocation ( overrideArtifactToolPath ) ; } let osName = tl . osType ( ) ; let arch = os . arch ( ) ; if ( osName === "Windows_NT" ) { osName = "windows" ; } if ( arch === "x64" ) { arch = "amd64" ; } const blobstoreAreaName = "clienttools" ; const blobstoreAreaId = "187ec90d-dd1e-4ec6-8c57-937d979261e5" ; const ApiVersion = "5.0-preview" ; const blobstoreConnection = pkgLocationUtils . getWebApiWithProxy ( serviceUri , accessToken ) ; try { const artifactToolGetUrl = await blobstoreConnection . vsoClient . getVersioningData ( ApiVersion , blobstoreAreaName , blobstoreAreaId , { toolName } , { osName , arch } ) ; const artifactToolUri = await blobstoreConnection . rest . get ( artifactToolGetUrl . requestUrl ) ; if ( artifactToolUri . statusCode !== 200 ) { tl . debug ( tl . loc ( "Error_UnexpectedErrorFailedToGetToolMetadata" , artifactToolUri . toString ( ) ) ) ; throw new Error ( tl . loc ( "Error_UnexpectedErrorFailedToGetToolMetadata" , artifactToolGetUrl . requestUrl ) ) ; } let artifactToolPath = toollib . findLocalTool ( toolName , artifactToolUri . result [ 'version' ] ) ; if ( ! artifactToolPath ) { tl . debug ( tl . loc ( "Info_DownloadingArtifactTool" , artifactToolUri . result [ 'uri' ] ) ) ; const zippedToolsDir = await toollib . downloadTool ( artifactToolUri . result [ 'uri' ] ) ; tl . debug ( "Downloaded zipped artifact tool to " + zippedToolsDir ) ; const unzippedToolsDir = await extractZip ( zippedToolsDir ) ; artifactToolPath = await toollib . cacheDir ( unzippedToolsDir , "ArtifactTool" , artifactToolUri . result [ 'version' ] ) ; } else { tl . debug ( tl . loc ( "Info_ResolvedToolFromCache" , artifactToolPath ) ) ; } return getArtifactToolLocation ( artifactToolPath ) ; } catch ( err ) { tl . warning ( err ) ; return null ; } } export function getVersionUtility ( versionRadio , highestVersion ) { switch ( versionRadio ) { case "patch" : return semver . inc ( highestVersion , "patch" ) ; case "minor" : return semver . inc ( highestVersion , "minor" ) ; case "major" : return semver . inc ( highestVersion , "major" ) ; default : return null ; } } export async function getPackageNameFromId ( serviceUri , accessToken , feedId , packageId ) { const ApiVersion = "3.0-preview.1" ; const PackagingAreaName = "Packaging" ; const PackageAreaId = "7a20d846-c929-4acc-9ea2-0d5a7df1b197" ; const feedConnection = pkgLocationUtils . getWebApiWithProxy ( serviceUri , accessToken ) ; const packageUrl = await new Promise < string > ( ( resolve , reject ) => { const getVersioningDataPromise = feedConnection . vsoClient . getVersioningData ( ApiVersion , PackagingAreaName , PackageAreaId , { feedId , packageId } ) ; getVersioningDataPromise . then ( result => { return resolve ( result . requestUrl ) ; } ) ; getVersioningDataPromise . catch ( error => { return reject ( error ) ; } ) ; } ) ; try { const response = await feedConnection . rest . get ( packageUrl ) ; if ( response . statusCode === 200 && response . result [ 'name' ] ) { return response . result [ 'name' ] ; } return packageId ; } catch ( err ) { return packageId ; } } export async function getHighestPackageVersionFromFeed ( serviceUri , accessToken , feedId , packageName ) { const ApiVersion = "3.0-preview.1" ; const PackagingAreaName = "Packaging" ; const PackageAreaId = "7a20d846-c929-4acc-9ea2-0d5a7df1b197" ; const feedConnection = pkgLocationUtils . getWebApiWithProxy ( serviceUri , accessToken ) ; const packageUrl = await new Promise < string > ( ( resolve , reject ) => { const getVersioningDataPromise = feedConnection . vsoClient . getVersioningData ( ApiVersion , PackagingAreaName , PackageAreaId , { feedId } , { packageNameQuery : packageName , protocolType : "upack" , includeDeleted : "true" , includeUrls : "false" } ) ; getVersioningDataPromise . then ( result => { return resolve ( result . requestUrl ) ; } ) ; getVersioningDataPromise . catch ( error => { return reject ( error ) ; } ) ; } ) ; const versionResponse = await new Promise < string > ( ( resolve , reject ) => { const responsePromise = feedConnection . rest . get ( packageUrl ) ; responsePromise . then ( result => { if ( result . result [ 'count' ] === 0 ) { return resolve ( "0.0.0" ) ; } else { result . result [ 'value' ] . forEach ( element => { if ( element . name === packageName . toLowerCase ( ) ) { return resolve ( element . versions [ 0 ] . version ) ; } } ) ; } } ) ; responsePromise . catch ( error => { return reject ( error ) ; } ) ; } ) ; return versionResponse ; }