compare-codecov.ps1 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #!/usr/bin/env pwsh
  2. <#
  3. .SYNOPSIS
  4. Compares code coverage percent of local coverage.xml file to dev 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. $devBuildJson = (
  30. Invoke-WebRequest "$ApiBase/_apis/build/builds?branchName=refs/heads/dev&api-version=5.1"
  31. ).Content | ConvertFrom-Json
  32. # Get the latest matching build ID from the list of builds.
  33. foreach ($build in $devBuildJson.value) {
  34. if ($build.definition.name -eq $pipeline_name) {
  35. $devLatestId = $build.id
  36. break
  37. }
  38. }
  39. # Retrieve code coverage for this build ID.
  40. $devCoverageJson = (
  41. Invoke-WebRequest "$ApiBase/_apis/test/codecoverage?buildId=$devLatestId&api-version=5.1-preview.1"
  42. ).Content | ConvertFrom-Json
  43. foreach ($cov in $devCoverageJson.coverageData.coverageStats) {
  44. if ($cov.label -eq "Lines") {
  45. $devlinerate = [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. $coveragePath = Get-ChildItem -Recurse -Filter "coverage.xml" $wd
  51. if (Test-Path -Path $coveragePath) {
  52. [xml]$BranchXML = Get-Content $coveragePath
  53. }
  54. else {
  55. Write-Host `
  56. "##vso[task.LogIssue type=warning;]No code coverage from this build. Is pytest configured to output code coverage?"
  57. exit 1
  58. }
  59. $branchlinerate = [math]::Round([decimal]$BranchXML.coverage.'line-rate' * 100, 2)
  60. # ---- PRINT OUTPUT ------------------------------------------------------------
  61. Write-Output ""
  62. Write-Output "Dev branch coverage rate: $devlinerate%"
  63. Write-Output "This branch coverage rate: $branchlinerate%"
  64. if ($devlinerate -eq 0) {
  65. $change = "Infinite"
  66. }
  67. else {
  68. $change = [math]::Abs($branchlinerate - $devlinerate)
  69. }
  70. if ($branchlinerate -gt $devlinerate) {
  71. Write-Host "Coverage increased by $change% 😀" -ForegroundColor Green
  72. exit 0
  73. }
  74. elseif ($branchlinerate -eq $devlinerate) {
  75. Write-Host "Coverage has not changed." -ForegroundColor Green
  76. exit 0
  77. }
  78. elseif ($change -gt 2) {
  79. # Coverage measurements seem to be a bit flaky in azure pipelines and will
  80. # report changes within a few fractions of a percent even when there are no
  81. # changes. If coverage decreased by more than 2%, fail with error.
  82. Write-Host "##vso[task.LogIssue type=error;]Coverage decreased by $change% 😭"
  83. exit 1
  84. }
  85. else {
  86. # Write the error in a way that shows up as a warning in Azure Pipelines.
  87. Write-Host "##vso[task.LogIssue type=warning;]Coverage decreased by $change% 😭"
  88. exit 0
  89. }