123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117 |
- param(
- [string] $wd = (Get-Item (Split-Path $PSCommandPath -Parent)).Parent,
- [string] $org = "coderedcorp",
- [string] $project = "cr-github",
- [string] $pipeline_name = "coderedcms"
- )
- $ProgressPreference = "SilentlyContinue"
- $ApiBase = "https://dev.azure.com/$org/$project"
- $mainBuildJson = (
- Invoke-WebRequest "$ApiBase/_apis/build/builds?branchName=refs/heads/main&api-version=5.1"
- ).Content | ConvertFrom-Json
- foreach ($build in $mainBuildJson.value) {
- if ($build.definition.name -eq $pipeline_name) {
- $mainLatestId = $build.id
- break
- }
- }
- $mainCoverageJson = (
- Invoke-WebRequest "$ApiBase/_apis/test/codecoverage?buildId=$mainLatestId&api-version=5.1-preview.1"
- ).Content | ConvertFrom-Json
- foreach ($cov in $mainCoverageJson.coverageData.coverageStats) {
- if ($cov.label -eq "Lines") {
- $mainLinerate = [math]::Round(($cov.covered / $cov.total) * 100, 2)
- }
- }
- $coveragePath = (Get-ChildItem -Recurse -Filter "coverage.xml" $wd)[0]
- if (Test-Path -Path $coveragePath) {
- [xml]$BranchXML = Get-Content $coveragePath
- }
- else {
- Write-Host `
- "##vso[task.LogIssue type=warning;]No code coverage from this build. Is pytest configured to output code coverage?"
- exit 1
- }
- $branchlinerate = [math]::Round([decimal]$BranchXML.coverage.'line-rate' * 100, 2)
- Write-Output ""
- Write-Output "Main branch coverage rate: $mainLinerate%"
- Write-Output "This branch coverage rate: $branchlinerate%"
- if ($mainLinerate -eq 0) {
- $change = "Infinite"
- }
- else {
- $change = [math]::Abs($branchlinerate - $mainLinerate)
- }
- if ($branchlinerate -gt $mainLinerate) {
- Write-Host "Coverage increased by $change% 😀" -ForegroundColor Green
- exit 0
- }
- elseif ($branchlinerate -eq $mainLinerate) {
- Write-Host "Coverage has not changed." -ForegroundColor Green
- exit 0
- }
- elseif ($change -gt 2) {
-
-
-
- Write-Host "##vso[task.LogIssue type=error;]Coverage decreased by $change% 😭"
- exit 1
- }
- else {
-
- Write-Host "##vso[task.LogIssue type=warning;]Coverage decreased by $change% 😭"
- exit 0
- }
|