Skip to content

Commit 06f483d

Browse files
jpraetzeripathnoerwlunnytechknowlogick
authored
Append to existing trailers in generated squash commit message (#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 <[email protected]> Co-authored-by: zeripath <[email protected]> Co-authored-by: Norwin <[email protected]> Co-authored-by: Lunny Xiao <[email protected]> Co-authored-by: techknowlogick <[email protected]>
1 parent 31acd3c commit 06f483d

File tree

2 files changed

+31
-9
lines changed

2 files changed

+31
-9
lines changed

services/pull/pull.go

Lines changed: 8 additions & 9 deletions
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

@@ -528,6 +529,8 @@ func CloseRepoBranchesPulls(doer *models.User, repo *models.Repository) error {
528529
return nil
529530
}
530531

532+
var commitMessageTrailersPattern = regexp.MustCompile(`(?:^|\n\n)(?:[\w-]+[ \t]*:[^\n]+\n*(?:[ \t]+[^\n]+\n*)*)+$`)
533+
531534
// GetSquashMergeCommitMessages returns the commit messages between head and merge base (if there is one)
532535
func GetSquashMergeCommitMessages(pr *models.PullRequest) string {
533536
if err := pr.LoadIssue(); err != nil {
@@ -583,10 +586,13 @@ func GetSquashMergeCommitMessages(pr *models.PullRequest) string {
583586
stringBuilder := strings.Builder{}
584587

585588
if !setting.Repository.PullRequest.PopulateSquashCommentWithCommitMessages {
586-
stringBuilder.WriteString(pr.Issue.Content)
589+
message := strings.TrimSpace(pr.Issue.Content)
590+
stringBuilder.WriteString(message)
587591
if stringBuilder.Len() > 0 {
588592
stringBuilder.WriteRune('\n')
589-
stringBuilder.WriteRune('\n')
593+
if !commitMessageTrailersPattern.MatchString(message) {
594+
stringBuilder.WriteRune('\n')
595+
}
590596
}
591597
}
592598

@@ -657,13 +663,6 @@ func GetSquashMergeCommitMessages(pr *models.PullRequest) string {
657663
}
658664
}
659665

660-
if len(authors) > 0 {
661-
if _, err := stringBuilder.WriteRune('\n'); err != nil {
662-
log.Error("Unable to write to string builder Error: %v", err)
663-
return ""
664-
}
665-
}
666-
667666
for _, author := range authors {
668667
if _, err := stringBuilder.Write([]byte("Co-authored-by: ")); err != nil {
669668
log.Error("Unable to write to string builder Error: %v", err)

services/pull/pull_test.go

Lines changed: 23 additions & 0 deletions
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 <[email protected]>\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 <[email protected]>"))
22+
// Valid trailer section
23+
assert.True(t, commitMessageTrailersPattern.MatchString("Signed-off-by: Bob <[email protected]>"))
24+
assert.True(t, commitMessageTrailersPattern.MatchString("Signed-off-by: Bob <[email protected]>\nOther-Trailer: Value"))
25+
assert.True(t, commitMessageTrailersPattern.MatchString("Message body correctly separated from trailer section by empty line.\n\nSigned-off-by: Bob <[email protected]>"))
26+
assert.True(t, commitMessageTrailersPattern.MatchString("Multiple trailers.\n\nSigned-off-by: Bob <[email protected]>\nOther-Trailer: Value"))
27+
assert.True(t, commitMessageTrailersPattern.MatchString("Newline after trailer section.\n\nSigned-off-by: Bob <[email protected]>\n"))
28+
assert.True(t, commitMessageTrailersPattern.MatchString("No space after colon is accepted.\n\nSigned-off-by:Bob <[email protected]>"))
29+
assert.True(t, commitMessageTrailersPattern.MatchString("Additional whitespace is accepted.\n\nSigned-off-by \t : \tBob <[email protected]> "))
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)