From 07bee6a63c091ecf6359785c06bb77ace6cf797d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mislav=20Marohni=C4=87?= Date: Wed, 15 Jul 2020 18:16:16 +0200 Subject: [PATCH] Improve support for legacy issue/PR template names Now supports names such as `PULL-REQUEST-TEMPLATE` (dashes instead of underscores) and `issue_template.txt` (any file extension, including no extension), is valid. --- pkg/githubtemplate/github_template.go | 5 +++- pkg/githubtemplate/github_template_test.go | 34 ++++++++++++++++++++-- 2 files changed, 35 insertions(+), 4 deletions(-) diff --git a/pkg/githubtemplate/github_template.go b/pkg/githubtemplate/github_template.go index e14ad04c8..cd54ae210 100644 --- a/pkg/githubtemplate/github_template.go +++ b/pkg/githubtemplate/github_template.go @@ -1,6 +1,7 @@ package githubtemplate import ( + "fmt" "io/ioutil" "path" "regexp" @@ -53,6 +54,8 @@ mainLoop: // FindLegacy returns the file path of the default(legacy) template func FindLegacy(rootDir string, name string) *string { + namePattern := regexp.MustCompile(fmt.Sprintf(`(?i)^%s(\.|$)`, strings.ReplaceAll(name, "_", "[_-]"))) + // https://help.github.com/en/github/building-a-strong-community/creating-a-pull-request-template-for-your-repository candidateDirs := []string{ path.Join(rootDir, ".github"), @@ -67,7 +70,7 @@ func FindLegacy(rootDir string, name string) *string { // detect a single template file for _, file := range files { - if strings.EqualFold(file.Name(), name+".md") { + if namePattern.MatchString(file.Name()) && !file.IsDir() { result := path.Join(dir, file.Name()) return &result } diff --git a/pkg/githubtemplate/github_template_test.go b/pkg/githubtemplate/github_template_test.go index ee5dfb625..d1c73fec7 100644 --- a/pkg/githubtemplate/github_template_test.go +++ b/pkg/githubtemplate/github_template_test.go @@ -160,7 +160,6 @@ func TestFindLegacy(t *testing.T) { name: "Template in root", prepare: []string{ "README.md", - "ISSUE_TEMPLATE", "issue_template.md", "issue_template.txt", "pull_request_template.md", @@ -172,6 +171,32 @@ func TestFindLegacy(t *testing.T) { }, want: path.Join(tmpdir, "issue_template.md"), }, + { + name: "No extension", + prepare: []string{ + "README.md", + "issue_template", + "docs/issue_template.md", + }, + args: args{ + rootDir: tmpdir, + name: "ISSUE_TEMPLATE", + }, + want: path.Join(tmpdir, "issue_template"), + }, + { + name: "Dash instead of underscore", + prepare: []string{ + "README.md", + "issue-template.txt", + "docs/issue_template.md", + }, + args: args{ + rootDir: tmpdir, + name: "ISSUE_TEMPLATE", + }, + want: path.Join(tmpdir, "issue-template.txt"), + }, { name: "Template in .github takes precedence", prepare: []string{ @@ -224,8 +249,11 @@ func TestFindLegacy(t *testing.T) { file.Close() } - if got := FindLegacy(tt.args.rootDir, tt.args.name); *got != tt.want { - t.Errorf("Find() = %v, want %v", got, tt.want) + got := FindLegacy(tt.args.rootDir, tt.args.name) + if got == nil { + t.Errorf("FindLegacy() = nil, want %v", tt.want) + } else if *got != tt.want { + t.Errorf("FindLegacy() = %v, want %v", *got, tt.want) } }) os.RemoveAll(tmpdir)