@@ -30,6 +30,7 @@ class ImportCSVDiscussionsAction implements IContributedMenuSource {
3030
3131 _fetch : any = fetchBuilder ( originalFetch , this . _options ) ;
3232 _json2csvParser = new Parser ( ) ;
33+ _failures : any [ ] = [ ] ;
3334
3435
3536 constructor ( ) {
@@ -78,12 +79,13 @@ class ImportCSVDiscussionsAction implements IContributedMenuSource {
7879
7980 // convert our csv data to json array
8081 const records : any [ ] = await csv ( ) . fromString ( result ) ;
81- let failures : any [ ] = [ ] ;
8282
8383 // do we have an array?
8484 if ( records ) {
8585 this . _logger . info ( `'${ records . length } ' records to import.` ) ;
8686
87+ let promises : Promise < boolean > [ ] = [ ] ;
88+
8789 let batches = records . reduce ( ( r , a ) => {
8890
8991 // Let's make sure we already have a return array intitialized
@@ -143,19 +145,7 @@ class ImportCSVDiscussionsAction implements IContributedMenuSource {
143145 // Loop through each record in this batch and generate the JSON
144146 batch . forEach ( async ( record : any ) => {
145147 this . _logger . debug ( "record" , record ) ;
146- try {
147- if ( ! await this . createComment ( `${ hostBaseUrl } ${ project . name } /_apis/wit/workItems/${ record . WorkItemId } /comments?api-version=6.0-preview.3` , accessToken , record ) )
148- {
149- this . _logger . info ( `Failed to add comment.` , record ) ;
150- // Save this failure for later
151- failures . push ( Object . assign ( { } , record ) ) ;
152- }
153- }
154- catch ( error ) {
155- this . _logger . info ( `Failed to add comment.` , record , error ) ;
156- // Save this failure for later
157- failures . push ( Object . assign ( { } , record ) ) ;
158- }
148+ promises . push ( this . createComment ( `${ hostBaseUrl } ${ project . name } /_apis/wit/workItems/${ record . WorkItemId } /comments?api-version=6.0-preview.3` , accessToken , record ) ) ;
159149 } ) ;
160150
161151 /* // Finally apply the batched updates
@@ -175,34 +165,37 @@ class ImportCSVDiscussionsAction implements IContributedMenuSource {
175165 }
176166 } */
177167 } ) ;
178- }
179-
180- this . _logger . info ( `'${ failures . length } ' records failed to import.` ) ;
181-
182- if ( failures . length > 0 ) {
183- try {
184- // convert our array of failed imports back into csv format
185- const csv = this . _json2csvParser . parse ( failures ) ;
186-
187- // create a buffer for our csv string
188- const buff = Buffer . from ( csv , 'utf-8' ) ;
189168
190- // decode buffer as Base64
191- const base64 = buff . toString ( 'base64' ) ;
169+ // wait for promises
170+ Promise . all ( promises ) . then ( ( ) => {
171+ this . _logger . info ( `'${ this . _failures . length } ' records failed to import.` ) ;
192172
193- // Attempt to send our file containing failures back to the user
194- let a : HTMLAnchorElement = document . createElement ( 'a' ) ;
195- document . body . appendChild ( a ) ;
196- a . download = "import-failed.csv" ;
197- a . href = `data:text/plain;base64,${ base64 } ` ;
198- a . click ( ) ;
199-
200- } catch ( error ) {
201- this . _logger . error ( error ) ;
202- }
173+ if ( this . _failures . length > 0 ) {
174+ try {
175+ // convert our array of failed imports back into csv format
176+ const csv = this . _json2csvParser . parse ( this . _failures ) ;
177+
178+ // create a buffer for our csv string
179+ const buff = Buffer . from ( csv , 'utf-8' ) ;
180+
181+ // decode buffer as Base64
182+ const base64 = buff . toString ( 'base64' ) ;
183+
184+ // Attempt to send our file containing failures back to the user
185+ let a : HTMLAnchorElement = document . createElement ( 'a' ) ;
186+ document . body . appendChild ( a ) ;
187+ a . download = "import-failed.csv" ;
188+ a . href = `data:text/plain;base64,${ base64 } ` ;
189+ a . click ( ) ;
190+
191+ } catch ( error ) {
192+ this . _logger . error ( error ) ;
193+ }
194+ }
195+
196+ this . _logger . info ( `Ended Import.` ) ;
197+ } ) ;
203198 }
204-
205- this . _logger . info ( `Ended Import.` ) ;
206199 }
207200 else {
208201 alert ( "Error : CSV File is Empty." )
@@ -236,6 +229,8 @@ class ImportCSVDiscussionsAction implements IContributedMenuSource {
236229
237230 async createComment ( url : string , accessToken : string , record : any ) : Promise < boolean > {
238231 return new Promise ( async ( resolve , reject ) => {
232+ this . _failures = [ ] ;
233+
239234 let header : Array < string > = [ ] ;
240235 let cols : Array < string > = [ ] ;
241236
@@ -303,9 +298,12 @@ class ImportCSVDiscussionsAction implements IContributedMenuSource {
303298 resolve ( true ) ;
304299 }
305300 else {
301+ this . _failures . push ( Object . assign ( { } , record ) ) ;
306302 resolve ( false ) ;
307303 }
308304 } ) . catch ( ( error : Error ) => {
305+ // Save this failure for later
306+ this . _failures . push ( Object . assign ( { } , record ) ) ;
309307 reject ( error ) ;
310308 } ) ;
311309
0 commit comments