Release Promotion Clean up (#8597)

This commit is contained in:
matt korwel
2025-09-16 23:47:05 -07:00
committed by GitHub
parent 4df8dbaa0d
commit 1a6e4a119e
7 changed files with 322 additions and 180 deletions

View File

@@ -8,11 +8,6 @@
import { execSync } from 'node:child_process';
import { fileURLToPath } from 'node:url';
import { readFileSync } from 'node:fs';
import path from 'node:path';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
function getArgs() {
const args = {};
@@ -26,96 +21,147 @@ function getArgs() {
}
function getLatestTag(pattern) {
const command = `gh release list --limit 100 --json tagName | jq -r '[.[] | select(.tagName | ${pattern})] | .[0].tagName'`;
const command = `git tag --sort=-creatordate -l '${pattern}' | head -n 1`;
try {
return execSync(command).toString().trim();
} catch {
// Suppress error output for cleaner test failures
return '';
}
}
export function getVersion(options = {}) {
const args = getArgs();
const type = options.type || args.type || 'nightly';
function getVersionFromNPM(distTag) {
const command = `npm view @google/gemini-cli version --tag=${distTag}`;
try {
return execSync(command).toString().trim();
} catch {
return '';
}
}
let releaseVersion;
let npmTag;
let previousReleaseTag;
if (type === 'nightly') {
const packageJson = JSON.parse(
readFileSync(path.join(__dirname, '..', 'package.json'), 'utf-8'),
);
const versionParts = packageJson.version.split('.');
const major = versionParts[0];
const minor = versionParts[1] ? parseInt(versionParts[1]) : 0;
const nextMinor = minor + 1;
const date = new Date().toISOString().slice(0, 10).replace(/-/g, '');
const gitShortHash = execSync('git rev-parse --short HEAD')
.toString()
.trim();
releaseVersion = `${major}.${nextMinor}.0-nightly.${date}.${gitShortHash}`;
npmTag = 'nightly';
previousReleaseTag = getLatestTag('contains("nightly")');
} else if (type === 'stable') {
const latestPreviewTag = getLatestTag('contains("preview")');
releaseVersion = latestPreviewTag
.replace(/-preview.*/, '')
.replace(/^v/, '');
npmTag = 'latest';
previousReleaseTag = getLatestTag(
'(contains("nightly") or contains("preview")) | not',
);
} else if (type === 'preview') {
const latestNightlyTag = getLatestTag('contains("nightly")');
releaseVersion =
latestNightlyTag.replace(/-nightly.*/, '').replace(/^v/, '') + '-preview';
npmTag = 'preview';
previousReleaseTag = getLatestTag('contains("preview")');
} else if (type === 'patch') {
const patchFrom = options.patchFrom || args.patchFrom;
if (!patchFrom || (patchFrom !== 'stable' && patchFrom !== 'preview')) {
function verifyGitHubReleaseExists(tagName) {
const command = `gh release view "${tagName}" --json tagName --jq .tagName`;
try {
const output = execSync(command).toString().trim();
if (output !== tagName) {
throw new Error(
'Patch type must be specified with --patch-from=stable or --patch-from=preview',
`Discrepancy found! NPM version ${tagName} is missing a corresponding GitHub release.`,
);
}
} catch (error) {
throw new Error(
`Discrepancy found! Failed to verify GitHub release for ${tagName}. Error: ${error.message}`,
);
}
}
if (patchFrom === 'stable') {
previousReleaseTag = getLatestTag(
'(contains("nightly") or contains("preview")) | not',
);
const versionParts = previousReleaseTag.replace(/^v/, '').split('.');
const major = versionParts[0];
const minor = versionParts[1];
const patch = versionParts[2] ? parseInt(versionParts[2]) : 0;
releaseVersion = `${major}.${minor}.${patch + 1}`;
npmTag = 'latest';
} else {
// patchFrom === 'preview'
previousReleaseTag = getLatestTag('contains("preview")');
const [version, prerelease] = previousReleaseTag
.replace(/^v/, '')
.split('-');
const versionParts = version.split('.');
const major = versionParts[0];
const minor = versionParts[1];
const patch = versionParts[2] ? parseInt(versionParts[2]) : 0;
releaseVersion = `${major}.${minor}.${patch + 1}-${prerelease}`;
npmTag = 'preview';
}
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}).`,
);
}
verifyGitHubReleaseExists(latestTag);
return { latestVersion, latestTag };
}
function getNightlyVersion() {
const { latestVersion, latestTag } = getAndVerifyTags(
'nightly',
'v*-nightly*',
);
const baseVersion = latestVersion.split('-')[0];
const versionParts = baseVersion.split('.');
const major = versionParts[0];
const minor = versionParts[1] ? parseInt(versionParts[1]) : 0;
const nextMinor = minor + 1;
const date = new Date().toISOString().slice(0, 10).replace(/-/g, '');
const gitShortHash = execSync('git rev-parse --short HEAD').toString().trim();
return {
releaseVersion: `${major}.${nextMinor}.0-nightly.${date}.${gitShortHash}`,
npmTag: 'nightly',
previousReleaseTag: latestTag,
};
}
function getStableVersion() {
const { latestVersion, latestTag } = getAndVerifyTags(
'preview',
'v*-preview*',
);
return {
releaseVersion: latestVersion.replace(/-preview.*/, ''),
npmTag: 'latest',
previousReleaseTag: latestTag,
};
}
function getPreviewVersion() {
const { latestVersion, latestTag } = getAndVerifyTags(
'nightly',
'v*-nightly*',
);
return {
releaseVersion: latestVersion.replace(/-nightly.*/, '') + '-preview',
npmTag: 'preview',
previousReleaseTag: latestTag,
};
}
function getPatchVersion(patchFrom) {
if (!patchFrom || (patchFrom !== 'stable' && patchFrom !== 'preview')) {
throw new Error(
'Patch type must be specified with --patch-from=stable or --patch-from=preview',
);
}
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 [version, ...prereleaseParts] = latestVersion.split('-');
const prerelease = prereleaseParts.join('-');
const versionParts = version.split('.');
const major = versionParts[0];
const minor = versionParts[1];
const patch = versionParts[2] ? parseInt(versionParts[2]) : 0;
const releaseVersion = prerelease
? `${major}.${minor}.${patch + 1}-${prerelease}`
: `${major}.${minor}.${patch + 1}`;
return {
releaseVersion,
npmTag: distTag,
previousReleaseTag: latestTag,
};
}
export function getVersion(options = {}) {
const args = { ...getArgs(), ...options };
const type = args.type || 'nightly';
let versionData;
switch (type) {
case 'nightly':
versionData = getNightlyVersion();
break;
case 'stable':
versionData = getStableVersion();
break;
case 'preview':
versionData = getPreviewVersion();
break;
case 'patch':
versionData = getPatchVersion(args['patch-from']);
break;
default:
throw new Error(`Unknown release type: ${type}`);
}
const releaseTag = `v${releaseVersion}`;
return {
releaseTag,
releaseVersion,
npmTag,
previousReleaseTag,
releaseTag: `v${versionData.releaseVersion}`,
...versionData,
};
}
if (process.argv[1] === fileURLToPath(import.meta.url)) {
console.log(JSON.stringify(getVersion(), null, 2));
console.log(JSON.stringify(getVersion(getArgs()), null, 2));
}

View File

@@ -7,120 +7,117 @@
import { vi, describe, it, expect, beforeEach } from 'vitest';
import { getVersion } from '../get-release-version.js';
import { execSync } from 'node:child_process';
import { readFileSync } from 'node:fs';
vi.mock('node:child_process');
vi.mock('node:fs');
describe('getReleaseVersion', () => {
describe('getVersion', () => {
beforeEach(() => {
vi.resetAllMocks();
// Mock date to be consistent
vi.setSystemTime(new Date('2025-09-11T00:00:00.000Z'));
vi.setSystemTime(new Date('2025-09-17T00:00:00.000Z'));
});
describe('Nightly Workflow Logic', () => {
it('should calculate the next nightly version based on package.json', () => {
vi.mocked(readFileSync).mockReturnValue('{"version": "0.5.0"}');
vi.mocked(execSync).mockImplementation((command) => {
if (command.includes('rev-parse')) return 'a1b2c3d';
if (command.includes('release list'))
return 'v0.5.0-nightly.20250910.abcdef';
return '';
});
const mockExecSync = (command) => {
// NPM Mocks
if (command.includes('npm view') && command.includes('--tag=latest'))
return '0.4.1';
if (command.includes('npm view') && command.includes('--tag=preview'))
return '0.5.0-preview-2';
if (command.includes('npm view') && command.includes('--tag=nightly'))
return '0.6.0-nightly.20250910.a31830a3';
const result = getVersion({ type: 'nightly' });
// Git Tag Mocks
if (command.includes("git tag --sort=-creatordate -l 'v[0-9].[0-9].[0-9]'"))
return 'v0.4.1';
if (command.includes("git tag --sort=-creatordate -l 'v*-preview*'"))
return 'v0.5.0-preview-2';
if (command.includes("git tag --sort=-creatordate -l 'v*-nightly*'"))
return 'v0.6.0-nightly.20250910.a31830a3';
expect(result.releaseVersion).toBe('0.6.0-nightly.20250911.a1b2c3d');
expect(result.npmTag).toBe('nightly');
expect(result.previousReleaseTag).toBe('v0.5.0-nightly.20250910.abcdef');
});
// GitHub Release Mocks
if (command.includes('gh release view "v0.4.1"')) return 'v0.4.1';
if (command.includes('gh release view "v0.5.0-preview-2"'))
return 'v0.5.0-preview-2';
if (command.includes('gh release view "v0.6.0-nightly.20250910.a31830a3"'))
return 'v0.6.0-nightly.20250910.a31830a3';
it('should default minor to 0 if missing in package.json version', () => {
vi.mocked(readFileSync).mockReturnValue('{"version": "0"}');
vi.mocked(execSync).mockImplementation((command) => {
if (command.includes('rev-parse')) return 'a1b2c3d';
if (command.includes('release list'))
return 'v0.0.0-nightly.20250910.abcdef';
return '';
});
// Git Hash Mock
if (command.includes('git rev-parse --short HEAD')) return 'd3bf8a3d';
const result = getVersion({ type: 'nightly' });
expect(result.releaseVersion).toBe('0.1.0-nightly.20250911.a1b2c3d');
expect(result.npmTag).toBe('nightly');
expect(result.previousReleaseTag).toBe('v0.0.0-nightly.20250910.abcdef');
});
});
describe('Promote Workflow Logic', () => {
it('should calculate stable version from the latest preview tag', () => {
const latestPreview = 'v0.5.0-preview';
const latestStable = 'v0.4.0';
vi.mocked(execSync).mockImplementation((command) => {
if (command.includes('not')) return latestStable;
if (command.includes('contains("preview")')) return latestPreview;
return '';
});
return '';
};
describe('Happy Path - Version Calculation', () => {
it('should calculate the next stable version from the latest preview', () => {
vi.mocked(execSync).mockImplementation(mockExecSync);
const result = getVersion({ type: 'stable' });
expect(result.releaseVersion).toBe('0.5.0');
expect(result.npmTag).toBe('latest');
expect(result.previousReleaseTag).toBe(latestStable);
expect(result.previousReleaseTag).toBe('v0.5.0-preview-2');
});
it('should calculate preview version from the latest nightly tag', () => {
const latestNightly = 'v0.6.0-nightly.20250910.abcdef';
const latestPreview = 'v0.5.0-preview';
vi.mocked(execSync).mockImplementation((command) => {
if (command.includes('nightly')) return latestNightly;
if (command.includes('preview')) return latestPreview;
return '';
});
it('should calculate the next preview version from the latest nightly', () => {
vi.mocked(execSync).mockImplementation(mockExecSync);
const result = getVersion({ type: 'preview' });
expect(result.releaseVersion).toBe('0.6.0-preview');
expect(result.npmTag).toBe('preview');
expect(result.previousReleaseTag).toBe(latestPreview);
expect(result.previousReleaseTag).toBe(
'v0.6.0-nightly.20250910.a31830a3',
);
});
it('should calculate the next nightly version from the latest nightly', () => {
vi.mocked(execSync).mockImplementation(mockExecSync);
const result = getVersion({ type: 'nightly' });
expect(result.releaseVersion).toBe('0.7.0-nightly.20250917.d3bf8a3d');
expect(result.npmTag).toBe('nightly');
expect(result.previousReleaseTag).toBe(
'v0.6.0-nightly.20250910.a31830a3',
);
});
});
describe('Patch Workflow Logic', () => {
it('should calculate the next patch version for a stable release', () => {
const latestStable = 'v0.5.1';
vi.mocked(execSync).mockReturnValue(latestStable);
const result = getVersion({ type: 'patch', patchFrom: 'stable' });
expect(result.releaseVersion).toBe('0.5.2');
vi.mocked(execSync).mockImplementation(mockExecSync);
const result = getVersion({ type: 'patch', 'patch-from': 'stable' });
expect(result.releaseVersion).toBe('0.4.2');
expect(result.npmTag).toBe('latest');
expect(result.previousReleaseTag).toBe(latestStable);
expect(result.previousReleaseTag).toBe('v0.4.1');
});
it('should calculate the next patch version for a preview release', () => {
const latestPreview = 'v0.6.0-preview';
vi.mocked(execSync).mockReturnValue(latestPreview);
const result = getVersion({ type: 'patch', patchFrom: 'preview' });
expect(result.releaseVersion).toBe('0.6.1-preview');
vi.mocked(execSync).mockImplementation(mockExecSync);
const result = getVersion({ type: 'patch', 'patch-from': 'preview' });
expect(result.releaseVersion).toBe('0.5.1-preview-2');
expect(result.npmTag).toBe('preview');
expect(result.previousReleaseTag).toBe(latestPreview);
expect(result.previousReleaseTag).toBe('v0.5.0-preview-2');
});
});
describe('Failure Path - Discrepancy Checks', () => {
it('should throw an error if the git tag does not match npm', () => {
const mockWithMismatchGitTag = (command) => {
if (command.includes("git tag --sort=-creatordate -l 'v*-preview*'"))
return 'v0.4.0-preview-99'; // Mismatch
return mockExecSync(command);
};
vi.mocked(execSync).mockImplementation(mockWithMismatchGitTag);
expect(() => getVersion({ type: 'stable' })).toThrow(
'Discrepancy found! NPM preview tag (0.5.0-preview-2) does not match latest git preview tag (v0.4.0-preview-99).',
);
});
it('should default patch to 0 if missing in stable release', () => {
const latestStable = 'v0.5';
vi.mocked(execSync).mockReturnValue(latestStable);
it('should throw an error if the GitHub release is missing', () => {
const mockWithMissingRelease = (command) => {
if (command.includes('gh release view "v0.5.0-preview-2"')) {
throw new Error('gh command failed'); // Simulate gh failure
}
return mockExecSync(command);
};
vi.mocked(execSync).mockImplementation(mockWithMissingRelease);
const result = getVersion({ type: 'patch', patchFrom: 'stable' });
expect(result.releaseVersion).toBe('0.5.1');
expect(result.npmTag).toBe('latest');
expect(result.previousReleaseTag).toBe(latestStable);
expect(() => getVersion({ type: 'stable' })).toThrow(
'Discrepancy found! Failed to verify GitHub release for v0.5.0-preview-2.',
);
});
});
});