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'
162 lines
5.8 KiB
TypeScript
162 lines
5.8 KiB
TypeScript
import type { NeutralMessage } from "../types";
|
|
import { t as translate } from "../lib/i18n";
|
|
import type { Translations } from "../lib/i18n";
|
|
|
|
export type T = (key: string, params?: Record<string, string | number>) => string;
|
|
|
|
export function emojiPrefix(emoji: string, show: boolean): string {
|
|
return show ? `${emoji} ` : "";
|
|
}
|
|
|
|
export function makeT(tr?: Translations): T {
|
|
return (key, params) => translate(key, params, undefined, tr);
|
|
}
|
|
|
|
export interface TitleParts {
|
|
/** `{repo}` or `{repo}#{number}` — what embed titles link (the repo). */
|
|
head: string;
|
|
/** Text after `": "`, undefined when the title has no `: ` separator. */
|
|
subject?: string;
|
|
}
|
|
|
|
/**
|
|
* Split a `{repo}{#number}: {subject}` title into the repo head and the
|
|
* subject. Discord embed titles can only link as a whole, so drivers render
|
|
* the head as the linked title and the subject as plain text (description /
|
|
* unlinked remainder) to avoid hyperlinking the whole title.
|
|
*/
|
|
export function splitMessageTitle(title: string): TitleParts {
|
|
const idx = title.indexOf(": ");
|
|
if (idx <= 0) return { head: title };
|
|
return { head: title.slice(0, idx), subject: title.slice(idx + 2) };
|
|
}
|
|
|
|
/**
|
|
* Repository URL derived from an event URL (origin + owner + repo). Returns
|
|
* undefined when there is no URL to derive from — callers then render the
|
|
* title without a link.
|
|
*/
|
|
export function repoUrlFromMessage(url: string | undefined): string | undefined {
|
|
if (!url) return undefined;
|
|
try {
|
|
const parsed = new URL(url);
|
|
const segments = parsed.pathname.split("/").filter(Boolean);
|
|
if (segments.length >= 2) return `${parsed.origin}/${segments[0]}/${segments[1]}`;
|
|
} catch {
|
|
// not a parseable URL — no link
|
|
}
|
|
return undefined;
|
|
}
|
|
|
|
export function buildMessage(
|
|
partial: Omit<Partial<NeutralMessage>, "title"> & { title: string },
|
|
t: T,
|
|
repo?: string,
|
|
): NeutralMessage {
|
|
return {
|
|
...partial,
|
|
footer: partial.footer ?? t("common.footer", { repo: repo ?? t("common.github") }),
|
|
timestamp: partial.timestamp ?? new Date().toISOString(),
|
|
};
|
|
}
|
|
|
|
/**
|
|
* Base URL of the forge repo (e.g. `https://github.com/owner/repo` or a Gitea
|
|
* instance URL). Derived from `repository.html_url` in the payload so it works
|
|
* for any provider; falls back to github.com for legacy payloads.
|
|
*/
|
|
export function repoBaseUrl(payload: Record<string, unknown>, repo?: string): string | undefined {
|
|
const html = (payload.repository as { html_url?: string } | undefined)?.html_url;
|
|
if (html) return html;
|
|
return repo ? `https://github.com/${repo}` : undefined;
|
|
}
|
|
|
|
function encodeRefPath(ref: string): string {
|
|
return ref
|
|
.split("/")
|
|
.map((seg) => encodeURIComponent(seg))
|
|
.join("/");
|
|
}
|
|
|
|
/** Inline code + hyperlink for a commit, e.g. [`abc123d`](.../commit/abc123def456). */
|
|
export function commitLink(baseUrl: string | undefined, sha: string, short?: string): string {
|
|
const label = short ?? sha.slice(0, 7);
|
|
return baseUrl ? `[\`${label}\`](${baseUrl}/commit/${encodeRefPath(sha)})` : `\`${label}\``;
|
|
}
|
|
|
|
/** Inline code + hyperlink for a branch (or bare ref), e.g. [`main`](.../tree/main). */
|
|
export function branchLink(baseUrl: string | undefined, branch: string, label?: string): string {
|
|
const clean = branch.replace("refs/heads/", "").replace("refs/tags/", "");
|
|
const display = label ?? clean;
|
|
return baseUrl ? `[\`${display}\`](${baseUrl}/tree/${encodeRefPath(clean)})` : `\`${display}\``;
|
|
}
|
|
|
|
/** Inline code + hyperlink for a tag, e.g. [`v1.0`](.../releases/tag/v1.0). */
|
|
export function tagLink(baseUrl: string | undefined, tag: string, label?: string): string {
|
|
const clean = tag.replace("refs/tags/", "");
|
|
const display = label ?? clean;
|
|
return baseUrl
|
|
? `[\`${display}\`](${baseUrl}/releases/tag/${encodeRefPath(clean)})`
|
|
: `\`${display}\``;
|
|
}
|
|
|
|
/* ---- Content size limits (mirror the Discord embed limits) ---- */
|
|
export const MAX_TITLE = 256;
|
|
export const MAX_DESCRIPTION = 4096;
|
|
export const MAX_FIELD_VALUE = 1024;
|
|
export const MAX_FIELDS = 25;
|
|
export const MAX_FOOTER = 2048;
|
|
/** First-line commit message length (stays well under the field value budget). */
|
|
export const MAX_COMMIT_SUBJECT = 200;
|
|
|
|
/** Truncate a string to `max` characters. */
|
|
export function cap(text: string, max: number): string {
|
|
return text.length > max ? text.slice(0, max) : text;
|
|
}
|
|
|
|
/**
|
|
* Normalize a check/workflow status: `queued` / `in_progress` keep their
|
|
* value (the latter becomes `running`), anything else falls back to the
|
|
* conclusion and finally `pending`.
|
|
*/
|
|
export function workflowStatus(status?: string, conclusion?: string): string {
|
|
if (status === "queued") return "queued";
|
|
if (status === "in_progress") return "running";
|
|
return conclusion ?? "pending";
|
|
}
|
|
|
|
/** workflow_run events derive their progress from the action, not `status`. */
|
|
export function workflowRunStatus(action?: string, conclusion?: string): string {
|
|
if (action === "in_progress") return "running";
|
|
if (action === "requested") return "queued";
|
|
return conclusion ?? "pending";
|
|
}
|
|
|
|
/** GITHUB_COLORS key for a success/failure/other status. */
|
|
export function statusColorKey(
|
|
prefix: "check_run" | "workflow_run",
|
|
status: string,
|
|
): `${typeof prefix}_${"success" | "failure" | "other"}` {
|
|
return status === "success"
|
|
? `${prefix}_success`
|
|
: status === "failure"
|
|
? `${prefix}_failure`
|
|
: `${prefix}_other`;
|
|
}
|
|
|
|
/**
|
|
* The sender's profile URL on the same forge as the repo (derived from the
|
|
* repo's html_url origin, e.g. `https://github.com/owner/repo` →
|
|
* `https://github.com/login`). Falls back to github.com when the repo URL is
|
|
* unavailable or unparseable.
|
|
*/
|
|
export function senderProfileUrl(repoUrl: string | undefined, login: string): string {
|
|
if (repoUrl) {
|
|
try {
|
|
return `${new URL(repoUrl).origin}/${encodeURIComponent(login)}`;
|
|
} catch {
|
|
// unparseable repo URL — fall through to github.com
|
|
}
|
|
}
|
|
return `https://github.com/${login}`;
|
|
}
|