Skip to content

Commit

Permalink
[devops] Improve GitHub comment when the build fails. (xamarin#20584)
Browse files Browse the repository at this point in the history
Currently we get a message saying all the tests failed catastrophically.

Example:
xamarin#20434 (comment)

This is both confusing and scary, so change the comment to say that the
build
failed instead.

---------

Co-authored-by: Manuel de la Pena <mandel@microsoft.com>
  • Loading branch information
rolfbjarne and mandel-macaque authored May 13, 2024
1 parent ff9acb9 commit 35bcda3
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 0 deletions.
59 changes: 59 additions & 0 deletions tools/devops/automation/scripts/TestResults.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ Describe "TestResults tests" {
"@
$stageDependencies = @"
{
"build_macos_tests": {
"build_macos_tests_job": {
"result": "Succeeded"
}
},
"configure_build": {
"configure": {
"outputs": {
Expand Down Expand Up @@ -152,6 +157,11 @@ Describe "TestResults tests" {

$stageDependenciesWithMissingResults = @"
{
"build_macos_tests": {
"build_macos_tests_job": {
"result": "Succeeded"
}
},
"configure_build": {
"configure": {
"outputs": {
Expand Down Expand Up @@ -193,6 +203,31 @@ Describe "TestResults tests" {
}
}
"@
$stageDependenciesWithBuildFailure = @"
{
"build_macos_tests": {
"build_macos_tests_job": {
"outputs": {
"fix_commit.GIT_HASH": "8a881722232ef37ed73f8926acd113a6ccc8eafd",
"configuration.BuildNugets": "True",
"configuration.BuildPkgs": "True",
"configuration.PR_ID": "20434",
"configuration.RunSampleTests": "",
"configuration.SignPkgs": "True",
"build.TESTS_BOT": "XAMBOT-1001.Sonoma"
},
"identifier": null,
"name": "build_macos_tests_job",
"attempt": 1,
"startTime": null,
"finishTime": null,
"state": "NotStarted",
"result": "Failed"
}
}
}
"@

}

It "is correctly created" {
Expand Down Expand Up @@ -508,6 +543,30 @@ Describe "TestResults tests" {
:white_check_mark: dotnettests (Multiple platforms): All 7 tests passed. [Html Report (VSDrops)](vsdropsIndex/simulator_testsdotnettests_Multiple-1/;/tests/vsdrops_index.html) [Download](/_apis/build/builds//artifacts?artifactName=HtmlReport-simulator_testsdotnettests_Multiple-1&api-version=6.0&`$format=zip)
:white_check_mark: dotnettests (tvOS): All 4 tests passed. [Html Report (VSDrops)](vsdropsIndex/simulator_testsdotnettests_tvOS-1/;/tests/vsdrops_index.html) [Download](/_apis/build/builds//artifacts?artifactName=HtmlReport-simulator_testsdotnettests_tvOS-1&api-version=6.0&`$format=zip)
[comment]: <> (This is a test result report added by Azure DevOps)
"
}

It "computes the right summary with build failure" {
$VerbosePreference = "Continue"
$Env:MyVerbosePreference = 'Continue'


$parallelResults = New-ParallelTestsResults -Path "path" -StageDependencies "$stageDependenciesWithBuildFailure" -Context "context" -VSDropsIndex "vsdropsIndex"

$parallelResults.IsSuccess() | Should -Be $false

$sb = [System.Text.StringBuilder]::new()
$parallelResults.WriteComment($sb)

$content = $sb.ToString()

Write-Host $content.Replace("&$", "&``$")

$content | Should -Be "# :x: Build failure :x:
Build result: [Failed](/_build/index?buildId=)
[comment]: <> (This is a test result report added by Azure DevOps)
"
}
Expand Down
27 changes: 27 additions & 0 deletions tools/devops/automation/scripts/TestResults.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@ class TestResult {
}

class ParallelTestsResults {
[string] $BuildFailureMessage
[string] $Context
[string] $TestPrefix
[string] $VSDropsIndex
Expand All @@ -264,6 +265,12 @@ class ParallelTestsResults {
$this.VSDropsIndex = $vsDropsIndex
}

ParallelTestsResults (
[string] $buildFailureMessage
) {
$this.BuildFailureMessage = $buildFailureMessage
}

[object] GetFailingTests() {
$failingTests = [System.Collections.ArrayList]@()
foreach ($result in $this.Results) {
Expand All @@ -285,6 +292,9 @@ class ParallelTestsResults {
}

[bool] IsSuccess() {
if (-not [string]::IsNullOrEmpty($this.BuildFailureMessage)) {
return $false
}
$failingTests = $this.GetFailingTests()
return $failingTests.Count -eq 0
}
Expand Down Expand Up @@ -332,6 +342,16 @@ class ParallelTestsResults {
}

[void] WriteComment($stringBuilder) {
if (-not [string]::IsNullOrEmpty($this.BuildFailureMessage)) {
$pipelineLink = "$Env:SYSTEM_TEAMFOUNDATIONCOLLECTIONURI$Env:SYSTEM_TEAMPROJECT/_build/index?buildId=$Env:BUILD_BUILDID"
$stringBuilder.AppendLine("# :x: Build failure :x:")
$stringBuilder.AppendLine()
$stringBuilder.AppendLine("Build result: [$($this.BuildFailureMessage)]($($pipelineLink))")
$stringBuilder.AppendLine()
$stringBuilder.AppendLine("[comment]: <> (This is a test result report added by Azure DevOps)")
return
}

$stringBuilder.AppendLine("# Test results")
# We need to add a small summary at the top. We check if it was a success, if that is
# the case, we just need to state it and
Expand Down Expand Up @@ -458,6 +478,13 @@ function New-ParallelTestsResults {

$stageDep = $StageDependencies | ConvertFrom-Json -AsHashtable

$buildResult = $stageDep.build_macos_tests.build_macos_tests_job.result
Write-Host "Build result: $buildResult"
if ($buildResult -ne "Succeeded") {
Write-Host "Build did not succeed: $buildResult"
return [ParallelTestsResults]::new($buildResult)
}

$matrix = $stageDep.configure_build.configure.outputs["test_matrix.TEST_MATRIX"] | ConvertFrom-Json -AsHashtable
$suites = [System.Collections.SortedList]::new()
foreach ($title in $matrix.Keys) {
Expand Down

0 comments on commit 35bcda3

Please sign in to comment.