Skip to content

Commit 40727e4

Browse files
jpraetzeripathnoerwlunnytechknowlogick
authored and
Stu Wehrly
committed
Append to existing trailers in generated squash commit message (go-gitea#15980)
* Remove superfluous newline before Co-authored-by trailers * Append to existing PR description trailer section If the existing PR description message already contains a trailer section (e.g. Signed-off-by: ), append to it instead of creating a new trailer section. * Reuse compiled regexp * Simplify regex and deal with trailing \n in PR description * Add tests for CommitMessageTrailersPattern - add support for Key:Value (no space after colon) - add support for whitespace "folding" * Update services/pull/pull_test.go Co-authored-by: Norwin <noerw@users.noreply.github.com> Co-authored-by: zeripath <art27@cantab.net> Co-authored-by: Norwin <noerw@users.noreply.github.com> Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com> Co-authored-by: techknowlogick <techknowlogick@gitea.io>
1 parent c5798e1 commit 40727e4

File tree

2 files changed

+31
-9
lines changed

2 files changed

+31
-9
lines changed

services/pull/pull.go

+8-9
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"bytes"
1010
"context"
1111
"fmt"
12+
"regexp"
1213
"strings"
1314
"time"
1415

@@ -516,6 +517,8 @@ func CloseRepoBranchesPulls(doer *models.User, repo *models.Repository) error {
516517
return nil
517518
}
518519

520+
var commitMessageTrailersPattern = regexp.MustCompile(`(?:^|\n\n)(?:[\w-]+[ \t]*:[^\n]+\n*(?:[ \t]+[^\n]+\n*)*)+$`)
521+
519522
// GetSquashMergeCommitMessages returns the commit messages between head and merge base (if there is one)
520523
func GetSquashMergeCommitMessages(pr *models.PullRequest) string {
521524
if err := pr.LoadIssue(); err != nil {
@@ -571,10 +574,13 @@ func GetSquashMergeCommitMessages(pr *models.PullRequest) string {
571574
stringBuilder := strings.Builder{}
572575

573576
if !setting.Repository.PullRequest.PopulateSquashCommentWithCommitMessages {
574-
stringBuilder.WriteString(pr.Issue.Content)
577+
message := strings.TrimSpace(pr.Issue.Content)
578+
stringBuilder.WriteString(message)
575579
if stringBuilder.Len() > 0 {
576580
stringBuilder.WriteRune('\n')
577-
stringBuilder.WriteRune('\n')
581+
if !commitMessageTrailersPattern.MatchString(message) {
582+
stringBuilder.WriteRune('\n')
583+
}
578584
}
579585
}
580586

@@ -645,13 +651,6 @@ func GetSquashMergeCommitMessages(pr *models.PullRequest) string {
645651
}
646652
}
647653

648-
if len(authors) > 0 {
649-
if _, err := stringBuilder.WriteRune('\n'); err != nil {
650-
log.Error("Unable to write to string builder Error: %v", err)
651-
return ""
652-
}
653-
}
654-
655654
for _, author := range authors {
656655
if _, err := stringBuilder.Write([]byte("Co-authored-by: ")); err != nil {
657656
log.Error("Unable to write to string builder Error: %v", err)

services/pull/pull_test.go

+23
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,27 @@
55

66
package pull
77

8+
import (
9+
"testing"
10+
11+
"github.com/stretchr/testify/assert"
12+
)
13+
814
// TODO TestPullRequest_PushToBaseRepo
15+
16+
func TestPullRequest_CommitMessageTrailersPattern(t *testing.T) {
17+
// Not a valid trailer section
18+
assert.False(t, commitMessageTrailersPattern.MatchString(""))
19+
assert.False(t, commitMessageTrailersPattern.MatchString("No trailer."))
20+
assert.False(t, commitMessageTrailersPattern.MatchString("Signed-off-by: Bob <bob@example.com>\nNot a trailer due to following text."))
21+
assert.False(t, commitMessageTrailersPattern.MatchString("Message body not correctly separated from trailer section by empty line.\nSigned-off-by: Bob <bob@example.com>"))
22+
// Valid trailer section
23+
assert.True(t, commitMessageTrailersPattern.MatchString("Signed-off-by: Bob <bob@example.com>"))
24+
assert.True(t, commitMessageTrailersPattern.MatchString("Signed-off-by: Bob <bob@example.com>\nOther-Trailer: Value"))
25+
assert.True(t, commitMessageTrailersPattern.MatchString("Message body correctly separated from trailer section by empty line.\n\nSigned-off-by: Bob <bob@example.com>"))
26+
assert.True(t, commitMessageTrailersPattern.MatchString("Multiple trailers.\n\nSigned-off-by: Bob <bob@example.com>\nOther-Trailer: Value"))
27+
assert.True(t, commitMessageTrailersPattern.MatchString("Newline after trailer section.\n\nSigned-off-by: Bob <bob@example.com>\n"))
28+
assert.True(t, commitMessageTrailersPattern.MatchString("No space after colon is accepted.\n\nSigned-off-by:Bob <bob@example.com>"))
29+
assert.True(t, commitMessageTrailersPattern.MatchString("Additional whitespace is accepted.\n\nSigned-off-by \t : \tBob <bob@example.com> "))
30+
assert.True(t, commitMessageTrailersPattern.MatchString("Folded value.\n\nFolded-trailer: This is\n a folded\n trailer value\nOther-Trailer: Value"))
31+
}

0 commit comments

Comments
 (0)