refactor(push): move commits to description, comparison to changes field

This commit is contained in:
RhenCloud 2026-08-16 22:27:43 +08:00
parent 47d7c9105b
commit df0edbb581
No known key found for this signature in database
GPG key ID: A574A617378C4E0B
6 changed files with 44 additions and 48 deletions

View file

@ -57,61 +57,55 @@ export function formatPush(
); );
} }
const descriptionParts: string[] = []; const descLines: string[] = [];
if (forced) { if (forced) {
descriptionParts.push(em("⚠️") + t("events.push.force_push")); descLines.push(em("⚠️") + t("events.push.force_push"));
} }
if (created) { if (created) {
descriptionParts.push( descLines.push(
em("🆕") + t(isTagPush ? "events.push.tag_created" : "events.push.branch_created"), em("🆕") + t(isTagPush ? "events.push.tag_created" : "events.push.branch_created"),
); );
} }
if (compareUrl) { const commitsToShow = count <= 5 ? commits : commits.slice(0, 3);
descriptionParts.push(t("events.push.view_comparison", { url: compareUrl })); for (const c of commitsToShow) {
}
const fields: Array<{ name: string; value: string; inline?: boolean }> = [];
const commitField = (c: (typeof commits)[number]): { name: string; value: string } => {
const shortId = c.id?.slice(0, 7) ?? "???????"; const shortId = c.id?.slice(0, 7) ?? "???????";
const msg = const msg =
(c.message?.split("\n")[0] ?? "").slice(0, MAX_COMMIT_SUBJECT) || t("common.no_message"); (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 url = baseUrl && c.id ? `${baseUrl}/commit/${c.id}` : null;
const hash = url ? `[\`${shortId}\`](${url})` : `\`${shortId}\``; const hash = url ? `[\`${shortId}\`](${url})` : `\`${shortId}\``;
return { name: `\u200b`, value: `${hash} ${msg}` }; descLines.push(`${hash} ${msg}`);
}; }
if (count > 5) {
if (count <= 5) { descLines.push(t("common.and_n_more", { count: count - 3 }));
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,
});
} }
const added = commits.flatMap((c) => c.added ?? []); const added = commits.flatMap((c) => c.added ?? []);
const removed = commits.flatMap((c) => c.removed ?? []); const removed = commits.flatMap((c) => c.removed ?? []);
const modified = commits.flatMap((c) => c.modified ?? []); const modified = commits.flatMap((c) => c.modified ?? []);
if (added.length > 0 || removed.length > 0 || modified.length > 0) { const fields: Array<{ name: string; value: string; inline?: boolean }> = [];
const changes: string[] = []; const changeParts: string[] = [];
if (added.length > 0) changes.push(t("events.push.added", { count: added.length })); if (added.length > 0) changeParts.push(t("events.push.added", { count: added.length }));
if (removed.length > 0) changes.push(t("events.push.removed", { count: removed.length })); if (removed.length > 0) changeParts.push(t("events.push.removed", { count: removed.length }));
if (modified.length > 0) changes.push(t("events.push.modified", { count: modified.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({ fields.push({
name: t("fields.changes"), name: t("fields.changes"),
value: changes.join(" | "), value: changesValue,
inline: true, inline: false,
}); });
} }
@ -122,11 +116,11 @@ export function formatPush(
count, count,
s: count !== 1 ? "s" : "", s: count !== 1 ? "s" : "",
repo: repo ?? t("common.repository"), repo: repo ?? t("common.repository"),
ref: isTagPush ? tagLink(baseUrl, rawRef, ref) : branchLink(baseUrl, rawRef, ref), ref,
}), }),
url: compareUrl, url: compareUrl,
color: GITHUB_COLORS.push, color: GITHUB_COLORS.push,
description: descriptionParts.join("\n"), description: descLines.join("\n"),
fields: fields.length > 0 ? fields : undefined, fields: fields.length > 0 ? fields : undefined,
}, },
t, t,

View file

@ -91,7 +91,7 @@ export const en = {
force_push: "**Force push**", force_push: "**Force push**",
branch_created: "Branch created", branch_created: "Branch created",
tag_created: "Tag created", tag_created: "Tag created",
view_comparison: "[View comparison]({url})", view_comparison: "[View comparison]({url}):",
added: "+{count} added", added: "+{count} added",
removed: "-{count} removed", removed: "-{count} removed",
modified: "~{count} modified", modified: "~{count} modified",

View file

@ -92,7 +92,7 @@ export const zh = {
branch_created: "分支已创建", branch_created: "分支已创建",
tag_created: "标签已创建", tag_created: "标签已创建",
commits_pushed: "**{count}** 个提交已推送到 {ref}", commits_pushed: "**{count}** 个提交已推送到 {ref}",
view_comparison: "[查看比较]({url})", view_comparison: "[查看比较]({url}):",
added: "+{count} 新增", added: "+{count} 新增",
removed: "-{count} 删除", removed: "-{count} 删除",
modified: "~{count} 修改", modified: "~{count} 修改",

View file

@ -8,17 +8,17 @@ exports[`formatter snapshots push 1`] = `
"url": "https://github.com/octocat", "url": "https://github.com/octocat",
}, },
"color": 3056719, "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": [ "fields": [
{ {
"inline": false, "inline": false,
"name": "", "name": "Changes",
"value": "[\`bbbbbbb\`](https://github.com/acme/widget/commit/bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) Fix login bug", "value": "[View comparison](https://github.com/acme/widget/compare/aaa...bbb):",
}, },
], ],
"footer": "acme/widget", "footer": "acme/widget",
"timestamp": "TIMESTAMP", "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", "url": "https://github.com/acme/widget/compare/aaa...bbb",
} }
`; `;

View file

@ -33,11 +33,14 @@ describe("message title spec", () => {
}), }),
); );
expect(msg.title).toBe( 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( 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", () => { it("pull_request title is repo#number: title", () => {
@ -346,8 +349,7 @@ describe("group emoji toggle", () => {
sender, sender,
}), }),
); );
expect(msg.fields![0].name).toBe("\u200b"); expect(msg.description).toBe(
expect(msg.fields![0].value).toBe(
"[`abcd123`](https://github.com/acme/widget/commit/abcd1234ef) fix stuff", "[`abcd123`](https://github.com/acme/widget/commit/abcd1234ef) fix stuff",
); );
}); });
@ -450,7 +452,7 @@ describe("limits and localization", () => {
sender, 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", () => { it("tag push created mentions the tag", () => {

View file

@ -148,7 +148,7 @@ describe("gitea event parsing", () => {
}; };
const msg = formatEvent(route, event!); const msg = formatEvent(route, event!);
expect(msg.title).toContain("org/repo"); 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", "[`abcd123`](https://git.example.com/org/repo/commit/abcd1234ef) fix stuff",
); );
}); });