WebHooker/server/lib/formatters/index.ts
RhenCloud a5324fb0ea
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'
2026-08-14 07:41:19 +08:00

109 lines
4.8 KiB
TypeScript

import type { Route, WebhookEvent, NeutralMessage, NeutralAuthor } from "../types";
import type { Translations } from "../lib/i18n";
import { makeT, senderProfileUrl, type T } from "./helpers";
import { formatPush } from "./push";
import { formatPullRequest } from "./pull-request";
import { formatPullRequestReview, formatPullRequestReviewComment } from "./review";
import { formatIssues } from "./issues";
import { formatIssueComment } from "./comments";
import { formatWorkflowRun, formatWorkflowJob } from "./workflow";
import { formatRelease } from "./release";
import { formatCreate, formatDelete } from "./create";
import { formatStar, formatFork } from "./repo";
import { formatCheckRun, formatCheckSuite, formatStatus } from "./check";
import { formatCommitComment } from "./commit-comment";
import { formatDeployment, formatDeploymentStatus } from "./deployment";
import { formatMember } from "./member";
import { formatLabel } from "./label";
import { formatMilestone } from "./milestone";
import { formatDiscussion, formatDiscussionComment } from "./discussion";
import { formatRepository } from "./repository";
import { formatCodeScanningAlert, formatDependabotAlert } from "./security";
import { formatGeneric } from "./generic";
import { formatPing } from "./ping";
import { formatCustom } from "./custom";
export function formatEvent(
route: Route,
event: WebhookEvent,
tr?: Translations,
showEmoji = true,
): NeutralMessage {
const { event: eventType, payload } = event;
const repo = (payload.repository as { full_name?: string })?.full_name;
const sender = (payload.sender as { login?: string })?.login;
const senderAvatar = (payload.sender as { avatar_url?: string })?.avatar_url;
const senderUrl = (payload.sender as { html_url?: string })?.html_url;
const repoUrl = (payload.repository as { html_url?: string })?.html_url;
const t: T = makeT(tr);
const author: NeutralAuthor = {
name: sender ?? t("common.unknown"),
iconUrl: senderAvatar,
url: senderUrl ?? (sender ? senderProfileUrl(repoUrl, sender) : undefined),
};
switch (eventType) {
case "push":
return formatPush(payload, repo, author, t, showEmoji);
case "pull_request":
return formatPullRequest(payload, repo, author, t, showEmoji);
case "pull_request_review":
return formatPullRequestReview(payload, repo, author, t, showEmoji);
case "pull_request_review_comment":
return formatPullRequestReviewComment(payload, repo, author, t, showEmoji);
case "issues":
return formatIssues(payload, repo, author, t, showEmoji);
case "issue_comment":
return formatIssueComment(payload, repo, author, t, showEmoji);
case "workflow_run":
return formatWorkflowRun(payload, repo, author, t, showEmoji);
case "workflow_job":
return formatWorkflowJob(payload, repo, author, t, showEmoji);
case "status":
return formatStatus(payload, repo, author, t, showEmoji);
case "deployment":
return formatDeployment(payload, repo, author, t, showEmoji);
case "ping":
return formatPing(payload, repo, author, t, showEmoji);
case "release":
return formatRelease(payload, repo, author, t, showEmoji);
case "create":
return formatCreate(payload, repo, author, t, showEmoji);
case "delete":
return formatDelete(payload, repo, author, t, showEmoji);
case "star":
return formatStar(payload, repo, repoUrl, author, t, showEmoji);
case "fork":
return formatFork(payload, repo, repoUrl, author, t, showEmoji);
case "check_run":
return formatCheckRun(payload, repo, author, t, showEmoji);
case "check_suite":
return formatCheckSuite(payload, repo, author, t, showEmoji);
case "commit_comment":
return formatCommitComment(payload, repo, author, t, showEmoji);
case "deployment_status":
return formatDeploymentStatus(payload, repo, author, t, showEmoji);
case "member":
return formatMember(payload, repo, author, t, showEmoji);
case "label":
return formatLabel(payload, repo, author, t, showEmoji);
case "milestone":
return formatMilestone(payload, repo, author, t, showEmoji);
case "discussion":
return formatDiscussion(payload, repo, author, t, showEmoji);
case "discussion_comment":
return formatDiscussionComment(payload, repo, author, t, showEmoji);
case "repository":
return formatRepository(payload, repo, repoUrl, author, t, showEmoji);
case "code_scanning_alert":
return formatCodeScanningAlert(payload, repo, author, t, showEmoji);
case "dependabot_alert":
return formatDependabotAlert(payload, repo, author, t, showEmoji);
case "custom":
return formatCustom(payload, repo, author, t, showEmoji);
default:
return formatGeneric(eventType, payload, repo, author, t, showEmoji);
}
}