refactor(all): uniformisation
This commit is contained in:
89 files changed
+10215
-10215
No files matched your search
@@ -1,31 +1,31 @@
|
||||
import { GITEA_EVENT_TYPES } from '../../config/constants.js';
|
||||
import { issueEmbed, pullRequestEmbed, pushEmbed, releaseEmbed, refEmbed } from '../gitea/gitea-embeds.js';
|
||||
|
||||
/**
|
||||
* Maps a raw Gitea webhook to `{ type, embed }`, or returns null when the event
|
||||
* should not be published (M9-T03).
|
||||
*/
|
||||
export function buildGiteaNotification(event, payload) {
|
||||
const type = GITEA_EVENT_TYPES[event];
|
||||
if (!type) return null;
|
||||
|
||||
switch (event) {
|
||||
case 'issues':
|
||||
return { type, embed: issueEmbed(payload.issue ?? payload) };
|
||||
case 'pull_request':
|
||||
return { type, embed: pullRequestEmbed(payload.pull_request ?? payload) };
|
||||
case 'push':
|
||||
if (!payload.commits?.length) return null;
|
||||
return { type, embed: pushEmbed(payload) };
|
||||
case 'create':
|
||||
case 'delete':
|
||||
return {
|
||||
type,
|
||||
embed: refEmbed(payload, payload.ref_type && event === 'delete' ? 'deleted' : 'created'),
|
||||
};
|
||||
case 'release':
|
||||
return { type, embed: releaseEmbed(payload.release ?? payload) };
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
}
|
||||
import { GITEA_EVENT_TYPES } from '../../config/constants.js';
|
||||
import { issueEmbed, pullRequestEmbed, pushEmbed, releaseEmbed, refEmbed } from '../gitea/gitea-embeds.js';
|
||||
|
||||
/**
|
||||
* Maps a raw Gitea webhook to `{ type, embed }`, or returns null when the event
|
||||
* should not be published (M9-T03).
|
||||
*/
|
||||
export function buildGiteaNotification(event, payload) {
|
||||
const type = GITEA_EVENT_TYPES[event];
|
||||
if (!type) return null;
|
||||
|
||||
switch (event) {
|
||||
case 'issues':
|
||||
return { type, embed: issueEmbed(payload.issue ?? payload) };
|
||||
case 'pull_request':
|
||||
return { type, embed: pullRequestEmbed(payload.pull_request ?? payload) };
|
||||
case 'push':
|
||||
if (!payload.commits?.length) return null;
|
||||
return { type, embed: pushEmbed(payload) };
|
||||
case 'create':
|
||||
case 'delete':
|
||||
return {
|
||||
type,
|
||||
embed: refEmbed(payload, payload.ref_type && event === 'delete' ? 'deleted' : 'created'),
|
||||
};
|
||||
case 'release':
|
||||
return { type, embed: releaseEmbed(payload.release ?? payload) };
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -1,61 +1,61 @@
|
||||
import { env } from '../../config/env.js';
|
||||
import { createLogger } from '../../core/logger.js';
|
||||
import { getNotificationChannel } from '../../database/repositories/notification-repository.js';
|
||||
import { errorEmbed } from '../../ui/embeds.js';
|
||||
|
||||
const log = createLogger('notifications');
|
||||
|
||||
/** Fallback routing from the environment when the guild has no override (M9-T01). */
|
||||
const defaultChannels = {
|
||||
issues: env.channels.notifIssues,
|
||||
pull_request: env.channels.notifPullRequests,
|
||||
push: env.channels.notifCommits,
|
||||
release: env.channels.notifReleases,
|
||||
errors: env.channels.notifErrors,
|
||||
announcements: env.channels.announcements,
|
||||
community: env.channels.logs,
|
||||
};
|
||||
|
||||
/** SQLite override first, then the environment default (M9-T01). */
|
||||
export function resolveChannelId(type, guildId = env.discord.guildId) {
|
||||
try {
|
||||
if (guildId) {
|
||||
const override = getNotificationChannel(guildId, type);
|
||||
if (override?.channel_id) return override.channel_id;
|
||||
}
|
||||
} catch {
|
||||
// The database is unavailable (tests, early startup): use the environment.
|
||||
}
|
||||
return defaultChannels[type] ?? null;
|
||||
}
|
||||
|
||||
/** Sends a notification payload to the channel configured for its type (M9). */
|
||||
export async function notify(client, type, payload) {
|
||||
const channelId = resolveChannelId(type);
|
||||
if (!channelId) {
|
||||
log.debug(`No channel configured for notification type ${type}`);
|
||||
return null;
|
||||
}
|
||||
|
||||
const channel = await client.channels.fetch(channelId).catch(() => null);
|
||||
if (!channel?.isSendable()) {
|
||||
log.warn(`Notification channel unavailable for ${type}`, { channelId });
|
||||
return null;
|
||||
}
|
||||
return channel.send(payload);
|
||||
}
|
||||
|
||||
/** Reports an unexpected failure in the technical channel (M9-T07). */
|
||||
export async function notifyError(client, error, context = 'application') {
|
||||
log.error(`Notification error (${context})`, error);
|
||||
return notify(client, 'errors', {
|
||||
embeds: [
|
||||
errorEmbed(
|
||||
`Un incident est survenu dans **${context}**. Rien d’inquiétant pour les membres : je continue de fonctionner, mais l’équipe technique gagne à y jeter un œil.`,
|
||||
).addFields({
|
||||
name: 'Détail',
|
||||
value: `\`${String(error?.message ?? error).slice(0, 500)}\``,
|
||||
}),
|
||||
],
|
||||
});
|
||||
}
|
||||
import { env } from '../../config/env.js';
|
||||
import { createLogger } from '../../core/logger.js';
|
||||
import { getNotificationChannel } from '../../database/repositories/notification-repository.js';
|
||||
import { errorEmbed } from '../../ui/embeds.js';
|
||||
|
||||
const log = createLogger('notifications');
|
||||
|
||||
/** Fallback routing from the environment when the guild has no override (M9-T01). */
|
||||
const defaultChannels = {
|
||||
issues: env.channels.notifIssues,
|
||||
pull_request: env.channels.notifPullRequests,
|
||||
push: env.channels.notifCommits,
|
||||
release: env.channels.notifReleases,
|
||||
errors: env.channels.notifErrors,
|
||||
announcements: env.channels.announcements,
|
||||
community: env.channels.logs,
|
||||
};
|
||||
|
||||
/** SQLite override first, then the environment default (M9-T01). */
|
||||
export function resolveChannelId(type, guildId = env.discord.guildId) {
|
||||
try {
|
||||
if (guildId) {
|
||||
const override = getNotificationChannel(guildId, type);
|
||||
if (override?.channel_id) return override.channel_id;
|
||||
}
|
||||
} catch {
|
||||
// The database is unavailable (tests, early startup): use the environment.
|
||||
}
|
||||
return defaultChannels[type] ?? null;
|
||||
}
|
||||
|
||||
/** Sends a notification payload to the channel configured for its type (M9). */
|
||||
export async function notify(client, type, payload) {
|
||||
const channelId = resolveChannelId(type);
|
||||
if (!channelId) {
|
||||
log.debug(`No channel configured for notification type ${type}`);
|
||||
return null;
|
||||
}
|
||||
|
||||
const channel = await client.channels.fetch(channelId).catch(() => null);
|
||||
if (!channel?.isSendable()) {
|
||||
log.warn(`Notification channel unavailable for ${type}`, { channelId });
|
||||
return null;
|
||||
}
|
||||
return channel.send(payload);
|
||||
}
|
||||
|
||||
/** Reports an unexpected failure in the technical channel (M9-T07). */
|
||||
export async function notifyError(client, error, context = 'application') {
|
||||
log.error(`Notification error (${context})`, error);
|
||||
return notify(client, 'errors', {
|
||||
embeds: [
|
||||
errorEmbed(
|
||||
`Un incident est survenu dans **${context}**. Rien d’inquiétant pour les membres : je continue de fonctionner, mais l’équipe technique gagne à y jeter un œil.`,
|
||||
).addFields({
|
||||
name: 'Détail',
|
||||
value: `\`${String(error?.message ?? error).slice(0, 500)}\``,
|
||||
}),
|
||||
],
|
||||
});
|
||||
}
|
||||
Reference in new issue
Block a user