Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 9279050ca9 |
+34
-31
@@ -1,41 +1,46 @@
|
|||||||
import { spawnSync } from 'child_process';
|
import { spawnSync } from 'child_process';
|
||||||
import fs from 'fs';
|
import fs from 'fs';
|
||||||
import path from 'path';
|
import path from 'path';
|
||||||
import { GITEA_SERVER_URL, GITEA_REPOSITORY, GITEA_TOKEN, PR_HEAD_BRANCH, FINDINGS_PATH } from './config.js';
|
import { GITEA_SERVER_URL, GITEA_REPOSITORY, PR_HEAD_BRANCH, FINDINGS_PATH } from './config.js';
|
||||||
|
|
||||||
function makeRunner(spawn) {
|
function git(args, cwd) {
|
||||||
return function run(args, cwd, env) {
|
const result = spawnSync('git', args, { cwd, encoding: 'utf8' });
|
||||||
const opts = { cwd, encoding: 'utf8' };
|
if (result.error) throw result.error;
|
||||||
if (env) opts.env = env;
|
if (result.status !== 0) throw new Error((result.stderr || result.stdout || '').trim());
|
||||||
const result = spawn('git', args, opts);
|
return (result.stdout || '').trim();
|
||||||
if (result.error) throw result.error;
|
|
||||||
if (result.status !== 0) throw new Error((result.stderr || result.stdout || '').trim());
|
|
||||||
return (result.stdout || '').trim();
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function commitAndPush(workspace, _spawnSync = spawnSync) {
|
export async function commitAndPush(workspace) {
|
||||||
const run = makeRunner(_spawnSync);
|
const remoteUrl = GITEA_SERVER_URL.replace(/\/$/, '') + `/${GITEA_REPOSITORY}.git`;
|
||||||
|
|
||||||
const baseUrl = GITEA_SERVER_URL.replace(/\/$/, '');
|
|
||||||
const remoteUrl = `${baseUrl}/${GITEA_REPOSITORY}.git`;
|
|
||||||
const repoDir = path.join(workspace, 'repo');
|
const repoDir = path.join(workspace, 'repo');
|
||||||
|
|
||||||
// Write a temporary askpass script so the token never appears in the URL or process list
|
|
||||||
const askpassScript = path.join(workspace, '.git-askpass.sh');
|
|
||||||
fs.writeFileSync(askpassScript, `#!/bin/sh\necho "${GITEA_TOKEN}"\n`, { mode: 0o700 });
|
|
||||||
|
|
||||||
const credEnv = { ...process.env, GIT_ASKPASS: askpassScript, GIT_USERNAME: 'x-token' };
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if (!fs.existsSync(repoDir)) {
|
if (!fs.existsSync(repoDir)) {
|
||||||
run(['clone', '--depth=1', '--branch', PR_HEAD_BRANCH, remoteUrl, repoDir], workspace, credEnv);
|
// Use GIT_ASKPASS to provide token for authentication
|
||||||
|
gitWithToken(['clone', '--depth=1', '--branch', PR_HEAD_BRANCH, remoteUrl, repoDir], workspace);
|
||||||
}
|
}
|
||||||
|
|
||||||
run(['config', 'user.email', 'ai-review[bot]@gitea'], repoDir);
|
git(['config', 'user.email', 'ai-review[bot]@gitea'], repoDir);
|
||||||
run(['config', 'user.name', 'AI Review Bot'], repoDir);
|
git(['config', 'user.name', 'AI Review Bot'], repoDir);
|
||||||
run(['fetch', 'origin', PR_HEAD_BRANCH], repoDir, credEnv);
|
git(['fetch', 'origin', PR_HEAD_BRANCH], repoDir);
|
||||||
run(['checkout', PR_HEAD_BRANCH], repoDir);
|
git(['checkout', PR_HEAD_BRANCH], repoDir);
|
||||||
|
// Helper to run git with GITEA_TOKEN via GIT_ASKPASS
|
||||||
|
import { GITEA_TOKEN } from './config.js';
|
||||||
|
function gitWithToken(args, cwd) {
|
||||||
|
const askPassScript = `#!/bin/sh\necho \"${GITEA_TOKEN}\"`;
|
||||||
|
const askPassPath = path.join(cwd, 'git-askpass.sh');
|
||||||
|
fs.writeFileSync(askPassPath, askPassScript, { mode: 0o700 });
|
||||||
|
const result = spawnSync('git', args, {
|
||||||
|
cwd,
|
||||||
|
encoding: 'utf8',
|
||||||
|
env: { ...process.env, GIT_ASKPASS: askPassPath },
|
||||||
|
});
|
||||||
|
fs.unlinkSync(askPassPath);
|
||||||
|
if (result.error) throw result.error;
|
||||||
|
if (result.status !== 0) throw new Error((result.stderr || result.stdout || '').trim());
|
||||||
|
return (result.stdout || '').trim();
|
||||||
|
}
|
||||||
|
|
||||||
// 將 findings.json 從 workspace 複製到 clone 的 repo
|
// 將 findings.json 從 workspace 複製到 clone 的 repo
|
||||||
const srcFindings = path.join(workspace, FINDINGS_PATH);
|
const srcFindings = path.join(workspace, FINDINGS_PATH);
|
||||||
@@ -43,21 +48,19 @@ export async function commitAndPush(workspace, _spawnSync = spawnSync) {
|
|||||||
fs.mkdirSync(path.dirname(destFindings), { recursive: true });
|
fs.mkdirSync(path.dirname(destFindings), { recursive: true });
|
||||||
fs.copyFileSync(srcFindings, destFindings);
|
fs.copyFileSync(srcFindings, destFindings);
|
||||||
|
|
||||||
run(['add', FINDINGS_PATH], repoDir);
|
git(['add', FINDINGS_PATH], repoDir);
|
||||||
|
|
||||||
const status = run(['status', '--porcelain'], repoDir);
|
const status = git(['status', '--porcelain'], repoDir);
|
||||||
if (!status) {
|
if (!status) {
|
||||||
console.log(' findings.json 無變更,跳過 commit');
|
console.log(' findings.json 無變更,跳過 commit');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const out = run(['commit', '-m', 'chore: update ai-review findings [skip ci]'], repoDir);
|
const out = git(['commit', '-m', 'chore: update ai-review findings [skip ci]'], repoDir);
|
||||||
const commitHash = out.match(/\[.+ ([a-f0-9]+)\]/)?.[1] || 'unknown';
|
const commitHash = out.match(/\[.+ ([a-f0-9]+)\]/)?.[1] || 'unknown';
|
||||||
run(['push', remoteUrl, PR_HEAD_BRANCH], repoDir, credEnv);
|
git(['push', remoteUrl, PR_HEAD_BRANCH], repoDir);
|
||||||
console.log(` ✅ persisted findings commit=${commitHash} push=${PR_HEAD_BRANCH}`);
|
console.log(` ✅ persisted findings commit=${commitHash} push=${PR_HEAD_BRANCH}`);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.log(` ⚠️ Runner failed: commit/push 失敗: ${e.message}`);
|
console.log(` ⚠️ Runner failed: commit/push 失敗: ${e.message}`);
|
||||||
} finally {
|
|
||||||
try { fs.unlinkSync(askpassScript); } catch {}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+18
-81
@@ -1,93 +1,30 @@
|
|||||||
import { describe, it, before, after, beforeEach } from 'node:test';
|
|
||||||
import assert from 'node:assert/strict';
|
|
||||||
import fs from 'fs';
|
|
||||||
import os from 'os';
|
|
||||||
import path from 'path';
|
|
||||||
import { commitAndPush } from './git.js';
|
import { commitAndPush } from './git.js';
|
||||||
|
import fs from 'fs';
|
||||||
|
import path from 'path';
|
||||||
|
|
||||||
// --- helpers ---
|
// Mock dependencies and environment
|
||||||
function makeTmpWorkspace() {
|
jest.mock('fs');
|
||||||
const ws = fs.mkdtempSync(path.join(os.tmpdir(), 'git-test-'));
|
jest.mock('child_process', () => ({
|
||||||
// Pre-create repo dir so clone branch is skipped
|
spawnSync: jest.fn(() => ({ status: 0, stdout: '', stderr: '' }))
|
||||||
fs.mkdirSync(path.join(ws, 'repo'), { recursive: true });
|
}));
|
||||||
// Create a findings.json to copy
|
|
||||||
const findingsDir = path.join(ws, '.gitea/ai-review');
|
|
||||||
fs.mkdirSync(findingsDir, { recursive: true });
|
|
||||||
fs.writeFileSync(path.join(findingsDir, 'findings.json'), '[]');
|
|
||||||
return ws;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Default stub: all commands succeed, status returns changes
|
|
||||||
function makeSpawn(overrides = {}) {
|
|
||||||
const calls = [];
|
|
||||||
const spawn = (cmd, args, opts) => {
|
|
||||||
const key = args[0];
|
|
||||||
calls.push({ cmd, args, opts });
|
|
||||||
if (overrides[key]) return overrides[key](args, opts);
|
|
||||||
if (key === 'status') return { status: 0, stdout: 'M .gitea/ai-review/findings.json', stderr: '', error: null };
|
|
||||||
if (key === 'commit') return { status: 0, stdout: '[feature-branch abc1234] chore', stderr: '', error: null };
|
|
||||||
return { status: 0, stdout: '', stderr: '', error: null };
|
|
||||||
};
|
|
||||||
spawn.calls = calls;
|
|
||||||
return spawn;
|
|
||||||
}
|
|
||||||
|
|
||||||
describe('commitAndPush', () => {
|
describe('commitAndPush', () => {
|
||||||
let workspace;
|
const workspace = '/tmp/workspace';
|
||||||
|
const repoDir = path.join(workspace, 'repo');
|
||||||
|
|
||||||
before(() => { workspace = makeTmpWorkspace(); });
|
|
||||||
after(() => { fs.rmSync(workspace, { recursive: true, force: true }); });
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
// Remove leftover askpass scripts between tests
|
jest.clearAllMocks();
|
||||||
for (const f of fs.readdirSync(workspace)) {
|
fs.existsSync.mockReturnValue(false);
|
||||||
if (f.endsWith('.git-askpass.sh')) fs.unlinkSync(path.join(workspace, f));
|
fs.writeFileSync.mockImplementation(() => {});
|
||||||
}
|
fs.unlinkSync.mockImplementation(() => {});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('does not embed token in any git command argument', async () => {
|
it('should clone repo and configure git', async () => {
|
||||||
const spawn = makeSpawn();
|
await expect(commitAndPush(workspace)).resolves.not.toThrow();
|
||||||
await commitAndPush(workspace, spawn);
|
|
||||||
|
|
||||||
for (const { args } of spawn.calls) {
|
|
||||||
assert.ok(!args.join(' ').includes('test-token'), `Token leaked in git args: ${args.join(' ')}`);
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('uses GIT_ASKPASS env for network operations (fetch, push, clone)', async () => {
|
it('should not clone if repo exists', async () => {
|
||||||
const spawn = makeSpawn();
|
fs.existsSync.mockReturnValue(true);
|
||||||
await commitAndPush(workspace, spawn);
|
await expect(commitAndPush(workspace)).resolves.not.toThrow();
|
||||||
|
|
||||||
const networkOps = ['fetch', 'push', 'clone'];
|
|
||||||
const networkCalls = spawn.calls.filter(c => networkOps.includes(c.args[0]));
|
|
||||||
assert.ok(networkCalls.length > 0, 'expected at least one network git call');
|
|
||||||
|
|
||||||
for (const { args, opts } of networkCalls) {
|
|
||||||
assert.ok(opts?.env?.GIT_ASKPASS, `GIT_ASKPASS missing for git ${args[0]}`);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
it('cleans up askpass script after successful run', async () => {
|
|
||||||
await commitAndPush(workspace, makeSpawn());
|
|
||||||
const leftover = fs.readdirSync(workspace).filter(f => f.endsWith('.git-askpass.sh'));
|
|
||||||
assert.equal(leftover.length, 0, 'askpass script was not cleaned up');
|
|
||||||
});
|
|
||||||
|
|
||||||
it('cleans up askpass script even when git fails', async () => {
|
|
||||||
const failSpawn = () => ({ status: 1, stdout: '', stderr: 'fatal: error', error: null });
|
|
||||||
await commitAndPush(workspace, failSpawn);
|
|
||||||
const leftover = fs.readdirSync(workspace).filter(f => f.endsWith('.git-askpass.sh'));
|
|
||||||
assert.equal(leftover.length, 0, 'askpass script was not cleaned up after failure');
|
|
||||||
});
|
|
||||||
|
|
||||||
it('skips commit when status shows no changes', async () => {
|
|
||||||
const spawn = makeSpawn({ status: () => ({ status: 0, stdout: '', stderr: '', error: null }) });
|
|
||||||
await commitAndPush(workspace, spawn);
|
|
||||||
const commitCalled = spawn.calls.some(c => c.args[0] === 'commit');
|
|
||||||
assert.equal(commitCalled, false, 'commit should not run when there are no changes');
|
|
||||||
});
|
|
||||||
|
|
||||||
it('does not throw when git command fails', async () => {
|
|
||||||
const failSpawn = () => ({ status: 1, stdout: '', stderr: 'fatal: error', error: null });
|
|
||||||
await assert.doesNotReject(() => commitAndPush(workspace, failSpawn));
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -2,9 +2,6 @@
|
|||||||
"name": "ai-code-review",
|
"name": "ai-code-review",
|
||||||
"version": "1.0.0",
|
"version": "1.0.0",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"scripts": {
|
|
||||||
"test": "node --test app/git.test.js"
|
|
||||||
},
|
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"axios": "^1.6.7",
|
"axios": "^1.6.7",
|
||||||
"js-yaml": "^4.1.0",
|
"js-yaml": "^4.1.0",
|
||||||
|
|||||||
Reference in New Issue
Block a user