microsoft/hve-core
Publicmirrored from https://github.com/microsoft/hve-coreAvailable
scripts/linting/Invoke-LinkLanguageCheck.ps1
150lines · modeblame
66be1367Bill Berry9 months ago | 1 | #!/usr/bin/env pwsh |
d19c9b3alittleKitchen7 months ago | 2 | # Copyright (c) Microsoft Corporation. |
| 3 | # SPDX-License-Identifier: MIT | |
66be1367Bill Berry9 months ago | 4 | # |
| 5 | # Invoke-LinkLanguageCheck.ps1 | |
| 6 | # | |
| 7 | # Purpose: Wrapper for Link-Lang-Check.ps1 with GitHub Actions integration | |
| 8 | # Author: HVE Core Team | |
| 9 | | |
6e262826little Kitchen6 months ago | 10 | #Requires -Version 7.0 |
| 11 | | |
66be1367Bill Berry9 months ago | 12 | [CmdletBinding()] |
5b659230Bill Berry7 months ago | 13 | param( |
| 14 | [string[]]$ExcludePaths = @() | |
| 15 | ) | |
66be1367Bill Berry9 months ago | 16 | |
6e262826little Kitchen6 months ago | 17 | $ErrorActionPreference = 'Stop' |
| 18 | | |
66be1367Bill Berry9 months ago | 19 | # Import shared helpers |
| 20 | Import-Module (Join-Path $PSScriptRoot "Modules/LintingHelpers.psm1") -Force | |
fdd75ed7Bill Berry7 months ago | 21 | Import-Module (Join-Path $PSScriptRoot "../lib/Modules/CIHelpers.psm1") -Force |
66be1367Bill Berry9 months ago | 22 | |
3587e6abBill Berry6 months ago | 23 | function Invoke-LinkLanguageCheckCore { |
| 24 | [CmdletBinding()] | |
| 25 | param( | |
| 26 | [string[]]$ExcludePaths = @() | |
| 27 | ) | |
66be1367Bill Berry9 months ago | 28 | |
3587e6abBill Berry6 months ago | 29 | $repoRoot = git rev-parse --show-toplevel 2>$null |
| 30 | if ($LASTEXITCODE -ne 0) { | |
| 31 | Write-Error "Not in a git repository" | |
| 32 | return 1 | |
| 33 | } | |
66be1367Bill Berry9 months ago | 34 | |
3587e6abBill Berry6 months ago | 35 | $logsDir = Join-Path $repoRoot "logs" |
| 36 | if (-not (Test-Path $logsDir)) { | |
| 37 | New-Item -ItemType Directory -Path $logsDir -Force | Out-Null | |
35c44178o77 months ago | 38 | } |
3587e6abBill Berry6 months ago | 39 | |
| 40 | Write-Host "🔍 Checking for URLs with language paths..." -ForegroundColor Cyan | |
| 41 | | |
| 42 | try { | |
| 43 | $scriptArgs = @{} | |
| 44 | if ($ExcludePaths.Count -gt 0) { | |
| 45 | $scriptArgs['ExcludePaths'] = $ExcludePaths | |
66be1367Bill Berry9 months ago | 46 | } |
3587e6abBill Berry6 months ago | 47 | $jsonOutput = & (Join-Path $PSScriptRoot "Link-Lang-Check.ps1") @scriptArgs 2>&1 |
| 48 | | |
| 49 | $results = $jsonOutput | ConvertFrom-Json | |
| 50 | | |
| 51 | if ($results -and $results.Count -gt 0) { | |
| 52 | Write-Host "Found $($results.Count) URLs with 'en-us' language paths`n" -ForegroundColor Yellow | |
| 53 | | |
| 54 | foreach ($item in $results) { | |
| 55 | Write-CIAnnotation ` | |
| 56 | -Message "URL contains language path: $($item.original_url)" ` | |
| 57 | -Level Warning ` | |
| 58 | -File $item.file ` | |
| 59 | -Line $item.line_number | |
f52352dfBill Berry9 months ago | 60 | } |
3587e6abBill Berry6 months ago | 61 | |
| 62 | $outputData = @{ | |
| 63 | timestamp = (Get-Date).ToUniversalTime().ToString("o") | |
| 64 | script = "link-lang-check" | |
| 65 | summary = @{ | |
| 66 | total_issues = $results.Count | |
| 67 | files_affected = ($results | Select-Object -ExpandProperty file -Unique).Count | |
| 68 | } | |
| 69 | issues = $results | |
| 70 | } | |
| 71 | $outputData | ConvertTo-Json -Depth 3 | Out-File (Join-Path $logsDir "link-lang-check-results.json") -Encoding utf8 | |
| 72 | | |
| 73 | Set-CIOutput -Name "issues" -Value $results.Count | |
| 74 | Set-CIEnv -Name "LINK_LANG_FAILED" -Value "true" | |
| 75 | | |
| 76 | $uniqueFiles = $results | Select-Object -ExpandProperty file -Unique | |
| 77 | | |
| 78 | Write-CIStepSummary -Content @" | |
66be1367Bill Berry9 months ago | 79 | ## Link Language Path Check Results |
| 80 | | |
| 81 | ⚠️ **Status**: Issues Found | |
| 82 | | |
| 83 | Found $($results.Count) URL(s) containing language path 'en-us'. | |
| 84 | | |
| 85 | **Why this matters:** | |
| 86 | Language-specific URLs don't adapt to user preferences and may break for non-English users. | |
| 87 | | |
| 88 | **To fix locally:** | |
| 89 | ``````powershell | |
| 90 | scripts/linting/Link-Lang-Check.ps1 -Fix | |
| 91 | `````` | |
| 92 | | |
| 93 | **Files affected:** | |
3587e6abBill Berry6 months ago | 94 | $(($uniqueFiles | ForEach-Object { |
| 95 | $count = ($results | Where-Object file -eq $_).Count | |
| 96 | $safePath = if ((Get-CIPlatform) -eq 'azdo') { | |
| 97 | ConvertTo-AzureDevOpsEscaped -Value $_ | |
| 98 | } else { $_ } | |
| 99 | "- $safePath ($count occurrence(s))" | |
| 100 | }) -join "`n") | |
66be1367Bill Berry9 months ago | 101 | "@ |
3587e6abBill Berry6 months ago | 102 | |
| 103 | return 1 | |
| 104 | } | |
| 105 | | |
66be1367Bill Berry9 months ago | 106 | Write-Host "✅ No URLs with language paths found" -ForegroundColor Green |
3587e6abBill Berry6 months ago | 107 | |
f52352dfBill Berry9 months ago | 108 | $emptyResults = @{ |
| 109 | timestamp = (Get-Date).ToUniversalTime().ToString("o") | |
| 110 | script = "link-lang-check" | |
| 111 | summary = @{ | |
| 112 | total_issues = 0 | |
| 113 | files_affected = 0 | |
| 114 | } | |
| 115 | issues = @() | |
| 116 | } | |
| 117 | $emptyResults | ConvertTo-Json -Depth 3 | Out-File (Join-Path $logsDir "link-lang-check-results.json") -Encoding utf8 | |
3587e6abBill Berry6 months ago | 118 | |
| 119 | Set-CIOutput -Name "issues" -Value "0" | |
| 120 | | |
| 121 | Write-CIStepSummary -Content @" | |
66be1367Bill Berry9 months ago | 122 | ## Link Language Path Check Results |
| 123 | | |
| 124 | ✅ **Status**: Passed | |
| 125 | | |
| 126 | No URLs with language-specific paths detected. | |
| 127 | "@ | |
3587e6abBill Berry6 months ago | 128 | |
| 129 | return 0 | |
66be1367Bill Berry9 months ago | 130 | } |
3587e6abBill Berry6 months ago | 131 | catch { |
| 132 | Write-Error -ErrorAction Continue "Link-language check failed: $($_.Exception.Message)" | |
| 133 | Write-CIAnnotation -Message "Link-language check failed: $($_.Exception.Message)" -Level Error | |
| 134 | return 1 | |
35c44178o77 months ago | 135 | } |
66be1367Bill Berry9 months ago | 136 | } |
35c44178o77 months ago | 137 | |
3587e6abBill Berry6 months ago | 138 | #region Main Execution |
6b84a8e4Bill Berry6 months ago | 139 | if ($MyInvocation.InvocationName -ne '.') { |
| 140 | try { | |
| 141 | $exitCode = Invoke-LinkLanguageCheckCore -ExcludePaths $ExcludePaths | |
| 142 | exit $exitCode | |
| 143 | } | |
| 144 | catch { | |
| 145 | Write-Error -ErrorAction Continue "Invoke-LinkLanguageCheck failed: $($_.Exception.Message)" | |
| 146 | Write-CIAnnotation -Message $_.Exception.Message -Level Error | |
| 147 | exit 1 | |
| 148 | } | |
3587e6abBill Berry6 months ago | 149 | } |
6b84a8e4Bill Berry6 months ago | 150 | #endregion Main Execution |