Compare commits
26 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| c1f8aa3c72 | |||
| 8a28d1f1ef | |||
| d04f4dd2bb | |||
| f5cf5950bd | |||
| eae73092ad | |||
| 69624a542e | |||
| 8aa273b8bd | |||
| 00458d4eb2 | |||
| 894ece033b | |||
| fe2a513fbb | |||
| 2193bdd4d6 | |||
| af51ffacee | |||
| d9c55ca347 | |||
| 21fb9c1d94 | |||
| 607c9b82ea | |||
| 8acea007e7 | |||
| 953951145f | |||
| 1576e783fb | |||
| e017705c64 | |||
| 5f77b83a0f | |||
| da43cb02b0 | |||
| 577a930438 | |||
| 121f66b0b3 | |||
| faa808bb5f | |||
| 07df3ef4a5 | |||
| fc537958ca |
+5
-4
@@ -1,8 +1,9 @@
|
||||
FROM node:20-slim
|
||||
FROM alpine
|
||||
|
||||
RUN apt-get update && apt-get install -y --no-install-recommends \
|
||||
git \
|
||||
&& rm -rf /var/lib/apt/lists/*
|
||||
RUN apk add --no-cache bash nodejs npm git \
|
||||
&& node --version \
|
||||
&& npm --version \
|
||||
&& git --version
|
||||
|
||||
WORKDIR /action
|
||||
|
||||
|
||||
@@ -27,7 +27,7 @@
|
||||
|
||||
---
|
||||
|
||||
|
||||
每個階段都會加上明確的 log,並確保即使部分功能未完成也能降級執行、不會中斷 pipeline。
|
||||
|
||||
每次執行後請貼 log,我會協助 debug。
|
||||
|
||||
每次執行後請貼 log,我會協助 debug。
|
||||
+42
-14
@@ -1,22 +1,50 @@
|
||||
import { spawnSync } from 'child_process';
|
||||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
import { commitFile } from './gitea.js';
|
||||
import { FINDINGS_PATH } from './config.js';
|
||||
import { GITEA_SERVER_URL, GITEA_REPOSITORY, GITEA_TOKEN, PR_HEAD_BRANCH, FINDINGS_PATH } from './config.js';
|
||||
|
||||
function git(args, cwd) {
|
||||
const result = spawnSync('git', args, { cwd, encoding: 'utf8' });
|
||||
if (result.error) throw result.error;
|
||||
if (result.status !== 0) throw new Error((result.stderr || result.stdout || '').trim());
|
||||
return (result.stdout || '').trim();
|
||||
}
|
||||
|
||||
/**
|
||||
* 透過 Gitea API 將 findings.json push 到來源分支(不需要 git binary)
|
||||
*/
|
||||
export async function commitAndPush(workspace) {
|
||||
const remoteUrl = GITEA_SERVER_URL.replace(/\/$/, '')
|
||||
.replace('https://', `https://${GITEA_TOKEN}@`)
|
||||
.replace('http://', `http://${GITEA_TOKEN}@`) + `/${GITEA_REPOSITORY}.git`;
|
||||
|
||||
const repoDir = path.join(workspace, 'repo');
|
||||
|
||||
try {
|
||||
const fullPath = path.join(workspace, FINDINGS_PATH);
|
||||
const content = fs.readFileSync(fullPath, 'utf8');
|
||||
const result = await commitFile(
|
||||
FINDINGS_PATH,
|
||||
content,
|
||||
'chore: update ai-review findings [skip ci]'
|
||||
);
|
||||
const commitHash = result.commit?.sha?.slice(0, 7) || 'unknown';
|
||||
console.log(` ✅ persisted findings commit=${commitHash} push=${process.env.PR_HEAD_BRANCH}`);
|
||||
if (!fs.existsSync(repoDir)) {
|
||||
git(['clone', '--depth=1', '--branch', PR_HEAD_BRANCH, remoteUrl, repoDir], workspace);
|
||||
}
|
||||
|
||||
git(['config', 'user.email', 'ai-review[bot]@gitea'], repoDir);
|
||||
git(['config', 'user.name', 'AI Review Bot'], repoDir);
|
||||
git(['fetch', 'origin', PR_HEAD_BRANCH], repoDir);
|
||||
git(['checkout', PR_HEAD_BRANCH], repoDir);
|
||||
|
||||
// 將 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);
|
||||
|
||||
const status = git(['status', '--porcelain'], repoDir);
|
||||
if (!status) {
|
||||
console.log(' findings.json 無變更,跳過 commit');
|
||||
return;
|
||||
}
|
||||
|
||||
const out = git(['commit', '-m', 'chore: update ai-review findings [skip ci]'], repoDir);
|
||||
const commitHash = out.match(/\[.+ ([a-f0-9]+)\]/)?.[1] || 'unknown';
|
||||
git(['push', remoteUrl, PR_HEAD_BRANCH], repoDir);
|
||||
console.log(` ✅ persisted findings commit=${commitHash} push=${PR_HEAD_BRANCH}`);
|
||||
} catch (e) {
|
||||
console.log(` ⚠️ Runner failed: commit/push 失敗: ${e.message}`);
|
||||
}
|
||||
|
||||
+1
-35
@@ -1,6 +1,6 @@
|
||||
import axios from 'axios';
|
||||
import https from 'https';
|
||||
import { GITEA_TOKEN, GITEA_SERVER_URL, GITEA_REPOSITORY, PR_NUMBER, PR_HEAD_BRANCH } from './config.js';
|
||||
import { GITEA_TOKEN, GITEA_SERVER_URL, GITEA_REPOSITORY, PR_NUMBER } from './config.js';
|
||||
|
||||
const httpsAgent = new https.Agent({ rejectUnauthorized: false });
|
||||
const headers = () => ({ Authorization: `token ${GITEA_TOKEN}`, 'Content-Type': 'application/json' });
|
||||
@@ -15,37 +15,3 @@ export async function postComment(body) {
|
||||
const resp = await axios.post(api(`/repos/${GITEA_REPOSITORY}/issues/${PR_NUMBER}/comments`), { body }, { headers: headers(), timeout: 30000, httpsAgent });
|
||||
return resp.data;
|
||||
}
|
||||
|
||||
/**
|
||||
* 透過 Gitea API 建立或更新檔案(不需要 git binary)
|
||||
*/
|
||||
export async function commitFile(filePath, content, message) {
|
||||
const encoded = Buffer.from(content).toString('base64');
|
||||
const url = api(`/repos/${GITEA_REPOSITORY}/contents/${filePath}`);
|
||||
|
||||
// 先嘗試取得現有檔案的 SHA
|
||||
let sha;
|
||||
try {
|
||||
const existing = await axios.get(`${url}?ref=${PR_HEAD_BRANCH}`, { headers: headers(), httpsAgent, timeout: 15000 });
|
||||
sha = existing.data.sha;
|
||||
} catch {
|
||||
sha = undefined;
|
||||
}
|
||||
|
||||
const payload = {
|
||||
message,
|
||||
content: encoded,
|
||||
branch: PR_HEAD_BRANCH,
|
||||
...(sha ? { sha } : {}),
|
||||
};
|
||||
|
||||
const resp = await axios.request({
|
||||
method: sha ? 'put' : 'post',
|
||||
url,
|
||||
headers: headers(),
|
||||
httpsAgent,
|
||||
timeout: 30000,
|
||||
data: payload,
|
||||
});
|
||||
return resp.data;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user