1
1
using System ;
2
2
using System . Collections . Generic ;
3
3
using System . ComponentModel . Composition ;
4
- using System . Diagnostics ;
5
4
using System . Linq ;
6
5
using System . Net . Http ;
7
6
using System . Net . Http . Headers ;
7
+ using System . Runtime . CompilerServices ;
8
8
using System . Text ;
9
9
using System . Text . RegularExpressions ;
10
10
using System . Threading . Tasks ;
18
18
using Newtonsoft . Json . Linq ;
19
19
using ResourceManager ;
20
20
21
+ [ assembly: InternalsVisibleTo ( "Gitextensions.AzureDevOpsCommitMessageTests" ) ]
22
+
21
23
namespace GitExtensions . AzureDevOpsCommitMessage
22
24
{
23
25
[ Export ( typeof ( IGitPlugin ) ) ]
@@ -365,24 +367,17 @@ private async Task<IList<CommitTemplate>> GetCommitTemplatesAsync(HttpClient cli
365
367
var response = await client . SendAsync ( request ) . ConfigureAwait ( true ) ;
366
368
if ( ! response . IsSuccessStatusCode )
367
369
{
368
- return new [ ] { new CommitTemplate ( $ "Call error", response . ReasonPhrase ) } ;
370
+ return new [ ] { new CommitTemplate ( "Call error" , response . ReasonPhrase ) } ;
369
371
}
370
372
371
373
try
372
374
{
373
- var commitTemplates = new List < CommitTemplate > ( ) ;
374
375
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 ) )
382
378
{
383
- commitTemplates . Add ( await GetWorkItemAsync ( client , workitem [ "id" ] . ToString ( ) , workitem [ "url" ] . ToString ( ) , stringTemplate ) ) ;
379
+ commitTemplates . Add ( await GetWorkItemAsync ( client , workitem . id , workitem . url , stringTemplate ) ) ;
384
380
}
385
-
386
381
return commitTemplates ;
387
382
}
388
383
catch ( Exception e )
@@ -397,8 +392,18 @@ private async Task<IList<CommitTemplate>> GetCommitTemplatesAsync(HttpClient cli
397
392
}
398
393
}
399
394
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 )
402
407
{
403
408
using ( var request = new HttpRequestMessage ( new HttpMethod ( "GET" ) , url ) )
404
409
{
@@ -407,14 +412,8 @@ private async Task<CommitTemplate> GetWorkItemAsync(HttpClient client, string id
407
412
{
408
413
try
409
414
{
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 ) ;
418
417
}
419
418
catch ( Exception e )
420
419
{
@@ -426,6 +425,18 @@ private async Task<CommitTemplate> GetWorkItemAsync(HttpClient client, string id
426
425
}
427
426
}
428
427
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
+
429
440
private string Elide ( string value )
430
441
{
431
442
if ( string . IsNullOrEmpty ( value ) )
@@ -560,7 +571,7 @@ public static string ExtractTextFromHtml(string html)
560
571
return s ;
561
572
}
562
573
563
- private class CommitTemplate
574
+ public class CommitTemplate
564
575
{
565
576
public string Title { get ; }
566
577
public string Text { get ; }
@@ -571,5 +582,24 @@ public CommitTemplate(string title, string text)
571
582
Text = text ;
572
583
}
573
584
}
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
+ }
574
604
}
575
605
}
0 commit comments