// Baut aus einem Gitea-Push ein hübsches Embed und postet es in den Commit-Kanal import { EmbedBuilder } from 'discord.js'; import { config } from '../config.js'; const GITEA_GREEN = 0x609926; const MAX_COMMITS_SHOWN = 10; /** Erste Zeile der Commit-Message, auf maxLen gekürzt */ function firstLine(message, maxLen = 72) { const line = message.split('\n')[0].trim(); return line.length > maxLen ? `${line.slice(0, maxLen - 1)}…` : line; } /** * Push-Embed in den Commit-Kanal posten. * @param {import('discord.js').Client} client * @param {{ repo: string, repoUrl: string, branch: string, compareUrl: string|null, * pusherName: string, pusherAvatar: string|null, * commits: Array<{sha: string, message: string, url: string, author_name: string}> }} push */ export async function postPushEmbed(client, push) { const channel = await client.channels.fetch(config.commitChannelId); if (!channel?.isTextBased()) { throw new Error(`Commit-Kanal ${config.commitChannelId} nicht gefunden oder kein Textkanal`); } const count = push.commits.length; const lines = push.commits.slice(0, MAX_COMMITS_SHOWN).map( (c) => `[\`${c.sha.slice(0, 7)}\`](${c.url}) ${firstLine(c.message)} — ${c.author_name}` ); if (count > MAX_COMMITS_SHOWN) { lines.push(`… und ${count - MAX_COMMITS_SHOWN} weitere`); } const embed = new EmbedBuilder() .setColor(GITEA_GREEN) .setAuthor({ name: push.pusherName, iconURL: push.pusherAvatar ?? undefined }) .setTitle(`📦 ${count} ${count === 1 ? 'neuer Commit' : 'neue Commits'} in ${push.repo}`) .setURL(push.compareUrl || push.repoUrl) .setDescription(lines.join('\n')) .setFooter({ text: `Branch: ${push.branch}` }) .setTimestamp(); await channel.send({ embeds: [embed] }); }