Compare commits
9 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| fe2a513fbb | |||
| 2193bdd4d6 | |||
| af51ffacee | |||
| d9c55ca347 | |||
| 21fb9c1d94 | |||
| 607c9b82ea | |||
| 8acea007e7 | |||
| 953951145f | |||
| 1576e783fb |
+1
-5
@@ -1,8 +1,4 @@
|
||||
FROM node:20-slim
|
||||
|
||||
RUN apt-get update && apt-get install -y --no-install-recommends \
|
||||
git \
|
||||
&& rm -rf /var/lib/apt/lists/*
|
||||
FROM node:20
|
||||
|
||||
WORKDIR /action
|
||||
|
||||
|
||||
@@ -27,7 +27,7 @@
|
||||
|
||||
---
|
||||
|
||||
|
||||
每個階段都會加上明確的 log,並確保即使部分功能未完成也能降級執行、不會中斷 pipeline。
|
||||
|
||||
每次執行後請貼 log,我會協助 debug。
|
||||
|
||||
每次執行後請貼 log,我會協助 debug。
|
||||
+37
-13
@@ -1,23 +1,47 @@
|
||||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
import { commitFile } from './gitea.js';
|
||||
import { FINDINGS_PATH } from './config.js';
|
||||
import axios from 'axios';
|
||||
import https from 'https';
|
||||
import { GITEA_TOKEN, GITEA_SERVER_URL, GITEA_REPOSITORY, PR_HEAD_BRANCH, FINDINGS_PATH } from './config.js';
|
||||
|
||||
const httpsAgent = new https.Agent({ rejectUnauthorized: false });
|
||||
const headers = () => ({ Authorization: `token ${GITEA_TOKEN}`, 'Content-Type': 'application/json' });
|
||||
const api = (p) => `${GITEA_SERVER_URL.replace(/\/$/, '')}/api/v1${p}`;
|
||||
|
||||
/**
|
||||
* 透過 Gitea API 將 findings.json push 到來源分支(不需要 git binary)
|
||||
*/
|
||||
export async function commitAndPush(workspace) {
|
||||
try {
|
||||
const fullPath = path.join(workspace, FINDINGS_PATH);
|
||||
const content = fs.readFileSync(fullPath, 'utf8');
|
||||
console.log(` [debug] FINDINGS_PATH=${FINDINGS_PATH} branch=${process.env.PR_HEAD_BRANCH} token=${process.env.GITEA_TOKEN ? '***' : 'EMPTY'}`);
|
||||
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}`);
|
||||
const encoded = Buffer.from(content).toString('base64');
|
||||
const url = api(`/repos/${GITEA_REPOSITORY}/contents/${FINDINGS_PATH}`);
|
||||
|
||||
// 取得現有檔案 SHA(若存在)
|
||||
let sha;
|
||||
try {
|
||||
const res = await axios.get(`${url}?ref=${encodeURIComponent(PR_HEAD_BRANCH)}`, { headers: headers(), httpsAgent, timeout: 15000 });
|
||||
sha = res.data.sha;
|
||||
} catch {
|
||||
sha = undefined;
|
||||
}
|
||||
|
||||
const payload = JSON.stringify({
|
||||
message: 'chore: update ai-review findings [skip ci]',
|
||||
content: encoded,
|
||||
branch: PR_HEAD_BRANCH,
|
||||
...(sha ? { sha } : {}),
|
||||
});
|
||||
|
||||
const resp = await axios.request({
|
||||
method: sha ? 'put' : 'post',
|
||||
url,
|
||||
headers: { ...headers(), 'Content-Type': 'application/json' },
|
||||
httpsAgent,
|
||||
timeout: 30000,
|
||||
data: payload,
|
||||
});
|
||||
|
||||
const commitHash = resp.data.commit?.sha?.slice(0, 7) || 'unknown';
|
||||
console.log(` ✅ persisted findings commit=${commitHash} push=${PR_HEAD_BRANCH}`);
|
||||
} catch (e) {
|
||||
const detail = e.response?.data ? JSON.stringify(e.response.data) : e.message;
|
||||
console.log(` ⚠️ Runner failed: commit/push 失敗: ${e.response?.status || ''} ${detail}`);
|
||||
|
||||
+1
-38
@@ -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,40 +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;
|
||||
console.log(` [debug] 取得現有檔案 SHA=${sha}`);
|
||||
} catch (e) {
|
||||
console.log(` [debug] 檔案不存在,將建立新檔案: ${e.response?.status || e.message}`);
|
||||
sha = undefined;
|
||||
}
|
||||
|
||||
const payload = {
|
||||
message,
|
||||
content: encoded,
|
||||
branch: PR_HEAD_BRANCH,
|
||||
...(sha ? { sha } : {}),
|
||||
};
|
||||
|
||||
console.log(` [debug] ${sha ? 'PUT' : 'POST'} ${url} branch=${PR_HEAD_BRANCH}`);
|
||||
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