WebHooker/server/lib/formatters/check.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

213 lines
5 KiB
TypeScript

import type { NeutralMessage, NeutralAuthor } from "../types";
import { GITHUB_COLORS, WORKFLOW_CONCLUSION_EMOJI } from "./colors";
import {
branchLink,
cap,
commitLink,
emojiPrefix,
MAX_FIELD_VALUE,
statusColorKey,
type T,
buildMessage,
repoBaseUrl,
workflowStatus,
} from "./helpers";
export function formatCheckSuite(
payload: Record<string, unknown>,
repo: string | undefined,
author: NeutralAuthor,
t: T,
showEmoji: boolean,
): NeutralMessage {
const suite = payload.check_suite as {
head_branch?: string;
head_sha?: string;
conclusion?: string;
status?: string;
app?: { name?: string };
html_url?: string;
};
const status = workflowStatus(suite.status, suite.conclusion);
const baseUrl = repoBaseUrl(payload, repo);
const emoji = WORKFLOW_CONCLUSION_EMOJI[status] ?? "⏳";
const em = (e: string): string => emojiPrefix(e, showEmoji);
const colorKey = statusColorKey("check_run", status);
const fields: Array<{ name: string; value: string; inline?: boolean }> = [];
fields.push({
name: t("fields.status"),
value: `${em(emoji)}${status}`,
inline: true,
});
if (suite.app?.name) {
fields.push({
name: t("fields.service"),
value: suite.app.name,
inline: true,
});
}
if (suite.head_branch) {
fields.push({
name: t("fields.branch"),
value: branchLink(baseUrl, suite.head_branch),
inline: true,
});
}
if (suite.head_sha) {
fields.push({
name: t("fields.commit"),
value: commitLink(baseUrl, suite.head_sha),
inline: true,
});
}
return buildMessage(
{
author,
title: t("events.check_suite.title", {
repo: repo ?? t("common.repository"),
conclusion: status,
}),
url:
suite.html_url ??
(baseUrl && suite.head_sha ? `${baseUrl}/commit/${suite.head_sha}/checks` : undefined),
color: GITHUB_COLORS[colorKey],
fields,
},
t,
repo,
);
}
export function formatStatus(
payload: Record<string, unknown>,
repo: string | undefined,
author: NeutralAuthor,
t: T,
showEmoji: boolean,
): NeutralMessage {
const state = (payload.state as string) ?? "pending";
const context = (payload.context as string) ?? "";
const description = payload.description as string | undefined;
const targetUrl = payload.target_url as string | undefined;
const sha = payload.sha as string | undefined;
const baseUrl = repoBaseUrl(payload, repo);
const emoji = state === "success" ? "✅" : state === "failure" || state === "error" ? "❌" : "⏳";
const em = (e: string): string => emojiPrefix(e, showEmoji);
const colorKey =
state === "success"
? "check_run_success"
: state === "failure" || state === "error"
? "check_run_failure"
: "check_run_other";
const fields: Array<{ name: string; value: string; inline?: boolean }> = [];
fields.push({
name: t("fields.status"),
value: `${em(emoji)}${state}`,
inline: true,
});
if (context) {
fields.push({
name: t("fields.context"),
value: context,
inline: true,
});
}
if (sha) {
fields.push({
name: t("fields.commit"),
value: commitLink(baseUrl, sha),
inline: true,
});
}
if (description) {
fields.push({
name: t("fields.description"),
value: description,
inline: false,
});
}
return buildMessage(
{
author,
title: t("events.status.title", {
repo: repo ?? t("common.repository"),
context: context || t("common.unknown"),
state,
}),
url: targetUrl,
color: GITHUB_COLORS[colorKey],
fields,
},
t,
repo,
);
}
export function formatCheckRun(
payload: Record<string, unknown>,
repo: string | undefined,
author: NeutralAuthor,
t: T,
showEmoji: boolean,
): NeutralMessage {
const checkRun = payload.check_run as {
id?: number;
name?: string;
conclusion?: string;
html_url?: string;
status?: string;
output?: { title?: string; summary?: string };
};
const status = workflowStatus(checkRun.status, checkRun.conclusion);
const emoji = WORKFLOW_CONCLUSION_EMOJI[status] ?? "⏳";
const em = (e: string): string => emojiPrefix(e, showEmoji);
const colorKey = statusColorKey("check_run", status);
const fields: Array<{ name: string; value: string; inline?: boolean }> = [];
fields.push({
name: t("fields.status"),
value: `${em(emoji)}${status}`,
inline: true,
});
if (checkRun.output?.title) {
fields.push({
name: t("fields.details"),
value: cap(checkRun.output.title, MAX_FIELD_VALUE),
inline: false,
});
}
return buildMessage(
{
author,
title: t("events.check_run.title", {
repo: repo ?? t("common.repository"),
name: checkRun.name ?? "Check Run",
conclusion: status,
}),
url: checkRun.html_url,
color: GITHUB_COLORS[colorKey],
fields,
updateKey: repo && checkRun.id != null ? `check_run:${repo}:${checkRun.id}` : undefined,
},
t,
repo,
);
}