From 5ee986c58e8a7e6ce9b1bdd5cbc132925c6d0724 Mon Sep 17 00:00:00 2001 From: Louis Lam Date: Mon, 28 Oct 2024 14:20:29 +0800 Subject: [PATCH] =?UTF-8?q?Check=20knex=20filenames=20and=20rename=20json-?= =?UTF-8?q?yaml-validate.yml=20=E2=86=92=20validate.yml=20for=20general=20?= =?UTF-8?q?purposes=20=20(#5263)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../{json-yaml-validate.yml => validate.yml} | 12 +++- extra/check-knex-filenames.mjs | 72 +++++++++++++++++++ 2 files changed, 81 insertions(+), 3 deletions(-) rename .github/workflows/{json-yaml-validate.yml => validate.yml} (76%) create mode 100644 extra/check-knex-filenames.mjs diff --git a/.github/workflows/json-yaml-validate.yml b/.github/workflows/validate.yml similarity index 76% rename from .github/workflows/json-yaml-validate.yml rename to .github/workflows/validate.yml index 7942884e..7e631ccd 100644 --- a/.github/workflows/json-yaml-validate.yml +++ b/.github/workflows/validate.yml @@ -1,4 +1,4 @@ -name: json-yaml-validate +name: validate on: push: branches: @@ -26,7 +26,8 @@ jobs: comment: "true" # enable comment mode exclude_file: ".github/config/exclude.txt" # gitignore style file for exclusions - check-lang-json: + # General validations + validate: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 @@ -34,4 +35,9 @@ jobs: uses: actions/setup-node@v4 with: node-version: 20 - - run: node ./extra/check-lang-json.js + + - name: Validate language JSON files + run: node ./extra/check-lang-json.js + + - name: Validate knex migrations filename + run: node ./extra/check-knex-filenames.mjs diff --git a/extra/check-knex-filenames.mjs b/extra/check-knex-filenames.mjs new file mode 100644 index 00000000..4911fc56 --- /dev/null +++ b/extra/check-knex-filenames.mjs @@ -0,0 +1,72 @@ +import fs from "fs"; +const dir = "./db/knex_migrations"; + +// Get the file list (ending with .js) from the directory +const files = fs.readdirSync(dir).filter((file) => file !== "README.md"); + +// They are wrong, but they had been merged, so allowed. +const exceptionList = [ + "2024-08-24-000-add-cache-bust.js", + "2024-10-1315-rabbitmq-monitor.js", +]; + +// Correct format: YYYY-MM-DD-HHmm-description.js + +for (const file of files) { + if (exceptionList.includes(file)) { + continue; + } + + // Check ending with .js + if (!file.endsWith(".js")) { + console.error(`It should end with .js: ${file}`); + process.exit(1); + } + + const parts = file.split("-"); + + // Should be at least 5 parts + if (parts.length < 5) { + console.error(`Invalid format: ${file}`); + process.exit(1); + } + + // First part should be a year >= 2024 + const year = parseInt(parts[0], 10); + if (isNaN(year) || year < 2023) { + console.error(`Invalid year: ${file}`); + process.exit(1); + } + + // Second part should be a month + const month = parseInt(parts[1], 10); + if (isNaN(month) || month < 1 || month > 12) { + console.error(`Invalid month: ${file}`); + process.exit(1); + } + + // Third part should be a day + const day = parseInt(parts[2], 10); + if (isNaN(day) || day < 1 || day > 31) { + console.error(`Invalid day: ${file}`); + process.exit(1); + } + + // Fourth part should be HHmm + const time = parts[3]; + + // Check length is 4 + if (time.length !== 4) { + console.error(`Invalid time: ${file}`); + process.exit(1); + } + + const hour = parseInt(time.substring(0, 2), 10); + const minute = parseInt(time.substring(2), 10); + if (isNaN(hour) || hour < 0 || hour > 23 || isNaN(minute) || minute < 0 || minute > 59) { + console.error(`Invalid time: ${file}`); + process.exit(1); + } +} + +console.log("All knex filenames are correct.");