From 70c955ae17967a7dfddb770cb8e7748fe0f4d8a6 Mon Sep 17 00:00:00 2001 From: jolheiser Date: Wed, 22 Mar 2023 04:35:58 +0800 Subject: [PATCH] Title-case PR title (#76) Resolves #60 Extracts the previous trim into a new func as well to keep any title munging consistent. Reviewed-on: https://gitea.com/gitea/changelog/pulls/76 Reviewed-by: Lunny Xiao Reviewed-by: techknowlogick Co-authored-by: jolheiser Co-committed-by: jolheiser --- service/gitea.go | 3 +-- service/github.go | 3 +-- service/service.go | 13 +++++++++++++ service/service_test.go | 24 ++++++++++++++++++++++++ 4 files changed, 39 insertions(+), 4 deletions(-) diff --git a/service/gitea.go b/service/gitea.go index a932f1a..2c85808 100644 --- a/service/gitea.go +++ b/service/gitea.go @@ -6,7 +6,6 @@ package service import ( "fmt" - "strings" "time" "code.gitea.io/sdk/gitea" @@ -93,7 +92,7 @@ func getGiteaTagURL(c *gitea.Client, baseURL, owner, repo, mileName, gitTag, fro func convertToEntry(issue gitea.Issue) Entry { entry := Entry{ Index: issue.Index, - Title: strings.TrimSpace(issue.Title), + Title: CleanTitle(issue.Title), } labels := make([]Label, len(issue.Labels)) diff --git a/service/github.go b/service/github.go index 93119ea..00894b6 100644 --- a/service/github.go +++ b/service/github.go @@ -9,7 +9,6 @@ import ( "fmt" "net/http" "os" - "strings" "time" "github.com/google/go-github/v50/github" @@ -59,7 +58,7 @@ func (gh *GitHub) Generate() (string, []Entry, error) { for _, pr := range result.Issues { if pr.IsPullRequest() == isPull { p := Entry{ - Title: strings.TrimSpace(pr.GetTitle()), + Title: CleanTitle(pr.GetTitle()), Index: int64(pr.GetNumber()), } diff --git a/service/service.go b/service/service.go index de7e76a..685d5b9 100644 --- a/service/service.go +++ b/service/service.go @@ -7,6 +7,7 @@ package service import ( "fmt" "strings" + "unicode" ) const defaultGitea = "https://gitea.com" @@ -86,3 +87,15 @@ func (cl ContributorList) Less(i, j int) bool { func (cl ContributorList) Swap(i, j int) { cl[i], cl[j] = cl[j], cl[i] } + +// CleanTitle returns the string with spaces trimmed and the first rune title-cased +func CleanTitle(s string) string { + + s = strings.TrimSpace(s) + + r := []rune(s) + r[0] = unicode.ToUpper(r[0]) + s = string(r) + + return s +} diff --git a/service/service_test.go b/service/service_test.go index 045e70f..47c57cb 100644 --- a/service/service_test.go +++ b/service/service_test.go @@ -12,3 +12,27 @@ import ( func TestMain(m *testing.M) { os.Exit(m.Run()) } + +func TestCleanTitle(t *testing.T) { + tt := []struct { + Title string + Expected string + }{ + {Title: "foo", Expected: "Foo"}, + {Title: " foo", Expected: "Foo"}, + {Title: "foo bar", Expected: "Foo bar"}, + {Title: "Foo bar", Expected: "Foo bar"}, + {Title: " Foo bar ", Expected: "Foo bar"}, + {Title: "1234", Expected: "1234"}, + } + + for _, tc := range tt { + t.Run(tc.Title, func(t *testing.T) { + s := CleanTitle(tc.Title) + if s != tc.Expected { + t.Logf("got %q | expected %q", s, tc.Expected) + t.Fail() + } + }) + } +}