Skip to content

Commit

Permalink
Merge pull request #44 from sbueringer/pr-pr-verify-allow-tag-prefixes
Browse files Browse the repository at this point in the history
🌱 Allow tag prefixes in PR titles
  • Loading branch information
k8s-ci-robot committed Feb 16, 2022
2 parents cf8721e + 8d0f60f commit 3892c77
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 2 deletions.
19 changes: 17 additions & 2 deletions verify/type.go
Expand Up @@ -19,6 +19,7 @@ package main
import (
"fmt"
"regexp"
"strings"

"github.com/google/go-github/v32/github"

Expand All @@ -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
}
Expand All @@ -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 {
Expand All @@ -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)
}
60 changes: 60 additions & 0 deletions 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)
}
})
}
}

0 comments on commit 3892c77

Please sign in to comment.