Skip to content

Commit a48ca72

Browse files
ci(validation): add regex pattern validation
1 parent 153de09 commit a48ca72

3 files changed

Lines changed: 129 additions & 0 deletions

File tree

.github/workflows/regex.yml

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
name: Validate Regex Patterns
2+
3+
on:
4+
push:
5+
paths:
6+
- 'regex_patterns/**/*.yml'
7+
- 'regex_patterns/**/*.yaml'
8+
- '.github/workflows/regex.yml'
9+
- 'scripts/validatePattern.ps1'
10+
pull_request:
11+
paths:
12+
- 'regex_patterns/**/*.yml'
13+
- 'regex_patterns/**/*.yaml'
14+
- '.github/workflows/regex.yml'
15+
- 'scripts/validatePattern.ps1'
16+
workflow_dispatch:
17+
18+
jobs:
19+
discover-patterns:
20+
runs-on: ubuntu-latest
21+
outputs:
22+
matrix: ${{ steps.set-matrix.outputs.matrix }}
23+
steps:
24+
- uses: actions/checkout@v4
25+
26+
- id: set-matrix
27+
run: |
28+
patterns=$(find regex_patterns -name "*.yml" -o -name "*.yaml" | jq -R -s -c 'split("\n")[:-1] | map({file: ., name: (. | split("/")[-1] | split(".")[0])})')
29+
echo "matrix=$patterns" >> $GITHUB_OUTPUT
30+
echo "Found patterns: $patterns"
31+
32+
validate-pattern:
33+
needs: discover-patterns
34+
runs-on: ubuntu-latest
35+
strategy:
36+
fail-fast: false
37+
matrix:
38+
pattern: ${{ fromJson(needs.discover-patterns.outputs.matrix) }}
39+
name: Validate ${{ matrix.pattern.name }}
40+
steps:
41+
- uses: actions/checkout@v4
42+
43+
- name: Setup PowerShell
44+
run: |
45+
sudo apt-get update
46+
sudo apt-get install -y powershell
47+
48+
- name: Validate Pattern
49+
run: |
50+
pwsh scripts/validatePattern.ps1 -YamlFilePath "${{ matrix.pattern.file }}"
51+
52+
- name: Run Unit Tests
53+
run: |
54+
echo "TODO: Implement unit tests for ${{ matrix.pattern.file }}"
55+
# TODO: Add test runner command here

scripts/unitTest.ps1

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
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.

scripts/validatePattern.ps1

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
param(
2+
[Parameter(Mandatory=$true)]
3+
[string]$YamlFilePath
4+
)
5+
6+
$moduleName = "regex"
7+
8+
function Write-Log {
9+
param(
10+
[Parameter(Mandatory=$true)]
11+
[string]$Level,
12+
[Parameter(Mandatory=$true)]
13+
[string]$Message
14+
)
15+
16+
$timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
17+
$levelPadded = $Level.ToUpper().PadRight(7)
18+
$modulePadded = $moduleName.PadRight(0)
19+
20+
# Write colored level
21+
Write-Host -NoNewline "["
22+
if ($Level -eq "ERROR") {
23+
Write-Host -NoNewline $levelPadded -ForegroundColor Red
24+
}
25+
elseif ($Level -eq "SUCCESS") {
26+
Write-Host -NoNewline $levelPadded -ForegroundColor Green
27+
}
28+
elseif ($Level -eq "INFO") {
29+
Write-Host -NoNewline $levelPadded -ForegroundColor Cyan
30+
}
31+
else {
32+
Write-Host -NoNewline $levelPadded
33+
}
34+
Write-Host -NoNewline "] "
35+
36+
# Write grey timestamp
37+
Write-Host -NoNewline "[$timestamp] " -ForegroundColor DarkGray
38+
39+
# Write module and message in normal color
40+
Write-Host "[$modulePadded] $Message"
41+
}
42+
43+
try {
44+
# Check if file exists
45+
if (-not (Test-Path $YamlFilePath)) {
46+
Write-Log -Level "ERROR" -Message "YAML file not found: $YamlFilePath"
47+
exit 1
48+
}
49+
50+
# Read YAML content
51+
$yamlContent = Get-Content -Path $YamlFilePath -Raw
52+
53+
# Extract pattern field from YAML
54+
$patternMatch = [regex]::Match($yamlContent, 'pattern:\s*(.+)')
55+
$pattern = $patternMatch.Groups[1].Value.Trim()
56+
57+
Write-Log -Level "INFO" -Message "Found pattern: $pattern"
58+
59+
# Validate the pattern against .NET regex engine
60+
try {
61+
$regex = New-Object System.Text.RegularExpressions.Regex($pattern)
62+
Write-Log -Level "SUCCESS" -Message "Valid regex pattern"
63+
exit 0
64+
}
65+
catch {
66+
Write-Log -Level "ERROR" -Message "Pattern validation failed: $_"
67+
exit 1
68+
}
69+
}
70+
catch {
71+
Write-Log -Level "ERROR" -Message "Script execution failed: $_"
72+
exit 1
73+
}

0 commit comments

Comments
 (0)