mirror of
https://github.com/ReCloudStudio/WebHooker.git
synced 2026-09-22 16:11:29 +00:00
- 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'
100 lines
3.3 KiB
TypeScript
100 lines
3.3 KiB
TypeScript
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, "&")
|
|
.replace(/</g, "<")
|
|
.replace(/>/g, ">")
|
|
.replace(/"/g, """);
|
|
}
|
|
|
|
function mdToHtml(s: string): string {
|
|
let out = esc(s);
|
|
out = out.replace(
|
|
/\[([^\]]+)\]\(([^)]+)\)/g,
|
|
(_m, label, url) => `<a href="${esc(url)}">${label}</a>`,
|
|
);
|
|
out = out.replace(/\*\*([^*]+)\*\*/g, "<b>$1</b>");
|
|
out = out.replace(/`([^`]+)`/g, "<code>$1</code>");
|
|
out = out.replace(/(^|[^*])\*([^*]+)\*/g, "$1<i>$2</i>");
|
|
out = out.replace(/~~([^~]+)~~/g, "<s>$1</s>");
|
|
return out;
|
|
}
|
|
|
|
function formatTimestamp(ts?: string): string {
|
|
if (!ts) return "";
|
|
const d = new Date(ts);
|
|
if (Number.isNaN(d.getTime())) return ts;
|
|
const pad = (n: number): string => String(n).padStart(2, "0");
|
|
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[] = [];
|
|
|
|
// HTML allows partial links, so keep the `{repo}{#number}: {subject}` line
|
|
// intact with only the repo head linked (the subject stays plain text).
|
|
const { head, subject } = splitMessageTitle(message.title);
|
|
const repoUrl = repoUrlFromMessage(message.url);
|
|
const title = subject
|
|
? `<b>${repoUrl ? `<a href="${esc(repoUrl)}">${mdToHtml(head)}</a>` : mdToHtml(head)}: ${mdToHtml(subject)}</b>`
|
|
: message.url
|
|
? `<b><a href="${esc(message.url)}">${mdToHtml(message.title)}</a></b>`
|
|
: `<b>${mdToHtml(message.title)}</b>`;
|
|
parts.push(title);
|
|
|
|
if (message.author) {
|
|
const name = mdToHtml(message.author.name);
|
|
const author = message.author.url ? `<a href="${esc(message.author.url)}">${name}</a>` : name;
|
|
parts.push(`👤 ${author}`);
|
|
}
|
|
|
|
if (message.description) {
|
|
parts.push(mdToHtml(message.description));
|
|
}
|
|
|
|
for (const field of message.fields ?? []) {
|
|
parts.push(`<b>${mdToHtml(field.name)}</b>: ${mdToHtml(field.value)}`);
|
|
}
|
|
|
|
const meta: string[] = [];
|
|
if (message.footer) meta.push(esc(message.footer));
|
|
const ts = formatTimestamp(message.timestamp);
|
|
if (ts) meta.push(ts);
|
|
if (meta.length > 0) {
|
|
parts.push(`<i>${meta.join(" · ")}</i>`);
|
|
}
|
|
|
|
return capHtml(parts.join("\n"), MAX_TEXT);
|
|
}
|