From 710cd7308e1e07edb86d68f3afe089f60873fca8 Mon Sep 17 00:00:00 2001 From: Jeffery Date: Mon, 11 May 2026 09:57:49 +0000 Subject: [PATCH] =?UTF-8?q?fix:=20=E6=94=B9=E7=94=A8=20Gitea=20API=20commi?= =?UTF-8?q?t=20findings.json=EF=BC=8C=E4=B8=8D=E4=BE=9D=E8=B3=B4=20git=20b?= =?UTF-8?q?inary?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/git.js | 45 +++++++++++++++------------------------------ app/gitea.js | 36 +++++++++++++++++++++++++++++++++++- 2 files changed, 50 insertions(+), 31 deletions(-) diff --git a/app/git.js b/app/git.js index ed80d20..7727d93 100644 --- a/app/git.js +++ b/app/git.js @@ -1,37 +1,22 @@ -import { spawnSync } from 'child_process'; -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); - return (result.stdout || '').trim(); -} +import fs from 'fs'; +import path from 'path'; +import { commitFile } from './gitea.js'; +import { FINDINGS_PATH } from './config.js'; /** - * Commit findings.json 並 push 到 PR 來源分支 + * 透過 Gitea API 將 findings.json push 到來源分支(不需要 git binary) */ -export function commitAndPush(workspace) { - const repoDir = `${workspace}/${GITEA_REPOSITORY}`; - const remoteUrl = GITEA_SERVER_URL.replace(/\/$/, '').replace('https://', `https://${GITEA_TOKEN}@`) + `/${GITEA_REPOSITORY}.git`; - +export async function commitAndPush(workspace) { try { - 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); - 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}`); + 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}`); } catch (e) { console.log(` ⚠️ Runner failed: commit/push 失敗: ${e.message}`); } diff --git a/app/gitea.js b/app/gitea.js index f8b4216..e30b1b1 100644 --- a/app/gitea.js +++ b/app/gitea.js @@ -1,6 +1,6 @@ import axios from 'axios'; import https from 'https'; -import { GITEA_TOKEN, GITEA_SERVER_URL, GITEA_REPOSITORY, PR_NUMBER } from './config.js'; +import { GITEA_TOKEN, GITEA_SERVER_URL, GITEA_REPOSITORY, PR_NUMBER, PR_HEAD_BRANCH } from './config.js'; const httpsAgent = new https.Agent({ rejectUnauthorized: false }); const headers = () => ({ Authorization: `token ${GITEA_TOKEN}`, 'Content-Type': 'application/json' }); @@ -15,3 +15,37 @@ 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; +}