Tolerate trailing newlines in AddCoAuthorToDescription

Callers currently hand this function a trimmed description, so the output is
always clean. An upcoming change to the commit-panel getters will stop trimming
at the callsite (so that whitespace typed by the user round-trips through the
preservation file exactly), at which point the description can end with one or
more newlines. Without this change, a user who presses Enter after their
description body and then invokes "Add co-author" would end up with two blank
lines between the body and the trailer instead of the expected one.
This commit is contained in:
Stefan Haller 2026-04-19 12:03:41 +02:00
parent 58309b02a9
commit 4ab36461cb
2 changed files with 6 additions and 0 deletions

View file

@ -61,6 +61,7 @@ func AddCoAuthorToMessage(message string, author string) string {
}
func AddCoAuthorToDescription(description string, author string) string {
description = strings.TrimRight(description, "\n")
if description != "" {
lines := strings.Split(description, "\n")
if strings.HasPrefix(lines[len(lines)-1], "Co-authored-by:") {

View file

@ -483,6 +483,11 @@ func TestAddCoAuthorToDescription(t *testing.T) {
description: "Body\n\nCo-authored-by: Jane Smith <jane@smith.com>",
expectedResult: "Body\n\nCo-authored-by: Jane Smith <jane@smith.com>\nCo-authored-by: John Doe <john@doe.com>",
},
{
name: "Description with trailing newlines",
description: "Body\n\n",
expectedResult: "Body\n\nCo-authored-by: John Doe <john@doe.com>",
},
}
for _, s := range scenarios {
t.Run(s.name, func(t *testing.T) {