diff --git a/verify/type.go b/verify/type.go index cf7a28b..986d42b 100644 --- a/verify/type.go +++ b/verify/type.go @@ -19,6 +19,7 @@ package main import ( "fmt" "regexp" + "strings" "github.com/google/go-github/v32/github" @@ -28,6 +29,8 @@ import ( // Extracted from kubernetes/test-infra/prow/plugins/wip/wip-label.go var wipRegex = regexp.MustCompile(`(?i)^\W?WIP\W`) +var tagRegex = regexp.MustCompile(`^\[[\w-\.]*\]`) + type prTitleTypeError struct { title string } @@ -53,8 +56,7 @@ More details can be found at [sigs.k8s.io/kubebuilder-release-tools/VERSIONING.m // verifyPRType checks that the PR title contains a prefix that defines its type func verifyPRType(pr *github.PullRequest) (string, string, error) { - // Remove the WIP prefix if found - title := wipRegex.ReplaceAllString(pr.GetTitle(), "") + title := trimTitle(pr.GetTitle()) prType, finalTitle := notes.PRTypeFromTitle(title) if prType == notes.UncategorizedPR { @@ -66,3 +68,16 @@ func verifyPRType(pr *github.PullRequest) (string, string, error) { %s `, finalTitle), nil } + +func trimTitle(title string) string { + // Remove the WIP prefix if found. + title = wipRegex.ReplaceAllString(title, "") + + // Trim to remove spaces after WIP. + title = strings.TrimSpace(title) + + // Remove a tag prefix if found. + title = tagRegex.ReplaceAllString(title, "") + + return strings.TrimSpace(title) +} diff --git a/verify/type_test.go b/verify/type_test.go new file mode 100644 index 0000000..ca72905 --- /dev/null +++ b/verify/type_test.go @@ -0,0 +1,60 @@ +/* +Copyright 2021 The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package main + +import "testing" + +func Test_trimTitle(t *testing.T) { + tests := []struct { + name string + title string + want string + }{ + { + name: "regular PR title", + title: "📖 book: Use relative links in generate CRDs doc", + want: "📖 book: Use relative links in generate CRDs doc", + }, + { + name: "PR title with WIP", + title: "WIP 📖 book: Use relative links in generate CRDs doc", + want: "📖 book: Use relative links in generate CRDs doc", + }, + { + name: "PR title with [WIP]", + title: "[WIP] 📖 book: Use relative links in generate CRDs doc", + want: "📖 book: Use relative links in generate CRDs doc", + }, + { + name: "PR title with [release-1.0]", + title: "[release-1.0] 📖 book: Use relative links in generate CRDs doc", + want: "📖 book: Use relative links in generate CRDs doc", + }, + { + name: "PR title with [WIP][release-1.0]", + title: "[WIP][release-1.0] 📖 book: Use relative links in generate CRDs doc", + want: "📖 book: Use relative links in generate CRDs doc", + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := trimTitle(tt.title); got != tt.want { + t.Errorf("trimTitle() = %v, want %v", got, tt.want) + } + }) + } +}