microsoft/hve-core
Publicmirrored fromhttps://github.com/microsoft/hve-coreAvailable
scripts/linting/Invoke-LinkLanguageCheck.ps1
190lines · modecode
| 1 | #!/usr/bin/env pwsh |
| 2 | # Copyright (c) Microsoft Corporation. |
| 3 | # SPDX-License-Identifier: MIT |
| 4 | |
| 5 | <# |
| 6 | .SYNOPSIS |
| 7 | Checks for URLs with language paths and outputs results to JSON. |
| 8 | .DESCRIPTION |
| 9 | Wrapper for Link-Lang-Check.ps1 with GitHub Actions integration. |
| 10 | Validates URLs and writes results to a specified output path. |
| 11 | .PARAMETER ExcludePaths |
| 12 | Array of paths to exclude from the check. |
| 13 | .PARAMETER OutputPath |
| 14 | Path where the JSON result file will be written. |
| 15 | Defaults to "logs/link-lang-check-results.json". |
| 16 | .EXAMPLE |
| 17 | Invoke-LinkLanguageCheck.ps1 |
| 18 | .EXAMPLE |
| 19 | Invoke-LinkLanguageCheck.ps1 -OutputPath "custom/results.json" |
| 20 | #> |
| 21 | |
| 22 | #Requires -Version 7.0 |
| 23 | |
| 24 | [CmdletBinding()] |
| 25 | param( |
| 26 | [string[]]$ExcludePaths = @(), |
| 27 | [string]$OutputPath = "logs/link-lang-check-results.json" |
| 28 | ) |
| 29 | |
| 30 | $ErrorActionPreference = 'Stop' |
| 31 | |
| 32 | # Import shared helpers |
| 33 | Import-Module (Join-Path $PSScriptRoot "Modules/LintingHelpers.psm1") -Force |
| 34 | Import-Module (Join-Path $PSScriptRoot "../lib/Modules/CIHelpers.psm1") -Force |
| 35 | |
| 36 | function Invoke-LinkLanguageCheckCore { |
| 37 | [CmdletBinding()] |
| 38 | param( |
| 39 | [string[]]$ExcludePaths = @(), |
| 40 | [string]$OutputPath # no default; outer script always supplies the value |
| 41 | ) |
| 42 | |
| 43 | $repoRoot = git rev-parse --show-toplevel 2>$null |
| 44 | if ($LASTEXITCODE -ne 0) { |
| 45 | Write-Error "Not in a git repository" |
| 46 | return 1 |
| 47 | } |
| 48 | |
| 49 | |
| 50 | if (-not [System.IO.Path]::IsPathRooted($OutputPath)) { |
| 51 | $OutputPath = Join-Path $repoRoot $OutputPath |
| 52 | } |
| 53 | |
| 54 | Write-Host "🔍 Checking for URLs with language paths..." -ForegroundColor Cyan |
| 55 | |
| 56 | try { |
| 57 | $scriptArgs = @{} |
| 58 | if ($ExcludePaths.Count -gt 0) { |
| 59 | $scriptArgs['ExcludePaths'] = $ExcludePaths |
| 60 | } |
| 61 | $jsonOutput = & (Join-Path $PSScriptRoot "Link-Lang-Check.ps1") @scriptArgs 2>&1 |
| 62 | |
| 63 | $results = $jsonOutput | ConvertFrom-Json |
| 64 | |
| 65 | if ($results -and $results.Count -gt 0) { |
| 66 | Write-Host "Found $($results.Count) URLs with 'en-us' language paths`n" -ForegroundColor Yellow |
| 67 | |
| 68 | $fileGroups = $results | Group-Object -Property file |
| 69 | $uniqueFiles = $fileGroups | ForEach-Object { $_.Name } |
| 70 | |
| 71 | foreach ($fileGroup in $fileGroups) { |
| 72 | Write-Host "📄 $($fileGroup.Name)" -ForegroundColor Cyan |
| 73 | foreach ($item in $fileGroup.Group) { |
| 74 | Write-Host " ⚠️ Line $($item.line_number): $($item.original_url)" -ForegroundColor Yellow |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | foreach ($item in $results) { |
| 79 | Write-CIAnnotation ` |
| 80 | -Message "URL contains language path: $($item.original_url)" ` |
| 81 | -Level Warning ` |
| 82 | -File $item.file ` |
| 83 | -Line $item.line_number |
| 84 | } |
| 85 | |
| 86 | $outputData = @{ |
| 87 | Timestamp = Get-StandardTimestamp |
| 88 | script = "link-lang-check" |
| 89 | summary = @{ |
| 90 | total_issues = $results.Count |
| 91 | files_affected = $uniqueFiles.Count |
| 92 | } |
| 93 | issues = $results |
| 94 | } |
| 95 | |
| 96 | # Ensure output directory exists |
| 97 | $outputDir = Split-Path -Parent $OutputPath |
| 98 | if ($outputDir -and -not (Test-Path $outputDir)) { |
| 99 | New-Item -ItemType Directory -Path $outputDir -Force | Out-Null |
| 100 | } |
| 101 | |
| 102 | # Write JSON to file |
| 103 | $outputData | ConvertTo-Json -Depth 3 | Out-File -FilePath $OutputPath -Encoding utf8 |
| 104 | |
| 105 | Set-CIOutput -Name "issues" -Value $results.Count |
| 106 | Set-CIEnv -Name "LINK_LANG_FAILED" -Value "true" |
| 107 | |
| 108 | Write-CIStepSummary -Content @" |
| 109 | ## Link Language Path Check Results |
| 110 | |
| 111 | ⚠️ **Status**: Issues Found |
| 112 | |
| 113 | Found $($results.Count) URL(s) containing language path 'en-us'. |
| 114 | |
| 115 | **Why this matters:** |
| 116 | Language-specific URLs don't adapt to user preferences and may break for non-English users. |
| 117 | |
| 118 | **To fix locally:** |
| 119 | ``````powershell |
| 120 | scripts/linting/Link-Lang-Check.ps1 -Fix |
| 121 | `````` |
| 122 | |
| 123 | **Files affected:** |
| 124 | $(($uniqueFiles | ForEach-Object { |
| 125 | $count = ($results | Where-Object file -eq $_).Count |
| 126 | $safePath = if ((Get-CIPlatform) -eq 'azdo') { |
| 127 | ConvertTo-AzureDevOpsEscaped -Value $_ |
| 128 | } else { $_ } |
| 129 | "- $safePath ($count occurrence(s))" |
| 130 | }) -join "`n") |
| 131 | "@ |
| 132 | |
| 133 | Write-Host "❌ Link language check failed with $($results.Count) issue(s) in $($uniqueFiles.Count) file(s)." -ForegroundColor Red |
| 134 | |
| 135 | return 1 |
| 136 | } |
| 137 | |
| 138 | Write-Host "✅ No URLs with language paths found" -ForegroundColor Green |
| 139 | |
| 140 | $emptyResults = @{ |
| 141 | Timestamp = Get-StandardTimestamp |
| 142 | script = "link-lang-check" |
| 143 | summary = @{ |
| 144 | total_issues = 0 |
| 145 | files_affected = 0 |
| 146 | } |
| 147 | issues = @() |
| 148 | } |
| 149 | |
| 150 | # Ensure output directory exists |
| 151 | $outputDir = Split-Path -Parent $OutputPath |
| 152 | if ($outputDir -and -not (Test-Path $outputDir)) { |
| 153 | New-Item -ItemType Directory -Path $outputDir -Force | Out-Null |
| 154 | } |
| 155 | |
| 156 | # Write JSON to file |
| 157 | $emptyResults | ConvertTo-Json -Depth 3 | Out-File -FilePath $OutputPath -Encoding utf8 |
| 158 | Set-CIOutput -Name "issues" -Value "0" |
| 159 | |
| 160 | Write-CIStepSummary -Content @" |
| 161 | ## Link Language Path Check Results |
| 162 | |
| 163 | ✅ **Status**: Passed |
| 164 | |
| 165 | No URLs with language-specific paths detected. |
| 166 | "@ |
| 167 | |
| 168 | return 0 |
| 169 | } |
| 170 | catch { |
| 171 | Write-Error -ErrorAction Continue "Link-language check failed: $($_.Exception.Message)" |
| 172 | Write-CIAnnotation -Message "Link-language check failed: $($_.Exception.Message)" -Level Error |
| 173 | return 1 |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | #region Main Execution |
| 178 | if ($MyInvocation.InvocationName -ne '.') { |
| 179 | try { |
| 180 | $exitCode = Invoke-LinkLanguageCheckCore -ExcludePaths $ExcludePaths -OutputPath $OutputPath |
| 181 | exit $exitCode |
| 182 | } |
| 183 | catch { |
| 184 | Write-Error -ErrorAction Continue "Invoke-LinkLanguageCheck failed: $($_.Exception.Message)" |
| 185 | Write-CIAnnotation -Message $_.Exception.Message -Level Error |
| 186 | exit 1 |
| 187 | } |
| 188 | } |
| 189 | #endregion Main Execution |
| 190 | |
| 191 | |