compare-codecov.ps1 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. #!/usr/bin/env pwsh
  2. <#
  3. .SYNOPSIS
  4. Compares code coverage percent of local coverage.xml file to master branch (Azure Pipeline API).
  5. .PARAMETER wd
  6. The working directory in which to search for current coverage.xml.
  7. #>
  8. param(
  9. [string] $wd = (Get-Item (Split-Path $PSCommandPath -Parent)).Parent,
  10. [string] $org = "coderedcorp",
  11. [string] $project = "coderedcms"
  12. )
  13. # Hide "UI" and progress bars.
  14. $ProgressPreference = "SilentlyContinue"
  15. # Get latest coverage from master.
  16. $ApiBase = "https://dev.azure.com/$org/$project"
  17. $masterBuildJson = (Invoke-WebRequest "$ApiBase/_apis/build/builds?branchName=refs/heads/master&api-version=5.1").Content | ConvertFrom-Json
  18. $masterLatestId = $masterBuildJson.value[0].id
  19. $masterCoverageJson = (Invoke-WebRequest "$ApiBase/_apis/test/codecoverage?buildId=$masterLatestId&api-version=5.1-preview.1").Content | ConvertFrom-Json
  20. foreach ($cov in $masterCoverageJson.coverageData.coverageStats) {
  21. if ($cov.label -eq "Lines") {
  22. $masterlinerate = [math]::Round(($cov.covered / $cov.total) * 100, 2)
  23. }
  24. }
  25. # Get current code coverage from coverage.xml file.
  26. $coveragePath = Get-ChildItem -Recurse -Filter "coverage.xml" $wd
  27. if (Test-Path -Path $coveragePath) {
  28. [xml]$BranchXML = Get-Content $coveragePath
  29. }
  30. else {
  31. Write-Host "No code coverage from this build. Is pytest configured to output code coverage? Exiting." -ForegroundColor Red
  32. exit 1
  33. }
  34. $branchlinerate = [math]::Round([decimal]$BranchXML.coverage.'line-rate' * 100, 2)
  35. Write-Output ""
  36. Write-Output "Master line coverage rate: $masterlinerate%"
  37. Write-Output "Branch line coverage rate: $branchlinerate%"
  38. if ($masterlinerate -eq 0) {
  39. $change = "Infinite"
  40. }
  41. else {
  42. $change = [math]::Abs($branchlinerate - $masterlinerate)
  43. }
  44. if ($branchlinerate -gt $masterlinerate) {
  45. Write-Host "Coverage increased by $change% 😀" -ForegroundColor Green
  46. exit 0
  47. }
  48. elseif ($branchlinerate -eq $masterlinerate) {
  49. Write-Host "Coverage has not changed." -ForegroundColor Green
  50. exit 0
  51. }
  52. else {
  53. Write-Host "Coverage decreased by $change% 😭" -ForegroundColor Red
  54. exit 4
  55. }