From b201fe9defbea9dc5924bfe54d2d4b62d011fa1b Mon Sep 17 00:00:00 2001 From: p0ny Date: Mon, 30 Dec 2024 17:49:59 +0800 Subject: [PATCH 1/9] update --- .github/workflows/bytebase-sql-review.yml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/.github/workflows/bytebase-sql-review.yml b/.github/workflows/bytebase-sql-review.yml index bdf43e3..ed3fcd0 100644 --- a/.github/workflows/bytebase-sql-review.yml +++ b/.github/workflows/bytebase-sql-review.yml @@ -11,10 +11,10 @@ jobs: bytebase-sql-review: runs-on: ubuntu-latest env: - BYTEBASE_URL: "https://bytebase-ci.zeabur.app" - BYTEBASE_SERVICE_ACCOUNT: "ci@service.bytebase.com" - DATABASE: "instances/prod-instance/databases/example" - PROJECT: "projects/prj-ex" + BYTEBASE_URL: "https://6da2-38-150-12-163.ngrok-free.app" + BYTEBASE_SERVICE_ACCOUNT: "xz@bytebase.com" + DATABASE: "instances/dbdbdb/databases/db_1" + PROJECT: "projects/db333" name: SQL Review steps: - name: Checkout From 56506c659b48a59f850605f085ba7238c6a942e1 Mon Sep 17 00:00:00 2001 From: p0ny Date: Tue, 31 Dec 2024 10:07:39 +0800 Subject: [PATCH 2/9] build --- .github/actions/sql-review/dist/index.js | 63 ++++++++++++++---------- 1 file changed, 37 insertions(+), 26 deletions(-) diff --git a/.github/actions/sql-review/dist/index.js b/.github/actions/sql-review/dist/index.js index 40b0c57..47d46cb 100644 --- a/.github/actions/sql-review/dist/index.js +++ b/.github/actions/sql-review/dist/index.js @@ -47,20 +47,21 @@ function run() { var _a; return __awaiter(this, void 0, void 0, function* () { try { - const githubToken = core.getInput('github-token', { required: true }); - const pattern = core.getInput('pattern', { required: true }); + const githubToken = core.getInput("github-token", { required: true }); + const pattern = core.getInput("pattern", { required: true }); const octokit = github.getOctokit(githubToken); const url = core.getInput("url", { required: true }); const token = core.getInput("token", { required: true }); const database = core.getInput("database", { required: true }); - const extraHeaders = core.getInput('headers'); - const failOnWarnings = core.getBooleanInput('fail-on-warnings'); + const project = core.getInput("project", { required: true }); + const extraHeaders = core.getInput("headers"); + const failOnWarnings = core.getBooleanInput("fail-on-warnings"); let headers = extraHeaders ? JSON.parse(extraHeaders) : {}; - headers = Object.assign({ "Content-Type": "application/json", 'Authorization': `Bearer ${token}` }, headers); + headers = Object.assign({ "Content-Type": "application/json", Authorization: `Bearer ${token}` }, headers); const { owner, repo } = github.context.repo; const prNumber = (_a = github.context.payload.pull_request) === null || _a === void 0 ? void 0 : _a.number; if (!prNumber) { - throw new Error('Could not get PR number from the context; this action should only be run on pull_request events.'); + throw new Error("Could not get PR number from the context; this action should only be run on pull_request events."); } let allChangedFiles = []; let page = 0; @@ -75,37 +76,47 @@ function run() { per_page: 100, page, }); - allChangedFiles.push(...fileList.data.map(file => file.filename)); + allChangedFiles.push(...fileList.data.map((file) => file.filename)); } while (fileList.data.length !== 0); // Use glob.sync to synchronously match files against the pattern const matchedFiles = glob.sync(pattern, { nodir: true }); // Filter matchedFiles to include only those that are also in allChangedFiles - const sqlFiles = matchedFiles.filter(file => allChangedFiles.includes(file)); + const sqlFiles = matchedFiles.filter((file) => allChangedFiles.includes(file)); let hasErrorOrWarning = false; + let files = []; for (const file of sqlFiles) { - const content = yield fs_1.promises.readFile(file, 'utf8'); - core.debug(`\nContent of ${file}:`); - core.debug(content); - const requestBody = { + const content = yield fs_1.promises.readFile(file, "utf8"); + files.push({ statement: content, - name: database, - }; - const response = yield fetch(`${url}/v1/sql/check`, { - method: 'POST', - headers, - body: JSON.stringify(requestBody), + version: "", + changeType: "DDL", }); - const httpStatus = response.status; - if (httpStatus !== 200) { - throw new Error(`Failed to check SQL file ${file} with response code ${httpStatus}`); - } - const responseData = yield response.json(); - core.debug("Reviews:" + JSON.stringify(responseData.advices)); - responseData.advices.forEach((advice) => { + } + const response = yield fetch(`${url}/v1/${project}/releases:check`, { + method: "POST", + headers, + body: JSON.stringify({ + release: { + files: files, + }, + targets: [database], + }), + }); + const httpStatus = response.status; + if (httpStatus !== 200) { + throw new Error(`Failed to check release with response code ${httpStatus}`); + } + const responseData = yield response.json(); + core.debug("Reviews:" + JSON.stringify(responseData.results)); + for (let i = 0; i < sqlFiles.length; i++) { + const advices = responseData.results[i].advices; + const file = sqlFiles[i]; + advices.forEach((advice) => { const annotation = `::${advice.status} file=${file},line=${advice.line},col=${advice.column},title=${advice.title} (${advice.code})::${advice.content}. https://www.bytebase.com/docs/reference/error-code/advisor#${advice.code}`; // Emit annotations for each advice core.info(annotation); - if (advice.status === 'ERROR' || (failOnWarnings && advice.status === 'WARNING')) { + if (advice.status === "ERROR" || + (failOnWarnings && advice.status === "WARNING")) { hasErrorOrWarning = true; } }); From f8c39f4d00448e27dc8130f17c6dce11860ad7da Mon Sep 17 00:00:00 2001 From: p0ny Date: Tue, 31 Dec 2024 10:11:36 +0800 Subject: [PATCH 3/9] u --- .github/actions/sql-review/action.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.github/actions/sql-review/action.yml b/.github/actions/sql-review/action.yml index 62dfbcf..70a76e2 100644 --- a/.github/actions/sql-review/action.yml +++ b/.github/actions/sql-review/action.yml @@ -17,6 +17,9 @@ inputs: required: true headers: description: 'JSON string of extra headers to include in the request. e.g Cloudflare Zero Trust headers {"CF-Access-Client-Id": "xxx", CF-Access-Client-Secret: "xxx"}' + project: + description: "The name of the project. Example: projects/example" + required: true database: description: "The name of database. Example: instances/prod-instance/databases/example" required: true From 37d9f061cd081a907ca72e880772f4deb22a5f62 Mon Sep 17 00:00:00 2001 From: p0ny Date: Tue, 31 Dec 2024 10:17:29 +0800 Subject: [PATCH 4/9] u --- .github/actions/sql-review/dist/index.js | 9 ++++++++- .github/actions/sql-review/index.ts | 9 ++++++++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/.github/actions/sql-review/dist/index.js b/.github/actions/sql-review/dist/index.js index 47d46cb..a3b6eb8 100644 --- a/.github/actions/sql-review/dist/index.js +++ b/.github/actions/sql-review/dist/index.js @@ -84,11 +84,18 @@ function run() { const sqlFiles = matchedFiles.filter((file) => allChangedFiles.includes(file)); let hasErrorOrWarning = false; let files = []; + const versionReg = /^\d+/; for (const file of sqlFiles) { + const versionM = file.match(versionReg); + if (!versionM) { + core.info(`failed to get version, ignore ${file}`); + continue; + } + const version = versionM[0]; const content = yield fs_1.promises.readFile(file, "utf8"); files.push({ statement: content, - version: "", + version: version, changeType: "DDL", }); } diff --git a/.github/actions/sql-review/index.ts b/.github/actions/sql-review/index.ts index cdd45b9..4a38bc5 100644 --- a/.github/actions/sql-review/index.ts +++ b/.github/actions/sql-review/index.ts @@ -59,11 +59,18 @@ async function run(): Promise { let hasErrorOrWarning = false; let files = []; + const versionReg = /^\d+/ for (const file of sqlFiles) { + const versionM = file.match(versionReg) + if (!versionM) { + core.info(`failed to get version, ignore ${file}`) + continue + } + const version = versionM[0] const content = await fs.readFile(file, "utf8"); files.push({ statement: content, - version: "", + version: version, changeType: "DDL", }); } From 12560244b1767c47ff4f9be6d149f0450d2e801d Mon Sep 17 00:00:00 2001 From: p0ny Date: Tue, 31 Dec 2024 10:20:15 +0800 Subject: [PATCH 5/9] u --- .github/actions/sql-review/dist/index.js | 5 ++++- .github/actions/sql-review/index.ts | 5 ++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/.github/actions/sql-review/dist/index.js b/.github/actions/sql-review/dist/index.js index a3b6eb8..eaf86b0 100644 --- a/.github/actions/sql-review/dist/index.js +++ b/.github/actions/sql-review/dist/index.js @@ -41,6 +41,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge Object.defineProperty(exports, "__esModule", ({ value: true })); const core = __importStar(__nccwpck_require__(9093)); const github = __importStar(__nccwpck_require__(5942)); +const path = __importStar(__nccwpck_require__(1017)); const fs_1 = __nccwpck_require__(7147); const glob = __importStar(__nccwpck_require__(5177)); function run() { @@ -86,7 +87,8 @@ function run() { let files = []; const versionReg = /^\d+/; for (const file of sqlFiles) { - const versionM = file.match(versionReg); + const filename = path.basename(file); + const versionM = filename.match(versionReg); if (!versionM) { core.info(`failed to get version, ignore ${file}`); continue; @@ -94,6 +96,7 @@ function run() { const version = versionM[0]; const content = yield fs_1.promises.readFile(file, "utf8"); files.push({ + name: file, statement: content, version: version, changeType: "DDL", diff --git a/.github/actions/sql-review/index.ts b/.github/actions/sql-review/index.ts index 4a38bc5..5386f04 100644 --- a/.github/actions/sql-review/index.ts +++ b/.github/actions/sql-review/index.ts @@ -1,5 +1,6 @@ import * as core from "@actions/core"; import * as github from "@actions/github"; +import * as path from 'path'; import { promises as fs } from "fs"; import * as glob from "glob"; @@ -61,7 +62,8 @@ async function run(): Promise { let files = []; const versionReg = /^\d+/ for (const file of sqlFiles) { - const versionM = file.match(versionReg) + const filename = path.basename(file) + const versionM = filename.match(versionReg) if (!versionM) { core.info(`failed to get version, ignore ${file}`) continue @@ -69,6 +71,7 @@ async function run(): Promise { const version = versionM[0] const content = await fs.readFile(file, "utf8"); files.push({ + name: file, statement: content, version: version, changeType: "DDL", From a93cf95dbf6aed56cdd862afe7330b13966c2d1f Mon Sep 17 00:00:00 2001 From: p0ny Date: Tue, 31 Dec 2024 10:21:27 +0800 Subject: [PATCH 6/9] u --- .github/actions/sql-review/dist/index.js | 1 + .github/actions/sql-review/index.ts | 1 + 2 files changed, 2 insertions(+) diff --git a/.github/actions/sql-review/dist/index.js b/.github/actions/sql-review/dist/index.js index eaf86b0..e2a5c35 100644 --- a/.github/actions/sql-review/dist/index.js +++ b/.github/actions/sql-review/dist/index.js @@ -100,6 +100,7 @@ function run() { statement: content, version: version, changeType: "DDL", + type: "VERSIONED", }); } const response = yield fetch(`${url}/v1/${project}/releases:check`, { diff --git a/.github/actions/sql-review/index.ts b/.github/actions/sql-review/index.ts index 5386f04..d02d082 100644 --- a/.github/actions/sql-review/index.ts +++ b/.github/actions/sql-review/index.ts @@ -75,6 +75,7 @@ async function run(): Promise { statement: content, version: version, changeType: "DDL", + type: "VERSIONED", }); } From 98314378011d020ad699091f3bf853c8e54df5fe Mon Sep 17 00:00:00 2001 From: p0ny Date: Tue, 31 Dec 2024 10:25:06 +0800 Subject: [PATCH 7/9] sort --- .github/actions/sql-review/dist/index.js | 9 +++++++++ .github/actions/sql-review/index.ts | 8 ++++++++ 2 files changed, 17 insertions(+) diff --git a/.github/actions/sql-review/dist/index.js b/.github/actions/sql-review/dist/index.js index e2a5c35..2f79e79 100644 --- a/.github/actions/sql-review/dist/index.js +++ b/.github/actions/sql-review/dist/index.js @@ -103,6 +103,15 @@ function run() { type: "VERSIONED", }); } + files.sort((a, b) => { + if (a.version < b.version) { + return -1; + } + else if (a.version > b.version) { + return 1; + } + return 0; + }); const response = yield fetch(`${url}/v1/${project}/releases:check`, { method: "POST", headers, diff --git a/.github/actions/sql-review/index.ts b/.github/actions/sql-review/index.ts index d02d082..3c7b8ba 100644 --- a/.github/actions/sql-review/index.ts +++ b/.github/actions/sql-review/index.ts @@ -78,6 +78,14 @@ async function run(): Promise { type: "VERSIONED", }); } + files.sort((a, b) => { + if (a.version < b.version) { + return -1 + } else if (a.version > b.version) { + return 1 + } + return 0 + }) const response = await fetch(`${url}/v1/${project}/releases:check`, { method: "POST", From 78e3b433df45effd09b2b652001849cd4f234701 Mon Sep 17 00:00:00 2001 From: p0ny Date: Tue, 31 Dec 2024 10:27:21 +0800 Subject: [PATCH 8/9] order --- .github/actions/sql-review/dist/index.js | 4 ++-- .github/actions/sql-review/index.ts | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/.github/actions/sql-review/dist/index.js b/.github/actions/sql-review/dist/index.js index 2f79e79..d0daaff 100644 --- a/.github/actions/sql-review/dist/index.js +++ b/.github/actions/sql-review/dist/index.js @@ -128,9 +128,9 @@ function run() { } const responseData = yield response.json(); core.debug("Reviews:" + JSON.stringify(responseData.results)); - for (let i = 0; i < sqlFiles.length; i++) { + for (let i = 0; i < files.length; i++) { const advices = responseData.results[i].advices; - const file = sqlFiles[i]; + const file = files[i].name; advices.forEach((advice) => { const annotation = `::${advice.status} file=${file},line=${advice.line},col=${advice.column},title=${advice.title} (${advice.code})::${advice.content}. https://www.bytebase.com/docs/reference/error-code/advisor#${advice.code}`; // Emit annotations for each advice diff --git a/.github/actions/sql-review/index.ts b/.github/actions/sql-review/index.ts index 3c7b8ba..6435dca 100644 --- a/.github/actions/sql-review/index.ts +++ b/.github/actions/sql-review/index.ts @@ -110,9 +110,9 @@ async function run(): Promise { core.debug("Reviews:" + JSON.stringify(responseData.results)); - for (let i = 0; i < sqlFiles.length; i++) { + for (let i = 0; i < files.length; i++) { const advices = responseData.results[i].advices; - const file = sqlFiles[i]; + const file = files[i].name; advices.forEach( (advice: { From 2f3be9fc55b6de25f875d8413bf7687955b8991b Mon Sep 17 00:00:00 2001 From: p0ny Date: Tue, 31 Dec 2024 11:04:15 +0800 Subject: [PATCH 9/9] Revert "update" This reverts commit b201fe9defbea9dc5924bfe54d2d4b62d011fa1b. --- .github/workflows/bytebase-sql-review.yml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/.github/workflows/bytebase-sql-review.yml b/.github/workflows/bytebase-sql-review.yml index ed3fcd0..bdf43e3 100644 --- a/.github/workflows/bytebase-sql-review.yml +++ b/.github/workflows/bytebase-sql-review.yml @@ -11,10 +11,10 @@ jobs: bytebase-sql-review: runs-on: ubuntu-latest env: - BYTEBASE_URL: "https://6da2-38-150-12-163.ngrok-free.app" - BYTEBASE_SERVICE_ACCOUNT: "xz@bytebase.com" - DATABASE: "instances/dbdbdb/databases/db_1" - PROJECT: "projects/db333" + BYTEBASE_URL: "https://bytebase-ci.zeabur.app" + BYTEBASE_SERVICE_ACCOUNT: "ci@service.bytebase.com" + DATABASE: "instances/prod-instance/databases/example" + PROJECT: "projects/prj-ex" name: SQL Review steps: - name: Checkout