From 1057a60de7766661176eebfe86bae0d952b26f6e Mon Sep 17 00:00:00 2001 From: RhenCloud Date: Wed, 30 Sep 2026 18:52:07 +0800 Subject: [PATCH] fix(push): show differing commit author --- docs/guide/message-format.md | 2 ++ docs/zh/guide/message-format.md | 2 ++ server/lib/formatters/push.ts | 9 ++++-- tests/formatter.test.ts | 52 +++++++++++++++++++++++++++++++++ 4 files changed, 63 insertions(+), 2 deletions(-) diff --git a/docs/guide/message-format.md b/docs/guide/message-format.md index a683562..9cf60e6 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. +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)`). + ## Emoji Event-specific emoji are added by the formatters; per-group `Group.emoji` (default true) strips them all when disabled. Milestone progress bars are exempt. See [Message Language](./i18n). diff --git a/docs/zh/guide/message-format.md b/docs/zh/guide/message-format.md index c2a73c9..7dda9a8 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)`)。 + ## 表情 事件专属表情由格式化器添加;分组级 `Group.emoji`(默认开启)关闭后会全部去除。里程碑进度条不受影响。见[消息语言](./i18n)。 diff --git a/server/lib/formatters/push.ts b/server/lib/formatters/push.ts index 24f5447..8e753f3 100644 --- a/server/lib/formatters/push.ts +++ b/server/lib/formatters/push.ts @@ -23,7 +23,7 @@ export function formatPush( const commits = (payload.commits ?? []) as Array<{ id?: string; message?: string; - author?: { name?: string; email?: string }; + author?: { name?: string; email?: string; username?: string }; added?: string[]; removed?: string[]; modified?: string[]; @@ -34,6 +34,7 @@ export function formatPush( const forced = payload.forced as boolean | undefined; const created = payload.created as boolean | undefined; const deleted = payload.deleted as boolean | undefined; + const pusher = (payload.pusher as { name?: string } | undefined)?.name; const em = (e: string): string => emojiPrefix(e, showEmoji); // A branch/tag deletion pushed via `git push --delete` arrives as a push @@ -93,8 +94,12 @@ export function formatPush( const commitsToShow = count <= 5 ? commits : commits.slice(0, 3); for (const c of commitsToShow) { const shortId = c.id?.slice(0, 7) ?? "???????"; - const msg = + let msg = (c.message?.split("\n")[0] ?? "").slice(0, MAX_COMMIT_SUBJECT) || t("common.no_message"); + const commitAuthor = c.author?.username; + if (commitAuthor && commitAuthor.toLowerCase() !== pusher?.toLowerCase()) { + msg += ` - [${commitAuthor}](https://github.com/${encodeURIComponent(commitAuthor)})`; + } const url = baseUrl && c.id ? `${baseUrl}/commit/${c.id}` : null; const hash = url ? `[\`${shortId}\`](${url})` : `\`${shortId}\``; descLines.push(`${hash} ${msg}`); diff --git a/tests/formatter.test.ts b/tests/formatter.test.ts index 5a7fd8a..d897df0 100644 --- a/tests/formatter.test.ts +++ b/tests/formatter.test.ts @@ -47,6 +47,58 @@ describe("message title spec", () => { ]); }); + it("adds a commit author link when it differs from the pusher", () => { + const msg = formatEvent( + route, + event("push", { + ref: "refs/heads/main", + commits: [ + { + id: "abcd1234ef", + message: "fix stuff", + author: { username: "commit-author" }, + added: [], + removed: [], + modified: [], + }, + ], + pusher: { name: "merge-bot" }, + repository: repo, + sender, + }), + ); + + expect(msg.description).toBe( + "[`abcd123`](https://github.com/acme/widget/commit/abcd1234ef) fix stuff - [commit-author](https://github.com/commit-author)", + ); + }); + + it("does not add a commit author link when it matches the pusher", () => { + const msg = formatEvent( + route, + event("push", { + ref: "refs/heads/main", + commits: [ + { + id: "abcd1234ef", + message: "fix stuff", + author: { username: "octocat" }, + added: [], + removed: [], + modified: [], + }, + ], + pusher: { name: "OctoCat" }, + repository: repo, + sender, + }), + ); + + expect(msg.description).toBe( + "[`abcd123`](https://github.com/acme/widget/commit/abcd1234ef) fix stuff", + ); + }); + it("pull_request title is repo#number: title", () => { const msg = formatEvent( route,