import axios from 'axios'; import https from 'https'; import { GITEA_TOKEN, GITEA_SERVER_URL, GITEA_REPOSITORY, GITEA_SKIP_TLS_VERIFY, PR_NUMBER, PR_HEAD_SHA, PR_HEAD_BRANCH } from './config.js'; const httpsAgent = GITEA_SKIP_TLS_VERIFY ? new https.Agent({ rejectUnauthorized: false }) : undefined; const headers = () => ({ Authorization: `token ${GITEA_TOKEN}`, 'Content-Type': 'application/json' }); const api = (path) => `${GITEA_SERVER_URL.replace(/\/$/, '')}/api/v1${path}`; /** * 取得 PR 的 Git Diff 內容,已自動排除 .gitea/ 資料夾。 */ export async function getPRDiff() { const resp = await axios.get(api(`/repos/${GITEA_REPOSITORY}/pulls/${PR_NUMBER}.diff`), { headers: headers(), timeout: 60000, httpsAgent }); return filterDiff(resp.data, [ '.amazonq/', '.claude/', '.codex/', '.gemini/', '.gitea/', '.github/', 'CLAUDE.md', 'GEMINI.md', 'README.md', 'TODO.md', ]); } export async function getCommitMessageBySha(sha) { if (!sha) return ''; try { const resp = await axios.get(api(`/repos/${GITEA_REPOSITORY}/git/commits/${encodeURIComponent(sha)}`), { headers: headers(), timeout: 30000, httpsAgent, }); return resp.data?.message || ''; } catch { return ''; } } export async function getBranchHeadCommitMessage(branch = PR_HEAD_BRANCH) { if (!branch) return ''; try { const resp = await axios.get(api(`/repos/${GITEA_REPOSITORY}/branches/${encodeURIComponent(branch)}`), { headers: headers(), timeout: 30000, httpsAgent, }); const sha = resp.data?.commit?.id || resp.data?.commit?.sha || ''; return await getCommitMessageBySha(sha); } catch { return ''; } } export async function shouldSkipBotCommit({ sha = PR_HEAD_SHA || process.env.GITHUB_SHA, branch = PR_HEAD_BRANCH } = {}) { const candidates = [ await getCommitMessageBySha(sha), await getBranchHeadCommitMessage(branch), ].filter(Boolean); return candidates.some(message => message.includes('[ai-review-bot]')); } /** * 過濾 diff 內容,移除路徑符合 excludePrefixes 的區塊。 * 每個區塊以 "diff --git a/" 開頭判斷,使用 startsWith 精確比對前綴。 */ export function filterDiff(diff, excludePrefixes) { return diff.split(/(?=^diff --git )/m) .filter(block => !excludePrefixes.some(p => { const prefix = `diff --git a/${p}`; const singleFile = `diff --git a/${p} b/${p}`; return block.startsWith(prefix) || block.startsWith(singleFile); })) .join(''); } 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; }