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