Files
git.stella-ops.org/devops/scripts/validate-before-commit.ps1

177 lines
4.6 KiB
PowerShell

<#
.SYNOPSIS
Pre-Commit Validation Script for Windows
.DESCRIPTION
Run this script before committing to ensure all CI checks will pass.
Wraps the Bash validation script via WSL2 or Git Bash.
.PARAMETER Level
Validation level:
- quick : Smoke test only (~2 min)
- pr : Full PR-gating suite (~15 min) [default]
- full : All tests including extended (~45 min)
.EXAMPLE
.\validate-before-commit.ps1
Run PR-gating validation
.EXAMPLE
.\validate-before-commit.ps1 quick
Run quick smoke test only
.EXAMPLE
.\validate-before-commit.ps1 full
Run full test suite
#>
[CmdletBinding()]
param(
[Parameter(Position = 0)]
[ValidateSet('quick', 'pr', 'full')]
[string]$Level = 'pr',
[switch]$Help
)
# Script location
$ScriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path
$RepoRoot = Split-Path -Parent (Split-Path -Parent $ScriptDir)
if ($Help) {
Get-Help $MyInvocation.MyCommand.Path -Detailed
exit 0
}
# Colors
function Write-ColoredOutput {
param(
[string]$Message,
[ConsoleColor]$Color = [ConsoleColor]::White
)
$originalColor = $Host.UI.RawUI.ForegroundColor
$Host.UI.RawUI.ForegroundColor = $Color
Write-Host $Message
$Host.UI.RawUI.ForegroundColor = $originalColor
}
function Write-Header {
param([string]$Message)
Write-Host ""
Write-ColoredOutput "=============================================" -Color Cyan
Write-ColoredOutput " $Message" -Color Cyan
Write-ColoredOutput "=============================================" -Color Cyan
Write-Host ""
}
function Write-Step { Write-ColoredOutput ">>> $args" -Color Blue }
function Write-Pass { Write-ColoredOutput "[PASS] $args" -Color Green }
function Write-Fail { Write-ColoredOutput "[FAIL] $args" -Color Red }
function Write-Warn { Write-ColoredOutput "[WARN] $args" -Color Yellow }
function Write-Info { Write-ColoredOutput "[INFO] $args" -Color Cyan }
# Find Bash
function Find-BashExecutable {
# Check WSL
$wsl = Get-Command wsl -ErrorAction SilentlyContinue
if ($wsl) {
$wslCheck = & wsl --status 2>&1
if ($LASTEXITCODE -eq 0) {
return @{ Type = 'wsl'; Path = 'wsl' }
}
}
# Check Git Bash
$gitBashPaths = @(
"C:\Program Files\Git\bin\bash.exe",
"C:\Program Files (x86)\Git\bin\bash.exe",
"$env:LOCALAPPDATA\Programs\Git\bin\bash.exe"
)
foreach ($path in $gitBashPaths) {
if (Test-Path $path) {
return @{ Type = 'gitbash'; Path = $path }
}
}
return $null
}
function Convert-ToUnixPath {
param([string]$WindowsPath)
if ($WindowsPath -match '^([A-Za-z]):(.*)$') {
$drive = $Matches[1].ToLower()
$rest = $Matches[2] -replace '\\', '/'
return "/mnt/$drive$rest"
}
return $WindowsPath -replace '\\', '/'
}
# Main
Write-Header "Pre-Commit Validation (Windows)"
Write-Info "Level: $Level"
Write-Info "Repository: $RepoRoot"
$bash = Find-BashExecutable
if (-not $bash) {
Write-Fail "Bash not found. Install WSL2 or Git for Windows."
exit 1
}
Write-Info "Using: $($bash.Type)"
$scriptPath = Join-Path $ScriptDir "validate-before-commit.sh"
if (-not (Test-Path $scriptPath)) {
Write-Fail "Script not found: $scriptPath"
exit 1
}
$startTime = Get-Date
try {
switch ($bash.Type) {
'wsl' {
$unixScript = Convert-ToUnixPath $scriptPath
& wsl bash $unixScript $Level
}
'gitbash' {
$unixScript = $scriptPath -replace '\\', '/'
& $bash.Path $unixScript $Level
}
}
$exitCode = $LASTEXITCODE
}
catch {
Write-Fail "Execution failed: $_"
$exitCode = 1
}
$duration = (Get-Date) - $startTime
$minutes = [math]::Floor($duration.TotalMinutes)
$seconds = $duration.Seconds
Write-Header "Summary"
Write-Info "Duration: ${minutes}m ${seconds}s"
if ($exitCode -eq 0) {
Write-Host ""
Write-ColoredOutput "=============================================" -Color Green
Write-ColoredOutput " ALL CHECKS PASSED - Ready to commit!" -Color Green
Write-ColoredOutput "=============================================" -Color Green
Write-Host ""
Write-Host "Next steps:"
Write-Host " git add -A"
Write-Host ' git commit -m "Your commit message"'
Write-Host ""
} else {
Write-Host ""
Write-ColoredOutput "=============================================" -Color Red
Write-ColoredOutput " VALIDATION FAILED - Do not commit!" -Color Red
Write-ColoredOutput "=============================================" -Color Red
Write-Host ""
Write-Host "Check the logs in: out/local-ci/logs/"
Write-Host ""
}
exit $exitCode