From 01ba8904e2744e1909af0d26a5e8dbf9bc587ccb Mon Sep 17 00:00:00 2001 From: AnnAngela Date: Wed, 4 Sep 2024 17:21:42 +0800 Subject: [PATCH] =?UTF-8?q?ci:=20=E5=BD=93=E4=BB=85=20version.json=20?= =?UTF-8?q?=E7=9A=84=20last=5Fupdated=20=E6=9C=89=E5=8F=98=E5=8A=A8?= =?UTF-8?q?=E6=97=B6=EF=BC=8C=E5=9B=9E=E9=80=80=E5=AF=B9=E5=BA=94=E6=9C=8D?= =?UTF-8?q?=E5=8A=A1=E5=99=A8=E6=89=80=E6=9C=89=E8=B5=84=E6=BA=90=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E6=94=B9=E5=8A=A8=20(#10453)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/res-update-game.yml | 49 ++-------------- tools/ResourceUpdateValidator/index.mjs | 77 +++++++++++++++++++++++++ 2 files changed, 83 insertions(+), 43 deletions(-) create mode 100644 tools/ResourceUpdateValidator/index.mjs diff --git a/.github/workflows/res-update-game.yml b/.github/workflows/res-update-game.yml index d1256e3d1c..351c799740 100644 --- a/.github/workflows/res-update-game.yml +++ b/.github/workflows/res-update-game.yml @@ -178,51 +178,14 @@ 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: | - git status - - $gitdiff = git diff --numstat HEAD 2>$null | findstr -i resource - if ($LASTEXITCODE -ne 0) { - Write-Output "no diff" - exit 0 - } - - Write-Output $gitdiff - - $diff = $false - $hasPng = $false - - $diffLines = $gitdiff -split "`n" - - foreach ($line in $diffLines) { - $parts = $line -split "\s+" - $filePath = $parts[2] - $firstNumber = $parts[0] - $secondNumber = $parts[1] - - if ($filePath -match "\.png$") { - $hasPng = $true - } - - if ($firstNumber -gt 1 -or $firstNumber -ne $secondNumber) { - $diff = $true - } - - if ($hasPng -and $diff) { - break - } - } - - $changes = $hasPng -or $diff - - Write-Output "diff: $diff" - Write-Output "hasPng: $hasPng" - Write-Output "changes: $changes" - - Write-Output "contains_png=$hasPng" >> $env:GITHUB_OUTPUT - Write-Output "changes=$changes" >> $env:GITHUB_OUTPUT + run: node tools/ResourceUpdateValidator/index.mjs - name: Setup python if: steps.check_sorted_templates.outputs.contains_png == 'True' diff --git a/tools/ResourceUpdateValidator/index.mjs b/tools/ResourceUpdateValidator/index.mjs new file mode 100644 index 0000000000..b774dc1b55 --- /dev/null +++ b/tools/ResourceUpdateValidator/index.mjs @@ -0,0 +1,77 @@ +import fs from "node:fs"; +import path from "node:path"; +import { exec as child_process_exec } from "node:child_process"; +import { promisify } from "node:util"; + +const exec = promisify(child_process_exec); +const log = (...args) => console.info(`[${new Date().toLocaleString("zh-Hans-CN")}]`, ...args); + +log("Check git status..."); +log(`git status:\n${(await exec("git status")).stdout}`); +log("Start to diff the file changes..."); +const { stdout } = await exec("git diff --numstat HEAD"); +log(`Diff:\n${stdout}`); +let diff = false; +let hasPngDiff = false; +const listPerServer = {}; +const mainlandChina = Symbol("mainlandChina"); +for (const line of stdout.split(/\r*\n+/)) { + const [added, deleted, pathname] = line.split(/\s+/); + const server = pathname.startsWith("resource/global/") ? pathname.split("/")[2] : mainlandChina; + if (!Reflect.has(listPerServer, server)) { + listPerServer[server] = []; + } + listPerServer[server].push([added, deleted, pathname]); +} +log("Parsed diff:", listPerServer); +for (const [server, list] of [...Object.entries(listPerServer), ...Object.getOwnPropertySymbols(listPerServer).map((sym) => [sym, listPerServer[sym]])]) { + const serverName = `[${typeof server === "symbol" ? server.description : server}]`; + log(serverName, `Server: ${typeof server === "symbol" ? server.description : server}`); + let localDiff = false; + let localHasPngDiff = false; + for (const [added, deleted, pathname] of list) { + if (path.posix.extname(pathname) === ".png") { + localHasPngDiff = true; + localDiff = true; + log(serverName, `PNG changed: ${pathname}, valid.`); + continue; + } + if (added > 1 || added !== deleted) { + log(serverName, `File changed: ${pathname}, valid.`); + localDiff = true; + continue; + } + if (path.posix.basename(pathname) === "version.json") { + const { stdout: versionDiff } = await exec(`git diff --unified=0 HEAD -- ${pathname}`); + const addedLine = versionDiff.split(/\r*\n+/).filter((line) => line.startsWith("+") && !line.startsWith("+++ ")); + const deletedLine = versionDiff.split(/\r*\n+/).filter((line) => line.startsWith("-") && !line.startsWith("--- ")); + if ( + addedLine.length === 1 && /^\+\s*"last_updated":/.test(addedLine[0]) + && deletedLine.length === 1 && /^-\s*"last_updated":/.test(deletedLine[0]) + ) { + log(serverName, `Version-only changed: ${pathname}, not valid.`); + continue; + } else { + log(serverName, `File changed: ${pathname}, valid.`); + localDiff = true; + continue; + } + } + } + if (localHasPngDiff) { + hasPngDiff = true; + } + if (localDiff) { + diff = true; + } else { + log(serverName, `No valid changes found in server: ${server}`); + log(serverName, "Revert the file changes..."); + for (const [, , pathname] of list) { + await exec(`git checkout HEAD -- ${pathname}`); + } + } + log(serverName, "Server check result:", { localHasPngDiff, localDiff }); +} +log("Diff check result:", { hasPngDiff, diff }); +await fs.promises.appendFile(process.env.GITHUB_OUTPUT, `contains_png=${hasPngDiff}\nchanges=${diff}\n`, { encoding: "utf-8" }); +log(`GITHUB_OUTPUT:\n${await fs.promises.readFile(process.env.GITHUB_OUTPUT, { encoding: "utf-8" })}`);