24 lines
1.1 KiB
JavaScript
24 lines
1.1 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}`;
|
|
|
|
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/']);
|
|
}
|
|
|
|
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;
|
|
}
|