@@ -196,34 +196,15 @@ class ImportCSVDiscussionsAction implements IContributedMenuSource {
196196 try {
197197 this . _logger . info ( `Adding comment for id '${ record . WorkItemId } '` ) ;
198198
199- // Make sure we retry
200- const json : string = await this . _fetch ( `${ hostBaseUrl } ${ project . name } /_apis/wit/workItems/${ record . WorkItemId } /comments?api-version=6.0-preview.3` , {
201- method : 'POST' ,
202- headers : {
203- 'Authorization' : `Bearer ${ accessToken } ` ,
204- 'Content-Type' : 'application/json' ,
205- } ,
206- body : JSON . stringify ( discussion_comment )
207- } ) . then ( async ( response : Response ) => {
208- if ( response . status >= 200 && response . status < 300 ) {
209- this . _logger . info ( `Successfully added comment for id '${ record . WorkItemId } '` ) ;
210- return await response . json ( )
211- }
212- else {
213- this . _logger . info ( `Failed to add comment, response status '${ response . status } '` , record ) ;
214- // Save this failure for later
215- failures . push ( Object . assign ( { } , record ) ) ;
216- return "" ;
217- }
218- } ) ;
199+ let json = await this . postComment ( `${ hostBaseUrl } ${ project . name } /_apis/wit/workItems/${ record . WorkItemId } /comments?api-version=6.0-preview.3` , accessToken , JSON . stringify ( discussion_comment ) ) ;
200+
201+ this . _logger . info ( `Successfully added comment for id '${ record . WorkItemId } '` ) ;
219202
220203 // log any JSON to debug
221204 this . _logger . debug ( "json" , json ) ;
222-
223205 }
224206 catch ( error ) {
225- this . _logger . info ( `Failed to add comment with unhandled error.` , record ) ;
226- this . _logger . error ( error ) ;
207+ this . _logger . info ( `Failed to add comment.` , record , error ) ;
227208
228209 // Save this failure for later
229210 failures . push ( Object . assign ( { } , record ) ) ;
@@ -306,6 +287,29 @@ class ImportCSVDiscussionsAction implements IContributedMenuSource {
306287 } ) ;
307288 }
308289
290+ async postComment ( url : string , accessToken : string , body : string ) : Promise < string > {
291+ return new Promise ( async ( resolve , reject ) => {
292+ let response : Response = await this . _fetch ( url , {
293+ method : 'POST' ,
294+ headers : {
295+ 'Authorization' : `Bearer ${ accessToken } ` ,
296+ 'Content-Type' : 'application/json' ,
297+ } ,
298+ body : body
299+ } ) . then ( async ( response : Response ) => {
300+ if ( response . status >= 200 && response . status < 300 ) {
301+ let json : string = await response . json ( ) ;
302+ resolve ( json ) ;
303+ }
304+ else {
305+ reject ( `Unsuccessful response with status '${ response . status } '` ) ;
306+ }
307+ } ) . catch ( ( error : Error ) => {
308+ reject ( error ) ;
309+ } ) ;
310+ } ) ;
311+ } ;
312+
309313 public execute ( actionContext : any ) {
310314 this . showFileUpload ( ) ;
311315 }
0 commit comments