diff --git a/package.json b/package.json index 5d140da..77fe901 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "labuh", - "version": "0.1.1", + "version": "0.1.2", "private": true, "type": "module", "description": "Every deploy request in one harbour: an agent reviews it, a person gives the Gas.", diff --git a/src/codex.ts b/src/codex.ts index 6f2650f..c3d3b82 100644 --- a/src/codex.ts +++ b/src/codex.ts @@ -162,7 +162,7 @@ export class CodexClient { async initialize() { await this.request('initialize', { - clientInfo: { name: 'labuh', title: 'Labuh', version: '0.1.1' }, + clientInfo: { name: 'labuh', title: 'Labuh', version: '0.1.2' }, capabilities: { experimentalApi: false, requestAttestation: false }, }); this.send({ method: 'initialized' }); diff --git a/src/gitlab.ts b/src/gitlab.ts index f64c54a..307bd9b 100644 --- a/src/gitlab.ts +++ b/src/gitlab.ts @@ -37,6 +37,7 @@ export type MrInfo = { projectPath: string; webUrl: string; sshUrl: string; + httpUrl: string; iid: number; title: string; description: string; @@ -90,6 +91,7 @@ export async function loadMr(ref: MrRef, sha?: string): Promise { projectPath: project.path_with_namespace, webUrl: project.web_url, sshUrl: project.ssh_url_to_repo, + httpUrl: project.http_url_to_repo, iid: mr.iid, title: mr.title, description: mr.description ?? '', @@ -118,8 +120,19 @@ export async function loadJob(projectId: number, jobId: number): Promise<{ id: n // A shallow clone of exactly the reviewed commit, read-only for the reviewer. Symlinks are // checked out as plain files holding their target, so an MR cannot add a link that makes // `cat` or a glob read something outside the checkout. +// A laptop fetches over SSH with the operator's key. A server has no key: with GITLAB_TOKEN set, +// git fetches over HTTPS and gets the token through its environment, so the token is never +// written into the checkout the reviewer reads. +function gitAccess(mr: MrInfo): { url: string; env: NodeJS.ProcessEnv } { + const token = process.env.GITLAB_TOKEN; + if (!token) return { url: mr.sshUrl, env: process.env }; + const header = `Authorization: Basic ${Buffer.from(`oauth2:${token}`).toString('base64')}`; + return { url: mr.httpUrl, env: { ...process.env, GIT_CONFIG_COUNT: '1', GIT_CONFIG_KEY_0: 'http.extraHeader', GIT_CONFIG_VALUE_0: header, GIT_TERMINAL_PROMPT: '0' } }; +} + export async function checkout(mr: MrInfo, dir: string) { - const git = (...args: string[]) => run('git', args, { cwd: dir, maxBuffer: 16 * 1024 * 1024 }); + const access = gitAccess(mr); + const git = (...args: string[]) => run('git', args, { cwd: dir, env: access.env, maxBuffer: 16 * 1024 * 1024 }); try { const { stdout } = await git('rev-parse', 'HEAD'); if (stdout.trim() === mr.sha) { @@ -129,8 +142,9 @@ export async function checkout(mr: MrInfo, dir: string) { } catch { await run('mkdir', ['-p', dir]); await git('init', '-q'); - await git('remote', 'add', 'origin', mr.sshUrl); + await git('remote', 'add', 'origin', access.url); } + await git('remote', 'set-url', 'origin', access.url); await git('config', 'core.symlinks', 'false'); await git('fetch', '-q', '--depth=50', 'origin', `refs/merge-requests/${mr.iid}/head`); try {