|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import { execSync } from 'child_process'; |
|
|
import { ProxyAgent } from 'undici'; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export const isGitHubRepository = (): boolean => { |
|
|
try { |
|
|
const remotes = ( |
|
|
execSync('git remote -v', { |
|
|
encoding: 'utf-8', |
|
|
}) || '' |
|
|
).trim(); |
|
|
|
|
|
const pattern = /github\.com/; |
|
|
|
|
|
return pattern.test(remotes); |
|
|
} catch (_error) { |
|
|
|
|
|
console.debug(`Failed to get git remote:`, _error); |
|
|
return false; |
|
|
} |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export const getGitRepoRoot = (): string => { |
|
|
const gitRepoRoot = ( |
|
|
execSync('git rev-parse --show-toplevel', { |
|
|
encoding: 'utf-8', |
|
|
}) || '' |
|
|
).trim(); |
|
|
|
|
|
if (!gitRepoRoot) { |
|
|
throw new Error(`Git repo returned empty value`); |
|
|
} |
|
|
|
|
|
return gitRepoRoot; |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export const getLatestGitHubRelease = async ( |
|
|
proxy?: string, |
|
|
): Promise<string> => { |
|
|
try { |
|
|
const controller = new AbortController(); |
|
|
|
|
|
const endpoint = `https://api.github.com/repos/google-github-actions/run-gemini-cli/releases/latest`; |
|
|
|
|
|
const response = await fetch(endpoint, { |
|
|
method: 'GET', |
|
|
headers: { |
|
|
Accept: 'application/vnd.github+json', |
|
|
'Content-Type': 'application/json', |
|
|
'X-GitHub-Api-Version': '2022-11-28', |
|
|
}, |
|
|
dispatcher: proxy ? new ProxyAgent(proxy) : undefined, |
|
|
signal: AbortSignal.any([AbortSignal.timeout(30_000), controller.signal]), |
|
|
} as RequestInit); |
|
|
|
|
|
if (!response.ok) { |
|
|
throw new Error( |
|
|
`Invalid response code: ${response.status} - ${response.statusText}`, |
|
|
); |
|
|
} |
|
|
|
|
|
const releaseTag = (await response.json()).tag_name; |
|
|
if (!releaseTag) { |
|
|
throw new Error(`Response did not include tag_name field`); |
|
|
} |
|
|
return releaseTag; |
|
|
} catch (_error) { |
|
|
console.debug(`Failed to determine latest run-gemini-cli release:`, _error); |
|
|
throw new Error( |
|
|
`Unable to determine the latest run-gemini-cli release on GitHub.`, |
|
|
); |
|
|
} |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function getGitHubRepoInfo(): { owner: string; repo: string } { |
|
|
const remoteUrl = execSync('git remote get-url origin', { |
|
|
encoding: 'utf-8', |
|
|
}).trim(); |
|
|
|
|
|
|
|
|
const match = remoteUrl.match( |
|
|
/(?:https?:\/\/|git@)github\.com(?::|\/)([^/]+)\/([^/]+?)(?:\.git)?$/, |
|
|
); |
|
|
|
|
|
|
|
|
if (!match || !match[1] || !match[2]) { |
|
|
throw new Error( |
|
|
`Owner & repo could not be extracted from remote URL: ${remoteUrl}`, |
|
|
); |
|
|
} |
|
|
|
|
|
return { owner: match[1], repo: match[2] }; |
|
|
} |
|
|
|