ci: 当仅 version.json 的 last_updated 有变动时,回退对应服务器所有资源文件改动 (#10453)

This commit is contained in:
AnnAngela
2024-09-04 17:21:42 +08:00
committed by GitHub
parent fa557cfc10
commit 01ba8904e2
2 changed files with 83 additions and 43 deletions

View File

@@ -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'

View File

@@ -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" })}`);