|
1 | 1 | # Helper functions for CodeChecks.ps1 |
2 | 2 | # Separated so they can be unit-tested without executing the main script. |
3 | 3 |
|
| 4 | +# Checks whether only CI pipeline config files (ci*.yml) were changed in the given |
| 5 | +# service directory relative to the merge base. Returns $true if all changed files |
| 6 | +# in sdk/<ServiceDirectory>/ match ci*.yml, meaning codegen/snippets/API export can be skipped. |
| 7 | +function Test-OnlyCiConfigChanged { |
| 8 | + param( |
| 9 | + [string]$ServiceDirectory, |
| 10 | + [string]$RepoRoot |
| 11 | + ) |
| 12 | + |
| 13 | + try { |
| 14 | + # Determine the merge base to compare against |
| 15 | + $targetBranch = $env:SYSTEM_PULLREQUEST_TARGETBRANCH |
| 16 | + $targetBranchName = $null |
| 17 | + if ($targetBranch) { |
| 18 | + $targetBranchName = $targetBranch -replace '^refs/heads/', '' |
| 19 | + } |
| 20 | + |
| 21 | + # Try to find the merge base, falling back through common branch names |
| 22 | + $mergeBase = $null |
| 23 | + $candidates = @() |
| 24 | + if ($targetBranchName) { |
| 25 | + $candidates += "origin/$targetBranchName" |
| 26 | + $candidates += $targetBranchName |
| 27 | + } |
| 28 | + $candidates += "origin/main", "main", "origin/master", "master" |
| 29 | + |
| 30 | + foreach ($candidate in $candidates) { |
| 31 | + $mergeBase = git -C $RepoRoot merge-base HEAD $candidate 2>$null |
| 32 | + if ($mergeBase) { break } |
| 33 | + } |
| 34 | + if (-not $mergeBase) { return $false } |
| 35 | + |
| 36 | + $svcPath = "sdk/$ServiceDirectory/" |
| 37 | + $changedFiles = git -C $RepoRoot diff --name-only $mergeBase -- $svcPath 2>$null |
| 38 | + if (-not $changedFiles) { return $false } |
| 39 | + |
| 40 | + foreach ($file in $changedFiles) { |
| 41 | + $fileName = Split-Path -Leaf $file |
| 42 | + if ($fileName -notmatch '^ci.*\.yml$') { |
| 43 | + return $false |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + return $true |
| 48 | + } |
| 49 | + catch { |
| 50 | + # On any error, fall back to running codegen (safe default) |
| 51 | + Write-Host "Warning: Could not determine changed files for $ServiceDirectory — running full checks. Error: $_" |
| 52 | + return $false |
| 53 | + } |
| 54 | +} |
| 55 | + |
4 | 56 | # Parses a single git status --porcelain line and returns the path(s) it contains. |
5 | 57 | # For renames ("old -> new"), returns both paths. Returns an empty array for blank/malformed lines. |
6 | 58 | function Get-PorcelainPaths([string]$line) { |
@@ -44,6 +96,153 @@ function Get-CodeCheckSummary { |
44 | 96 | } |
45 | 97 | } |
46 | 98 |
|
| 99 | +# Validates that packages in the given service directory which are depended on by |
| 100 | +# other services have TestDependsOnDependency set in their CI file. |
| 101 | +# Returns an array of objects describing any missing entries, each with: |
| 102 | +# Package - the package name that needs to be covered |
| 103 | +# CiFile - the CI file path where TestDependsOnDependency should be added |
| 104 | +# Dependents - list of service directories that depend on this package |
| 105 | +function Get-MissingTestDependsOnDependency { |
| 106 | + param( |
| 107 | + [string]$ServiceDirectory, |
| 108 | + [string]$RepoRoot |
| 109 | + ) |
| 110 | + |
| 111 | + $sdkDir = Join-Path $RepoRoot "sdk" |
| 112 | + $svcDir = Join-Path $sdkDir $ServiceDirectory |
| 113 | + |
| 114 | + if (-not (Test-Path $svcDir)) { return @() } |
| 115 | + |
| 116 | + $coreInfra = @( |
| 117 | + 'Azure.Core', 'Azure.Core.TestFramework', 'Azure.Core.Experimental', |
| 118 | + 'Azure.Identity', 'Azure.ResourceManager', |
| 119 | + 'System.ClientModel', 'System.ClientModel.SourceGeneration', |
| 120 | + 'Microsoft.ClientModel.TestFramework' |
| 121 | + ) |
| 122 | + |
| 123 | + # Find all packages in THIS service directory |
| 124 | + $localPackages = @{} |
| 125 | + Get-ChildItem $svcDir -Directory | ForEach-Object { |
| 126 | + $srcDir = Join-Path $_.FullName "src" |
| 127 | + if (Test-Path $srcDir) { |
| 128 | + Get-ChildItem $srcDir -Filter "*.csproj" -File | ForEach-Object { |
| 129 | + $localPackages[$_.BaseName] = $true |
| 130 | + } |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + # Find which local packages are depended on by OTHER services' test projects |
| 135 | + $dependedOn = @{} # packageName -> Set of dependent service dirs |
| 136 | + $refPattern = '<(?:Package|Project)Reference[^>]*Include\s*=\s*"([^"]+)"' |
| 137 | + |
| 138 | + foreach ($svcItem in (Get-ChildItem $sdkDir -Directory)) { |
| 139 | + $otherSvc = $svcItem.Name |
| 140 | + if ($otherSvc -eq $ServiceDirectory) { continue } |
| 141 | + |
| 142 | + Get-ChildItem $svcItem.FullName -Recurse -Filter "*.csproj" | Where-Object { |
| 143 | + $_.FullName -match '[/\\]tests[/\\]' |
| 144 | + } | ForEach-Object { |
| 145 | + $content = Get-Content $_.FullName -Raw |
| 146 | + $matches2 = [regex]::Matches($content, $refPattern) |
| 147 | + foreach ($match in $matches2) { |
| 148 | + $refName = $match.Groups[1].Value |
| 149 | + if ($refName -match '[/\\]') { |
| 150 | + $refName = [System.IO.Path]::GetFileNameWithoutExtension(($refName -replace '\\', '/')) |
| 151 | + } |
| 152 | + if ($localPackages.ContainsKey($refName) -and $refName -notin $coreInfra) { |
| 153 | + if (-not $dependedOn.ContainsKey($refName)) { |
| 154 | + $dependedOn[$refName] = [System.Collections.Generic.HashSet[string]]::new() |
| 155 | + } |
| 156 | + [void]$dependedOn[$refName].Add($otherSvc) |
| 157 | + } |
| 158 | + } |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + if ($dependedOn.Count -eq 0) { return @() } |
| 163 | + |
| 164 | + # Find which CI file each depended-on package is an artifact in |
| 165 | + $ciFiles = Get-ChildItem $svcDir -Filter "ci*.yml" -File |
| 166 | + $pkgToCiFile = @{} |
| 167 | + foreach ($ciFile in $ciFiles) { |
| 168 | + $content = Get-Content $ciFile.FullName -Raw |
| 169 | + foreach ($pkg in $dependedOn.Keys) { |
| 170 | + $escaped = [regex]::Escape($pkg) |
| 171 | + if ($content -match "(?m)^\s*-\s*name:\s*$escaped\s*$") { |
| 172 | + $pkgToCiFile[$pkg] = $ciFile.FullName |
| 173 | + } |
| 174 | + } |
| 175 | + } |
| 176 | + |
| 177 | + # Check which are already covered by TestDependsOnDependency |
| 178 | + $missing = @() |
| 179 | + $ciFileCoverage = @{} # ciFilePath -> Set of covered package names |
| 180 | + foreach ($ciFile in $ciFiles) { |
| 181 | + $content = Get-Content $ciFile.FullName -Raw |
| 182 | + if ($content -match 'TestDependsOnDependency:\s*(.+)') { |
| 183 | + $coveredPkgs = $Matches[1].Trim() -split '\s+' |
| 184 | + $ciFileCoverage[$ciFile.FullName] = $coveredPkgs |
| 185 | + } else { |
| 186 | + $ciFileCoverage[$ciFile.FullName] = @() |
| 187 | + } |
| 188 | + } |
| 189 | + |
| 190 | + foreach ($pkg in $dependedOn.Keys | Sort-Object) { |
| 191 | + $ciFile = $pkgToCiFile[$pkg] |
| 192 | + if (-not $ciFile) { continue } |
| 193 | + |
| 194 | + $coveredInFile = $ciFileCoverage[$ciFile] |
| 195 | + if ($pkg -notin $coveredInFile) { |
| 196 | + $missing += [PSCustomObject]@{ |
| 197 | + Package = $pkg |
| 198 | + CiFile = $ciFile |
| 199 | + Dependents = @($dependedOn[$pkg] | Sort-Object) |
| 200 | + } |
| 201 | + } |
| 202 | + } |
| 203 | + |
| 204 | + return $missing |
| 205 | +} |
| 206 | + |
| 207 | +# Adds missing TestDependsOnDependency entries to a CI YAML file. |
| 208 | +# If the file already has a TestDependsOnDependency line, merges the new packages. |
| 209 | +# If not, appends it at the end of the extends.parameters block. |
| 210 | +function Add-TestDependsOnDependency { |
| 211 | + param( |
| 212 | + [string]$CiFilePath, |
| 213 | + [string[]]$PackageNames |
| 214 | + ) |
| 215 | + |
| 216 | + $content = Get-Content $CiFilePath -Raw |
| 217 | + $lines = $content -split "`n" |
| 218 | + |
| 219 | + if ($content -match 'TestDependsOnDependency:\s*(.+)') { |
| 220 | + $existing = $Matches[1].Trim() -split '\s+' |
| 221 | + $all = @($existing + $PackageNames) | Sort-Object -Unique |
| 222 | + $newValue = "TestDependsOnDependency: $($all -join ' ')" |
| 223 | + $content = $content -replace 'TestDependsOnDependency:\s*.+', $newValue |
| 224 | + } else { |
| 225 | + # Find indentation from extends.parameters block |
| 226 | + $indent = " " |
| 227 | + for ($i = 0; $i -lt $lines.Length; $i++) { |
| 228 | + if ($lines[$i].Trim() -eq 'parameters:' -and $i -gt 0 -and $lines[$i - 1].Trim() -match 'template:') { |
| 229 | + $paramIndent = $lines[$i].Length - $lines[$i].TrimStart().Length |
| 230 | + $indent = ' ' * ($paramIndent + 2) |
| 231 | + break |
| 232 | + } |
| 233 | + } |
| 234 | + |
| 235 | + $newLine = "${indent}TestDependsOnDependency: $(($PackageNames | Sort-Object) -join ' ')" |
| 236 | + |
| 237 | + # Append before the last line of the file (or at end) |
| 238 | + $trimmed = $content.TrimEnd() |
| 239 | + $content = "$trimmed`n$newLine`n" |
| 240 | + } |
| 241 | + |
| 242 | + $utf8NoBom = New-Object System.Text.UTF8Encoding($false) |
| 243 | + [System.IO.File]::WriteAllText($CiFilePath, $content, $utf8NoBom) |
| 244 | +} |
| 245 | + |
47 | 246 | # Determines the action to take when a git diff is detected after code checks. |
48 | 247 | # Returns an object with: |
49 | 248 | # Action - "none" (no diffs), "error" (fail with message), or "report" (informational summary) |
|
0 commit comments