2
0

compare-codecov.ps1 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #!/usr/bin/env pwsh
  2. <#
  3. .SYNOPSIS
  4. Compares code coverage percent of local coverage.xml file to main branch
  5. (Azure Pipeline API).
  6. .PARAMETER wd
  7. The working directory in which to search for current coverage.xml.
  8. .PARAMETER org
  9. Name of the Azure DevOps organization where the pipeline is hosted.
  10. .PARAMETER project
  11. Name of the Azure DevOps project to which the pipeline belongs.
  12. .PARAMETER pipeline_name
  13. Name of the desired pipeline within the project. This is to support projects
  14. with multiple pipelines.
  15. #>
  16. # ---- SETUP -------------------------------------------------------------------
  17. param(
  18. [string] $wd = (Get-Item (Split-Path $PSCommandPath -Parent)).Parent,
  19. [string] $org = "coderedcorp",
  20. [string] $project = "cr-github",
  21. [string] $pipeline_name = "coderedcms"
  22. )
  23. # Hide "UI" and progress bars.
  24. $ProgressPreference = "SilentlyContinue"
  25. # API setup.
  26. $ApiBase = "https://dev.azure.com/$org/$project"
  27. # ---- GET CODE COVERAGE FROM RECENT BUILD -------------------------------------
  28. # Get list of all recent builds.
  29. $mainBuildJson = (
  30. Invoke-WebRequest "$ApiBase/_apis/build/builds?branchName=refs/heads/main&api-version=5.1"
  31. ).Content | ConvertFrom-Json
  32. # Get the latest matching build ID from the list of builds.
  33. foreach ($build in $mainBuildJson.value) {
  34. if ($build.definition.name -eq $pipeline_name) {
  35. $mainLatestId = $build.id
  36. break
  37. }
  38. }
  39. # Retrieve code coverage for this build ID.
  40. $mainCoverageJson = (
  41. Invoke-WebRequest "$ApiBase/_apis/test/codecoverage?buildId=$mainLatestId&api-version=5.1-preview.1"
  42. ).Content | ConvertFrom-Json
  43. foreach ($cov in $mainCoverageJson.coverageData.coverageStats) {
  44. if ($cov.label -eq "Lines") {
  45. $mainLinerate = [math]::Round(($cov.covered / $cov.total) * 100, 2)
  46. }
  47. }
  48. # ---- GET COVERAGE FROM LOCAL RUN ---------------------------------------------
  49. # Get current code coverage from coverage.xml file.
  50. # Use the first one, if there are multiple i.e. from multiple runs.
  51. $coveragePath = (Get-ChildItem -Recurse -Filter "coverage.xml" $wd)[0]
  52. if (Test-Path -Path $coveragePath) {
  53. [xml]$BranchXML = Get-Content $coveragePath
  54. }
  55. else {
  56. Write-Host `
  57. "##vso[task.LogIssue type=warning;]No code coverage from this build. Is pytest configured to output code coverage?"
  58. exit 1
  59. }
  60. $branchlinerate = [math]::Round([decimal]$BranchXML.coverage.'line-rate' * 100, 2)
  61. # ---- PRINT OUTPUT ------------------------------------------------------------
  62. Write-Output ""
  63. Write-Output "Main branch coverage rate: $mainLinerate%"
  64. Write-Output "This branch coverage rate: $branchlinerate%"
  65. if ($mainLinerate -eq 0) {
  66. $change = "Infinite"
  67. }
  68. else {
  69. $change = [math]::Abs($branchlinerate - $mainLinerate)
  70. }
  71. if ($branchlinerate -gt $mainLinerate) {
  72. Write-Host "Coverage increased by $change% 😀" -ForegroundColor Green
  73. exit 0
  74. }
  75. elseif ($branchlinerate -eq $mainLinerate) {
  76. Write-Host "Coverage has not changed." -ForegroundColor Green
  77. exit 0
  78. }
  79. elseif ($change -gt 2) {
  80. # Coverage measurements seem to be a bit flaky in azure pipelines and will
  81. # report changes within a few fractions of a percent even when there are no
  82. # changes. If coverage decreased by more than 2%, fail with error.
  83. Write-Host "##vso[task.LogIssue type=error;]Coverage decreased by $change% 😭"
  84. exit 1
  85. }
  86. else {
  87. # Write the error in a way that shows up as a warning in Azure Pipelines.
  88. Write-Host "##vso[task.LogIssue type=warning;]Coverage decreased by $change% 😭"
  89. exit 0
  90. }