refactor: optimize PR title generation and expand test coverage (#549)

- Import "regexp" package in pull_create.go
- Replace existing code for `GetDefaultPRTitle` with a more efficient regular expression approach
- Add a new test file pull_create_test.go with a test function for `GetDefaultPRTitle`

Signed-off-by: appleboy <appleboy.tw@gmail.com>
Reviewed-on: https://gitea.com/gitea/tea/pulls/549
Reviewed-by: Lunny Xiao <xiaolunwen@gmail.com>
Reviewed-by: techknowlogick <techknowlogick@noreply.gitea.io>
Co-authored-by: appleboy <appleboy.tw@gmail.com>
Co-committed-by: appleboy <appleboy.tw@gmail.com>
This commit is contained in:
appleboy 2023-04-30 00:16:18 +08:00 committed by techknowlogick
parent cd6a7b35c3
commit 4915862b95
2 changed files with 46 additions and 7 deletions

View File

@ -6,6 +6,7 @@ package task
import ( import (
"fmt" "fmt"
"regexp"
"strings" "strings"
"code.gitea.io/sdk/gitea" "code.gitea.io/sdk/gitea"
@ -16,6 +17,12 @@ import (
"code.gitea.io/tea/modules/utils" "code.gitea.io/tea/modules/utils"
) )
var (
spaceRegex = regexp.MustCompile(`[\s_-]+`)
noSpace = regexp.MustCompile(`^[^a-zA-Z\s]*`)
consecutive = regexp.MustCompile(`[\s]{2,}`)
)
// CreatePull creates a PR in the given repo and prints the result // CreatePull creates a PR in the given repo and prints the result
func CreatePull(ctx *context.TeaContext, base, head string, allowMaintainerEdits bool, opts *gitea.CreateIssueOption) (err error) { func CreatePull(ctx *context.TeaContext, base, head string, allowMaintainerEdits bool, opts *gitea.CreateIssueOption) (err error) {
// default is default branch // default is default branch
@ -65,7 +72,6 @@ func CreatePull(ctx *context.TeaContext, base, head string, allowMaintainerEdits
Milestone: opts.Milestone, Milestone: opts.Milestone,
Deadline: opts.Deadline, Deadline: opts.Deadline,
}) })
if err != nil { if err != nil {
return fmt.Errorf("could not create PR from %s to %s:%s: %s", head, ctx.Owner, base, err) return fmt.Errorf("could not create PR from %s to %s:%s: %s", head, ctx.Owner, base, err)
} }
@ -133,13 +139,18 @@ func GetHeadSpec(owner, branch, baseOwner string) string {
} }
// GetDefaultPRTitle transforms a string like a branchname to a readable text // GetDefaultPRTitle transforms a string like a branchname to a readable text
func GetDefaultPRTitle(head string) string { func GetDefaultPRTitle(header string) string {
title := head // Extract the part after the last colon in the input string
if strings.Contains(title, ":") { colonIndex := strings.LastIndex(header, ":")
title = strings.SplitN(title, ":", 2)[1] if colonIndex != -1 {
header = header[colonIndex+1:]
} }
title = strings.Replace(title, "-", " ", -1)
title = strings.Replace(title, "_", " ", -1) title := noSpace.ReplaceAllString(header, "")
title = spaceRegex.ReplaceAllString(title, " ")
title = strings.TrimSpace(title)
title = strings.Title(strings.ToLower(title)) title = strings.Title(strings.ToLower(title))
title = consecutive.ReplaceAllString(title, " ")
return title return title
} }

View File

@ -0,0 +1,28 @@
// Copyright 2023 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package task
import "testing"
func TestGetDefaultPRTitle(t *testing.T) {
tests := []struct {
input string
want string
}{
{input: "Add new feature", want: "Add New Feature"},
{input: "update-docs: Fix typo", want: "Fix Typo"},
{input: "remove_long-string", want: "Remove Long String"},
{input: "Replace_Underscores_With_Spaces", want: "Replace Underscores With Spaces"},
{input: " leading-and-trailing-spaces ", want: "Leading And Trailing Spaces"},
{input: "-----No--Upper--Case-----", want: "No Upper Case"},
{input: "", want: ""},
}
for _, test := range tests {
got := GetDefaultPRTitle(test.input)
if got != test.want {
t.Errorf("GetDefaultPRTitle(%q) = %q, want %q", test.input, got, test.want)
}
}
}