fix(formatter): link branch and tag titles

This commit is contained in:
RhenCloud committed 2026-10-01 18:40:02 +08:00
1 parent b7fd31a665
commit 3567c6d321
7 files changed
+45 -8

No files matched your search

+2
View File
@@ -23,6 +23,8 @@ Messages whose title has no colon separator (a `:` followed by a space) keep the
Commit hashes, branches, and tags render as inline code wrapped in a hyperlink (e.g. ``[`abc123d`](https://…/commit/abc123def456)``), falling back to plain inline code when the repo base URL is unavailable.
Branch and tag creation or deletion messages link their repository title to the corresponding branch or tag page.
For push events, when a commit author's GitHub username differs from the pusher, the commit line ends with a link to that author (e.g. `- [octocat](https://github.com/octocat)`). When GitHub cannot resolve a bot account and returns `invalid-email-address`, the commit's author name is shown without a link.
## Emoji
+2
View File
@@ -23,6 +23,8 @@
提交哈希、分支与标签渲染为带超链接的行内代码(如 ``[`abc123d`](https://…/commit/abc123def456)``),仓库基地址不可用时回退为纯行内代码。
创建或删除分支、标签的消息会将仓库标题链接到对应的分支或标签页面。
对于 push 事件,若提交作者的 GitHub 用户名与推送者不同,提交行末尾会附上作者链接(如 `- [octocat](https://github.com/octocat)`)。GitHub 无法解析机器人账户并返回 `invalid-email-address` 时,会显示提交中的作者名称而不添加链接。
## 表情
+7 -4
View File
@@ -1,6 +1,6 @@
import type { NeutralMessage, NeutralAuthor } from "../types";
import { GITHUB_COLORS } from "./colors";
import { branchLink, emojiPrefix, tagLink, type T, buildMessage, repoBaseUrl } from "./helpers";
import { branchLink, emojiPrefix, refUrl, tagLink, type T, buildMessage, repoBaseUrl } from "./helpers";
export function formatCreate(
payload: Record<string, unknown>,
@@ -13,6 +13,7 @@ export function formatCreate(
const ref = (payload.ref as string) ?? t("common.unknown");
const baseUrl = repoBaseUrl(payload, repo);
const refText = refType === "tag" ? tagLink(baseUrl, ref) : branchLink(baseUrl, ref);
const url = refUrl(baseUrl, ref, refType === "tag" ? "tag" : "branch");
const emoji = refType === "tag" ? "🏷️" : "🌿";
const em = (e: string): string => emojiPrefix(e, showEmoji);
@@ -46,8 +47,9 @@ export function formatCreate(
repo: repo ?? t("common.repository"),
emoji: em(emoji),
type: refType,
ref: refText,
ref,
}),
url,
color: GITHUB_COLORS.create,
fields,
},
@@ -66,7 +68,7 @@ export function formatDelete(
const refType = (payload.ref_type as string) ?? "branch";
const ref = (payload.ref as string) ?? t("common.unknown");
const baseUrl = repoBaseUrl(payload, repo);
const refText = refType === "tag" ? tagLink(baseUrl, ref) : branchLink(baseUrl, ref);
const url = refUrl(baseUrl, ref, refType === "tag" ? "tag" : "branch");
const emoji = refType === "tag" ? "🏷️" : "🌿";
const em = (e: string): string => emojiPrefix(e, showEmoji);
@@ -78,8 +80,9 @@ export function formatDelete(
repo: repo ?? t("common.repository"),
emoji: em(emoji),
type: refType,
ref: refText,
ref,
}),
url,
color: GITHUB_COLORS.delete,
},
t,
+13
View File
@@ -83,6 +83,19 @@ export function tagLink(baseUrl: string | undefined, tag: string, label?: string
: `\`${display}\``;
}
/** URL for a branch or tag, when the repository base URL is available. */
export function refUrl(
baseUrl: string | undefined,
ref: string,
type: "branch" | "tag",
): string | undefined {
if (!baseUrl) return undefined;
const clean = ref.replace("refs/heads/", "").replace("refs/tags/", "");
return type === "tag"
? `${baseUrl}/releases/tag/${encodeRefPath(clean)}`
: `${baseUrl}/tree/${encodeRefPath(clean)}`;
}
/* ---- Content size limits (mirror the Discord embed limits) ---- */
export const MAX_TITLE = 256;
export const MAX_DESCRIPTION = 4096;
+5 -2
View File
@@ -4,6 +4,7 @@ import {
branchLink,
emojiPrefix,
MAX_COMMIT_SUBJECT,
refUrl,
tagLink,
type T,
buildMessage,
@@ -49,8 +50,9 @@ export function formatPush(
repo: repo ?? t("common.repository"),
emoji: em(isTagPush ? "🏷️" : "🌿"),
type: refType,
ref: isTagPush ? tagLink(baseUrl, rawRef, ref) : branchLink(baseUrl, rawRef, ref),
ref,
}),
url: refUrl(baseUrl, rawRef, refType),
color: GITHUB_COLORS.delete,
},
t,
@@ -71,8 +73,9 @@ export function formatPush(
repo: repo ?? t("common.repository"),
emoji: em(isTagPush ? "🏷️" : "🌿"),
type: refType,
ref: isTagPush ? tagLink(baseUrl, cleanRef) : branchLink(baseUrl, cleanRef),
ref: cleanRef,
}),
url: refUrl(baseUrl, cleanRef, refType),
color: GITHUB_COLORS.create,
},
t,
+10
View File
@@ -100,6 +100,16 @@ describe("discord role mentions", () => {
icon_url: "https://github.com/fluidicon.png",
});
});
it("links the repository heading for a tag title", () => {
const out = renderNeutralMessage({
title: "acme/widget: 🏷️ Created tag v1.0",
url: "https://github.com/acme/widget/releases/tag/v1.0",
});
expect(out.embeds?.[0]?.title).toBe("acme/widget");
expect(out.embeds?.[0]?.url).toBe("https://github.com/acme/widget/releases/tag/v1.0");
expect(out.embeds?.[0]?.description).toBe("🏷️ Created tag v1.0");
});
});
describe("dispatchEvent fallback routing", () => {
+6 -2
View File
@@ -491,7 +491,10 @@ describe("group emoji toggle", () => {
}),
);
expect(msg.title).toBe(
"acme/widget: 🌿 Deleted branch [`dependabot/npm_and_yarn/multi-e1b34b8be3`](https://github.com/acme/widget/tree/dependabot/npm_and_yarn/multi-e1b34b8be3)",
"acme/widget: 🌿 Deleted branch dependabot/npm_and_yarn/multi-e1b34b8be3",
);
expect(msg.url).toBe(
"https://github.com/acme/widget/tree/dependabot/npm_and_yarn/multi-e1b34b8be3",
);
expect(msg.description).toBeUndefined();
expect(msg.fields).toBeUndefined();
@@ -630,8 +633,9 @@ describe("limits and localization", () => {
}),
);
expect(msg.title).toBe(
"acme/widget: 🏷️ Created tag [`v1.0`](https://github.com/acme/widget/releases/tag/v1.0)",
"acme/widget: 🏷️ Created tag v1.0",
);
expect(msg.url).toBe("https://github.com/acme/widget/releases/tag/v1.0");
expect(msg.color).toBe(4176208);
expect(msg.description).toBeUndefined();
});