Skip to content

Commit

Permalink
Merge pull request #31 from RainbowDashy/main
Browse files Browse the repository at this point in the history
feat: check release
  • Loading branch information
d-bytebase authored Dec 31, 2024
2 parents 6d26e03 + 2f3be9f commit 0ca30be
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 28 deletions.
3 changes: 3 additions & 0 deletions .github/actions/sql-review/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
81 changes: 56 additions & 25 deletions .github/actions/sql-review/dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,26 +41,28 @@ 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() {
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;
Expand All @@ -75,37 +77,66 @@ 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 = [];
const versionReg = /^\d+/;
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 filename = path.basename(file);
const versionM = filename.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({
name: file,
statement: content,
name: database,
};
const response = yield fetch(`${url}/v1/sql/check`, {
method: 'POST',
headers,
body: JSON.stringify(requestBody),
version: version,
changeType: "DDL",
type: "VERSIONED",
});
const httpStatus = response.status;
if (httpStatus !== 200) {
throw new Error(`Failed to check SQL file ${file} with response code ${httpStatus}`);
}
files.sort((a, b) => {
if (a.version < b.version) {
return -1;
}
const responseData = yield response.json();
core.debug("Reviews:" + JSON.stringify(responseData.advices));
responseData.advices.forEach((advice) => {
else if (a.version > b.version) {
return 1;
}
return 0;
});
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 < files.length; i++) {
const advices = responseData.results[i].advices;
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
core.info(annotation);
if (advice.status === 'ERROR' || (failOnWarnings && advice.status === 'WARNING')) {
if (advice.status === "ERROR" ||
(failOnWarnings && advice.status === "WARNING")) {
hasErrorOrWarning = true;
}
});
Expand Down
25 changes: 22 additions & 3 deletions .github/actions/sql-review/index.ts
Original file line number Diff line number Diff line change
@@ -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";

Expand Down Expand Up @@ -59,14 +60,32 @@ async function run(): Promise<void> {
let hasErrorOrWarning = false;

let files = [];
const versionReg = /^\d+/
for (const file of sqlFiles) {
const filename = path.basename(file)
const versionM = filename.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({
name: file,
statement: content,
version: "",
version: version,
changeType: "DDL",
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",
Expand All @@ -91,9 +110,9 @@ async function run(): Promise<void> {

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: {
Expand Down

0 comments on commit 0ca30be

Please sign in to comment.