Compare commits
22 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 9279050ca9 | |||
| 66d93abe24 | |||
| a4b87f9108 | |||
| 09533ff741 | |||
| e217b18c62 | |||
| 0063f3282f | |||
| 8c3d0d9a6d | |||
| 3849bb2168 | |||
| 379938d6dc | |||
| 5bf39966d0 | |||
| 3509a882e1 | |||
| 1d2e8236de | |||
| d8423c74b1 | |||
| 94e974b5dc | |||
| a9a0b43ea5 | |||
| aa8234b5c7 | |||
| b0f2d45c11 | |||
| 3fd9a7e13d | |||
| 39cc5c932c | |||
| 255adbabe4 | |||
| a10fc8f176 | |||
| 9b39908394 |
@@ -8,25 +8,11 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"level": "critical",
|
"level": "critical",
|
||||||
"role": "Leo",
|
"role": "Maya",
|
||||||
"location": "app/git.js:1",
|
"location": "app/git.js:1",
|
||||||
"suggestion": "缺少對 commitAndPush 函數的單元測試,應該為其添加測試以確保其正確性。",
|
"suggestion": "缺少對 commitAndPush 函數的單元測試,應該為其添加測試以確保其正確性。",
|
||||||
"is_new": true
|
"is_new": true
|
||||||
},
|
},
|
||||||
{
|
|
||||||
"level": "warning",
|
|
||||||
"role": "Leo",
|
|
||||||
"location": "app/git.js:11",
|
|
||||||
"suggestion": "建議將 git 函式的實作細節封裝到一個獨立的模組中,以提高模組化和可重用性。",
|
|
||||||
"is_new": true
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"level": "warning",
|
|
||||||
"role": "Leo",
|
|
||||||
"location": "app/git.js:41",
|
|
||||||
"suggestion": "建議在 try-catch 區塊中增加更詳細的錯誤處理,以便於未來的除錯和維護。",
|
|
||||||
"is_new": true
|
|
||||||
},
|
|
||||||
{
|
{
|
||||||
"level": "warning",
|
"level": "warning",
|
||||||
"role": "Leo",
|
"role": "Leo",
|
||||||
@@ -58,15 +44,8 @@
|
|||||||
{
|
{
|
||||||
"level": "info",
|
"level": "info",
|
||||||
"role": "Leo",
|
"role": "Leo",
|
||||||
"location": "app/git.js:5",
|
"location": ".gitea/workflows/review.yaml:5",
|
||||||
"suggestion": "建議為函式添加 JSDoc 註解,以提高文件完整性和可讀性。",
|
"suggestion": "建議在 'branches-ignore' 前加上空行,以提高可讀性。",
|
||||||
"is_new": true
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"level": "info",
|
|
||||||
"role": "Leo",
|
|
||||||
"location": "app/git.js:10",
|
|
||||||
"suggestion": "建議將常數如 'repo' 提取為變數,以提高可讀性和可維護性。",
|
|
||||||
"is_new": true
|
"is_new": true
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -75,26 +54,5 @@
|
|||||||
"location": "app/git.js:45",
|
"location": "app/git.js:45",
|
||||||
"suggestion": "考慮使用 async/await 來處理 fs.copyFileSync,以提高可讀性和錯誤處理能力。",
|
"suggestion": "考慮使用 async/await 來處理 fs.copyFileSync,以提高可讀性和錯誤處理能力。",
|
||||||
"is_new": true
|
"is_new": true
|
||||||
},
|
|
||||||
{
|
|
||||||
"level": "info",
|
|
||||||
"role": "Leo",
|
|
||||||
"location": ".gitea/workflows/review.yaml:5",
|
|
||||||
"suggestion": "建議在 'branches-ignore' 前加上空行,以提高可讀性。",
|
|
||||||
"is_new": true
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"level": "info",
|
|
||||||
"role": "Maya",
|
|
||||||
"location": "app/git.js:11",
|
|
||||||
"suggestion": "建議為 git 函數添加測試,以確保其在不同參數下的行為正確。",
|
|
||||||
"is_new": true
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"level": "info",
|
|
||||||
"role": "Maya",
|
|
||||||
"location": "app/git.js:11",
|
|
||||||
"suggestion": "建議對 repoDir 的存在性檢查進行單元測試,以確保在不存在時能正確執行 clone 操作。",
|
|
||||||
"is_new": true
|
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
name: AI
|
name: AI
|
||||||
concurrency:
|
concurrency:
|
||||||
group: ${{ github.workflow }}-${{ github.ref }}
|
group: ${{ github.workflow }}-${{ github.head_ref }}
|
||||||
cancel-in-progress: true
|
cancel-in-progress: true
|
||||||
on:
|
on:
|
||||||
pull_request:
|
pull_request:
|
||||||
|
|||||||
+20
-5
@@ -1,7 +1,7 @@
|
|||||||
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 git(args, cwd) {
|
function git(args, cwd) {
|
||||||
const result = spawnSync('git', args, { cwd, encoding: 'utf8' });
|
const result = spawnSync('git', args, { cwd, encoding: 'utf8' });
|
||||||
@@ -11,21 +11,36 @@ function git(args, cwd) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export async function commitAndPush(workspace) {
|
export async function commitAndPush(workspace) {
|
||||||
const remoteUrl = GITEA_SERVER_URL.replace(/\/$/, '')
|
const remoteUrl = GITEA_SERVER_URL.replace(/\/$/, '') + `/${GITEA_REPOSITORY}.git`;
|
||||||
.replace('https://', `https://${GITEA_TOKEN}@`)
|
|
||||||
.replace('http://', `http://${GITEA_TOKEN}@`) + `/${GITEA_REPOSITORY}.git`;
|
|
||||||
|
|
||||||
const repoDir = path.join(workspace, 'repo');
|
const repoDir = path.join(workspace, 'repo');
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if (!fs.existsSync(repoDir)) {
|
if (!fs.existsSync(repoDir)) {
|
||||||
git(['clone', '--depth=1', '--branch', PR_HEAD_BRANCH, remoteUrl, repoDir], workspace);
|
// 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
|
// 將 findings.json 從 workspace 複製到 clone 的 repo
|
||||||
const srcFindings = path.join(workspace, FINDINGS_PATH);
|
const srcFindings = path.join(workspace, FINDINGS_PATH);
|
||||||
|
|||||||
@@ -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