refactor: extract shared release helper utilities (#3834)

Move four duplicated utility functions (getArgs, readJson,
validateVersion, isExpectedMissingGitHubRelease) from the three
get-release-version.js scripts into a shared module at
scripts/lib/release-helpers.js so that changes only need to happen
in one place.

Also fixes a pre-existing bug in getArgs where argument values
containing '=' were silently truncated (e.g. --msg=a=b produced
{msg:'a'} instead of {msg:'a=b'}).

Closes #3795

🤖 Generated with [Qwen Code](https://github.com/QwenLM/qwen-code)

Co-authored-by: jinye.djy <jinye.djy@alibaba-inc.com>
This commit is contained in:
jinye 2026-05-05 10:15:17 +08:00 committed by GitHub
parent 59845407fc
commit 2c93fd670c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 211 additions and 108 deletions

View file

@ -10,6 +10,11 @@ import { execSync } from 'node:child_process';
import { readFileSync } from 'node:fs';
import { dirname, join } from 'node:path';
import { fileURLToPath } from 'node:url';
import {
getArgs,
isExpectedMissingGitHubRelease,
validateVersion,
} from '../../../scripts/lib/release-helpers.js';
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
@ -27,18 +32,6 @@ function readPyprojectVersion() {
return match[1];
}
function getArgs() {
const args = {};
for (const arg of process.argv.slice(2)) {
if (!arg.startsWith('--')) {
continue;
}
const [key, value] = arg.slice(2).split('=');
args[key] = value === undefined ? true : value;
}
return args;
}
function parseVersion(version) {
let match = version.match(/^(\d+)\.(\d+)\.(\d+)$/);
if (match) {
@ -251,26 +244,6 @@ function getGitShortHash() {
return execSync('git rev-parse --short HEAD').toString().trim();
}
function validateVersion(version, format, name) {
const versionRegex = {
'X.Y.Z': /^\d+\.\d+\.\d+$/,
'X.Y.Z-preview.N': /^\d+\.\d+\.\d+-preview\.\d+$/,
};
if (!versionRegex[format]?.test(version)) {
throw new Error(
`Invalid ${name}: ${version}. Must be in ${format} format.`,
);
}
}
function isExpectedMissingGitHubRelease(error) {
const stderr = error.stderr?.toString() ?? '';
const stdout = error.stdout?.toString() ?? '';
const message = `${error.message}\n${stderr}\n${stdout}`;
return message.includes('release not found') || message.includes('Not Found');
}
async function getReleaseState({ packageVersion, releaseTag }, allVersions) {
const state = {
packageVersionExistsOnPyPI: allVersions.includes(packageVersion),