fix: support multi-line body
All checks were successful
build / build (push) Successful in 1m12s
build / trigger-build-image (push) Successful in 5s

This commit is contained in:
张泊明518370910136 2024-10-06 07:20:11 -04:00
parent 279dce65d1
commit 92bfca19b8
GPG Key ID: D47306D7062CDA9D
2 changed files with 25 additions and 1 deletions

View File

@ -74,7 +74,7 @@ type ConventionalCommit struct {
}
func parseConventionalCommit(commit string) (*ConventionalCommit, error) {
re := regexp.MustCompile(`^(\w+)(\(([^)]+)\))?!?: (.+)(\n\n(.+))?(\n\n(.+))?$`)
re := regexp.MustCompile(`(?s)^(\w+)(\(([^)]+)\))?!?: (.+?)(\n\n(.+?))?(\n\n(.+))?$`)
matches := re.FindStringSubmatch(strings.TrimSpace(commit))
if matches == nil {
return nil, fmt.Errorf("invalid conventional commit format")

View File

@ -69,6 +69,30 @@ func TestParseConventionalCommit(t *testing.T) {
want: nil,
wantErr: true,
},
{
name: "Multi-line body",
commit: "feat(h1/e2): group (#86)\n\nReviewed-on: https://focs.ji.sjtu.edu.cn/git/test/test/pulls/86\nReviewed-by: foo <foo@sjtu.edu.cn>\nReviewed-by: bar <bar@sjtu.edu.cn>\nReviewed-by: nobody <nobody@sjtu.edu.cn>\n",
want: &ConventionalCommit{
Type: "feat",
Scope: "h1/e2",
Description: "group (#86)",
Body: "Reviewed-on: https://focs.ji.sjtu.edu.cn/git/test/test/pulls/86\nReviewed-by: foo <foo@sjtu.edu.cn>\nReviewed-by: bar <bar@sjtu.edu.cn>\nReviewed-by: nobody <nobody@sjtu.edu.cn>",
Footer: "",
},
wantErr: false,
},
{
name: "Multi-line body with footer",
commit: "feat(h1/e2): group (#86)\n\nReviewed-on: https://focs.ji.sjtu.edu.cn/git/test/test/pulls/86\nReviewed-by: foo <foo@sjtu.edu.cn>\nReviewed-by: bar <bar@sjtu.edu.cn>\nReviewed-by: nobody <nobody@sjtu.edu.cn>\n\nFooter here\n",
want: &ConventionalCommit{
Type: "feat",
Scope: "h1/e2",
Description: "group (#86)",
Body: "Reviewed-on: https://focs.ji.sjtu.edu.cn/git/test/test/pulls/86\nReviewed-by: foo <foo@sjtu.edu.cn>\nReviewed-by: bar <bar@sjtu.edu.cn>\nReviewed-by: nobody <nobody@sjtu.edu.cn>",
Footer: "Footer here",
},
wantErr: false,
},
}
for _, tt := range tests {