11# !/usr/bin/env pwsh
2-
32<#
43. SYNOPSIS
54 Validates the code coverage policy for each project.
@@ -58,6 +57,8 @@ Get-ChildItem -Path src -Include '*.*sproj' -Recurse | ForEach-Object {
5857$esc = [char ]27
5958$Errors = New-Object System.Collections.ArrayList
6059$Kudos = New-Object System.Collections.ArrayList
60+ $ErrorsMarkdown = @ ();
61+ $KudosMarkdown = @ ();
6162
6263Write-Verbose " Collecting projects from code coverage report..."
6364$CoberturaReport.coverage.packages.package | ForEach-Object {
@@ -80,10 +81,11 @@ $CoberturaReport.coverage.packages.package | ForEach-Object {
8081 # Detect the under-coverage
8182 if ($MinCodeCoverage -gt $LineCoverage ) {
8283 $IsFailed = $true
84+ $ErrorsMarkdown += " | $Name | Line | **$MinCodeCoverage ** | $LineCoverage :small_red_triangle_down: |"
8385 [void ]$Errors.Add (
8486 (
8587 New-Object PSObject - Property @ {
86- " Project" = $Name ;
88+ " Project" = $Name.Replace ( ' Microsoft.Extensions. ' , ' M.E. ' ).Replace( ' Microsoft.AspNetCore. ' , ' M.AC. ' ) ;
8789 " Coverage Type" = " Line" ;
8890 " Expected" = $MinCodeCoverage ;
8991 " Actual" = " $esc [1m$esc [0;31m$ ( $LineCoverage ) $esc [0m"
@@ -94,10 +96,11 @@ $CoberturaReport.coverage.packages.package | ForEach-Object {
9496
9597 if ($MinCodeCoverage -gt $BranchCoverage ) {
9698 $IsFailed = $true
99+ $ErrorsMarkdown += " | $Name | Branch | **$MinCodeCoverage ** | $BranchCoverage :small_red_triangle_down: |"
97100 [void ]$Errors.Add (
98101 (
99102 New-Object PSObject - Property @ {
100- " Project" = $Name ;
103+ " Project" = $Name.Replace ( ' Microsoft.Extensions. ' , ' M.E. ' ).Replace( ' Microsoft.AspNetCore. ' , ' M.AC. ' ) ;
101104 " Coverage Type" = " Branch" ;
102105 " Expected" = $MinCodeCoverage ;
103106 " Actual" = " $esc [1m$esc [0;31m$ ( $BranchCoverage ) $esc [0m"
@@ -110,10 +113,11 @@ $CoberturaReport.coverage.packages.package | ForEach-Object {
110113 [int ]$lowestReported = [math ]::Min([math ]::Truncate($LineCoverage ), [math ]::Truncate($BranchCoverage ));
111114 Write-Debug " line: $LineCoverage , branch: $BranchCoverage , min: $lowestReported , threshold: $MinCodeCoverage "
112115 if ([int ]$MinCodeCoverage -lt $lowestReported ) {
116+ $KudosMarkdown += " | $Name | $MinCodeCoverage | **$lowestReported ** |"
113117 [void ]$Kudos.Add (
114118 (
115119 New-Object PSObject - Property @ {
116- " Project" = $Name ;
120+ " Project" = $Name.Replace ( ' Microsoft.Extensions. ' , ' M.E. ' ).Replace( ' Microsoft.AspNetCore. ' , ' M.AC. ' ) ;
117121 " Expected" = $MinCodeCoverage ;
118122 " Actual" = " $esc [1m$esc [0;32m$ ( $lowestReported ) $esc [0m" ;
119123 }
@@ -129,8 +133,7 @@ $CoberturaReport.coverage.packages.package | ForEach-Object {
129133 }
130134}
131135
132- if ($Kudos.Count -ne 0 )
133- {
136+ if ($Kudos.Count -ne 0 ) {
134137 Write-Header - message " `r`n Good job! The coverage increased" - isError $false
135138 $Kudos | `
136139 Sort-Object Project | `
@@ -139,6 +142,37 @@ if ($Kudos.Count -ne 0)
139142 @ { Name = " Actual" ; Expression = " Actual" ; Width = 10 ; Alignment = " Right" } `
140143 - AutoSize - Wrap
141144 Write-Host " ##vso[task.logissue type=warning;]Good job! The coverage increased, please update your projects"
145+
146+ $KudosMarkdown = @ (' :tada: **Good job! The coverage increased** :tada:' , ' Update `MinCodeCoverage` in the project files.' , " `r`n " , ' | Project | Expected | Actual |' , ' | --- | ---: | ---: |' , $KudosMarkdown , " `r`n`r`n " );
147+ }
148+
149+ if ($Errors.Count -ne 0 ) {
150+ Write-Header - message " `r`n [!!] Found $ ( $Errors.Count ) issues!" - isError ($Errors.Count -ne 0 )
151+ $Errors | `
152+ Sort-Object Project, ' Coverage Type' | `
153+ Format-Table " Project" , `
154+ @ { Name = " Expected" ; Expression = " Expected" ; Width = 10 ; Alignment = " Right" }, `
155+ @ { Name = " Actual" ; Expression = " Actual" ; Width = 10 ; Alignment = " Right" }, `
156+ @ { Name = " Coverage Type" ; Expression = " Coverage Type" ; Width = 10 ; Alignment = " Center" } `
157+ - AutoSize - Wrap
158+
159+ $ErrorsMarkdown = @ (" :bangbang: **Found issues** :bangbang: " , " `r`n " , ' | Project | Coverage Type |Expected | Actual | ' , ' | --- | :---: | ---: | ---: |' , $ErrorsMarkdown , " `r`n`r`n " );
160+ }
161+
162+ # Write out markdown for publishing back to AzDO
163+ ' ' | Out-File coverage- report.md - Encoding ascii
164+ $ErrorsMarkdown | Out-File coverage- report.md - Encoding ascii - Append
165+ $KudosMarkdown | Out-File coverage- report.md - Encoding ascii - Append
166+
167+ # Set the AzDO variable used by GitHubComment@0 task
168+ [string ]$markdown = Get-Content coverage- report.md - Raw
169+ if (! [string ]::IsNullOrWhiteSpace($markdown )) {
170+ # Add link back to the Code Coverage board
171+ $link = " $ ( $env: SYSTEM_COLLECTIONURI ) $env: SYSTEM_TEAMPROJECT /_build/results?buildId=$env: BUILD_BUILDID &view=codecoverage-tab"
172+ $markdown = " $markdown `n`n Full code coverage report: $link "
173+
174+ $gitHubCommentVar = ' ##vso[task.setvariable variable=GITHUB_COMMENT]' + $markdown.Replace (" `r`n " , " `n " ).Replace(" `n " , " %0D%0A" )
175+ Write-Host $gitHubCommentVar
142176}
143177
144178if ($Errors.Count -eq 0 )
@@ -147,13 +181,4 @@ if ($Errors.Count -eq 0)
147181 exit 0 ;
148182}
149183
150- Write-Header - message " `r`n [!!] Found $ ( $Errors.Count ) issues!" - isError ($Errors.Count -ne 0 )
151- $Errors | `
152- Sort-Object Project, ' Coverage Type' | `
153- Format-Table " Project" , `
154- @ { Name = " Expected" ; Expression = " Expected" ; Width = 10 ; Alignment = " Right" }, `
155- @ { Name = " Actual" ; Expression = " Actual" ; Width = 10 ; Alignment = " Right" }, `
156- @ { Name = " Coverage Type" ; Expression = " Coverage Type" ; Width = 10 ; Alignment = " Center" } `
157- - AutoSize - Wrap
158184exit -1 ;
159-
0 commit comments