31 lines
1.3 KiB
JavaScript
31 lines
1.3 KiB
JavaScript
import axios from 'axios';
|
|
import https from 'https';
|
|
import { GITEA_TOKEN, GITEA_SERVER_URL, GITEA_REPOSITORY, GITEA_SKIP_TLS_VERIFY, PR_NUMBER } 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, ['.gitea/']);
|
|
}
|
|
|
|
/**
|
|
* 過濾 diff 內容,移除路徑符合 excludePrefixes 的區塊。
|
|
* 每個區塊以 "diff --git a/<prefix>" 開頭判斷,使用 startsWith 精確比對前綴。
|
|
*/
|
|
export function filterDiff(diff, excludePrefixes) {
|
|
return diff.split(/(?=^diff --git )/m)
|
|
.filter(block => !excludePrefixes.some(p => block.startsWith(`diff --git a/${p}`)))
|
|
.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;
|
|
}
|