11using System ;
22using System . Collections . Generic ;
33using System . ComponentModel . Composition ;
4- using System . Diagnostics ;
54using System . Linq ;
65using System . Net . Http ;
76using System . Net . Http . Headers ;
7+ using System . Runtime . CompilerServices ;
88using System . Text ;
99using System . Text . RegularExpressions ;
1010using System . Threading . Tasks ;
1818using Newtonsoft . Json . Linq ;
1919using ResourceManager ;
2020
21+ [ assembly: InternalsVisibleTo ( "Gitextensions.AzureDevOpsCommitMessageTests" ) ]
22+
2123namespace GitExtensions . AzureDevOpsCommitMessage
2224{
2325 [ Export ( typeof ( IGitPlugin ) ) ]
@@ -365,24 +367,17 @@ private async Task<IList<CommitTemplate>> GetCommitTemplatesAsync(HttpClient cli
365367 var response = await client . SendAsync ( request ) . ConfigureAwait ( true ) ;
366368 if ( ! response . IsSuccessStatusCode )
367369 {
368- return new [ ] { new CommitTemplate ( $ "Call error", response . ReasonPhrase ) } ;
370+ return new [ ] { new CommitTemplate ( "Call error" , response . ReasonPhrase ) } ;
369371 }
370372
371373 try
372374 {
373- var commitTemplates = new List < CommitTemplate > ( ) ;
374375 var responseContent = await response . Content . ReadAsStringAsync ( ) . ConfigureAwait ( false ) ;
375- var workItems = JObject . Parse ( responseContent ) [ "workItems" ] ;
376- if ( workItems == null )
377- {
378- return commitTemplates ;
379- }
380-
381- foreach ( JToken workitem in workItems )
376+ var commitTemplates = new List < CommitTemplate > ( ) ;
377+ foreach ( var workitem in GetWorkitems ( responseContent ) )
382378 {
383- commitTemplates . Add ( await GetWorkItemAsync ( client , workitem [ "id" ] . ToString ( ) , workitem [ "url" ] . ToString ( ) , stringTemplate ) ) ;
379+ commitTemplates . Add ( await GetWorkItemAsync ( client , workitem . id , workitem . url , stringTemplate ) ) ;
384380 }
385-
386381 return commitTemplates ;
387382 }
388383 catch ( Exception e )
@@ -397,8 +392,18 @@ private async Task<IList<CommitTemplate>> GetCommitTemplatesAsync(HttpClient cli
397392 }
398393 }
399394
400- private async Task < CommitTemplate > GetWorkItemAsync ( HttpClient client , string id , string url ,
401- string template )
395+ private IEnumerable < ( string id , string url ) > GetWorkitems ( string jsonWorkitems )
396+ {
397+ var workItems = JObject . Parse ( jsonWorkitems ) [ "workItems" ] ;
398+ if ( workItems == null )
399+ {
400+ return Enumerable . Empty < ( string id , string url ) > ( ) ;
401+ }
402+
403+ return workItems . Select ( w => ( w [ "id" ] . ToString ( ) , w [ "url" ] . ToString ( ) ) ) ;
404+ }
405+
406+ private async Task < CommitTemplate > GetWorkItemAsync ( HttpClient client , string id , string url , string template )
402407 {
403408 using ( var request = new HttpRequestMessage ( new HttpMethod ( "GET" ) , url ) )
404409 {
@@ -407,14 +412,8 @@ private async Task<CommitTemplate> GetWorkItemAsync(HttpClient client, string id
407412 {
408413 try
409414 {
410- var responseContent = await response . Content . ReadAsStringAsync ( ) ;
411- var workItemData = JObject . Parse ( responseContent ) [ "fields" ] ;
412- if ( _btnPreview != null )
413- {
414- _allFieldsAndValues = ExtractAllFields ( workItemData ) ;
415- }
416-
417- return new CommitTemplate ( PopulateTemplate ( "{id}: {System.Title}" , id , workItemData ) , PopulateTemplate ( template , id , workItemData ) ) ;
415+ var jsonWorkitem = await response . Content . ReadAsStringAsync ( ) ;
416+ return GetCommitTemplateFromWorkitemData ( id , template , jsonWorkitem ) ;
418417 }
419418 catch ( Exception e )
420419 {
@@ -426,6 +425,18 @@ private async Task<CommitTemplate> GetWorkItemAsync(HttpClient client, string id
426425 }
427426 }
428427
428+ private CommitTemplate GetCommitTemplateFromWorkitemData ( string id , string template , string jsonWorkitem )
429+ {
430+ var workItemData = JObject . Parse ( jsonWorkitem ) [ "fields" ] ;
431+ if ( _btnPreview != null )
432+ {
433+ _allFieldsAndValues = ExtractAllFields ( workItemData ) ;
434+ }
435+
436+ return new CommitTemplate ( PopulateTemplate ( "{id}: {System.Title}" , id , workItemData ) ,
437+ PopulateTemplate ( template , id , workItemData ) ) ;
438+ }
439+
429440 private string Elide ( string value )
430441 {
431442 if ( string . IsNullOrEmpty ( value ) )
@@ -560,7 +571,7 @@ public static string ExtractTextFromHtml(string html)
560571 return s ;
561572 }
562573
563- private class CommitTemplate
574+ public class CommitTemplate
564575 {
565576 public string Title { get ; }
566577 public string Text { get ; }
@@ -571,5 +582,24 @@ public CommitTemplate(string title, string text)
571582 Text = text ;
572583 }
573584 }
585+
586+
587+ internal TestAccessor GetTestAccessor ( )
588+ => new ( this ) ;
589+
590+ internal readonly struct TestAccessor
591+ {
592+ private readonly Plugin _plugin ;
593+
594+ public TestAccessor ( Plugin plugin )
595+ {
596+ _plugin = plugin ;
597+ }
598+
599+ public CommitTemplate GetCommitTemplateFromWorkItemData ( string id , string template , string jsonWorkitem )
600+ => _plugin . GetCommitTemplateFromWorkitemData ( id , template , jsonWorkitem ) ;
601+ public IEnumerable < ( string id , string url ) > GetWorkItems ( string jsonWorkitem )
602+ => _plugin . GetWorkitems ( jsonWorkitem ) ;
603+ }
574604 }
575605}
0 commit comments