@@ -4,25 +4,19 @@ import path = require('path');
44import fs = require( 'fs' ) ;
55import tl = require( 'vsts-task-lib/task' ) ;
66import tr = require( 'vsts-task-lib/toolrunner' ) ;
7- import locationHelpers = require ( "nuget-task- common/LocationHelpers" ) ; // TODO: refactor
7+ import * as pkgLocationUtils from "utility- common/packaging/locationUtilities" ;
88import systemToken = require( 'utility-common/accesstoken' ) ;
99
1010import * as url from "url" ;
11- import * as str from 'string' ;
1211import * as xml2js from 'xml2js' ;
1312import * as fse from 'fs-extra' ;
14- import * as cheerio from 'cheerio' ;
15- import * as vsts from "vso-node-api/WebApi" ;
1613
1714let stripbom = require ( 'strip-bom' ) ;
1815let base64 = require ( 'base-64' ) ;
1916let utf8 = require ( 'utf8' ) ;
2017let uuidV4 = require ( "uuid/v4" ) ;
2118
2219const accessTokenEnvSetting : string = 'ENV_MAVEN_ACCESS_TOKEN' ;
23- const ApiVersion = "3.0-preview.1" ;
24- const PackagingAreaName : string = "maven" ;
25- const PackageAreaId : string = "F285A171-0DF5-4C49-AAF2-17D0D37D9F0E" ;
2620
2721function readXmlFileAsJson ( filePath : string ) : Q . Promise < any > {
2822 return readFile ( filePath , 'utf-8' )
@@ -33,7 +27,7 @@ function readFile(filePath: string, encoding: string): Q.Promise<string> {
3327 return Q . nfcall < string > ( fs . readFile , filePath , encoding ) ;
3428}
3529
36- function convertXmlStringToJson ( xmlContent : string ) : Q . Promise < any > {
30+ async function convertXmlStringToJson ( xmlContent : string ) : Promise < any > {
3731 return Q . nfcall < any > ( xml2js . parseString , stripbom ( xmlContent ) ) ;
3832}
3933
@@ -44,7 +38,7 @@ function writeJsonAsXmlFile(filePath: string, jsonContent: any, rootName:string)
4438 rootName : rootName
4539 } ) ;
4640 let xml = builder . buildObject ( jsonContent ) ;
47- xml = str ( xml ) . replaceAll ( ' 
' , '' ) . s ;
41+ xml = xml . replace ( / & # x D ; / g , "" ) ;
4842 return writeFile ( filePath , xml ) ;
4943}
5044
@@ -194,64 +188,78 @@ interface RepositoryInfo {
194188 id :string ;
195189}
196190
197- function collectFeedRepositories ( pomContents :string ) : Q . Promise < any > {
198- return convertXmlStringToJson ( pomContents ) . then ( function ( pomJson ) {
199- let repos :RepositoryInfo [ ] = [ ] ;
200- if ( ! pomJson ) {
201- tl . debug ( 'Incomplete pom: ' + pomJson ) ;
202- return Q . resolve ( repos ) ;
203- }
204- let collectionUrl = tl . getVariable ( "System.TeamFoundationCollectionUri" ) ;
205- return locationHelpers . assumeNuGetUriPrefixes ( collectionUrl ) . then ( function ( packageUrl ) {
206- tl . debug ( 'collectionUrl=' + collectionUrl ) ;
207- tl . debug ( 'packageUrl=' + packageUrl ) ;
208- let collectionHostname :string = url . parse ( collectionUrl ) . hostname . toLowerCase ( ) ;
209- let packageHostname :string = packageUrl [ 1 ] ;
210- if ( packageHostname ) {
211- url . parse ( packageHostname ) . hostname . toLowerCase ( ) ;
212- } else {
213- packageHostname = collectionHostname ;
214- }
215- let parseRepos :( project ) => void = function ( project ) {
216- if ( project && project . repositories ) {
217- for ( let r of project . repositories ) {
218- r = r instanceof Array ? r [ 0 ] : r ;
219- if ( r . repository ) {
220- for ( let repo of r . repository ) {
221- repo = repo instanceof Array ? repo [ 0 ] : repo ;
222- let url :string = repo . url instanceof Array ? repo . url [ 0 ] : repo . url ;
223- if ( url && ( url . toLowerCase ( ) . includes ( collectionHostname ) ||
224- url . toLowerCase ( ) . includes ( packageHostname ) ) ) {
225- tl . debug ( 'using credentials for url: ' + url ) ;
226- repos . push ( {
227- id : ( repo . id && repo . id instanceof Array )
228- ? repo . id [ 0 ]
229- : repo . id
230- } ) ;
231- }
232- }
191+ async function collectFeedRepositories ( pomContents :string ) : Promise < any > {
192+ let pomJson = await convertXmlStringToJson ( pomContents ) ;
193+ let repos :RepositoryInfo [ ] = [ ] ;
194+ if ( ! pomJson ) {
195+ tl . debug ( 'Incomplete pom: ' + pomJson ) ;
196+ return Promise . resolve ( repos ) ;
197+ }
198+ const collectionUrl = tl . getVariable ( "System.TeamFoundationCollectionUri" ) ;
199+ let packagingLocation : pkgLocationUtils . PackagingLocation ;
200+ try {
201+ packagingLocation = await pkgLocationUtils . getPackagingUris ( pkgLocationUtils . ProtocolType . Maven ) ;
202+ } catch ( error ) {
203+ tl . debug ( "Unable to get packaging URIs, using default collection URI" ) ;
204+ tl . debug ( JSON . stringify ( error ) ) ;
205+ packagingLocation = {
206+ PackagingUris : [ collectionUrl ] ,
207+ DefaultPackagingUri : collectionUrl
208+ } ;
209+ }
210+
211+ let packageUrl = packagingLocation . DefaultPackagingUri ;
212+ tl . debug ( 'collectionUrl=' + collectionUrl ) ;
213+ tl . debug ( 'packageUrl=' + packageUrl ) ;
214+ let collectionName :string = url . parse ( collectionUrl ) . hostname . toLowerCase ( ) ;
215+ let collectionPathName = url . parse ( collectionUrl ) . pathname ;
216+ if ( collectionPathName && collectionPathName . length > 1 ) {
217+ collectionName = collectionName + collectionPathName . toLowerCase ( ) ;
218+ tl . debug ( 'collectionName=' + collectionName ) ;
219+ }
220+ if ( packageUrl ) {
221+ url . parse ( packageUrl ) . hostname . toLowerCase ( ) ;
222+ } else {
223+ packageUrl = collectionName ;
224+ }
225+ let parseRepos :( project ) => void = function ( project ) {
226+ if ( project && project . repositories ) {
227+ for ( let r of project . repositories ) {
228+ r = r instanceof Array ? r [ 0 ] : r ;
229+ if ( r . repository ) {
230+ for ( let repo of r . repository ) {
231+ repo = repo instanceof Array ? repo [ 0 ] : repo ;
232+ let url :string = repo . url instanceof Array ? repo . url [ 0 ] : repo . url ;
233+ if ( url && ( url . toLowerCase ( ) . includes ( collectionName ) ||
234+ url . toLowerCase ( ) . includes ( packageUrl ) ) ) {
235+ tl . debug ( 'using credentials for url: ' + url ) ;
236+ repos . push ( {
237+ id : ( repo . id && repo . id instanceof Array )
238+ ? repo . id [ 0 ]
239+ : repo . id
240+ } ) ;
233241 }
234242 }
235243 }
236- } ;
237-
238- if ( pomJson . projects && pomJson . projects . project ) {
239- for ( let project of pomJson . projects . project ) {
240- parseRepos ( project ) ;
241- }
242- } else if ( pomJson . project ) {
243- parseRepos ( pomJson . project ) ;
244- } else {
245- tl . warning ( tl . loc ( 'EffectivePomInvalid' ) ) ;
246244 }
245+ }
246+ } ;
247247
248- tl . debug ( 'Feeds found: ' + JSON . stringify ( repos ) ) ;
249- return Q . resolve ( repos ) ;
250- } ) ;
251- } ) ;
248+ if ( pomJson . projects && pomJson . projects . project ) {
249+ for ( let project of pomJson . projects . project ) {
250+ parseRepos ( project ) ;
251+ }
252+ } else if ( pomJson . project ) {
253+ parseRepos ( pomJson . project ) ;
254+ } else {
255+ tl . warning ( tl . loc ( 'EffectivePomInvalid' ) ) ;
256+ }
257+
258+ tl . debug ( 'Feeds found: ' + JSON . stringify ( repos ) ) ;
259+ return Promise . resolve ( repos ) ;
252260}
253261
254- export function collectFeedRepositoriesFromEffectivePom ( mavenOutput :string ) : Q . Promise < any > {
262+ export function collectFeedRepositoriesFromEffectivePom ( mavenOutput :string ) : Promise < any > {
255263 tl . debug ( 'collecting account feeds from effective pom' ) ;
256264 const effectivePomStartTag :string = '<!-- Effective POM' ;
257265 const projectsBeginTag :string = '<projects' ;
@@ -263,7 +271,7 @@ export function collectFeedRepositoriesFromEffectivePom(mavenOutput:string): Q.P
263271 let effectivePomStart :number = xml . lastIndexOf ( effectivePomStartTag ) ;
264272 if ( effectivePomStart === - 1 ) {
265273 tl . warning ( tl . loc ( 'EffectivePomInvalid' ) ) ;
266- return Q . resolve ( true ) ;
274+ return Promise . resolve ( true ) ;
267275 }
268276
269277 let xmlStart :number = xml . indexOf ( projectsBeginTag , effectivePomStart ) ;
@@ -281,7 +289,7 @@ export function collectFeedRepositoriesFromEffectivePom(mavenOutput:string): Q.P
281289 }
282290
283291 tl . warning ( tl . loc ( 'EffectivePomInvalid' ) ) ;
284- return Q . resolve ( true ) ;
292+ return Promise . resolve ( true ) ;
285293}
286294
287295export function getExecOptions ( ) : tr . IExecOptions {
0 commit comments