perf: resource updater validator in native powershell (#10493)

* perf: native pwsh resourceupdatevalidator

* chore: pwsh implementation in workflow

* style: ps1 formatting

* style: remove log warning

* style: more log styling output

* style: too many lines lol

* style: log end output format new line instead of json
for readibility
This commit is contained in:
Constrat
2024-09-06 09:11:27 +01:00
committed by GitHub
parent 9b92283954
commit 1293504c64
2 changed files with 109 additions and 6 deletions

View File

@@ -178,14 +178,9 @@ jobs:
with:
args: -w ${{ steps.task_sorting.outputs.gitdiff }}
- name: Use Node.js
uses: actions/setup-node@v4
with:
node-version: lts/*
check-latest: true
- name: Check if only sorted and Templates
id: check_sorted_templates
run: node tools/ResourceUpdateValidator/index.mjs
run: pwsh tools/ResourceUpdateValidator/script.ps1
- name: Setup python
if: steps.check_sorted_templates.outputs.contains_png == 'True'

View File

@@ -0,0 +1,108 @@
# Check git status
Write-Output "Check git status..."
$gitStatus = git status
Write-Output $gitStatus
Write-Output "-------------------------------------------------------------------------------------------"
# Start to diff the file changes
Write-Output "Start to diff the file changes..."
$gitdiff = git diff --numstat HEAD 2>$null | findstr -i resource
if ($LASTEXITCODE -ne 0) {
Write-Output "no diff"
exit 0
}
Write-Output $gitdiff
Write-Output "-------------------------------------------------------------------------------------------"
$diff = $false
$hasPngDiff = $false
$listPerServer = @{}
$mainlandChina = "mainlandChina"
# Parse the diff output
$diffLines = $gitdiff -split "`n"
foreach ($line in $diffLines) {
$parts = $line -split "\s+"
$added = $parts[0]
$deleted = $parts[1]
$pathname = $parts[2]
$server = if ($pathname -like "resource/global/*") { $pathname -split "/" | Select-Object -Index 2 } else { $mainlandChina }
if (-not $listPerServer.ContainsKey($server)) {
$listPerServer[$server] = @()
}
$listPerServer[$server] += , @($added, $deleted, $pathname)
}
# Improved logging for parsed diff
Write-Output "Parsed diff result:"
foreach ($server in $listPerServer.Keys) {
Write-Output "${server}: ["
foreach ($entry in $listPerServer[$server]) {
Write-Output " [ '$($entry[0])', '$($entry[1])', '$($entry[2])' ],"
}
Write-Output "]`n"
}
Write-Output "-------------------------------------------------------------------------------------------"
# Process each server's changes
foreach ($server in $listPerServer.Keys) {
$serverName = if ($server -eq $mainlandChina) { "[mainlandChina]" } else { "[$server]" }
$localDiff = $false
$localHasPngDiff = $false
foreach ($entry in $listPerServer[$server]) {
$added = $entry[0]
$deleted = $entry[1]
$pathname = $entry[2]
if ($pathname -like "*.png") {
$localHasPngDiff = $true
$localDiff = $true
Write-Output "$serverName PNG changed: $pathname, valid."
continue
}
if ($added -gt 1 -or $added -ne $deleted) {
Write-Output "$serverName File changed: $pathname, valid."
$localDiff = $true
continue
}
if ($pathname -like "*/version.json") {
$versionDiff = git diff --unified=0 HEAD -- $pathname 2>$null
$addedLines = @($versionDiff -split "`n" | Where-Object { $_ -match "^\+" -and $_ -notmatch "^\+\+\+" })
$deletedLines = @($versionDiff -split "`n" | Where-Object { $_ -match "^\-" -and $_ -notmatch "^\-\-\-" })
if ($addedLines.Count -eq 1 -and $deletedLines.Count -eq 1 -and $addedLines[0] -match "^\+\s*`"last_updated`":" -and $deletedLines[0] -match "^\-\s*`"last_updated`":") {
Write-Output "$serverName Version-only changed: $pathname, not valid."
continue
}
else {
Write-Output "$serverName File changed: $pathname, valid."
$localDiff = $true
continue
}
}
}
if ($localHasPngDiff) {
$hasPngDiff = $true
}
if ($localDiff) {
$diff = $true
}
else {
Write-Output "$serverName No valid changes found in server: $server"
Write-Output "$serverName Revert the file changes..."
foreach ($entry in $listPerServer[$server]) {
$pathname = $entry[2]
git checkout HEAD -- $pathname
}
}
Write-Output "$serverName Server check result: { localHasPngDiff = $localHasPngDiff, localDiff = $localDiff }`n"
}
Write-Output "-------------------------------------------------------------------------------------------"
Write-Output "Diff check result:"
Write-Output "hasPngDiff: $hasPngDiff"
Write-Output "diff: $diff"
Write-Output "contains_png=$hasPngDiff" >> $env:GITHUB_OUTPUT
Write-Output "changes=$diff" >> $env:GITHUB_OUTPUT