feat(notifications): dispatch Gitea webhooks through the notification service

A guild can reroute a notification type to its own channel from SQLite, with the environment values as fallback, without editing the configuration or restarting the bot.
This commit is contained in:
BlackAngelTV committed 2026-09-15 14:50:41 +02:00
1 parent 26d7cce05f
commit a5280ce205
4 files changed
+184 -10

No files matched your search

+34 -5
View File
@@ -2,6 +2,9 @@ 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';
const log = createLogger('webhooks');
@@ -20,8 +23,8 @@ function markDelivered(deliveryId, event) {
return result.changes > 0;
}
/** Validates and dispatches a Gitea webhook. Returns the HTTP status code. */
export async function handleGiteaWebhook(_client, headers, rawBody) {
/** 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;
@@ -32,9 +35,35 @@ export async function handleGiteaWebhook(_client, headers, rawBody) {
if (!event || !deliveryId) return 400;
if (!markDelivered(deliveryId, event)) return 200;
const payload = JSON.parse(rawBody.toString('utf8'));
log.info(`Received Gitea event ${event}`, { deliveryId, repository: payload.repository?.full_name });
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 },
});
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));
// TODO(M9-T03..T07): filter events and publish embeds through notification-service.
return 202;
}