Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix missing commit message body when the message has leading newlines (#25418) #25422

Merged
merged 1 commit into from
Jun 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions modules/templates/util_render.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,16 +81,16 @@ func RenderCommitMessageLinkSubject(ctx context.Context, msg, urlPrefix, urlDefa

// RenderCommitBody extracts the body of a commit message without its title.
func RenderCommitBody(ctx context.Context, msg, urlPrefix string, metas map[string]string) template.HTML {
msgLine := strings.TrimRightFunc(msg, unicode.IsSpace)
msgLine := strings.TrimSpace(msg)
lineEnd := strings.IndexByte(msgLine, '\n')
if lineEnd > 0 {
msgLine = msgLine[lineEnd+1:]
} else {
return template.HTML("")
return ""
}
msgLine = strings.TrimLeftFunc(msgLine, unicode.IsSpace)
if len(msgLine) == 0 {
return template.HTML("")
return ""
}

renderedMessage, err := markup.RenderCommitMessage(&markup.RenderContext{
Expand Down
56 changes: 56 additions & 0 deletions modules/templates/util_render_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Copyright 2023 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT

package templates

import (
"context"
"html/template"
"testing"

"github.com/stretchr/testify/assert"
)

func TestRenderCommitBody(t *testing.T) {
type args struct {
ctx context.Context
msg string
urlPrefix string
metas map[string]string
}
tests := []struct {
name string
args args
want template.HTML
}{
{
name: "multiple lines",
args: args{
ctx: context.Background(),
msg: "first line\nsecond line",
},
want: "second line",
},
{
name: "multiple lines with leading newlines",
args: args{
ctx: context.Background(),
msg: "\n\n\n\nfirst line\nsecond line",
},
want: "second line",
},
{
name: "multiple lines with trailing newlines",
args: args{
ctx: context.Background(),
msg: "first line\nsecond line\n\n\n",
},
want: "second line",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equalf(t, tt.want, RenderCommitBody(tt.args.ctx, tt.args.msg, tt.args.urlPrefix, tt.args.metas), "RenderCommitBody(%v, %v, %v, %v)", tt.args.ctx, tt.args.msg, tt.args.urlPrefix, tt.args.metas)
})
}
}