From 3567c6d321087ee7ef396cc918eb3570e82cea75 Mon Sep 17 00:00:00 2001 From: RhenCloud Date: Thu, 1 Oct 2026 18:40:02 +0800 Subject: [PATCH] fix(formatter): link branch and tag titles --- docs/guide/message-format.md | 2 ++ docs/zh/guide/message-format.md | 2 ++ server/lib/formatters/create.ts | 11 +++++++---- server/lib/formatters/helpers.ts | 13 +++++++++++++ server/lib/formatters/push.ts | 7 +++++-- tests/discord.test.ts | 10 ++++++++++ tests/formatter.test.ts | 8 ++++++-- 7 files changed, 45 insertions(+), 8 deletions(-) diff --git a/docs/guide/message-format.md b/docs/guide/message-format.md index 18f7b86..0b2ba86 100644 --- a/docs/guide/message-format.md +++ b/docs/guide/message-format.md @@ -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 diff --git a/docs/zh/guide/message-format.md b/docs/zh/guide/message-format.md index 371f72f..e6a2999 100644 --- a/docs/zh/guide/message-format.md +++ b/docs/zh/guide/message-format.md @@ -23,6 +23,8 @@ 提交哈希、分支与标签渲染为带超链接的行内代码(如 ``[`abc123d`](https://…/commit/abc123def456)``),仓库基地址不可用时回退为纯行内代码。 +创建或删除分支、标签的消息会将仓库标题链接到对应的分支或标签页面。 + 对于 push 事件,若提交作者的 GitHub 用户名与推送者不同,提交行末尾会附上作者链接(如 `- [octocat](https://github.com/octocat)`)。GitHub 无法解析机器人账户并返回 `invalid-email-address` 时,会显示提交中的作者名称而不添加链接。 ## 表情 diff --git a/server/lib/formatters/create.ts b/server/lib/formatters/create.ts index 194b1ed..44984f7 100644 --- a/server/lib/formatters/create.ts +++ b/server/lib/formatters/create.ts @@ -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, @@ -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, diff --git a/server/lib/formatters/helpers.ts b/server/lib/formatters/helpers.ts index 61b38c7..37c52c8 100644 --- a/server/lib/formatters/helpers.ts +++ b/server/lib/formatters/helpers.ts @@ -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; diff --git a/server/lib/formatters/push.ts b/server/lib/formatters/push.ts index 1e6837a..266e22c 100644 --- a/server/lib/formatters/push.ts +++ b/server/lib/formatters/push.ts @@ -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, diff --git a/tests/discord.test.ts b/tests/discord.test.ts index a8a4545..dc73710 100644 --- a/tests/discord.test.ts +++ b/tests/discord.test.ts @@ -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", () => { diff --git a/tests/formatter.test.ts b/tests/formatter.test.ts index b5e9e65..f37a88e 100644 --- a/tests/formatter.test.ts +++ b/tests/formatter.test.ts @@ -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(); });