Browse Source

Update azure pipeline code coverage comparison script

Vince Salvino 5 years ago
parent
commit
422b74bb4a
1 changed files with 51 additions and 7 deletions
  1. 51 7
      ci/compare-codecov.ps1

+ 51 - 7
ci/compare-codecov.ps1

@@ -2,43 +2,87 @@
 
 <#
 .SYNOPSIS
-Compares code coverage percent of local coverage.xml file to master branch (Azure Pipeline API).
+Compares code coverage percent of local coverage.xml file to master branch
+(Azure Pipeline API).
 
 .PARAMETER wd
 The working directory in which to search for current coverage.xml.
+
+.PARAMETER org
+Name of the Azure DevOps organization where the pipeline is hosted.
+
+.PARAMETER project
+Name of the Azure DevOps project to which the pipeline belongs.
+
+.PARAMETER pipeline_name
+Name of the desired pipeline within the project. This is to support projects
+with multiple pipelines.
 #>
 
+
+# ---- SETUP -------------------------------------------------------------------
+
+
 param(
     [string] $wd = (Get-Item (Split-Path $PSCommandPath -Parent)).Parent,
     [string] $org = "coderedcorp",
-    [string] $project = "coderedcms"
+    [string] $project = "coderedcms",
+    [string] $pipeline_name = "coderedcms"
 )
 
 # Hide "UI" and progress bars.
 $ProgressPreference = "SilentlyContinue"
 
-# Get latest coverage from master.
+# API setup.
 $ApiBase = "https://dev.azure.com/$org/$project"
-$masterBuildJson = (Invoke-WebRequest "$ApiBase/_apis/build/builds?branchName=refs/heads/master&api-version=5.1").Content | ConvertFrom-Json
-$masterLatestId = $masterBuildJson.value[0].id
-$masterCoverageJson = (Invoke-WebRequest "$ApiBase/_apis/test/codecoverage?buildId=$masterLatestId&api-version=5.1-preview.1").Content | ConvertFrom-Json
+
+
+# ---- GET CODE COVERAGE FROM RECENT BUILD -------------------------------------
+
+
+# Get list of all recent builds.
+$masterBuildJson = (
+    Invoke-WebRequest "$ApiBase/_apis/build/builds?branchName=refs/heads/master&api-version=5.1"
+).Content | ConvertFrom-Json
+
+# Get the latest matching build ID from the list of builds.
+foreach ($build in $masterBuildJson.value) {
+    if ($build.definition.name -eq $pipeline_name) {
+        $masterLatestId = $build.id
+        break
+    }
+}
+
+# Retrieve code coverage for this build ID.
+$masterCoverageJson = (
+    Invoke-WebRequest "$ApiBase/_apis/test/codecoverage?buildId=$masterLatestId&api-version=5.1-preview.1"
+).Content | ConvertFrom-Json
 foreach ($cov in $masterCoverageJson.coverageData.coverageStats) {
     if ($cov.label -eq "Lines") {
         $masterlinerate = [math]::Round(($cov.covered / $cov.total) * 100, 2)
     }
 }
 
+
+# ---- GET COVERAGE FROM LOCAL RUN ---------------------------------------------
+
+
 # Get current code coverage from coverage.xml file.
 $coveragePath = Get-ChildItem -Recurse -Filter "coverage.xml" $wd
 if (Test-Path -Path $coveragePath) {
     [xml]$BranchXML = Get-Content $coveragePath
 }
 else {
-    Write-Host "No code coverage from this build. Is pytest configured to output code coverage? Exiting." -ForegroundColor Red
+    Write-Host  -ForegroundColor Red `
+        "No code coverage from this build. Is pytest configured to output code coverage? Exiting."
     exit 1
 }
 $branchlinerate = [math]::Round([decimal]$BranchXML.coverage.'line-rate' * 100, 2)
 
+
+# ---- PRINT OUTPUT ------------------------------------------------------------
+
+
 Write-Output ""
 Write-Output "Master line coverage rate:  $masterlinerate%"
 Write-Output "Branch line coverage rate:  $branchlinerate%"