fix(formatters): localize PR buttons, cap oversized content, dedupe status logic

- PR merge/close button labels now follow the group language (actions.merge/close)
  instead of hardcoded Chinese
- clamp content to Discord embed limits (title 256, description 4096, field value
  1024, 25 fields) in formatters plus a render-layer safety net; Telegram gets a
  tag-safe 4096-char cap (capHtml closes dangling tags)
- raise commit subject truncation from 72 to 200 chars (MAX_COMMIT_SUBJECT)
- extract workflowStatus/workflowRunStatus/statusColorKey helpers shared by
  check_run/check_suite/workflow_run/workflow_job
- dedupe deployment ref/sha fields via addDeploymentRefFields
- commit_comment without a commit id uses title_plain (no dangling ???????)
- tag pushes now report 'Tag created'; zh push title includes the {ref}
- sender profile link derives from the repo's forge origin instead of github.com
- group webhook log emoji injected via emojiPrefix, removed from locale files
- dispatchEvent accepts preloaded groups (single KV read per webhook)
- webhook 401 logs distinguish 'secret not configured' from 'invalid signature'
This commit is contained in:
RhenCloud 2026-08-14 07:41:19 +08:00
parent e59b10f739
commit a5324fb0ea
No known key found for this signature in database
GPG key ID: A574A617378C4E0B
20 changed files with 425 additions and 136 deletions

View file

@ -1,5 +1,14 @@
import type { FormattedMessage, NeutralActionStyle, NeutralMessage } from "../../types";
import { repoUrlFromMessage, splitMessageTitle } from "../../formatters/helpers";
import {
cap,
MAX_DESCRIPTION,
MAX_FIELDS,
MAX_FIELD_VALUE,
MAX_FOOTER,
MAX_TITLE,
repoUrlFromMessage,
splitMessageTitle,
} from "../../formatters/helpers";
function toStyle(style: NeutralActionStyle): number {
switch (style) {
@ -22,29 +31,37 @@ export function renderNeutralMessage(message: NeutralMessage): FormattedMessage
// rendered as the first line of the description, unlinked.
const { head, subject } = splitMessageTitle(message.title);
const repoUrl = repoUrlFromMessage(message.url);
const description = subject
const rawDescription = subject
? message.description
? `${subject}\n${message.description}`
: subject
: message.description;
: (message.description ?? "");
// Safety net: clamp every embed part to the Discord API limits so a single
// oversized formatter or custom payload can never hard-fail the request.
return {
content,
embeds: [
{
title: head,
title: cap(head, MAX_TITLE),
url: subject ? repoUrl : message.url,
color: message.color,
description,
description: cap(rawDescription, MAX_DESCRIPTION) || undefined,
author: message.author
? {
name: message.author.name,
name: cap(message.author.name, MAX_TITLE),
icon_url: message.author.iconUrl,
url: message.author.url,
}
: undefined,
fields: message.fields,
footer: message.footer ? { text: message.footer } : undefined,
fields: message.fields
?.slice(0, MAX_FIELDS)
.map((f) => ({
name: cap(f.name, MAX_TITLE),
value: cap(f.value, MAX_FIELD_VALUE),
inline: f.inline,
})),
footer: message.footer ? { text: cap(message.footer, MAX_FOOTER) } : undefined,
timestamp: message.timestamp,
},
],

View file

@ -1,6 +1,9 @@
import type { NeutralMessage } from "../../types";
import { repoUrlFromMessage, splitMessageTitle } from "../../formatters/helpers";
/** Telegram caps a single message at 4096 characters (HTML entities included). */
const MAX_TEXT = 4096;
function esc(s: string): string {
return s
.replace(/&/g, "&")
@ -30,6 +33,33 @@ function formatTimestamp(ts?: string): string {
return `${d.getUTCFullYear()}-${pad(d.getUTCMonth() + 1)}-${pad(d.getUTCDate())} ${pad(d.getUTCHours())}:${pad(d.getUTCMinutes())} UTC`;
}
/**
* Truncate Telegram HTML to `max` characters without breaking markup: drops
* an incomplete trailing tag and closes every tag still open at the cut
* point, so parse_mode HTML never rejects the message.
*/
function capHtml(text: string, max: number): string {
if (text.length <= max) return text;
let cut = text.slice(0, max).replace(/<[^>]*$/u, "");
const stack: string[] = [];
const re = /<(\/?)([a-z]+)(?:\s[^>]*)?>/g;
let m: RegExpExecArray | null;
while ((m = re.exec(cut)) !== null) {
const tag = m[2]!;
if (m[1] === "/") {
const open = stack.lastIndexOf(tag);
if (open >= 0) stack.splice(open);
} else {
stack.push(tag);
}
}
const closers = stack
.reverse()
.map((tag) => `</${tag}>`)
.join("");
return `${cut}${closers}`;
}
export function renderNeutralMessage(message: NeutralMessage): string {
const parts: string[] = [];
@ -66,5 +96,5 @@ export function renderNeutralMessage(message: NeutralMessage): string {
parts.push(`<i>${meta.join(" · ")}</i>`);
}
return parts.join("\n");
return capHtml(parts.join("\n"), MAX_TEXT);
}