ci(validation): add regex pattern validation

This commit is contained in:
Sam Chau
2025-08-27 04:41:43 +09:30
parent 153de092c5
commit a48ca72395
3 changed files with 129 additions and 0 deletions

55
.github/workflows/regex.yml vendored Normal file
View File

@@ -0,0 +1,55 @@
name: Validate Regex Patterns
on:
push:
paths:
- 'regex_patterns/**/*.yml'
- 'regex_patterns/**/*.yaml'
- '.github/workflows/regex.yml'
- 'scripts/validatePattern.ps1'
pull_request:
paths:
- 'regex_patterns/**/*.yml'
- 'regex_patterns/**/*.yaml'
- '.github/workflows/regex.yml'
- 'scripts/validatePattern.ps1'
workflow_dispatch:
jobs:
discover-patterns:
runs-on: ubuntu-latest
outputs:
matrix: ${{ steps.set-matrix.outputs.matrix }}
steps:
- uses: actions/checkout@v4
- id: set-matrix
run: |
patterns=$(find regex_patterns -name "*.yml" -o -name "*.yaml" | jq -R -s -c 'split("\n")[:-1] | map({file: ., name: (. | split("/")[-1] | split(".")[0])})')
echo "matrix=$patterns" >> $GITHUB_OUTPUT
echo "Found patterns: $patterns"
validate-pattern:
needs: discover-patterns
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
pattern: ${{ fromJson(needs.discover-patterns.outputs.matrix) }}
name: Validate ${{ matrix.pattern.name }}
steps:
- uses: actions/checkout@v4
- name: Setup PowerShell
run: |
sudo apt-get update
sudo apt-get install -y powershell
- name: Validate Pattern
run: |
pwsh scripts/validatePattern.ps1 -YamlFilePath "${{ matrix.pattern.file }}"
- name: Run Unit Tests
run: |
echo "TODO: Implement unit tests for ${{ matrix.pattern.file }}"
# TODO: Add test runner command here

1
scripts/unitTest.ps1 Normal file
View File

@@ -0,0 +1 @@
# TODO: Run a single unit test given a regular expression and unit test. A unit test includes the input string and whether it should match or not.

View File

@@ -0,0 +1,73 @@
param(
[Parameter(Mandatory=$true)]
[string]$YamlFilePath
)
$moduleName = "regex"
function Write-Log {
param(
[Parameter(Mandatory=$true)]
[string]$Level,
[Parameter(Mandatory=$true)]
[string]$Message
)
$timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
$levelPadded = $Level.ToUpper().PadRight(7)
$modulePadded = $moduleName.PadRight(0)
# Write colored level
Write-Host -NoNewline "["
if ($Level -eq "ERROR") {
Write-Host -NoNewline $levelPadded -ForegroundColor Red
}
elseif ($Level -eq "SUCCESS") {
Write-Host -NoNewline $levelPadded -ForegroundColor Green
}
elseif ($Level -eq "INFO") {
Write-Host -NoNewline $levelPadded -ForegroundColor Cyan
}
else {
Write-Host -NoNewline $levelPadded
}
Write-Host -NoNewline "] "
# Write grey timestamp
Write-Host -NoNewline "[$timestamp] " -ForegroundColor DarkGray
# Write module and message in normal color
Write-Host "[$modulePadded] $Message"
}
try {
# Check if file exists
if (-not (Test-Path $YamlFilePath)) {
Write-Log -Level "ERROR" -Message "YAML file not found: $YamlFilePath"
exit 1
}
# Read YAML content
$yamlContent = Get-Content -Path $YamlFilePath -Raw
# Extract pattern field from YAML
$patternMatch = [regex]::Match($yamlContent, 'pattern:\s*(.+)')
$pattern = $patternMatch.Groups[1].Value.Trim()
Write-Log -Level "INFO" -Message "Found pattern: $pattern"
# Validate the pattern against .NET regex engine
try {
$regex = New-Object System.Text.RegularExpressions.Regex($pattern)
Write-Log -Level "SUCCESS" -Message "Valid regex pattern"
exit 0
}
catch {
Write-Log -Level "ERROR" -Message "Pattern validation failed: $_"
exit 1
}
}
catch {
Write-Log -Level "ERROR" -Message "Script execution failed: $_"
exit 1
}