44// export export API_TOKEN=<yourAllScopesApiToken>
55// export API_PROJECT=test
66import * as cm from './common' ;
7+ import * as nodeApi from 'azure-devops-node-api' ;
8+ import * as cApi from 'azure-devops-node-api/CoreApi' ;
9+ import * as coreInterfaces from 'azure-devops-node-api/interfaces/CoreInterfaces'
710
811let samples : string [ ] = require ( './samples.json' ) ;
12+ let coreApi : cApi . ICoreApi ;
13+ const maxLoops : number = 500 ;
914
1015let selection : string = process . argv [ 2 ] ;
1116if ( selection ) {
@@ -17,7 +22,77 @@ if (selection) {
1722 samples = [ selection ] ;
1823}
1924
25+ async function createProject ( projectId : string ) : Promise < boolean > {
26+ console . log ( 'Cleaning up from last run' ) ;
27+ if ( ! ( await deleteProject ( projectId ) ) ) {
28+ console . log ( 'Failed to delete previous project' ) ;
29+ return false ;
30+ }
31+
32+ const projectToCreate : coreInterfaces . TeamProject = { name : projectId ,
33+ description : 'sample project created automatically by azure-devops-node-api.' ,
34+ visibility : 1 ,
35+ capabilities : { versioncontrol : { sourceControlType : 'Git' } ,
36+ processTemplate : { templateTypeId : '6b724908-ef14-45cf-84f8-768b5384da45' } } ,
37+ _links : null ,
38+ defaultTeam : null ,
39+ abbreviation : null ,
40+ id : null ,
41+ revision : null ,
42+ state : null ,
43+ url : null } ;
44+ await coreApi . queueCreateProject ( projectToCreate ) ;
45+
46+ //Poll until project exists
47+ let project : coreInterfaces . TeamProject = null ;
48+ console . log ( 'Waiting for project to spin up' ) ;
49+ let numLoops = 0 ;
50+ while ( numLoops < maxLoops ) {
51+ project = await coreApi . getProject ( projectId ) ;
52+ numLoops += 1 ;
53+ if ( project ) {
54+ return true ;
55+ }
56+ }
57+ return false ;
58+ }
59+
60+ async function deleteProject ( projectId : string ) : Promise < boolean > {
61+ let project : coreInterfaces . TeamProject = await coreApi . getProject ( projectId ) ;
62+ if ( ! project ) {
63+ //If no project to clean up, just return
64+ console . log ( "Nothing to clean up" ) ;
65+ return true ;
66+ }
67+ await coreApi . queueDeleteProject ( project . id ) ;
68+
69+ //Poll until project no longer exists
70+ console . log ( 'Waiting for project to be deleted' ) ;
71+ let numLoops = 0 ;
72+ while ( project && numLoops < maxLoops ) {
73+ project = await coreApi . getProject ( projectId ) ;
74+ numLoops += 1 ;
75+ if ( ! project ) {
76+ return true ;
77+ }
78+ }
79+ return false ;
80+ }
81+
2082async function runSamples ( selected ?: string ) {
83+ const webApi : nodeApi . WebApi = await cm . getWebApi ( ) ;
84+ coreApi = await webApi . getCoreApi ( ) ;
85+ const projectId : string = 'azureDevopsNodeSampleProject' ;
86+
87+ cm . heading ( 'Creating example project' ) ;
88+ if ( await createProject ( projectId ) ) {
89+ console . log ( 'Project created' ) ;
90+ }
91+ else {
92+ console . log ( 'Failed to create project, exiting' ) ;
93+ return ;
94+ }
95+
2196 for ( let i : number = 0 ; i < samples . length ; i ++ ) {
2297 let sample : string = samples [ i ] ;
2398
@@ -27,7 +102,15 @@ async function runSamples(selected?: string) {
27102
28103 cm . banner ( 'Sample ' + sample ) ;
29104 var sm = require ( './' + sample + '.js' ) ;
30- await sm . run ( ) ;
105+ await sm . run ( projectId ) ;
106+ }
107+
108+ cm . heading ( 'Cleaning up project' ) ;
109+ if ( await deleteProject ( projectId ) ) {
110+ console . log ( 'Done' ) ;
111+ }
112+ else {
113+ console . log ( 'Failed to delete project' ) ;
31114 }
32115}
33116
0 commit comments