3
3
using System ;
4
4
using System . Collections . Generic ;
5
5
using System . Linq ;
6
+ using System . Threading . Tasks ;
6
7
using Cake . AzureDevOps . Authentication ;
7
8
using Cake . Core . Diagnostics ;
8
9
using Microsoft . TeamFoundation . Build . WebApi ;
10
+ using Microsoft . TeamFoundation . TestManagement . WebApi ;
9
11
10
12
/// <summary>
11
13
/// Class for writing issues to Azure DevOps pull requests.
@@ -16,6 +18,7 @@ public sealed class AzureDevOpsBuild
16
18
private readonly IAzureDevOpsCredentials credentials ;
17
19
private readonly bool throwExceptionIfBuildCouldNotBeFound ;
18
20
private readonly IBuildClientFactory buildClientFactory ;
21
+ private readonly ITestManagementClientFactory testClientFactory ;
19
22
private readonly Build build ;
20
23
21
24
/// <summary>
@@ -45,6 +48,7 @@ internal AzureDevOpsBuild(ICakeLog log, AzureDevOpsBuildsSettings settings, Buil
45
48
this . log = log ;
46
49
this . build = build ;
47
50
this . buildClientFactory = new BuildClientFactory ( ) ;
51
+ this . testClientFactory = new TestManagementClientFactory ( ) ;
48
52
this . credentials = settings . Credentials ;
49
53
this . CollectionUrl = settings . CollectionUrl ;
50
54
}
@@ -65,6 +69,7 @@ internal AzureDevOpsBuild(ICakeLog log, AzureDevOpsBuildSettings settings, IBuil
65
69
66
70
this . log = log ;
67
71
this . buildClientFactory = buildClientFactory ;
72
+ this . testClientFactory = new TestManagementClientFactory ( ) ;
68
73
this . credentials = settings . Credentials ;
69
74
this . CollectionUrl = settings . CollectionUrl ;
70
75
this . throwExceptionIfBuildCouldNotBeFound = settings . ThrowExceptionIfBuildCouldNotBeFound ;
@@ -401,6 +406,62 @@ public IEnumerable<AzureDevOpsBuildArtifact> GetArtifacts()
401
406
}
402
407
}
403
408
409
+ /// <summary>
410
+ /// Gets the test run ID's of a build.
411
+ /// </summary>
412
+ /// <returns>A list of RunIDs as int.</returns>
413
+ public IEnumerable < AzureDevOpsTestRun > GetTestRuns ( )
414
+ {
415
+ if ( ! this . ValidateBuild ( ) )
416
+ {
417
+ return new List < AzureDevOpsTestRun > ( ) ;
418
+ }
419
+
420
+ using ( var testClient = this . testClientFactory . CreateTestManagementClient ( this . CollectionUrl , this . credentials . ToVssCredentials ( ) ) )
421
+ {
422
+ return
423
+ testClient
424
+ . GetTestResultDetailsForBuildAsync ( this . ProjectId , this . build . Id )
425
+ . GetAwaiter ( )
426
+ . GetResult ( )
427
+ . ResultsForGroup
428
+ . SelectMany ( testResultGroup => testResultGroup . Results ) . ToList ( )
429
+ . GroupBy ( testResult => testResult . TestRun . Id )
430
+ . Select ( runGroup => int . Parse ( runGroup . Key ) )
431
+ . Select ( runId =>
432
+ new AzureDevOpsTestRun
433
+ {
434
+ RunId = runId ,
435
+ TestResults =
436
+ this . GetTestResults ( testClient , runId )
437
+ . GetAwaiter ( )
438
+ . GetResult ( ) ,
439
+ } ) ;
440
+ }
441
+ }
442
+
443
+ /// <summary>
444
+ /// Gets the test runs of this build.
445
+ /// </summary>
446
+ /// <param name="testClient">Instance of a <see cref="TestManagementHttpClient"/> class.</param>
447
+ /// <param name="runId">Id of the test run.</param>
448
+ /// <returns>The test results for a build or an empty list if no build could be found and
449
+ /// <see cref="AzureDevOpsBuildSettings.ThrowExceptionIfBuildCouldNotBeFound"/> is set to <c>false</c>.</returns>
450
+ /// <exception cref="AzureDevOpsBuildNotFoundException">If build could not be found and
451
+ /// <see cref="AzureDevOpsBuildSettings.ThrowExceptionIfBuildCouldNotBeFound"/> is set to <c>true</c>.</exception>
452
+ private async Task < IEnumerable < AzureDevOpsTestResult > > GetTestResults ( TestManagementHttpClient testClient , int runId )
453
+ {
454
+ return
455
+ ( await testClient . GetTestResultsAsync ( this . ProjectId , runId ) . ConfigureAwait ( false ) )
456
+ . Select ( test =>
457
+ new AzureDevOpsTestResult
458
+ {
459
+ AutomatedTestName = test . AutomatedTestName ,
460
+ Outcome = test . Outcome ,
461
+ ErrorMessage = test . ErrorMessage ,
462
+ } ) ;
463
+ }
464
+
404
465
/// <summary>
405
466
/// Validates if a build could be found.
406
467
/// Depending on <see cref="AzureDevOpsBuildSettings.ThrowExceptionIfBuildCouldNotBeFound"/>
0 commit comments