import fs from 'fs'; import path from 'path'; 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}`; export async function commitAndPush(workspace) { try { const fullPath = path.join(workspace, FINDINGS_PATH); const content = fs.readFileSync(fullPath, 'utf8'); 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}`); } }