import axios from 'axios'; import https from 'https'; import { GITEA_TOKEN, GITEA_COMMENT_TOKEN, GITEA_SERVER_URL, GITEA_REPOSITORY, GITEA_SKIP_TLS_VERIFY, PR_NUMBER, PR_HEAD_SHA, PR_HEAD_BRANCH } from './config.js'; import { line, ok, warn } from './log.js'; const httpsAgent = GITEA_SKIP_TLS_VERIFY ? new https.Agent({ rejectUnauthorized: false }) : undefined; const headers = (token = GITEA_TOKEN) => ({ Authorization: `token ${token}`, 'Content-Type': 'application/json' }); const api = (path) => `${GITEA_SERVER_URL.replace(/\/$/, '')}/api/v1${path}`; function extractCommitMessage(payload) { return payload?.message || payload?.commit?.message || payload?.commit?.commit?.message || ''; } export function getBotReviewOutcome(message) { const match = String(message || '').match(/\[ai-review-bot\](?:\[(success|failure)\])?/i); return match?.[1]?.toLowerCase() || 'unknown'; } /** * 取得 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/', '.agents/', '.antigravity/', '.claude/', '.codex/', '.gemini/', '.gitea/', '.github/', 'AGENTS.md', 'ANTIGRAVITY.md', '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, }); const message = extractCommitMessage(resp.data); line(`bot-check commit api: sha=${sha} keys=${Object.keys(resp.data || {}).join(',') || 'empty'} message=${message ? 'found' : 'empty'}`); return message; } catch (e) { warn(`bot-check commit api 失敗: sha=${sha} error=${e.message}`); 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 || ''; line(`bot-check branch api: branch=${branch} keys=${Object.keys(resp.data || {}).join(',') || 'empty'} sha=${sha || 'empty'} message=${extractCommitMessage(resp.data?.commit) ? 'found' : 'empty'}`); return await getCommitMessageBySha(sha); } catch (e) { warn(`bot-check branch api 失敗: branch=${branch} error=${e.message}`); return ''; } } export async function shouldSkipBotCommit({ sha = PR_HEAD_SHA || process.env.GITHUB_SHA, branch = PR_HEAD_BRANCH } = {}) { line(`bot-check start: PR_HEAD_SHA=${PR_HEAD_SHA || 'empty'} GITHUB_SHA=${process.env.GITHUB_SHA || 'empty'} sha=${sha || 'empty'} branch=${branch || 'empty'}`); const shaMessage = await getCommitMessageBySha(sha); if (sha) { line(`bot-check sha: sha=${sha} message=${shaMessage ? 'found' : 'empty'} outcome=${getBotReviewOutcome(shaMessage)}`); if (shaMessage.includes('[ai-review-bot]')) { ok('bot-check matched commit sha marker'); return true; } } else { line('bot-check skip sha lookup because sha is empty'); } const branchMessage = await getBranchHeadCommitMessage(branch); if (branch) { line(`bot-check branch: branch=${branch} head_message=${branchMessage ? 'found' : 'empty'} outcome=${getBotReviewOutcome(branchMessage)}`); if (branchMessage.includes('[ai-review-bot]')) { ok('bot-check matched branch head marker'); return true; } } else { line('bot-check skip branch lookup because branch is empty'); } line('bot-check no [ai-review-bot] marker found'); return false; } /** * 過濾 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(GITEA_COMMENT_TOKEN || GITEA_TOKEN), timeout: 30000, httpsAgent }, ); return resp.data; }