Skip to content

Commit 86ce795

Browse files
authored
fix(server): keep characters intact in generated commit subjects (pingdotgg#19181)
Co-authored-by: Simone <[email protected]>
1 parent 19f3f6d commit 86ce795

2 files changed

Lines changed: 42 additions & 1 deletion

File tree

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { describe, expect, it } from "vite-plus/test";
2+
3+
import { sanitizeCommitSubject } from "./textGenerationUtils.ts";
4+
5+
describe("sanitizeCommitSubject", () => {
6+
it.each([
7+
{ name: "an emoji", prefixLength: 71, character: "🚀" },
8+
{ name: "an accented character", prefixLength: 71, character: "e\u0301" },
9+
{ name: "an emoji with a skin tone", prefixLength: 70, character: "👍🏽" },
10+
{ name: "a flag", prefixLength: 69, character: "🇮🇹" },
11+
{ name: "a joined emoji", prefixLength: 65, character: "👨‍👩‍👧‍👦" },
12+
])("keeps $name intact at the subject limit", ({ prefixLength, character }) => {
13+
const prefix = "a".repeat(prefixLength);
14+
15+
expect(sanitizeCommitSubject(`${prefix}${character} more`)).toBe(prefix);
16+
});
17+
18+
it("keeps a complete character that ends exactly at the limit", () => {
19+
const expected = `${"a".repeat(70)}🚀`;
20+
21+
expect(sanitizeCommitSubject(`${expected} more`)).toBe(expected);
22+
});
23+
24+
it("uses the existing fallback when a single character exceeds the limit", () => {
25+
expect(sanitizeCommitSubject(`a${"\u0301".repeat(72)} more`)).toBe("Update project files");
26+
});
27+
28+
it("keeps the existing ASCII limit and subject normalization", () => {
29+
expect(sanitizeCommitSubject("a".repeat(80))).toBe("a".repeat(72));
30+
expect(sanitizeCommitSubject(" Fix tests...\nignored ")).toBe("Fix tests");
31+
expect(sanitizeCommitSubject("...")).toBe("Update project files");
32+
});
33+
});

‎packages/provider-core/src/server/textGenerationUtils.ts‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,15 @@ export function sanitizeCommitSubject(raw: string): string {
3333
if (withoutTrailingPeriod.length <= 72) {
3434
return withoutTrailingPeriod;
3535
}
36-
return withoutTrailingPeriod.slice(0, 72).trimEnd();
36+
const segments = new Intl.Segmenter(undefined, { granularity: "grapheme" }).segment(
37+
withoutTrailingPeriod,
38+
);
39+
let subject = "";
40+
for (const { segment } of segments) {
41+
if (subject.length + segment.length > 72) break;
42+
subject += segment;
43+
}
44+
return subject.trimEnd() || "Update project files";
3745
}
3846

3947
export function sanitizePrTitle(raw: string): string {

0 commit comments

Comments
 (0)