ci(jdbc): auto bump plugin releases

This commit is contained in:
t8y2
2026-05-18 21:01:39 +08:00
parent 244eb23287
commit df87ec994e
4 changed files with 178 additions and 28 deletions
@@ -0,0 +1,88 @@
#!/usr/bin/env node
import { execFileSync } from "node:child_process";
import { readFileSync, writeFileSync } from "node:fs";
const POM_PATH = "plugins/jdbc/pom.xml";
const MANIFEST_PATH = "plugins/jdbc/manifest.json";
function firstProjectVersion(pomXml) {
const match = pomXml.match(/<project[\s\S]*?<version>([^<]+)<\/version>/);
return match?.[1]?.trim() ?? "";
}
function manifestVersion(manifestJson) {
return JSON.parse(manifestJson).version ?? "";
}
function bumpPatchVersion(version) {
const match = version.match(/^(\d+)\.(\d+)\.(\d+)(.*)$/);
if (!match) {
throw new Error(`JDBC plugin version '${version}' is not a patchable semver version.`);
}
return `${match[1]}.${match[2]}.${Number(match[3]) + 1}${match[4]}`;
}
function isReleaseBumpRelevantJdbcPluginChange(file) {
if (file.startsWith("plugins/jdbc/src/") || file.startsWith("plugins/jdbc/bin/")) return true;
if (!file.startsWith("plugins/jdbc/")) return false;
if (file.startsWith("plugins/jdbc/dist/") || file.startsWith("plugins/jdbc/target/")) return false;
if (file === "plugins/jdbc/README.md" || file === "plugins/jdbc/package.sh") return false;
if (file === POM_PATH || file === MANIFEST_PATH) return false;
return true;
}
function updatePomVersion(pomXml, version) {
return pomXml.replace(/(<project[\s\S]*?<version>)([^<]+)(<\/version>)/, `$1${version}$3`);
}
function updateManifestVersion(manifestJson, version) {
const manifest = JSON.parse(manifestJson);
manifest.version = version;
return `${JSON.stringify(manifest, null, 2)}\n`;
}
export function evaluateJdbcPluginReleaseBump({ changedFiles, pomXml, manifestJson }) {
const pomVersion = firstProjectVersion(pomXml);
const currentManifestVersion = manifestVersion(manifestJson);
if (pomVersion !== currentManifestVersion) {
throw new Error(`JDBC plugin version mismatch: pom.xml is ${pomVersion} but manifest.json is ${currentManifestVersion}.`);
}
const shouldBump = changedFiles.some(isReleaseBumpRelevantJdbcPluginChange);
const newVersion = shouldBump ? bumpPatchVersion(pomVersion) : pomVersion;
return {
changed: shouldBump,
oldVersion: pomVersion,
newVersion,
pomXml: shouldBump ? updatePomVersion(pomXml, newVersion) : pomXml,
manifestJson: shouldBump ? updateManifestVersion(manifestJson, newVersion) : manifestJson,
};
}
function git(args) {
return execFileSync("git", args, { encoding: "utf8" }).trim();
}
function main() {
const [baseRef = "HEAD~1", headRef = "HEAD", ...flags] = process.argv.slice(2);
const write = flags.includes("--write");
const changedFiles = git(["diff", "--name-only", baseRef, headRef]).split("\n").filter(Boolean);
const result = evaluateJdbcPluginReleaseBump({
changedFiles,
pomXml: readFileSync(POM_PATH, "utf8"),
manifestJson: readFileSync(MANIFEST_PATH, "utf8"),
});
if (write && result.changed) {
writeFileSync(POM_PATH, result.pomXml);
writeFileSync(MANIFEST_PATH, result.manifestJson);
}
console.log(`changed=${result.changed}`);
console.log(`old_version=${result.oldVersion}`);
console.log(`new_version=${result.newVersion}`);
}
if (import.meta.url === `file://${process.argv[1]}`) {
main();
}
+1 -25
View File
@@ -13,17 +13,7 @@ function manifestVersion(manifestJson) {
return JSON.parse(manifestJson).version ?? "";
}
function isRelevantJdbcPluginChange(file) {
if (!file.startsWith("plugins/jdbc/")) return false;
if (file.startsWith("plugins/jdbc/dist/") || file.startsWith("plugins/jdbc/target/")) return false;
if (file === "plugins/jdbc/README.md" || file === "plugins/jdbc/package.sh") return false;
return true;
}
export function evaluateJdbcPluginVersionChange({
changedFiles,
basePomVersion,
baseManifestVersion,
headPomVersion,
headManifestVersion,
}) {
@@ -32,14 +22,6 @@ export function evaluateJdbcPluginVersionChange({
errors.push(`JDBC plugin version mismatch: pom.xml is ${headPomVersion} but manifest.json is ${headManifestVersion}.`);
return errors;
}
const baseVersion = basePomVersion || baseManifestVersion;
const relevantChanged = changedFiles.some(isRelevantJdbcPluginChange);
if (relevantChanged && headPomVersion === baseVersion) {
errors.push(
`JDBC plugin files changed, but the plugin version is still ${headPomVersion}. Bump plugins/jdbc/pom.xml and plugins/jdbc/manifest.json.`,
);
}
return errors;
}
@@ -52,16 +34,10 @@ function readFileAt(ref, path) {
}
function main() {
const [baseRef = "HEAD~1", headRef = "HEAD"] = process.argv.slice(2);
const changedFiles = git(["diff", "--name-only", baseRef, headRef]).split("\n").filter(Boolean);
const basePomVersion = firstProjectVersion(readFileAt(baseRef, POM_PATH));
const baseManifestVersion = manifestVersion(readFileAt(baseRef, MANIFEST_PATH));
const [, headRef = "HEAD"] = process.argv.slice(2);
const headPomVersion = firstProjectVersion(readFileAt(headRef, POM_PATH));
const headManifestVersion = manifestVersion(readFileAt(headRef, MANIFEST_PATH));
const errors = evaluateJdbcPluginVersionChange({
changedFiles,
basePomVersion,
baseManifestVersion,
headPomVersion,
headManifestVersion,
});