feat: Use PAT for gemini-cli-robot in release workflows (#9804)

Co-authored-by: Gal Zahavi <38544478+galz10@users.noreply.github.com>
This commit is contained in:
matt korwel
2025-09-26 16:35:26 -07:00
committed by GitHub
parent 24c15b9d43
commit 38dccf32c1
8 changed files with 295 additions and 469 deletions

View File

@@ -7,53 +7,65 @@
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('getVersion', () => {
beforeEach(() => {
vi.resetAllMocks();
vi.setSystemTime(new Date('2025-09-17T00:00:00.000Z'));
// Mock package.json being read by getNightlyVersion
vi.mocked(readFileSync).mockReturnValue(
JSON.stringify({ version: '0.8.0' }),
);
});
// This is the base mock for a clean state with no conflicts or rollbacks
const mockExecSync = (command) => {
// NPM dist-tags - source of truth
// NPM dist-tags
if (command.includes('npm view') && command.includes('--tag=latest'))
return '0.4.1';
return '0.6.1';
if (command.includes('npm view') && command.includes('--tag=preview'))
return '0.5.0-preview.2';
return '0.7.0-preview.1';
if (command.includes('npm view') && command.includes('--tag=nightly'))
return '0.6.0-nightly.20250910.a31830a3';
return '0.8.0-nightly.20250916.abcdef';
// NPM versions list - for conflict validation
// NPM versions list
if (command.includes('npm view') && command.includes('versions --json'))
return JSON.stringify([
'0.4.1',
'0.5.0-preview.2',
'0.6.0-nightly.20250910.a31830a3',
'0.6.0',
'0.6.1',
'0.7.0-preview.0',
'0.7.0-preview.1',
'0.8.0-nightly.20250916.abcdef',
]);
// Git Tag Mocks - with semantic sorting
if (command.includes("git tag -l 'v[0-9].[0-9].[0-9]'")) return 'v0.4.1';
if (command.includes("git tag -l 'v*-preview*'")) return 'v0.5.0-preview.2';
// Deprecation checks (default to not deprecated)
if (command.includes('deprecated')) return '';
// Git Tag Mocks
if (command.includes("git tag -l 'v[0-9].[0-9].[0-9]'")) return 'v0.6.1';
if (command.includes("git tag -l 'v*-preview*'")) return 'v0.7.0-preview.1';
if (command.includes("git tag -l 'v*-nightly*'"))
return 'v0.6.0-nightly.20250910.a31830a3';
// Conflict validation - Git tag checks
if (command.includes("git tag -l 'v0.5.0'")) return ''; // Version doesn't exist yet
if (command.includes("git tag -l 'v0.4.2'")) return ''; // Version doesn't exist yet
if (command.includes("git tag -l 'v0.6.0-preview.0'")) return ''; // Version doesn't exist yet
// 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';
return 'v0.8.0-nightly.20250916.abcdef';
// Git Hash Mock
if (command.includes('git rev-parse --short HEAD')) return 'd3bf8a3d';
// For doesVersionExist checks - default to not found
if (
command.includes('npm view') &&
command.includes('@google/gemini-cli@')
) {
throw new Error('NPM version not found');
}
if (command.includes('git tag -l')) return '';
if (command.includes('gh release view')) {
throw new Error('GH release not found');
}
return '';
};
@@ -61,345 +73,114 @@ describe('getVersion', () => {
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.releaseVersion).toBe('0.7.0');
expect(result.npmTag).toBe('latest');
expect(result.previousReleaseTag).toBe('v0.4.1');
});
it('should use the override version for stable if provided', () => {
vi.mocked(execSync).mockImplementation(mockExecSync);
const result = getVersion({
type: 'stable',
stable_version_override: '1.2.3',
});
expect(result.releaseVersion).toBe('1.2.3');
expect(result.npmTag).toBe('latest');
expect(result.previousReleaseTag).toBe('v0.4.1');
expect(result.previousReleaseTag).toBe('v0.6.1');
});
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.0');
expect(result.releaseVersion).toBe('0.8.0-preview.0');
expect(result.npmTag).toBe('preview');
expect(result.previousReleaseTag).toBe('v0.5.0-preview.2');
expect(result.previousReleaseTag).toBe('v0.7.0-preview.1');
});
it('should use the override version for preview if provided', () => {
vi.mocked(execSync).mockImplementation(mockExecSync);
const result = getVersion({
type: 'preview',
preview_version_override: '4.5.6-preview.0',
});
expect(result.releaseVersion).toBe('4.5.6-preview.0');
expect(result.npmTag).toBe('preview');
expect(result.previousReleaseTag).toBe('v0.5.0-preview.2');
});
it('should calculate the next nightly version from the latest nightly', () => {
it('should calculate the next nightly version from package.json', () => {
vi.mocked(execSync).mockImplementation(mockExecSync);
const result = getVersion({ type: 'nightly' });
expect(result.releaseVersion).toBe('0.7.0-nightly.20250917.d3bf8a3d');
// Note: The base version now comes from package.json, not the previous nightly tag.
expect(result.releaseVersion).toBe('0.8.0-nightly.20250917.d3bf8a3d');
expect(result.npmTag).toBe('nightly');
expect(result.previousReleaseTag).toBe(
'v0.6.0-nightly.20250910.a31830a3',
);
expect(result.previousReleaseTag).toBe('v0.8.0-nightly.20250916.abcdef');
});
it('should calculate the next patch version for a stable release', () => {
vi.mocked(execSync).mockImplementation(mockExecSync);
const result = getVersion({ type: 'patch', 'patch-from': 'stable' });
expect(result.releaseVersion).toBe('0.4.2');
expect(result.releaseVersion).toBe('0.6.2');
expect(result.npmTag).toBe('latest');
expect(result.previousReleaseTag).toBe('v0.4.1');
expect(result.previousReleaseTag).toBe('v0.6.1');
});
it('should calculate the next patch version for a preview release', () => {
vi.mocked(execSync).mockImplementation(mockExecSync);
const result = getVersion({ type: 'patch', 'patch-from': 'preview' });
expect(result.releaseVersion).toBe('0.5.0-preview.3');
expect(result.releaseVersion).toBe('0.7.0-preview.2');
expect(result.npmTag).toBe('preview');
expect(result.previousReleaseTag).toBe('v0.5.0-preview.2');
expect(result.previousReleaseTag).toBe('v0.7.0-preview.1');
});
});
describe('Failure Path - Invalid Overrides', () => {
it('should throw an error for an invalid stable_version_override', () => {
vi.mocked(execSync).mockImplementation(mockExecSync);
expect(() =>
getVersion({
type: 'stable',
stable_version_override: '1.2.3-beta',
}),
).toThrow(
'Invalid stable_version_override: 1.2.3-beta. Must be in X.Y.Z format.',
);
});
it('should throw an error for an invalid preview_version_override format', () => {
vi.mocked(execSync).mockImplementation(mockExecSync);
expect(() =>
getVersion({
type: 'preview',
preview_version_override: '4.5.6-preview', // Missing .N
}),
).toThrow(
'Invalid preview_version_override: 4.5.6-preview. Must be in X.Y.Z-preview.N format.',
);
});
it('should throw an error for another invalid preview_version_override format', () => {
vi.mocked(execSync).mockImplementation(mockExecSync);
expect(() =>
getVersion({
type: 'preview',
preview_version_override: '4.5.6',
}),
).toThrow(
'Invalid preview_version_override: 4.5.6. Must be in X.Y.Z-preview.N format.',
);
});
});
describe('Semver Sorting Edge Cases', () => {
it('should handle Git tag creation date vs semantic version sorting', () => {
const mockWithSemverGitSorting = (command) => {
// NPM dist-tags are correct (source of truth)
if (command.includes('npm view') && command.includes('--tag=latest'))
return '0.5.0'; // NPM correctly has 0.5.0 as latest
if (command.includes('npm view') && command.includes('--tag=preview'))
return '0.6.0-preview.2';
if (command.includes('npm view') && command.includes('--tag=nightly'))
return '0.7.0-nightly.20250910.a31830a3';
// NPM versions list for conflict validation
describe('Advanced Scenarios', () => {
it('should ignore a deprecated version and use the next highest', () => {
const mockWithDeprecated = (command) => {
// The highest nightly is 0.9.0, but it's deprecated
if (command.includes('npm view') && command.includes('versions --json'))
return JSON.stringify([
'0.0.77', // This was the problematic dev version
'0.4.1',
'0.5.0',
'0.6.0-preview.1',
'0.6.0-preview.2',
'0.7.0-nightly.20250910.a31830a3',
'0.8.0-nightly.20250916.abcdef',
'0.9.0-nightly.20250917.deprecated', // This one is deprecated
]);
// Git tags - test that semantic sorting works correctly
if (command.includes("git tag -l 'v[0-9].[0-9].[0-9]'"))
return 'v0.0.77\nv0.5.0\nv0.4.1'; // Multiple tags - should pick v0.5.0 semantically
if (command.includes("git tag -l 'v*-preview*'"))
return 'v0.6.0-preview.2';
if (command.includes("git tag -l 'v*-nightly*'"))
return 'v0.7.0-nightly.20250910.a31830a3';
// Conflict validation - new versions don't exist yet
if (command.includes("git tag -l 'v0.5.1'")) return '';
if (command.includes("git tag -l 'v0.6.0'")) return '';
// GitHub releases
if (command.includes('gh release view "v0.5.0"')) return 'v0.5.0';
if (command.includes('gh release view "v0.6.0-preview.2"'))
return 'v0.6.0-preview.2';
// Mock the deprecation check
if (
command.includes('gh release view "v0.7.0-nightly.20250910.a31830a3"')
command.includes(
'npm view @google/gemini-cli@0.9.0-nightly.20250917.deprecated deprecated',
)
)
return 'v0.7.0-nightly.20250910.a31830a3';
// GitHub conflict validation - new versions don't exist
if (command.includes('gh release view "v0.5.1"'))
throw new Error('Not found');
if (command.includes('gh release view "v0.6.0"'))
throw new Error('Not found');
// Git Hash Mock
if (command.includes('git rev-parse --short HEAD')) return 'd3bf8a3d';
return mockExecSync(command);
};
vi.mocked(execSync).mockImplementation(mockWithSemverGitSorting);
// Test patch calculation - should be 0.5.1 from NPM's latest=0.5.0
const patchResult = getVersion({ type: 'patch', 'patch-from': 'stable' });
expect(patchResult.releaseVersion).toBe('0.5.1');
expect(patchResult.previousReleaseTag).toBe('v0.5.0');
// Verify no rollback information is included in normal scenarios
expect(patchResult.rollbackDetected).toBeUndefined();
// Test stable calculation - should be 0.6.0 from preview
const stableResult = getVersion({ type: 'stable' });
expect(stableResult.releaseVersion).toBe('0.6.0');
expect(stableResult.previousReleaseTag).toBe('v0.5.0');
// Verify no rollback information for stable calculation either
expect(stableResult.rollbackDetected).toBeUndefined();
});
it('should fail when git tags are not semver-sorted correctly', () => {
const mockWithIncorrectGitSorting = (command) => {
// NPM correctly returns 0.5.0 as latest
if (command.includes('npm view') && command.includes('--tag=latest'))
return '0.5.0';
// But git tag sorting returns wrong semantic version
if (command.includes("git tag -l 'v[0-9].[0-9].[0-9]'"))
return 'v0.4.1'; // This should cause a discrepancy error (NPM says 0.5.0)
return mockExecSync(command);
};
vi.mocked(execSync).mockImplementation(mockWithIncorrectGitSorting);
// This should throw because NPM says 0.5.0 but git tag sorting says v0.4.1
expect(() =>
getVersion({ type: 'patch', 'patch-from': 'stable' }),
).toThrow(
'Discrepancy found! NPM latest tag (0.5.0) does not match latest git latest tag (v0.4.1).',
);
});
it('should handle rollback scenarios by using highest existing version', () => {
const mockWithRollback = (command) => {
// NPM dist-tag was rolled back from 0.5.0 to 0.4.1 due to issues
if (command.includes('npm view') && command.includes('--tag=latest'))
return '0.4.1'; // Rolled back version
if (command.includes('npm view') && command.includes('--tag=preview'))
return '0.6.0-preview.2';
return 'This version is deprecated';
// The dist-tag still points to the older, valid version
if (command.includes('npm view') && command.includes('--tag=nightly'))
return '0.7.0-nightly.20250910.a31830a3';
return '0.8.0-nightly.20250916.abcdef';
// NPM versions list shows 0.5.0 was published (but rolled back)
if (command.includes('npm view') && command.includes('versions --json'))
return JSON.stringify([
'0.3.0',
'0.4.1', // Current dist-tag
'0.5.0', // Published but rolled back
'0.6.0-preview.1',
'0.6.0-preview.2',
'0.7.0-nightly.20250910.a31830a3',
]);
return mockExecSync(command);
};
vi.mocked(execSync).mockImplementation(mockWithDeprecated);
// Git tags show both versions exist
if (command.includes("git tag -l 'v[0-9].[0-9].[0-9]'"))
return 'v0.4.1\nv0.5.0'; // Both tags exist
if (command.includes("git tag -l 'v*-preview*'"))
return 'v0.6.0-preview.2';
if (command.includes("git tag -l 'v*-nightly*'"))
return 'v0.7.0-nightly.20250910.a31830a3';
const result = getVersion({ type: 'preview' });
// It should base the preview off 0.8.0, not the deprecated 0.9.0
expect(result.releaseVersion).toBe('0.8.0-preview.0');
});
// Specific git tag checks for rollback validation
if (command.includes("git tag -l 'v0.5.0'")) return 'v0.5.0';
it('should auto-increment patch version if the calculated one already exists', () => {
const mockWithConflict = (command) => {
// The calculated version 0.7.0 already exists as a git tag
if (command.includes("git tag -l 'v0.7.0'")) return 'v0.7.0';
// The next version, 0.7.1, is available
if (command.includes("git tag -l 'v0.7.1'")) return '';
// Conflict validation - new versions don't exist yet
if (command.includes("git tag -l 'v0.5.1'")) return '';
if (command.includes("git tag -l 'v0.6.0'")) return '';
return mockExecSync(command);
};
vi.mocked(execSync).mockImplementation(mockWithConflict);
// GitHub releases exist for both versions
if (command.includes('gh release view "v0.4.1"')) return 'v0.4.1';
if (command.includes('gh release view "v0.5.0"')) return 'v0.5.0'; // Exists but rolled back
if (command.includes('gh release view "v0.6.0-preview.2"'))
return 'v0.6.0-preview.2';
const result = getVersion({ type: 'stable' });
// Should have skipped 0.7.0 and landed on 0.7.1
expect(result.releaseVersion).toBe('0.7.1');
});
// GitHub conflict validation - new versions don't exist
if (command.includes('gh release view "v0.5.1"'))
throw new Error('Not found');
if (command.includes('gh release view "v0.6.0"'))
it('should auto-increment preview number if the calculated one already exists', () => {
const mockWithConflict = (command) => {
// The calculated preview 0.8.0-preview.0 already exists on NPM
if (
command.includes(
'npm view @google/gemini-cli@0.8.0-preview.0 version',
)
)
return '0.8.0-preview.0';
// The next one is available
if (
command.includes(
'npm view @google/gemini-cli@0.8.0-preview.1 version',
)
)
throw new Error('Not found');
// Git Hash Mock
if (command.includes('git rev-parse --short HEAD')) return 'd3bf8a3d';
return mockExecSync(command);
};
vi.mocked(execSync).mockImplementation(mockWithConflict);
vi.mocked(execSync).mockImplementation(mockWithRollback);
// Mock console.warn to capture rollback warning
const consoleSpy = vi.spyOn(console, 'warn').mockImplementation(() => {});
// Test patch calculation - should be 0.5.1 (from rolled back 0.5.0, not current dist-tag 0.4.1)
const patchResult = getVersion({ type: 'patch', 'patch-from': 'stable' });
expect(patchResult.releaseVersion).toBe('0.5.1'); // Fix for 0.5.0, not 0.4.2
expect(patchResult.previousReleaseTag).toBe('v0.5.0'); // Uses highest existing, not dist-tag
// Verify rollback information is included in output
expect(patchResult.rollbackDetected).toBeDefined();
expect(patchResult.rollbackDetected.rollbackScenario).toBe(true);
expect(patchResult.rollbackDetected.distTagVersion).toBe('0.4.1');
expect(patchResult.rollbackDetected.highestExistingVersion).toBe('0.5.0');
expect(patchResult.rollbackDetected.baselineUsed).toBe('0.5.0');
expect(patchResult.rollbackDetected.message).toContain(
'Rollback detected: NPM tag was 0.4.1, but using 0.5.0 as baseline for next version calculation',
);
// Verify rollback was detected and warning was shown
expect(consoleSpy).toHaveBeenCalledWith(
expect.stringContaining(
'Rollback detected! NPM latest tag is 0.4.1, but using 0.5.0 as baseline for next version calculation',
),
);
// Test stable calculation - should be 0.6.0 from preview
const stableResult = getVersion({ type: 'stable' });
expect(stableResult.releaseVersion).toBe('0.6.0');
expect(stableResult.previousReleaseTag).toBe('v0.5.0'); // Uses rollback baseline
consoleSpy.mockRestore();
});
it('should fail rollback scenario when git tag for highest version is missing', () => {
const mockWithMissingGitTag = (command) => {
// NPM rolled back but git tag was deleted (bad practice)
if (command.includes('npm view') && command.includes('--tag=latest'))
return '0.4.1'; // Rolled back
if (command.includes('npm view') && command.includes('versions --json'))
return JSON.stringify(['0.4.1', '0.5.0']); // 0.5.0 exists in NPM
if (command.includes("git tag -l 'v[0-9].[0-9].[0-9]'"))
return 'v0.4.1'; // Only old tag exists
if (command.includes("git tag -l 'v0.5.0'")) return ''; // Missing!
return mockExecSync(command);
};
vi.mocked(execSync).mockImplementation(mockWithMissingGitTag);
expect(() =>
getVersion({ type: 'patch', 'patch-from': 'stable' }),
).toThrow(
'Rollback scenario detected, but git tag v0.5.0 does not exist! This is required to calculate the next version.',
);
});
});
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 -l 'v*-preview*'"))
return 'v0.4.0-preview.99'; // Mismatch with NPM's 0.5.0-preview.2
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 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);
expect(() => getVersion({ type: 'stable' })).toThrow(
'Discrepancy found! Failed to verify GitHub release for v0.5.0-preview.2.',
);
const result = getVersion({ type: 'preview' });
// Should have skipped preview.0 and landed on preview.1
expect(result.releaseVersion).toBe('0.8.0-preview.1');
});
});
});