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) {
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 });
descLines.push(`${hash} ${msg}`);
}
} 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,
});
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,

View file

@ -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",

View file

@ -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} 修改",

View file

@ -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",
}
`;

View file

@ -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", () => {

View file

@ -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",
);
});