Files
code-review/app/gitea.js
T

125 lines
4.7 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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';
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/',
'.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,
});
const message = extractCommitMessage(resp.data);
console.log(` 🔎 bot-check: commit api sha=${sha} keys=${Object.keys(resp.data || {}).join(',') || 'empty'} message=${message ? 'found' : 'empty'}`);
return message;
} catch (e) {
console.log(` ⚠️ bot-check: 讀取 commit sha=${sha} 失敗: ${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 || '';
console.log(` 🔎 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) {
console.log(` ⚠️ bot-check: 讀取 branch=${branch} head commit 失敗: ${e.message}`);
return '';
}
}
export async function shouldSkipBotCommit({ sha = PR_HEAD_SHA || process.env.GITHUB_SHA, branch = PR_HEAD_BRANCH } = {}) {
console.log(` 🔎 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) {
console.log(` 🔎 bot-check: sha=${sha} message=${shaMessage ? 'found' : 'empty'} outcome=${getBotReviewOutcome(shaMessage)}`);
if (shaMessage.includes('[ai-review-bot]')) {
console.log(' ✅ bot-check: matched commit sha marker');
return true;
}
} else {
console.log(' 🔎 bot-check: skip sha lookup because sha is empty');
}
const branchMessage = await getBranchHeadCommitMessage(branch);
if (branch) {
console.log(` 🔎 bot-check: branch=${branch} head_message=${branchMessage ? 'found' : 'empty'} outcome=${getBotReviewOutcome(branchMessage)}`);
if (branchMessage.includes('[ai-review-bot]')) {
console.log(' ✅ bot-check: matched branch head marker');
return true;
}
} else {
console.log(' 🔎 bot-check: skip branch lookup because branch is empty');
}
console.log(' ️ bot-check: no [ai-review-bot] marker found');
return false;
}
/**
* 過濾 diff 內容,移除路徑符合 excludePrefixes 的區塊。
* 每個區塊以 "diff --git a/<prefix>" 開頭判斷,使用 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;
}