Update server.mjs
This commit is contained in:
+11
-16
@@ -1,5 +1,4 @@
|
||||
import http from 'node:http';
|
||||
import crypto from 'node:crypto';
|
||||
import { execFile } from 'node:child_process';
|
||||
import { promisify } from 'node:util';
|
||||
import fs from 'node:fs/promises';
|
||||
@@ -7,7 +6,8 @@ import os from 'node:os';
|
||||
import path from 'node:path';
|
||||
|
||||
const run = promisify(execFile);
|
||||
const { GITEA_URL, GITEA_TOKEN, WEBHOOK_SECRET, OWNER_LOGIN = 'selimaj-dev', PORT = 3000 } = process.env;
|
||||
const { GITEA_URL, GITEA_TOKEN } = process.env;
|
||||
const OWNER = 'selimaj-dev';
|
||||
|
||||
const gitEnv = {
|
||||
...process.env,
|
||||
@@ -17,7 +17,6 @@ const gitEnv = {
|
||||
};
|
||||
const claudeEnv = { ...process.env };
|
||||
delete claudeEnv.GITEA_TOKEN;
|
||||
delete claudeEnv.WEBHOOK_SECRET;
|
||||
|
||||
const api = async (method, p, body) => {
|
||||
const r = await fetch(`${GITEA_URL}/api/v1${p}`, {
|
||||
@@ -29,7 +28,6 @@ const api = async (method, p, body) => {
|
||||
return r.status === 204 ? null : r.json();
|
||||
};
|
||||
const git = (dir, ...args) => run('git', args, { cwd: dir, env: gitEnv });
|
||||
|
||||
const busy = new Set();
|
||||
|
||||
async function handle(ev) {
|
||||
@@ -102,17 +100,14 @@ http.createServer((req, res) => {
|
||||
const chunks = [];
|
||||
req.on('data', (c) => chunks.push(c));
|
||||
req.on('end', () => {
|
||||
const raw = Buffer.concat(chunks);
|
||||
const sig = req.headers['x-gitea-signature'] || '';
|
||||
const want = crypto.createHmac('sha256', WEBHOOK_SECRET).update(raw).digest('hex');
|
||||
const ok = sig.length === want.length && crypto.timingSafeEqual(Buffer.from(sig), Buffer.from(want));
|
||||
if (!ok) { res.writeHead(401).end(); return; }
|
||||
res.writeHead(200).end('ok');
|
||||
if (req.headers['x-gitea-event'] !== 'issue_comment') return;
|
||||
const ev = JSON.parse(raw);
|
||||
if (ev.action !== 'created') return;
|
||||
if (ev.comment.user.login !== OWNER_LOGIN) return;
|
||||
if (!ev.comment.body.includes('@claude')) return;
|
||||
handle(ev);
|
||||
try {
|
||||
if (req.headers['x-gitea-event'] !== 'issue_comment') return;
|
||||
const ev = JSON.parse(Buffer.concat(chunks));
|
||||
if (ev.action !== 'created') return;
|
||||
if (ev.comment.user.login !== OWNER) return;
|
||||
if (!ev.comment.body.includes('@claude')) return;
|
||||
handle(ev);
|
||||
} catch (e) { console.error(e.message); }
|
||||
});
|
||||
}).listen(PORT);
|
||||
}).listen(3000);
|
||||
Reference in New Issue
Block a user