Compare commits
29 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 9279050ca9 | |||
| 66d93abe24 | |||
| a4b87f9108 | |||
| 09533ff741 | |||
| e217b18c62 | |||
| cd0ced1b7f | |||
| 65cf45c558 | |||
| 09c78835e7 | |||
| ec05ce7869 | |||
| 323be94a72 | |||
| 0063f3282f | |||
| d7336dbe6c | |||
| c1f8aa3c72 | |||
| 8c3d0d9a6d | |||
| 3849bb2168 | |||
| 379938d6dc | |||
| 5bf39966d0 | |||
| 3509a882e1 | |||
| 1d2e8236de | |||
| d8423c74b1 | |||
| 94e974b5dc | |||
| a9a0b43ea5 | |||
| aa8234b5c7 | |||
| b0f2d45c11 | |||
| 3fd9a7e13d | |||
| 39cc5c932c | |||
| 255adbabe4 | |||
| a10fc8f176 | |||
| 9b39908394 |
@@ -0,0 +1,58 @@
|
|||||||
|
[
|
||||||
|
{
|
||||||
|
"level": "critical",
|
||||||
|
"role": "Leo",
|
||||||
|
"location": "app/git.js:11",
|
||||||
|
"suggestion": "GITEA_TOKEN 直接嵌入 URL 中,可能導致憑證洩漏。建議使用環境變數或安全的憑證管理方式來處理敏感資訊。",
|
||||||
|
"is_new": true
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"level": "critical",
|
||||||
|
"role": "Maya",
|
||||||
|
"location": "app/git.js:1",
|
||||||
|
"suggestion": "缺少對 commitAndPush 函數的單元測試,應該為其添加測試以確保其正確性。",
|
||||||
|
"is_new": true
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"level": "warning",
|
||||||
|
"role": "Leo",
|
||||||
|
"location": "app/git.js:25",
|
||||||
|
"suggestion": "在使用 fs.existsSync 檢查目錄是否存在時,應考慮使用非同步方法以避免阻塞事件循環。",
|
||||||
|
"is_new": true
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"level": "warning",
|
||||||
|
"role": "Leo",
|
||||||
|
"location": "app/git.js:29",
|
||||||
|
"suggestion": "在 git clone 時使用 --depth=1 可能會導致未來需要完整歷史紀錄時的性能問題,建議根據實際需求調整。",
|
||||||
|
"is_new": true
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"level": "warning",
|
||||||
|
"role": "Leo",
|
||||||
|
"location": "app/git.js:11",
|
||||||
|
"suggestion": "在使用 fs.copyFileSync 時,未檢查目標檔案是否存在,可能會覆蓋重要資料。建議在複製之前檢查檔案是否存在。",
|
||||||
|
"is_new": true
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"level": "warning",
|
||||||
|
"role": "Leo",
|
||||||
|
"location": "app/git.js:11",
|
||||||
|
"suggestion": "在 commitAndPush 函數中,對於 git 操作的錯誤處理不夠完善,應該添加更多的測試來驗證不同情況下的行為。",
|
||||||
|
"is_new": true
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"level": "info",
|
||||||
|
"role": "Leo",
|
||||||
|
"location": ".gitea/workflows/review.yaml:5",
|
||||||
|
"suggestion": "建議在 'branches-ignore' 前加上空行,以提高可讀性。",
|
||||||
|
"is_new": true
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"level": "info",
|
||||||
|
"role": "Leo",
|
||||||
|
"location": "app/git.js:45",
|
||||||
|
"suggestion": "考慮使用 async/await 來處理 fs.copyFileSync,以提高可讀性和錯誤處理能力。",
|
||||||
|
"is_new": true
|
||||||
|
}
|
||||||
|
]
|
||||||
@@ -1,6 +1,11 @@
|
|||||||
name: AI
|
name: AI
|
||||||
|
concurrency:
|
||||||
|
group: ${{ github.workflow }}-${{ github.head_ref }}
|
||||||
|
cancel-in-progress: true
|
||||||
on:
|
on:
|
||||||
pull_request:
|
pull_request:
|
||||||
|
branches-ignore:
|
||||||
|
- master
|
||||||
types: [opened, synchronize]
|
types: [opened, synchronize]
|
||||||
jobs:
|
jobs:
|
||||||
version:
|
version:
|
||||||
|
|||||||
+34
-5
@@ -1,5 +1,7 @@
|
|||||||
import { spawnSync } from 'child_process';
|
import { spawnSync } from 'child_process';
|
||||||
import { GITEA_SERVER_URL, GITEA_REPOSITORY, GITEA_TOKEN, PR_HEAD_BRANCH, FINDINGS_PATH } from './config.js';
|
import fs from 'fs';
|
||||||
|
import path from 'path';
|
||||||
|
import { GITEA_SERVER_URL, GITEA_REPOSITORY, PR_HEAD_BRANCH, FINDINGS_PATH } from './config.js';
|
||||||
|
|
||||||
function git(args, cwd) {
|
function git(args, cwd) {
|
||||||
const result = spawnSync('git', args, { cwd, encoding: 'utf8' });
|
const result = spawnSync('git', args, { cwd, encoding: 'utf8' });
|
||||||
@@ -9,16 +11,43 @@ function git(args, cwd) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export async function commitAndPush(workspace) {
|
export async function commitAndPush(workspace) {
|
||||||
const repoDir = workspace;
|
const remoteUrl = GITEA_SERVER_URL.replace(/\/$/, '') + `/${GITEA_REPOSITORY}.git`;
|
||||||
const remoteUrl = GITEA_SERVER_URL.replace(/\/$/, '')
|
|
||||||
.replace('https://', `https://${GITEA_TOKEN}@`)
|
const repoDir = path.join(workspace, 'repo');
|
||||||
.replace('http://', `http://${GITEA_TOKEN}@`) + `/${GITEA_REPOSITORY}.git`;
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
if (!fs.existsSync(repoDir)) {
|
||||||
|
// Use GIT_ASKPASS to provide token for authentication
|
||||||
|
gitWithToken(['clone', '--depth=1', '--branch', PR_HEAD_BRANCH, remoteUrl, repoDir], workspace);
|
||||||
|
}
|
||||||
|
|
||||||
git(['config', 'user.email', 'ai-review[bot]@gitea'], repoDir);
|
git(['config', 'user.email', 'ai-review[bot]@gitea'], repoDir);
|
||||||
git(['config', 'user.name', 'AI Review Bot'], repoDir);
|
git(['config', 'user.name', 'AI Review Bot'], repoDir);
|
||||||
git(['fetch', 'origin', PR_HEAD_BRANCH], repoDir);
|
git(['fetch', 'origin', PR_HEAD_BRANCH], repoDir);
|
||||||
git(['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
|
||||||
|
const srcFindings = path.join(workspace, FINDINGS_PATH);
|
||||||
|
const destFindings = path.join(repoDir, FINDINGS_PATH);
|
||||||
|
fs.mkdirSync(path.dirname(destFindings), { recursive: true });
|
||||||
|
fs.copyFileSync(srcFindings, destFindings);
|
||||||
|
|
||||||
git(['add', FINDINGS_PATH], repoDir);
|
git(['add', FINDINGS_PATH], repoDir);
|
||||||
|
|
||||||
const status = git(['status', '--porcelain'], repoDir);
|
const status = git(['status', '--porcelain'], repoDir);
|
||||||
|
|||||||
@@ -0,0 +1,30 @@
|
|||||||
|
import { commitAndPush } from './git.js';
|
||||||
|
import fs from 'fs';
|
||||||
|
import path from 'path';
|
||||||
|
|
||||||
|
// Mock dependencies and environment
|
||||||
|
jest.mock('fs');
|
||||||
|
jest.mock('child_process', () => ({
|
||||||
|
spawnSync: jest.fn(() => ({ status: 0, stdout: '', stderr: '' }))
|
||||||
|
}));
|
||||||
|
|
||||||
|
describe('commitAndPush', () => {
|
||||||
|
const workspace = '/tmp/workspace';
|
||||||
|
const repoDir = path.join(workspace, 'repo');
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
jest.clearAllMocks();
|
||||||
|
fs.existsSync.mockReturnValue(false);
|
||||||
|
fs.writeFileSync.mockImplementation(() => {});
|
||||||
|
fs.unlinkSync.mockImplementation(() => {});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should clone repo and configure git', async () => {
|
||||||
|
await expect(commitAndPush(workspace)).resolves.not.toThrow();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should not clone if repo exists', async () => {
|
||||||
|
fs.existsSync.mockReturnValue(true);
|
||||||
|
await expect(commitAndPush(workspace)).resolves.not.toThrow();
|
||||||
|
});
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user