Reading a page and wondering whether it is still true is a poor way to watch a deployment, and polling every page on a timer costs far more than it returns. The server keeps one `text/event-stream` per open tab and publishes on it: a write once it has actually succeeded, tagged with the page it concerns, a deployment when it starts and when it ends, an incoming Gitea webhook, plus a tick and a heartbeat that stops proxies closing an idle stream. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
73 lines
2.7 KiB
JavaScript
73 lines
2.7 KiB
JavaScript
import { createHmac, timingSafeEqual } from 'node:crypto';
|
|
import { env } from '../config/env.js';
|
|
import { createLogger } from '../core/logger.js';
|
|
import { getDatabase } from '../database/index.js';
|
|
import { logEvent } from '../database/repositories/event-log-repository.js';
|
|
import { buildGiteaNotification } from '../services/notifications/gitea-notifications.js';
|
|
import { notify } from '../services/notifications/notification-service.js';
|
|
import { broadcast } from '../web/events.js';
|
|
|
|
const log = createLogger('webhooks');
|
|
|
|
export function isValidSignature(rawBody, signature, secret = env.webhook.secret) {
|
|
if (!secret || !signature) return false;
|
|
const expected = Buffer.from(createHmac('sha256', secret).update(rawBody).digest('hex'));
|
|
const received = Buffer.from(signature);
|
|
return expected.length === received.length && timingSafeEqual(expected, received);
|
|
}
|
|
|
|
/** Returns true the first time a delivery id is seen (M9-T08). */
|
|
function markDelivered(deliveryId, event) {
|
|
const result = getDatabase()
|
|
.prepare('INSERT OR IGNORE INTO webhook_deliveries (delivery_id, event) VALUES (?, ?)')
|
|
.run(deliveryId, event);
|
|
return result.changes > 0;
|
|
}
|
|
|
|
/** Validates, deduplicates and publishes a Gitea webhook. Returns the HTTP status code. */
|
|
export async function handleGiteaWebhook(client, headers, rawBody) {
|
|
if (!isValidSignature(rawBody, headers['x-gitea-signature'])) {
|
|
log.warn('Rejected webhook with invalid signature');
|
|
return 401;
|
|
}
|
|
|
|
const event = headers['x-gitea-event'];
|
|
const deliveryId = headers['x-gitea-delivery'];
|
|
if (!event || !deliveryId) return 400;
|
|
if (!markDelivered(deliveryId, event)) return 200;
|
|
|
|
let payload;
|
|
try {
|
|
payload = JSON.parse(rawBody.toString('utf8'));
|
|
} catch {
|
|
log.warn('Rejected webhook with invalid JSON body', { deliveryId, event });
|
|
return 400;
|
|
}
|
|
|
|
log.info(`Received Gitea event ${event}`, {
|
|
deliveryId,
|
|
repository: payload.repository?.full_name,
|
|
});
|
|
|
|
const notification = buildGiteaNotification(event, payload);
|
|
if (!notification) {
|
|
log.debug(`No notification mapping for Gitea event ${event}`);
|
|
return 202;
|
|
}
|
|
|
|
logEvent({
|
|
guildId: env.discord.guildId || null,
|
|
type: `gitea.${event}`,
|
|
payload: { deliveryId, type: notification.type },
|
|
});
|
|
|
|
broadcast('gitea', { event, repository: payload.repository?.full_name ?? null });
|
|
|
|
await notify(client, notification.type, {
|
|
embeds: [notification.embed],
|
|
content: payload.repository?.full_name ? `**${payload.repository.full_name}**` : undefined,
|
|
}).catch((error) => log.error('Could not publish the Gitea notification', error));
|
|
|
|
return 202;
|
|
}
|