Skip to content

Commit 07bee6a

Browse files
committed
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.
1 parent c8cf54c commit 07bee6a

File tree

2 files changed

+35
-4
lines changed

2 files changed

+35
-4
lines changed

pkg/githubtemplate/github_template.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package githubtemplate
22

33
import (
4+
"fmt"
45
"io/ioutil"
56
"path"
67
"regexp"
@@ -53,6 +54,8 @@ mainLoop:
5354

5455
// FindLegacy returns the file path of the default(legacy) template
5556
func FindLegacy(rootDir string, name string) *string {
57+
namePattern := regexp.MustCompile(fmt.Sprintf(`(?i)^%s(\.|$)`, strings.ReplaceAll(name, "_", "[_-]")))
58+
5659
// https://help.github.com/en/github/building-a-strong-community/creating-a-pull-request-template-for-your-repository
5760
candidateDirs := []string{
5861
path.Join(rootDir, ".github"),
@@ -67,7 +70,7 @@ func FindLegacy(rootDir string, name string) *string {
6770

6871
// detect a single template file
6972
for _, file := range files {
70-
if strings.EqualFold(file.Name(), name+".md") {
73+
if namePattern.MatchString(file.Name()) && !file.IsDir() {
7174
result := path.Join(dir, file.Name())
7275
return &result
7376
}

pkg/githubtemplate/github_template_test.go

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,6 @@ func TestFindLegacy(t *testing.T) {
160160
name: "Template in root",
161161
prepare: []string{
162162
"README.md",
163-
"ISSUE_TEMPLATE",
164163
"issue_template.md",
165164
"issue_template.txt",
166165
"pull_request_template.md",
@@ -172,6 +171,32 @@ func TestFindLegacy(t *testing.T) {
172171
},
173172
want: path.Join(tmpdir, "issue_template.md"),
174173
},
174+
{
175+
name: "No extension",
176+
prepare: []string{
177+
"README.md",
178+
"issue_template",
179+
"docs/issue_template.md",
180+
},
181+
args: args{
182+
rootDir: tmpdir,
183+
name: "ISSUE_TEMPLATE",
184+
},
185+
want: path.Join(tmpdir, "issue_template"),
186+
},
187+
{
188+
name: "Dash instead of underscore",
189+
prepare: []string{
190+
"README.md",
191+
"issue-template.txt",
192+
"docs/issue_template.md",
193+
},
194+
args: args{
195+
rootDir: tmpdir,
196+
name: "ISSUE_TEMPLATE",
197+
},
198+
want: path.Join(tmpdir, "issue-template.txt"),
199+
},
175200
{
176201
name: "Template in .github takes precedence",
177202
prepare: []string{
@@ -224,8 +249,11 @@ func TestFindLegacy(t *testing.T) {
224249
file.Close()
225250
}
226251

227-
if got := FindLegacy(tt.args.rootDir, tt.args.name); *got != tt.want {
228-
t.Errorf("Find() = %v, want %v", got, tt.want)
252+
got := FindLegacy(tt.args.rootDir, tt.args.name)
253+
if got == nil {
254+
t.Errorf("FindLegacy() = nil, want %v", tt.want)
255+
} else if *got != tt.want {
256+
t.Errorf("FindLegacy() = %v, want %v", *got, tt.want)
229257
}
230258
})
231259
os.RemoveAll(tmpdir)

0 commit comments

Comments
 (0)