Releasing: Version mgmt (#8964)

Co-authored-by: gemini-cli-robot <gemini-cli-robot@google.com>
This commit is contained in:
matt korwel
2025-09-24 21:02:00 -07:00
committed by GitHub
parent 422eb78b41
commit 9ba1640c07
5 changed files with 478 additions and 38 deletions

View File

@@ -8,6 +8,7 @@
import { execSync } from 'node:child_process';
import { fileURLToPath } from 'node:url';
import semver from 'semver';
function getArgs() {
const args = {};
@@ -21,10 +22,29 @@ function getArgs() {
}
function getLatestTag(pattern) {
const command = `git tag --sort=-creatordate -l '${pattern}' | head -n 1`;
const command = `git tag -l '${pattern}'`;
try {
return execSync(command).toString().trim();
} catch {
const tags = execSync(command)
.toString()
.trim()
.split('\n')
.filter(Boolean);
if (tags.length === 0) return '';
// Convert tags to versions (remove 'v' prefix) and sort by semver
const versions = tags
.map((tag) => tag.replace(/^v/, ''))
.filter((version) => semver.valid(version))
.sort((a, b) => semver.rcompare(a, b)); // rcompare for descending order
if (versions.length === 0) return '';
// Return the latest version with 'v' prefix restored
return `v${versions[0]}`;
} catch (error) {
console.error(
`Failed to get latest git tag for pattern "${pattern}": ${error.message}`,
);
return '';
}
}
@@ -33,11 +53,75 @@ function getVersionFromNPM(distTag) {
const command = `npm view @google/gemini-cli version --tag=${distTag}`;
try {
return execSync(command).toString().trim();
} catch {
} catch (error) {
console.error(
`Failed to get NPM version for dist-tag "${distTag}": ${error.message}`,
);
return '';
}
}
function getAllVersionsFromNPM() {
const command = `npm view @google/gemini-cli versions --json`;
try {
const versionsJson = execSync(command).toString().trim();
return JSON.parse(versionsJson);
} catch (error) {
console.error(`Failed to get all NPM versions: ${error.message}`);
return [];
}
}
function detectRollbackAndGetBaseline(npmDistTag) {
// Get the current dist-tag version
const distTagVersion = getVersionFromNPM(npmDistTag);
if (!distTagVersion) return { baseline: '', isRollback: false };
// Get all published versions
const allVersions = getAllVersionsFromNPM();
if (allVersions.length === 0)
return { baseline: distTagVersion, isRollback: false };
// Filter versions by type to match the dist-tag
let matchingVersions;
if (npmDistTag === 'latest') {
// Stable versions: no prerelease identifiers
matchingVersions = allVersions.filter(
(v) => semver.valid(v) && !semver.prerelease(v),
);
} else if (npmDistTag === 'preview') {
// Preview versions: contain -preview
matchingVersions = allVersions.filter(
(v) => semver.valid(v) && v.includes('-preview'),
);
} else if (npmDistTag === 'nightly') {
// Nightly versions: contain -nightly
matchingVersions = allVersions.filter(
(v) => semver.valid(v) && v.includes('-nightly'),
);
} else {
// For other dist-tags, just use the dist-tag version
return { baseline: distTagVersion, isRollback: false };
}
if (matchingVersions.length === 0)
return { baseline: distTagVersion, isRollback: false };
// Sort by semver and get the highest existing version
matchingVersions.sort((a, b) => semver.rcompare(a, b));
const highestExistingVersion = matchingVersions[0];
// Check if we're in a rollback scenario
const isRollback = semver.gt(highestExistingVersion, distTagVersion);
return {
baseline: isRollback ? highestExistingVersion : distTagVersion,
isRollback,
distTagVersion,
highestExistingVersion,
};
}
function verifyGitHubReleaseExists(tagName) {
const command = `gh release view "${tagName}" --json tagName --jq .tagName`;
try {
@@ -54,20 +138,122 @@ function verifyGitHubReleaseExists(tagName) {
}
}
function getAndVerifyTags(npmDistTag, gitTagPattern) {
const latestVersion = getVersionFromNPM(npmDistTag);
const latestTag = getLatestTag(gitTagPattern);
if (`v${latestVersion}` !== latestTag) {
throw new Error(
`Discrepancy found! NPM ${npmDistTag} tag (${latestVersion}) does not match latest git ${npmDistTag} tag (${latestTag}).`,
function validateVersionConflicts(newVersion) {
// Check if the calculated version already exists in any of the 3 sources
const conflicts = [];
// Check NPM - get all published versions
try {
const command = `npm view @google/gemini-cli versions --json`;
const versionsJson = execSync(command).toString().trim();
const allVersions = JSON.parse(versionsJson);
if (allVersions.includes(newVersion)) {
conflicts.push(`NPM registry already has version ${newVersion}`);
}
} catch (error) {
console.warn(
`Failed to check NPM versions for conflicts: ${error.message}`,
);
}
verifyGitHubReleaseExists(latestTag);
return { latestVersion, latestTag };
// Check Git tags
try {
const command = `git tag -l 'v${newVersion}'`;
const tagOutput = execSync(command).toString().trim();
if (tagOutput === `v${newVersion}`) {
conflicts.push(`Git tag v${newVersion} already exists`);
}
} catch (error) {
console.warn(`Failed to check git tags for conflicts: ${error.message}`);
}
// Check GitHub releases
try {
const command = `gh release view "v${newVersion}" --json tagName --jq .tagName`;
const output = execSync(command).toString().trim();
if (output === `v${newVersion}`) {
conflicts.push(`GitHub release v${newVersion} already exists`);
}
} catch (error) {
// This is expected if the release doesn't exist - only warn on unexpected errors
const isExpectedNotFound =
error.message.includes('release not found') ||
error.message.includes('Not Found') ||
error.message.includes('not found') ||
error.status === 1; // gh command exit code for not found
if (!isExpectedNotFound) {
console.warn(
`Failed to check GitHub releases for conflicts: ${error.message}`,
);
}
}
if (conflicts.length > 0) {
throw new Error(
`Version conflict! Cannot create ${newVersion}:\n${conflicts.join('\n')}`,
);
}
}
function getAndVerifyTags(npmDistTag, gitTagPattern) {
// Detect rollback scenarios and get the correct baseline
const rollbackInfo = detectRollbackAndGetBaseline(npmDistTag);
const baselineVersion = rollbackInfo.baseline;
if (!baselineVersion) {
throw new Error(`Unable to determine baseline version for ${npmDistTag}`);
}
const latestTag = getLatestTag(gitTagPattern);
// In rollback scenarios, we don't require git tags to match the dist-tag
// Instead, we verify the baseline version exists as a git tag
if (!rollbackInfo.isRollback) {
// Normal scenario: NPM dist-tag should match latest git tag
if (`v${baselineVersion}` !== latestTag) {
throw new Error(
`Discrepancy found! NPM ${npmDistTag} tag (${baselineVersion}) does not match latest git ${npmDistTag} tag (${latestTag}).`,
);
}
} else {
// Rollback scenario: warn about the rollback but don't fail
console.warn(
`Rollback detected! NPM ${npmDistTag} tag is ${rollbackInfo.distTagVersion}, but using ${baselineVersion} as baseline for next version calculation (highest existing version).`,
);
// Verify the baseline version has corresponding git tag
try {
const baselineTagExists = execSync(`git tag -l 'v${baselineVersion}'`)
.toString()
.trim();
if (baselineTagExists !== `v${baselineVersion}`) {
throw new Error(
`Rollback scenario detected, but git tag v${baselineVersion} does not exist! This is required to calculate the next version.`,
);
}
} catch (error) {
// If the git command itself failed, log the original error
console.error(
`Failed to check for git tag v${baselineVersion}: ${error.message}`,
);
throw new Error(
`Rollback scenario detected, but git tag v${baselineVersion} does not exist! This is required to calculate the next version.`,
);
}
}
// Always verify GitHub release exists for the baseline version (not necessarily the dist-tag)
verifyGitHubReleaseExists(`v${baselineVersion}`);
return {
latestVersion: baselineVersion,
latestTag: `v${baselineVersion}`,
rollbackInfo,
};
}
function getNightlyVersion() {
const { latestVersion, latestTag } = getAndVerifyTags(
const { latestVersion, latestTag, rollbackInfo } = getAndVerifyTags(
'nightly',
'v*-nightly*',
);
@@ -82,6 +268,7 @@ function getNightlyVersion() {
releaseVersion: `${major}.${nextMinor}.0-nightly.${date}.${gitShortHash}`,
npmTag: 'nightly',
previousReleaseTag: latestTag,
rollbackInfo,
};
}
@@ -112,7 +299,7 @@ function getStableVersion(args) {
releaseVersion = latestPreviewVersion.replace(/-preview.*/, '');
}
const { latestTag: previousStableTag } = getAndVerifyTags(
const { latestTag: previousStableTag, rollbackInfo } = getAndVerifyTags(
'latest',
'v[0-9].[0-9].[0-9]',
);
@@ -121,6 +308,7 @@ function getStableVersion(args) {
releaseVersion,
npmTag: 'latest',
previousReleaseTag: previousStableTag,
rollbackInfo,
};
}
@@ -143,7 +331,7 @@ function getPreviewVersion(args) {
latestNightlyVersion.replace(/-nightly.*/, '') + '-preview.0';
}
const { latestTag: previousPreviewTag } = getAndVerifyTags(
const { latestTag: previousPreviewTag, rollbackInfo } = getAndVerifyTags(
'preview',
'v*-preview*',
);
@@ -152,6 +340,7 @@ function getPreviewVersion(args) {
releaseVersion,
npmTag: 'preview',
previousReleaseTag: previousPreviewTag,
rollbackInfo,
};
}
@@ -163,7 +352,10 @@ function getPatchVersion(patchFrom) {
}
const distTag = patchFrom === 'stable' ? 'latest' : 'preview';
const pattern = distTag === 'latest' ? 'v[0-9].[0-9].[0-9]' : 'v*-preview*';
const { latestVersion, latestTag } = getAndVerifyTags(distTag, pattern);
const { latestVersion, latestTag, rollbackInfo } = getAndVerifyTags(
distTag,
pattern,
);
if (patchFrom === 'stable') {
// For stable versions, increment the patch number: 0.5.4 -> 0.5.5
@@ -176,6 +368,7 @@ function getPatchVersion(patchFrom) {
releaseVersion,
npmTag: distTag,
previousReleaseTag: latestTag,
rollbackInfo,
};
} else {
// For preview versions, increment the preview number: 0.6.0-preview.2 -> 0.6.0-preview.3
@@ -196,6 +389,7 @@ function getPatchVersion(patchFrom) {
releaseVersion,
npmTag: distTag,
previousReleaseTag: latestTag,
rollbackInfo,
};
}
}
@@ -222,10 +416,27 @@ export function getVersion(options = {}) {
throw new Error(`Unknown release type: ${type}`);
}
return {
// Validate that the calculated version doesn't conflict with existing versions
validateVersionConflicts(versionData.releaseVersion);
// Include rollback information in the output if available
const result = {
releaseTag: `v${versionData.releaseVersion}`,
...versionData,
};
// Add rollback information to output if it exists
if (versionData.rollbackInfo && versionData.rollbackInfo.isRollback) {
result.rollbackDetected = {
rollbackScenario: true,
distTagVersion: versionData.rollbackInfo.distTagVersion,
highestExistingVersion: versionData.rollbackInfo.highestExistingVersion,
baselineUsed: versionData.rollbackInfo.baseline,
message: `Rollback detected: NPM tag was ${versionData.rollbackInfo.distTagVersion}, but using ${versionData.rollbackInfo.baseline} as baseline for next version calculation (highest existing version)`,
};
}
return result;
}
if (process.argv[1] === fileURLToPath(import.meta.url)) {