feat(agents): implement team registry client and installation service

- Add RegistryTeam interface and TeamRegistryClient for remote/local discovery.
- Enhance TeamScaffolder with installRegistryTeam supporting Git and metadata-only teams.
- Refactor GitHub/Git utilities from cli to core for shared usage.
- Add comprehensive unit tests for new services.

TASK-09, TASK-10
This commit is contained in:
Taylor Mullen
2026-04-06 16:20:00 -07:00
parent 59153cc1c5
commit 19b9e3e8e4
24 changed files with 3195 additions and 1206 deletions
+11
View File
@@ -625,6 +625,16 @@ export async function loadCliConfig(
);
}
let teamRegistryURI =
process.env['GEMINI_CLI_TEAM_REGISTRY_URI'] ??
(trustedFolder ? settings.experimental?.teamRegistryURI : undefined);
if (teamRegistryURI && !teamRegistryURI.startsWith('http')) {
teamRegistryURI = resolveToRealPath(
path.resolve(cwd, resolvePath(teamRegistryURI)),
);
}
let memoryContent: string | HierarchicalMemory = '';
let fileCount = 0;
let filePaths: string[] = [];
@@ -964,6 +974,7 @@ export async function loadCliConfig(
enabledExtensions: argv.extensions,
extensionLoader: extensionManager,
extensionRegistryURI,
teamRegistryURI,
enableExtensionReloading: settings.experimental?.extensionReloading,
enableAgents: settings.experimental?.enableAgents,
plan: settings.general?.plan?.enabled ?? true,
+18 -500
View File
@@ -5,29 +5,13 @@
*/
import { describe, it, expect, vi, beforeEach } from 'vitest';
import {
cloneFromGit,
tryParseGithubUrl,
fetchReleaseFromGithub,
checkForExtensionUpdate,
downloadFromGitHubRelease,
findReleaseAsset,
downloadFile,
extractFile,
} from './github.js';
import { checkForExtensionUpdate } from './github.js';
import { simpleGit, type SimpleGit } from 'simple-git';
import { ExtensionUpdateState } from '../../ui/state/extensions.js';
import * as os from 'node:os';
import * as fs from 'node:fs';
import * as https from 'node:https';
import * as tar from 'tar';
import * as extract from 'extract-zip';
import type { ExtensionManager } from '../extension-manager.js';
import { fetchJson } from './github_fetch.js';
import { EventEmitter } from 'node:events';
import type {
GeminiCLIExtension,
ExtensionInstallMetadata,
import {
fetchReleaseFromGithub,
type GeminiCLIExtension,
} from '@google/gemini-cli-core';
import type { ExtensionConfig } from '../extension.js';
@@ -45,161 +29,23 @@ vi.mock('@google/gemini-cli-core', async (importOriginal) => {
log: vi.fn(),
warn: vi.fn(),
},
fetchJson: vi.fn(),
fetchReleaseFromGithub: vi.fn(),
};
});
vi.mock('simple-git');
vi.mock('node:os');
vi.mock('node:fs');
vi.mock('node:https');
vi.mock('tar');
vi.mock('extract-zip');
vi.mock('./github_fetch.js');
vi.mock('../extension-manager.js');
// Mock settings.ts to avoid top-level side effects if possible, or just rely on Storage mock
vi.mock('../settings.js', () => ({
loadSettings: vi.fn(),
USER_SETTINGS_PATH: '/mock/settings.json',
}));
describe('github.ts', () => {
describe('github.ts (CLI specific)', () => {
beforeEach(() => {
vi.resetAllMocks();
});
describe('cloneFromGit', () => {
let mockGit: {
clone: ReturnType<typeof vi.fn>;
getRemotes: ReturnType<typeof vi.fn>;
fetch: ReturnType<typeof vi.fn>;
checkout: ReturnType<typeof vi.fn>;
listRemote: ReturnType<typeof vi.fn>;
revparse: ReturnType<typeof vi.fn>;
};
beforeEach(() => {
mockGit = {
clone: vi.fn(),
getRemotes: vi.fn(),
fetch: vi.fn(),
checkout: vi.fn(),
listRemote: vi.fn(),
revparse: vi.fn(),
};
vi.mocked(simpleGit).mockReturnValue(mockGit as unknown as SimpleGit);
});
it('should clone, fetch and checkout a repo', async () => {
mockGit.getRemotes.mockResolvedValue([{ name: 'origin' }]);
await cloneFromGit(
{
type: 'git',
source: 'https://github.com/owner/repo.git',
ref: 'v1.0.0',
},
'/dest',
);
expect(mockGit.clone).toHaveBeenCalledWith(
'https://github.com/owner/repo.git',
'./',
['--depth', '1'],
);
expect(mockGit.fetch).toHaveBeenCalledWith('origin', 'v1.0.0');
expect(mockGit.checkout).toHaveBeenCalledWith('FETCH_HEAD');
});
it('should throw if no remotes found', async () => {
mockGit.getRemotes.mockResolvedValue([]);
await expect(
cloneFromGit({ type: 'git', source: 'src' }, '/dest'),
).rejects.toThrow('Unable to find any remotes');
});
it('should throw on clone error', async () => {
mockGit.clone.mockRejectedValue(new Error('Clone failed'));
await expect(
cloneFromGit({ type: 'git', source: 'src' }, '/dest'),
).rejects.toThrow('Failed to clone Git repository');
});
});
describe('tryParseGithubUrl', () => {
it.each([
['https://github.com/owner/repo', 'owner', 'repo'],
['https://github.com/owner/repo.git', 'owner', 'repo'],
['git@github.com:owner/repo.git', 'owner', 'repo'],
['owner/repo', 'owner', 'repo'],
])('should parse %s to %s/%s', (url, owner, repo) => {
expect(tryParseGithubUrl(url)).toEqual({ owner, repo });
});
it.each([
'https://gitlab.com/owner/repo',
'https://my-git-host.com/owner/group/repo',
'git@gitlab.com:some-group/some-project/some-repo.git',
])('should return null for non-GitHub URLs', (url) => {
expect(tryParseGithubUrl(url)).toBeNull();
});
it('should throw for invalid formats', () => {
expect(() => tryParseGithubUrl('invalid')).toThrow(
'Invalid GitHub repository source',
);
});
});
describe('fetchReleaseFromGithub', () => {
it('should fetch latest release if no ref provided', async () => {
vi.mocked(fetchJson).mockResolvedValue({ tag_name: 'v1.0.0' });
await fetchReleaseFromGithub('owner', 'repo');
expect(fetchJson).toHaveBeenCalledWith(
'https://api.github.com/repos/owner/repo/releases/latest',
);
});
it('should fetch specific ref if provided', async () => {
vi.mocked(fetchJson).mockResolvedValue({ tag_name: 'v1.0.0' });
await fetchReleaseFromGithub('owner', 'repo', 'v1.0.0');
expect(fetchJson).toHaveBeenCalledWith(
'https://api.github.com/repos/owner/repo/releases/tags/v1.0.0',
);
});
it('should handle pre-releases if allowed', async () => {
vi.mocked(fetchJson).mockResolvedValueOnce([{ tag_name: 'v1.0.0-beta' }]);
const result = await fetchReleaseFromGithub(
'owner',
'repo',
undefined,
true,
);
expect(result).toEqual({ tag_name: 'v1.0.0-beta' });
});
it('should return null if no releases found', async () => {
vi.mocked(fetchJson).mockResolvedValueOnce([]);
const result = await fetchReleaseFromGithub(
'owner',
'repo',
undefined,
true,
);
expect(result).toBeNull();
});
});
describe('checkForExtensionUpdate', () => {
let mockExtensionManager: ExtensionManager;
let mockGit: {
@@ -302,348 +148,20 @@ describe('github.ts', () => {
ExtensionUpdateState.UPDATE_AVAILABLE,
);
});
});
describe('downloadFromGitHubRelease', () => {
it('should fail if no release data found', async () => {
// Mock fetchJson to throw for latest release check
vi.mocked(fetchJson).mockRejectedValue(new Error('Not found'));
it('should return UPDATE_AVAILABLE if github release tag differs', async () => {
vi.mocked(fetchReleaseFromGithub).mockResolvedValue({ tag_name: 'v2.0.0' } as any);
const result = await downloadFromGitHubRelease(
{
type: 'github-release',
source: 'owner/repo',
ref: 'v1',
} as unknown as ExtensionInstallMetadata,
'/dest',
{ owner: 'owner', repo: 'repo' },
);
const ext = {
installMetadata: {
type: 'github-release',
source: 'owner/repo',
releaseTag: 'v1.0.0'
},
} as unknown as GeminiCLIExtension;
expect(result.success).toBe(false);
if (!result.success) {
expect(result.failureReason).toBe('failed to fetch release data');
}
});
it('should use correct headers for release assets', async () => {
vi.mocked(fetchJson).mockResolvedValue({
tag_name: 'v1.0.0',
assets: [{ name: 'asset.tar.gz', url: 'http://asset.url' }],
});
vi.mocked(os.platform).mockReturnValue('linux');
vi.mocked(os.arch).mockReturnValue('x64');
// Mock https.get and fs.createWriteStream for downloadFile
const mockReq = new EventEmitter();
const mockRes =
new EventEmitter() as unknown as import('node:http').IncomingMessage;
Object.assign(mockRes, { statusCode: 200, pipe: vi.fn() });
vi.mocked(https.get).mockImplementation((url, options, cb) => {
if (typeof options === 'function') {
cb = options;
}
if (cb) cb(mockRes);
return mockReq as unknown as import('node:http').ClientRequest;
});
const mockStream = new EventEmitter() as unknown as fs.WriteStream;
Object.assign(mockStream, { close: vi.fn((cb) => cb && cb()) });
vi.mocked(fs.createWriteStream).mockReturnValue(mockStream);
// Mock fs.promises.readdir to return empty array (no cleanup needed)
vi.mocked(fs.promises.readdir).mockResolvedValue([]);
// Mock fs.promises.unlink
vi.mocked(fs.promises.unlink).mockResolvedValue(undefined);
const promise = downloadFromGitHubRelease(
{
type: 'github-release',
source: 'owner/repo',
ref: 'v1.0.0',
} as unknown as ExtensionInstallMetadata,
'/dest',
{ owner: 'owner', repo: 'repo' },
);
// Wait for downloadFile to be called and stream to be created
await vi.waitUntil(
() => vi.mocked(fs.createWriteStream).mock.calls.length > 0,
);
// Trigger stream events to complete download
mockRes.emit('end');
mockStream.emit('finish');
await promise;
expect(https.get).toHaveBeenCalledWith(
'http://asset.url',
expect.objectContaining({
headers: expect.objectContaining({
Accept: 'application/octet-stream',
}),
}),
expect.anything(),
);
});
it('should use correct headers for source tarballs', async () => {
vi.mocked(fetchJson).mockResolvedValue({
tag_name: 'v1.0.0',
assets: [],
tarball_url: 'http://tarball.url',
});
// Mock https.get and fs.createWriteStream for downloadFile
const mockReq = new EventEmitter();
const mockRes =
new EventEmitter() as unknown as import('node:http').IncomingMessage;
Object.assign(mockRes, { statusCode: 200, pipe: vi.fn() });
vi.mocked(https.get).mockImplementation((url, options, cb) => {
if (typeof options === 'function') {
cb = options;
}
if (cb) cb(mockRes);
return mockReq as unknown as import('node:http').ClientRequest;
});
const mockStream = new EventEmitter() as unknown as fs.WriteStream;
Object.assign(mockStream, { close: vi.fn((cb) => cb && cb()) });
vi.mocked(fs.createWriteStream).mockReturnValue(mockStream);
// Mock fs.promises.readdir to return empty array
vi.mocked(fs.promises.readdir).mockResolvedValue([]);
// Mock fs.promises.unlink
vi.mocked(fs.promises.unlink).mockResolvedValue(undefined);
const promise = downloadFromGitHubRelease(
{
type: 'github-release',
source: 'owner/repo',
ref: 'v1.0.0',
} as unknown as ExtensionInstallMetadata,
'/dest',
{ owner: 'owner', repo: 'repo' },
);
// Wait for downloadFile to be called and stream to be created
await vi.waitUntil(
() => vi.mocked(fs.createWriteStream).mock.calls.length > 0,
);
// Trigger stream events to complete download
mockRes.emit('end');
mockStream.emit('finish');
await promise;
expect(https.get).toHaveBeenCalledWith(
'http://tarball.url',
expect.objectContaining({
headers: expect.objectContaining({
Accept: 'application/vnd.github+json',
}),
}),
expect.anything(),
);
});
});
describe('findReleaseAsset', () => {
it('should find platform/arch specific asset', () => {
vi.mocked(os.platform).mockReturnValue('darwin');
vi.mocked(os.arch).mockReturnValue('arm64');
const assets = [
{ name: 'darwin.arm64.tar.gz', url: 'url1' },
{ name: 'linux.x64.tar.gz', url: 'url2' },
];
expect(findReleaseAsset(assets)).toEqual(assets[0]);
});
it('should find generic asset', () => {
vi.mocked(os.platform).mockReturnValue('darwin');
const assets = [{ name: 'generic.tar.gz', url: 'url' }];
expect(findReleaseAsset(assets)).toEqual(assets[0]);
});
});
describe('downloadFile', () => {
it('should download file successfully', async () => {
const mockReq = new EventEmitter();
const mockRes =
new EventEmitter() as unknown as import('node:http').IncomingMessage;
Object.assign(mockRes, { statusCode: 200, pipe: vi.fn() });
vi.mocked(https.get).mockImplementation((url, options, cb) => {
if (typeof options === 'function') {
cb = options;
}
if (cb) cb(mockRes);
return mockReq as unknown as import('node:http').ClientRequest;
});
const mockStream = new EventEmitter() as unknown as fs.WriteStream;
Object.assign(mockStream, { close: vi.fn((cb) => cb && cb()) });
vi.mocked(fs.createWriteStream).mockReturnValue(mockStream);
const promise = downloadFile('url', '/dest');
mockRes.emit('end');
mockStream.emit('finish');
await expect(promise).resolves.toBeUndefined();
});
it('should fail on non-200 status', async () => {
const mockReq = new EventEmitter();
const mockRes =
new EventEmitter() as unknown as import('node:http').IncomingMessage;
Object.assign(mockRes, { statusCode: 404 });
vi.mocked(https.get).mockImplementation((url, options, cb) => {
if (typeof options === 'function') {
cb = options;
}
if (cb) cb(mockRes);
return mockReq as unknown as import('node:http').ClientRequest;
});
await expect(downloadFile('url', '/dest')).rejects.toThrow(
'Request failed with status code 404',
);
});
it('should follow redirects', async () => {
const mockReq = new EventEmitter();
const mockResRedirect =
new EventEmitter() as unknown as import('node:http').IncomingMessage;
Object.assign(mockResRedirect, {
statusCode: 302,
headers: { location: 'new-url' },
});
const mockResSuccess =
new EventEmitter() as unknown as import('node:http').IncomingMessage;
Object.assign(mockResSuccess, { statusCode: 200, pipe: vi.fn() });
vi.mocked(https.get)
.mockImplementationOnce((url, options, cb) => {
if (typeof options === 'function') cb = options;
if (cb) cb(mockResRedirect);
return mockReq as unknown as import('node:http').ClientRequest;
})
.mockImplementationOnce((url, options, cb) => {
if (typeof options === 'function') cb = options;
if (cb) cb(mockResSuccess);
return mockReq as unknown as import('node:http').ClientRequest;
});
const mockStream = new EventEmitter() as unknown as fs.WriteStream;
Object.assign(mockStream, { close: vi.fn((cb) => cb && cb()) });
vi.mocked(fs.createWriteStream).mockReturnValue(mockStream);
const promise = downloadFile('url', '/dest');
mockResSuccess.emit('end');
mockStream.emit('finish');
await expect(promise).resolves.toBeUndefined();
expect(https.get).toHaveBeenCalledTimes(2);
expect(https.get).toHaveBeenLastCalledWith(
'new-url',
expect.anything(),
expect.anything(),
);
});
it('should fail after too many redirects', async () => {
const mockReq = new EventEmitter();
const mockResRedirect =
new EventEmitter() as unknown as import('node:http').IncomingMessage;
Object.assign(mockResRedirect, {
statusCode: 302,
headers: { location: 'new-url' },
});
vi.mocked(https.get).mockImplementation((url, options, cb) => {
if (typeof options === 'function') cb = options;
if (cb) cb(mockResRedirect);
return mockReq as unknown as import('node:http').ClientRequest;
});
await expect(downloadFile('url', '/dest')).rejects.toThrow(
'Too many redirects',
);
}, 10000); // Increase timeout for this test if needed, though with mocks it should be fast
it('should fail if redirect location is missing', async () => {
const mockReq = new EventEmitter();
const mockResRedirect =
new EventEmitter() as unknown as import('node:http').IncomingMessage;
Object.assign(mockResRedirect, {
statusCode: 302,
headers: {}, // No location
});
vi.mocked(https.get).mockImplementation((url, options, cb) => {
if (typeof options === 'function') cb = options;
if (cb) cb(mockResRedirect);
return mockReq as unknown as import('node:http').ClientRequest;
});
await expect(downloadFile('url', '/dest')).rejects.toThrow(
'Redirect response missing Location header',
);
});
it('should pass custom headers', async () => {
const mockReq = new EventEmitter();
const mockRes =
new EventEmitter() as unknown as import('node:http').IncomingMessage;
Object.assign(mockRes, { statusCode: 200, pipe: vi.fn() });
vi.mocked(https.get).mockImplementation((url, options, cb) => {
if (typeof options === 'function') cb = options;
if (cb) cb(mockRes);
return mockReq as unknown as import('node:http').ClientRequest;
});
const mockStream = new EventEmitter() as unknown as fs.WriteStream;
Object.assign(mockStream, { close: vi.fn((cb) => cb && cb()) });
vi.mocked(fs.createWriteStream).mockReturnValue(mockStream);
const promise = downloadFile('url', '/dest', {
headers: { 'X-Custom': 'value' },
});
mockRes.emit('end');
mockStream.emit('finish');
await expect(promise).resolves.toBeUndefined();
expect(https.get).toHaveBeenCalledWith(
'url',
expect.objectContaining({
headers: expect.objectContaining({ 'X-Custom': 'value' }),
}),
expect.anything(),
);
});
});
describe('extractFile', () => {
it('should extract tar.gz using tar', async () => {
await extractFile('file.tar.gz', '/dest');
expect(tar.x).toHaveBeenCalled();
});
it('should extract zip using extract-zip', async () => {
vi.mocked(extract.default || extract).mockResolvedValue(undefined);
await extractFile('file.zip', '/dest');
// Check if extract was called. Note: extract-zip export might be default or named depending on mock
});
it('should throw for unsupported extensions', async () => {
await expect(extractFile('file.txt', '/dest')).rejects.toThrow(
'Unsupported file extension',
expect(await checkForExtensionUpdate(ext, mockExtensionManager)).toBe(
ExtensionUpdateState.UPDATE_AVAILABLE,
);
});
});
+20 -423
View File
@@ -8,165 +8,39 @@ import { simpleGit } from 'simple-git';
import {
debugLogger,
getErrorMessage,
type ExtensionInstallMetadata,
type GeminiCLIExtension,
} from '@google/gemini-cli-core';
import { ExtensionUpdateState } from '../../ui/state/extensions.js';
import * as os from 'node:os';
import * as https from 'node:https';
import * as fs from 'node:fs';
import * as path from 'node:path';
import * as tar from 'tar';
import extract from 'extract-zip';
import { fetchJson, getGitHubToken } from './github_fetch.js';
import type { ExtensionConfig } from '../extension.js';
import type { ExtensionManager } from '../extension-manager.js';
import { EXTENSIONS_CONFIG_FILENAME } from './variables.js';
/**
* Clones a Git repository to a specified local path.
* @param installMetadata The metadata for the extension to install.
* @param destination The destination path to clone the repository to.
*/
export async function cloneFromGit(
installMetadata: ExtensionInstallMetadata,
destination: string,
): Promise<void> {
try {
const git = simpleGit(destination);
let sourceUrl = installMetadata.source;
const token = getGitHubToken();
if (token) {
try {
const parsedUrl = new URL(sourceUrl);
if (
parsedUrl.protocol === 'https:' &&
parsedUrl.hostname === 'github.com'
) {
if (!parsedUrl.username) {
parsedUrl.username = token;
}
sourceUrl = parsedUrl.toString();
}
} catch {
// If source is not a valid URL, we don't inject the token.
// We let git handle the source as is.
}
}
await git.clone(sourceUrl, './', ['--depth', '1']);
import {
cloneFromGit,
downloadFromGitHubRelease,
tryParseGithubUrl,
fetchReleaseFromGithub,
findReleaseAsset,
downloadFile,
extractFile,
fetchJson,
} from '@google/gemini-cli-core';
const remotes = await git.getRemotes(true);
if (remotes.length === 0) {
throw new Error(
`Unable to find any remotes for repo ${installMetadata.source}`,
);
}
const refToFetch = installMetadata.ref || 'HEAD';
await git.fetch(remotes[0].name, refToFetch);
// After fetching, checkout FETCH_HEAD to get the content of the fetched ref.
// This results in a detached HEAD state, which is fine for this purpose.
await git.checkout('FETCH_HEAD');
} catch (error) {
throw new Error(
`Failed to clone Git repository from ${installMetadata.source} ${getErrorMessage(error)}`,
{
cause: error,
},
);
}
}
export {
cloneFromGit,
downloadFromGitHubRelease,
tryParseGithubUrl,
fetchReleaseFromGithub,
findReleaseAsset,
downloadFile,
extractFile,
fetchJson,
};
export interface GithubRepoInfo {
owner: string;
repo: string;
}
export function tryParseGithubUrl(source: string): GithubRepoInfo | null {
// Handle SCP-style SSH URLs.
if (source.startsWith('git@')) {
if (source.startsWith('git@github.com:')) {
// It's a GitHub SSH URL, so normalize it for the URL parser.
source = source.replace('git@github.com:', '');
} else {
// It's another provider's SSH URL (e.g., gitlab), so not a GitHub repo.
return null;
}
}
// Default to a github repo path, so `source` can be just an org/repo
let parsedUrl: URL;
try {
// Use the standard URL constructor for backward compatibility.
parsedUrl = new URL(source, 'https://github.com');
} catch (e) {
// Throw a TypeError to maintain a consistent error contract for invalid URLs.
// This avoids a breaking change for consumers who might expect a TypeError.
throw new TypeError(`Invalid repo URL: ${source}`, { cause: e });
}
if (!parsedUrl) {
throw new Error(`Invalid repo URL: ${source}`);
}
if (parsedUrl?.host !== 'github.com') {
return null;
}
// The pathname should be "/owner/repo".
const parts = parsedUrl?.pathname
.split('/')
// Remove the empty segments, fixes trailing and leading slashes
.filter((part) => part !== '');
if (parts?.length !== 2) {
throw new Error(
`Invalid GitHub repository source: ${source}. Expected "owner/repo" or a github repo uri.`,
);
}
const owner = parts[0];
const repo = parts[1].replace('.git', '');
return {
owner,
repo,
};
}
export async function fetchReleaseFromGithub(
owner: string,
repo: string,
ref?: string,
allowPreRelease?: boolean,
): Promise<GithubReleaseData | null> {
if (ref) {
return fetchJson(
`https://api.github.com/repos/${owner}/${repo}/releases/tags/${ref}`,
);
}
if (!allowPreRelease) {
// Grab the release that is tagged as the "latest", github does not allow
// this to be a pre-release so we can blindly grab it.
try {
return await fetchJson(
`https://api.github.com/repos/${owner}/${repo}/releases/latest`,
);
} catch (_) {
// This can fail if there is no release marked latest. In that case
// we want to just try the pre-release logic below.
}
}
// If pre-releases are allowed, we just grab the most recent release.
const releases = await fetchJson<GithubReleaseData[]>(
`https://api.github.com/repos/${owner}/${repo}/releases?per_page=1`,
);
if (releases.length === 0) {
return null;
}
return releases[0];
}
export async function checkForExtensionUpdate(
extension: GeminiCLIExtension,
extensionManager: ExtensionManager,
@@ -296,280 +170,3 @@ export async function checkForExtensionUpdate(
return ExtensionUpdateState.ERROR;
}
}
export type GitHubDownloadResult =
| {
tagName?: string;
type: 'git' | 'github-release';
success: false;
failureReason:
| 'failed to fetch release data'
| 'no release data'
| 'no release asset found'
| 'failed to download asset'
| 'failed to extract asset'
| 'unknown';
errorMessage: string;
}
| {
tagName?: string;
type: 'git' | 'github-release';
success: true;
};
export async function downloadFromGitHubRelease(
installMetadata: ExtensionInstallMetadata,
destination: string,
githubRepoInfo: GithubRepoInfo,
): Promise<GitHubDownloadResult> {
const { ref, allowPreRelease: preRelease } = installMetadata;
const { owner, repo } = githubRepoInfo;
let releaseData: GithubReleaseData | null = null;
try {
try {
releaseData = await fetchReleaseFromGithub(owner, repo, ref, preRelease);
if (!releaseData) {
return {
failureReason: 'no release data',
success: false,
type: 'github-release',
errorMessage: `No release data found for ${owner}/${repo} at tag ${ref}`,
};
}
} catch (error) {
return {
failureReason: 'failed to fetch release data',
success: false,
type: 'github-release',
errorMessage: `Failed to fetch release data for ${owner}/${repo} at tag ${ref}: ${getErrorMessage(error)}`,
};
}
const asset = findReleaseAsset(releaseData.assets);
let archiveUrl: string | undefined;
let isTar = false;
let isZip = false;
let fileName: string | undefined;
if (asset) {
archiveUrl = asset.url;
fileName = asset.name;
} else {
if (releaseData.tarball_url) {
archiveUrl = releaseData.tarball_url;
isTar = true;
} else if (releaseData.zipball_url) {
archiveUrl = releaseData.zipball_url;
isZip = true;
}
}
if (!archiveUrl) {
return {
failureReason: 'no release asset found',
success: false,
type: 'github-release',
tagName: releaseData.tag_name,
errorMessage: `No assets found for release with tag ${releaseData.tag_name}`,
};
}
if (!fileName) {
fileName = path.basename(new URL(archiveUrl).pathname);
}
let downloadedAssetPath = path.join(destination, fileName);
if (isTar && !downloadedAssetPath.endsWith('.tar.gz')) {
downloadedAssetPath += '.tar.gz';
} else if (isZip && !downloadedAssetPath.endsWith('.zip')) {
downloadedAssetPath += '.zip';
}
try {
// GitHub API requires different Accept headers for different types of downloads:
// 1. Binary Assets (e.g. release artifacts): Require 'application/octet-stream' to return the raw content.
// 2. Source Tarballs (e.g. /tarball/{ref}): Require 'application/vnd.github+json' (or similar) to return
// a 302 Redirect to the actual download location (codeload.github.com).
// Sending 'application/octet-stream' for tarballs results in a 415 Unsupported Media Type error.
const headers = {
...(asset
? { Accept: 'application/octet-stream' }
: { Accept: 'application/vnd.github+json' }),
};
await downloadFile(archiveUrl, downloadedAssetPath, { headers });
} catch (error) {
return {
failureReason: 'failed to download asset',
success: false,
type: 'github-release',
tagName: releaseData.tag_name,
errorMessage: `Failed to download asset from ${archiveUrl}: ${getErrorMessage(error)}`,
};
}
try {
await extractFile(downloadedAssetPath, destination);
} catch (error) {
return {
failureReason: 'failed to extract asset',
success: false,
type: 'github-release',
tagName: releaseData.tag_name,
errorMessage: `Failed to extract asset from ${downloadedAssetPath}: ${getErrorMessage(error)}`,
};
}
// For regular github releases, the repository is put inside of a top level
// directory. In this case we should see exactly two file in the destination
// dir, the archive and the directory. If we see that, validate that the
// dir has a gemini extension configuration file and then move all files
// from the directory up one level into the destination directory.
const entries = await fs.promises.readdir(destination, {
withFileTypes: true,
});
if (entries.length === 2) {
const lonelyDir = entries.find((entry) => entry.isDirectory());
if (
lonelyDir &&
fs.existsSync(
path.join(destination, lonelyDir.name, EXTENSIONS_CONFIG_FILENAME),
)
) {
const dirPathToExtract = path.join(destination, lonelyDir.name);
const extractedDirFiles = await fs.promises.readdir(dirPathToExtract);
for (const file of extractedDirFiles) {
await fs.promises.rename(
path.join(dirPathToExtract, file),
path.join(destination, file),
);
}
await fs.promises.rmdir(dirPathToExtract);
}
}
await fs.promises.unlink(downloadedAssetPath);
return {
tagName: releaseData.tag_name,
type: 'github-release',
success: true,
};
} catch (error) {
return {
failureReason: 'unknown',
success: false,
type: 'github-release',
tagName: releaseData?.tag_name,
errorMessage: `Failed to download release from ${installMetadata.source}: ${getErrorMessage(error)}`,
};
}
}
interface GithubReleaseData {
assets: Asset[];
tag_name: string;
tarball_url?: string;
zipball_url?: string;
}
interface Asset {
name: string;
url: string;
}
export function findReleaseAsset(assets: Asset[]): Asset | undefined {
const platform = os.platform();
const arch = os.arch();
const platformArchPrefix = `${platform}.${arch}.`;
const platformPrefix = `${platform}.`;
// Check for platform + architecture specific asset
const platformArchAsset = assets.find((asset) =>
asset.name.toLowerCase().startsWith(platformArchPrefix),
);
if (platformArchAsset) {
return platformArchAsset;
}
// Check for platform specific asset
const platformAsset = assets.find((asset) =>
asset.name.toLowerCase().startsWith(platformPrefix),
);
if (platformAsset) {
return platformAsset;
}
// Check for generic asset if only one is available
const genericAsset = assets.find(
(asset) =>
!asset.name.toLowerCase().includes('darwin') &&
!asset.name.toLowerCase().includes('linux') &&
!asset.name.toLowerCase().includes('win32'),
);
if (assets.length === 1) {
return genericAsset;
}
return undefined;
}
export interface DownloadOptions {
headers?: Record<string, string>;
}
export async function downloadFile(
url: string,
dest: string,
options?: DownloadOptions,
redirectCount: number = 0,
): Promise<void> {
const headers: Record<string, string> = {
'User-agent': 'gemini-cli',
Accept: 'application/octet-stream',
...options?.headers,
};
const token = getGitHubToken();
if (token) {
headers['Authorization'] = `token ${token}`;
}
return new Promise((resolve, reject) => {
https
.get(url, { headers }, (res) => {
if (res.statusCode === 302 || res.statusCode === 301) {
if (redirectCount >= 10) {
return reject(new Error('Too many redirects'));
}
if (!res.headers.location) {
return reject(
new Error('Redirect response missing Location header'),
);
}
downloadFile(res.headers.location, dest, options, redirectCount + 1)
.then(resolve)
.catch(reject);
return;
}
if (res.statusCode !== 200) {
return reject(
new Error(`Request failed with status code ${res.statusCode}`),
);
}
const file = fs.createWriteStream(dest);
res.pipe(file);
file.on('finish', () => file.close(resolve as () => void));
})
.on('error', reject);
});
}
export async function extractFile(file: string, dest: string): Promise<void> {
if (file.endsWith('.tar.gz')) {
await tar.x({
file,
cwd: dest,
});
} else if (file.endsWith('.zip')) {
await extract(file, { dir: dest });
} else {
throw new Error(`Unsupported file extension for extraction: ${file}`);
}
}
@@ -1,199 +0,0 @@
/**
* @license
* Copyright 2025 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import { describe, it, expect, vi, afterEach, beforeEach } from 'vitest';
import * as https from 'node:https';
import { EventEmitter } from 'node:events';
import { fetchJson, getGitHubToken } from './github_fetch.js';
import type { ClientRequest, IncomingMessage } from 'node:http';
vi.mock('node:https');
describe('getGitHubToken', () => {
const originalToken = process.env['GITHUB_TOKEN'];
afterEach(() => {
if (originalToken) {
process.env['GITHUB_TOKEN'] = originalToken;
} else {
delete process.env['GITHUB_TOKEN'];
}
});
it('should return the token if GITHUB_TOKEN is set', () => {
process.env['GITHUB_TOKEN'] = 'test-token';
expect(getGitHubToken()).toBe('test-token');
});
it('should return undefined if GITHUB_TOKEN is not set', () => {
delete process.env['GITHUB_TOKEN'];
expect(getGitHubToken()).toBeUndefined();
});
});
describe('fetchJson', () => {
const getMock = vi.mocked(https.get);
afterEach(() => {
vi.resetAllMocks();
});
it('should fetch and parse JSON successfully', async () => {
getMock.mockImplementationOnce((_url, _options, callback) => {
const res = new EventEmitter() as IncomingMessage;
res.statusCode = 200;
(callback as (res: IncomingMessage) => void)(res);
res.emit('data', Buffer.from('{"foo":'));
res.emit('data', Buffer.from('"bar"}'));
res.emit('end');
return new EventEmitter() as ClientRequest;
});
await expect(fetchJson('https://example.com/data.json')).resolves.toEqual({
foo: 'bar',
});
});
it('should handle redirects (301 and 302)', async () => {
// Test 302
getMock.mockImplementationOnce((_url, _options, callback) => {
const res = new EventEmitter() as IncomingMessage;
res.statusCode = 302;
res.headers = { location: 'https://example.com/final' };
(callback as (res: IncomingMessage) => void)(res);
res.emit('end');
return new EventEmitter() as ClientRequest;
});
getMock.mockImplementationOnce((url, _options, callback) => {
expect(url).toBe('https://example.com/final');
const res = new EventEmitter() as IncomingMessage;
res.statusCode = 200;
(callback as (res: IncomingMessage) => void)(res);
res.emit('data', Buffer.from('{"success": true}'));
res.emit('end');
return new EventEmitter() as ClientRequest;
});
await expect(fetchJson('https://example.com/redirect')).resolves.toEqual({
success: true,
});
// Test 301
getMock.mockImplementationOnce((_url, _options, callback) => {
const res = new EventEmitter() as IncomingMessage;
res.statusCode = 301;
res.headers = { location: 'https://example.com/final-permanent' };
(callback as (res: IncomingMessage) => void)(res);
res.emit('end');
return new EventEmitter() as ClientRequest;
});
getMock.mockImplementationOnce((url, _options, callback) => {
expect(url).toBe('https://example.com/final-permanent');
const res = new EventEmitter() as IncomingMessage;
res.statusCode = 200;
(callback as (res: IncomingMessage) => void)(res);
res.emit('data', Buffer.from('{"permanent": true}'));
res.emit('end');
return new EventEmitter() as ClientRequest;
});
await expect(
fetchJson('https://example.com/redirect-perm'),
).resolves.toEqual({ permanent: true });
});
it('should reject on non-200/30x status code', async () => {
getMock.mockImplementationOnce((_url, _options, callback) => {
const res = new EventEmitter() as IncomingMessage;
res.statusCode = 404;
(callback as (res: IncomingMessage) => void)(res);
res.emit('end');
return new EventEmitter() as ClientRequest;
});
await expect(fetchJson('https://example.com/error')).rejects.toThrow(
'Request failed with status code 404',
);
});
it('should reject on request error', async () => {
const error = new Error('Network error');
getMock.mockImplementationOnce(() => {
const req = new EventEmitter() as ClientRequest;
req.emit('error', error);
return req;
});
await expect(fetchJson('https://example.com/error')).rejects.toThrow(
'Network error',
);
});
describe('with GITHUB_TOKEN', () => {
const originalToken = process.env['GITHUB_TOKEN'];
beforeEach(() => {
process.env['GITHUB_TOKEN'] = 'my-secret-token';
});
afterEach(() => {
if (originalToken) {
process.env['GITHUB_TOKEN'] = originalToken;
} else {
delete process.env['GITHUB_TOKEN'];
}
});
it('should include Authorization header if token is present', async () => {
getMock.mockImplementationOnce((_url, options, callback) => {
expect(options.headers).toEqual({
'User-Agent': 'gemini-cli',
Authorization: 'token my-secret-token',
});
const res = new EventEmitter() as IncomingMessage;
res.statusCode = 200;
(callback as (res: IncomingMessage) => void)(res);
res.emit('data', Buffer.from('{"foo": "bar"}'));
res.emit('end');
return new EventEmitter() as ClientRequest;
});
await expect(fetchJson('https://api.github.com/user')).resolves.toEqual({
foo: 'bar',
});
});
});
describe('without GITHUB_TOKEN', () => {
const originalToken = process.env['GITHUB_TOKEN'];
beforeEach(() => {
delete process.env['GITHUB_TOKEN'];
});
afterEach(() => {
if (originalToken) {
process.env['GITHUB_TOKEN'] = originalToken;
}
});
it('should not include Authorization header if token is not present', async () => {
getMock.mockImplementationOnce((_url, options, callback) => {
expect(options.headers).toEqual({
'User-Agent': 'gemini-cli',
});
const res = new EventEmitter() as IncomingMessage;
res.statusCode = 200;
(callback as (res: IncomingMessage) => void)(res);
res.emit('data', Buffer.from('{"foo": "bar"}'));
res.emit('end');
return new EventEmitter() as ClientRequest;
});
await expect(fetchJson('https://api.github.com/user')).resolves.toEqual({
foo: 'bar',
});
});
});
});
@@ -4,51 +4,4 @@
* SPDX-License-Identifier: Apache-2.0
*/
import * as https from 'node:https';
export function getGitHubToken(): string | undefined {
return process.env['GITHUB_TOKEN'];
}
export async function fetchJson<T>(
url: string,
redirectCount: number = 0,
): Promise<T> {
const headers: { 'User-Agent': string; Authorization?: string } = {
'User-Agent': 'gemini-cli',
};
const token = getGitHubToken();
if (token) {
headers.Authorization = `token ${token}`;
}
return new Promise((resolve, reject) => {
https
.get(url, { headers }, (res) => {
if (res.statusCode === 302 || res.statusCode === 301) {
if (redirectCount >= 10) {
return reject(new Error('Too many redirects'));
}
if (!res.headers.location) {
return reject(new Error('No location header in redirect response'));
}
fetchJson<T>(res.headers.location, redirectCount++)
.then(resolve)
.catch(reject);
return;
}
if (res.statusCode !== 200) {
return reject(
new Error(`Request failed with status code ${res.statusCode}`),
);
}
const chunks: Buffer[] = [];
res.on('data', (chunk) => chunks.push(chunk));
res.on('end', () => {
const data = Buffer.concat(chunks).toString();
// eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion
resolve(JSON.parse(data) as T);
});
})
.on('error', reject);
});
}
export { getGitHubToken, fetchJson } from '@google/gemini-cli-core';
+10
View File
@@ -2009,6 +2009,16 @@ const SETTINGS_SCHEMA = {
'The URI (web URL or local file path) of the extension registry.',
showInDialog: false,
},
teamRegistryURI: {
type: 'string',
label: 'Team Registry URI',
category: 'Experimental',
requiresRestart: true,
default: 'https://geminicli.com/teams.json',
description:
'The URI (web URL or local file path) of the agent team registry.',
showInDialog: false,
},
extensionReloading: {
type: 'boolean',
label: 'Extension Reloading',