compare-codecov.ps1 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. #!/usr/bin/env pwsh
  2. <#
  3. .SYNOPSIS
  4. Compares code coverage percent of local coverage.xml file to master 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 = "coderedcms",
  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. $masterBuildJson = (
  30. Invoke-WebRequest "$ApiBase/_apis/build/builds?branchName=refs/heads/master&api-version=5.1"
  31. ).Content | ConvertFrom-Json
  32. # Get the latest matching build ID from the list of builds.
  33. foreach ($build in $masterBuildJson.value) {
  34. if ($build.definition.name -eq $pipeline_name) {
  35. $masterLatestId = $build.id
  36. break
  37. }
  38. }
  39. # Retrieve code coverage for this build ID.
  40. $masterCoverageJson = (
  41. Invoke-WebRequest "$ApiBase/_apis/test/codecoverage?buildId=$masterLatestId&api-version=5.1-preview.1"
  42. ).Content | ConvertFrom-Json
  43. foreach ($cov in $masterCoverageJson.coverageData.coverageStats) {
  44. if ($cov.label -eq "Lines") {
  45. $masterlinerate = [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 -ForegroundColor Red `
  56. "No code coverage from this build. Is pytest configured to output code coverage? Exiting."
  57. exit 1
  58. }
  59. $branchlinerate = [math]::Round([decimal]$BranchXML.coverage.'line-rate' * 100, 2)
  60. # ---- PRINT OUTPUT ------------------------------------------------------------
  61. Write-Output ""
  62. Write-Output "Master line coverage rate: $masterlinerate%"
  63. Write-Output "Branch line coverage rate: $branchlinerate%"
  64. if ($masterlinerate -eq 0) {
  65. $change = "Infinite"
  66. }
  67. else {
  68. $change = [math]::Abs($branchlinerate - $masterlinerate)
  69. }
  70. if ($branchlinerate -gt $masterlinerate) {
  71. Write-Host "Coverage increased by $change% 😀" -ForegroundColor Green
  72. exit 0
  73. }
  74. elseif ($branchlinerate -eq $masterlinerate) {
  75. Write-Host "Coverage has not changed." -ForegroundColor Green
  76. exit 0
  77. }
  78. else {
  79. # Write the error in a way that shows up as the failure reason in Azure Pipelines.
  80. Write-Host "##vso[task.LogIssue type=error;]Coverage decreased by $change% 😭"
  81. exit 4
  82. }