feat: add fallback routes and repository event enrichment

This commit is contained in:
RhenCloud 2026-08-02 23:23:07 +08:00
parent 44b8fead78
commit d419b9c940
No known key found for this signature in database
GPG key ID: A574A617378C4E0B
11 changed files with 104 additions and 8 deletions

View file

@ -124,7 +124,7 @@ export function formatEvent(route: Route, event: WebhookEvent, tr?: Translations
case "discussion_comment":
return formatDiscussionComment(payload, repo, author, t);
case "repository":
return formatRepository(payload, repo, author, t);
return formatRepository(payload, repo, repoUrl, author, t);
case "code_scanning_alert":
return formatCodeScanningAlert(payload, repo, author, t);
case "dependabot_alert":
@ -1231,6 +1231,7 @@ function formatDiscussionComment(
function formatRepository(
payload: Record<string, unknown>,
repo: string | undefined,
repoUrl: string | undefined,
author: { name: string; icon_url?: string; url?: string },
t: T,
): FormattedMessage {
@ -1261,12 +1262,50 @@ function formatRepository(
});
}
// Enrich create / visibility-change notifications with a clickable link and
// basic metadata. repoUrl also makes the embed title a hyperlink for all actions.
const repoData = payload.repository as {
visibility?: string;
fork?: boolean;
description?: string | null;
};
const isCreateOrVisibility =
action === "created" || action === "publicized" || action === "privatized";
if (isCreateOrVisibility) {
if (repoData.visibility) {
fields.push({
name: t("events.repository.visibility"),
value: t("events.repository." + repoData.visibility) ?? repoData.visibility,
inline: true,
});
}
if (repoData.fork) {
fields.push({
name: t("common.repository"),
value: t("events.repository.is_fork"),
inline: true,
});
}
}
const descriptionParts: string[] = [];
if (isCreateOrVisibility && repoUrl) {
descriptionParts.push(`[${t("events.repository.open")}](${repoUrl})`);
}
if (isCreateOrVisibility && repoData.description) {
descriptionParts.push(`> ${repoData.description}`);
}
return {
embeds: [
{
author,
title: t("events.repository.title", { action: al, repo: repo ?? t("common.repository") }),
url: repoUrl,
color: GITHUB_COLORS.repository,
description: descriptionParts.length > 0 ? descriptionParts.join("\n") : undefined,
fields: fields.length > 0 ? fields : undefined,
footer: { text: t("common.footer", { repo: repo ?? t("common.github") }) },
timestamp: new Date().toISOString(),