From df0edbb581b1877106cabdc7cff770a9fc896489 Mon Sep 17 00:00:00 2001 From: RhenCloud Date: Sun, 16 Aug 2026 22:27:43 +0800 Subject: [PATCH] refactor(push): move commits to description, comparison to changes field --- server/lib/formatters/push.ts | 66 +++++++++---------- server/lib/lib/locales/en.ts | 2 +- server/lib/lib/locales/zh.ts | 2 +- .../formatter-snapshot.test.ts.snap | 8 +-- tests/formatter.test.ts | 12 ++-- tests/providers.test.ts | 2 +- 6 files changed, 44 insertions(+), 48 deletions(-) diff --git a/server/lib/formatters/push.ts b/server/lib/formatters/push.ts index 098bfc0..f4a542d 100644 --- a/server/lib/formatters/push.ts +++ b/server/lib/formatters/push.ts @@ -57,61 +57,55 @@ export function formatPush( ); } - const descriptionParts: string[] = []; + const descLines: string[] = []; if (forced) { - descriptionParts.push(em("⚠️") + t("events.push.force_push")); + descLines.push(em("⚠️") + t("events.push.force_push")); } if (created) { - descriptionParts.push( + descLines.push( em("🆕") + t(isTagPush ? "events.push.tag_created" : "events.push.branch_created"), ); } - if (compareUrl) { - descriptionParts.push(t("events.push.view_comparison", { url: compareUrl })); - } - - const fields: Array<{ name: string; value: string; inline?: boolean }> = []; - - const commitField = (c: (typeof commits)[number]): { name: string; value: string } => { + const commitsToShow = count <= 5 ? commits : commits.slice(0, 3); + for (const c of commitsToShow) { const shortId = c.id?.slice(0, 7) ?? "???????"; const msg = (c.message?.split("\n")[0] ?? "").slice(0, MAX_COMMIT_SUBJECT) || t("common.no_message"); const url = baseUrl && c.id ? `${baseUrl}/commit/${c.id}` : null; const hash = url ? `[\`${shortId}\`](${url})` : `\`${shortId}\``; - return { name: `\u200b`, value: `${hash} ${msg}` }; - }; - - if (count <= 5) { - for (const c of commits) { - fields.push({ ...commitField(c), inline: false }); - } - } else { - const first3 = commits.slice(0, 3); - for (const c of first3) { - fields.push({ ...commitField(c), inline: false }); - } - fields.push({ - name: `\u200b`, - value: t("common.and_n_more", { count: count - 3 }), - inline: false, - }); + descLines.push(`${hash} ${msg}`); + } + if (count > 5) { + descLines.push(t("common.and_n_more", { count: count - 3 })); } const added = commits.flatMap((c) => c.added ?? []); const removed = commits.flatMap((c) => c.removed ?? []); const modified = commits.flatMap((c) => c.modified ?? []); - if (added.length > 0 || removed.length > 0 || modified.length > 0) { - const changes: string[] = []; - if (added.length > 0) changes.push(t("events.push.added", { count: added.length })); - if (removed.length > 0) changes.push(t("events.push.removed", { count: removed.length })); - if (modified.length > 0) changes.push(t("events.push.modified", { count: modified.length })); + const fields: Array<{ name: string; value: string; inline?: boolean }> = []; + const changeParts: string[] = []; + if (added.length > 0) changeParts.push(t("events.push.added", { count: added.length })); + if (removed.length > 0) changeParts.push(t("events.push.removed", { count: removed.length })); + if (modified.length > 0) changeParts.push(t("events.push.modified", { count: modified.length })); + + let changesValue = ""; + if (compareUrl) { + changesValue = t("events.push.view_comparison", { url: compareUrl }); + if (changeParts.length > 0) { + changesValue += " " + changeParts.join(" | "); + } + } else if (changeParts.length > 0) { + changesValue = changeParts.join(" | "); + } + + if (changesValue) { fields.push({ name: t("fields.changes"), - value: changes.join(" | "), - inline: true, + value: changesValue, + inline: false, }); } @@ -122,11 +116,11 @@ export function formatPush( count, s: count !== 1 ? "s" : "", repo: repo ?? t("common.repository"), - ref: isTagPush ? tagLink(baseUrl, rawRef, ref) : branchLink(baseUrl, rawRef, ref), + ref, }), url: compareUrl, color: GITHUB_COLORS.push, - description: descriptionParts.join("\n"), + description: descLines.join("\n"), fields: fields.length > 0 ? fields : undefined, }, t, diff --git a/server/lib/lib/locales/en.ts b/server/lib/lib/locales/en.ts index 9d75165..df4797d 100644 --- a/server/lib/lib/locales/en.ts +++ b/server/lib/lib/locales/en.ts @@ -91,7 +91,7 @@ export const en = { force_push: "**Force push**", branch_created: "Branch created", tag_created: "Tag created", - view_comparison: "[View comparison]({url})", + view_comparison: "[View comparison]({url}):", added: "+{count} added", removed: "-{count} removed", modified: "~{count} modified", diff --git a/server/lib/lib/locales/zh.ts b/server/lib/lib/locales/zh.ts index 9a2a7d4..9c32ea4 100644 --- a/server/lib/lib/locales/zh.ts +++ b/server/lib/lib/locales/zh.ts @@ -92,7 +92,7 @@ export const zh = { branch_created: "分支已创建", tag_created: "标签已创建", commits_pushed: "**{count}** 个提交已推送到 {ref}", - view_comparison: "[查看比较]({url})", + view_comparison: "[查看比较]({url}):", added: "+{count} 新增", removed: "-{count} 删除", modified: "~{count} 修改", diff --git a/tests/__snapshots__/formatter-snapshot.test.ts.snap b/tests/__snapshots__/formatter-snapshot.test.ts.snap index a312325..bf3f710 100644 --- a/tests/__snapshots__/formatter-snapshot.test.ts.snap +++ b/tests/__snapshots__/formatter-snapshot.test.ts.snap @@ -8,17 +8,17 @@ exports[`formatter snapshots push 1`] = ` "url": "https://github.com/octocat", }, "color": 3056719, - "description": "[View comparison](https://github.com/acme/widget/compare/aaa...bbb)", + "description": "[\`bbbbbbb\`](https://github.com/acme/widget/commit/bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) Fix login bug", "fields": [ { "inline": false, - "name": "​", - "value": "[\`bbbbbbb\`](https://github.com/acme/widget/commit/bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) Fix login bug", + "name": "Changes", + "value": "[View comparison](https://github.com/acme/widget/compare/aaa...bbb):", }, ], "footer": "acme/widget", "timestamp": "TIMESTAMP", - "title": "acme/widget: Pushed 1 commit to [\`main\`](https://github.com/acme/widget/tree/main)", + "title": "acme/widget: Pushed 1 commit to main", "url": "https://github.com/acme/widget/compare/aaa...bbb", } `; diff --git a/tests/formatter.test.ts b/tests/formatter.test.ts index f65ce7f..e8a6601 100644 --- a/tests/formatter.test.ts +++ b/tests/formatter.test.ts @@ -33,11 +33,14 @@ describe("message title spec", () => { }), ); expect(msg.title).toBe( - "acme/widget: Pushed 1 commit to [`main`](https://github.com/acme/widget/tree/main)", + "acme/widget: Pushed 1 commit to main", ); expect(msg.description).toBe( - "[View comparison](https://github.com/acme/widget/compare/abc...def)", + "[`abcd123`](https://github.com/acme/widget/commit/abcd1234ef) fix stuff", ); + expect(msg.fields).toEqual([ + { name: "Changes", value: "[View comparison](https://github.com/acme/widget/compare/abc...def):", inline: false }, + ]); }); it("pull_request title is repo#number: title", () => { @@ -346,8 +349,7 @@ describe("group emoji toggle", () => { sender, }), ); - expect(msg.fields![0].name).toBe("\u200b"); - expect(msg.fields![0].value).toBe( + expect(msg.description).toBe( "[`abcd123`](https://github.com/acme/widget/commit/abcd1234ef) fix stuff", ); }); @@ -450,7 +452,7 @@ describe("limits and localization", () => { sender, }), ); - expect(msg.fields![0].value).toBe(`${link} ${"x".repeat(MAX_COMMIT_SUBJECT)}`); + expect(msg.description).toBe(`${link} ${"x".repeat(MAX_COMMIT_SUBJECT)}`); }); it("tag push created mentions the tag", () => { diff --git a/tests/providers.test.ts b/tests/providers.test.ts index 79c29db..982fa5f 100644 --- a/tests/providers.test.ts +++ b/tests/providers.test.ts @@ -148,7 +148,7 @@ describe("gitea event parsing", () => { }; const msg = formatEvent(route, event!); expect(msg.title).toContain("org/repo"); - expect(msg.fields![0].value).toBe( + expect(msg.description).toContain( "[`abcd123`](https://git.example.com/org/repo/commit/abcd1234ef) fix stuff", ); });