X Tutup
Skip to content

Commit 864d74d

Browse files
Categorize Templates as Legacy and NonLegacy
1 parent 01f272d commit 864d74d

File tree

5 files changed

+152
-31
lines changed

5 files changed

+152
-31
lines changed

command/issue.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ func issueCreate(cmd *cobra.Command, args []string) error {
356356
if baseOverride == "" {
357357
if rootDir, err := git.ToplevelDir(); err == nil {
358358
// TODO: figure out how to stub this in tests
359-
templateFiles = githubtemplate.Find(rootDir, "ISSUE_TEMPLATE")
359+
templateFiles = githubtemplate.FindNonLegacy(rootDir, "ISSUE_TEMPLATE")
360360
}
361361
}
362362

command/pr_create.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ func prCreate(cmd *cobra.Command, _ []string) error {
217217
var templateFiles []string
218218
if rootDir, err := git.ToplevelDir(); err == nil {
219219
// TODO: figure out how to stub this in tests
220-
templateFiles = githubtemplate.Find(rootDir, "PULL_REQUEST_TEMPLATE")
220+
templateFiles = githubtemplate.FindNonLegacy(rootDir, "PULL_REQUEST_TEMPLATE")
221221
}
222222

223223
err := titleBodySurvey(cmd, &tb, client, baseRepo, title, body, defs, templateFiles, true, baseRepo.ViewerCanTriage())

command/title_body_survey.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ type Action int
1616
type metadataStateType int
1717

1818
const (
19-
issueMetadata metadataStateType = 0
20-
prMetadata metadataStateType = 1
19+
issueMetadata metadataStateType = iota
20+
prMetadata
2121
)
2222

2323
type issueMetadataState struct {
@@ -118,7 +118,7 @@ func selectTemplate(templatePaths []string, metadataType metadataStateType) (str
118118
if metadataType == issueMetadata {
119119
templateNames = append(templateNames, "Open a blank issue")
120120
} else if metadataType == prMetadata {
121-
templateNames = append(templateNames, "Open a blank PR")
121+
templateNames = append(templateNames, "Open a blank pull request")
122122
}
123123

124124
selectQs := []*survey.Question{

pkg/githubtemplate/github_template.go

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ import (
1010
"gopkg.in/yaml.v3"
1111
)
1212

13-
// Find returns the list of template file paths
14-
func Find(rootDir string, name string) []string {
13+
// FindNonLegacy returns the list of template file paths from the template folder (according to the "upgraded multiple template builder")
14+
func FindNonLegacy(rootDir string, name string) []string {
1515
results := []string{}
1616

1717
// https://help.github.com/en/github/building-a-strong-community/creating-a-pull-request-template-for-your-repository
@@ -46,21 +46,34 @@ mainLoop:
4646
break
4747
}
4848
}
49+
}
50+
sort.Strings(results)
51+
return results
52+
}
53+
54+
// FindLegacy returns the file path of the default(legacy) template
55+
func FindLegacy(rootDir string, name string) *string {
56+
// https://help.github.com/en/github/building-a-strong-community/creating-a-pull-request-template-for-your-repository
57+
candidateDirs := []string{
58+
path.Join(rootDir, ".github"),
59+
rootDir,
60+
path.Join(rootDir, "docs"),
61+
}
62+
for _, dir := range candidateDirs {
63+
files, err := ioutil.ReadDir(dir)
64+
if err != nil {
65+
continue
66+
}
4967

5068
// detect a single template file
5169
for _, file := range files {
5270
if strings.EqualFold(file.Name(), name+".md") {
53-
results = append(results, path.Join(dir, file.Name()))
54-
break
71+
result := path.Join(dir, file.Name())
72+
return &result
5573
}
5674
}
57-
if len(results) > 0 {
58-
break
59-
}
6075
}
61-
62-
sort.Strings(results)
63-
return results
76+
return nil
6477
}
6578

6679
// ExtractName returns the name of the template from YAML front-matter

pkg/githubtemplate/github_template_test.go

Lines changed: 124 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
"testing"
99
)
1010

11-
func TestFind(t *testing.T) {
11+
func TestFindNonLegacy(t *testing.T) {
1212
tmpdir, err := ioutil.TempDir("", "gh-cli")
1313
if err != nil {
1414
t.Fatal(err)
@@ -25,52 +25,69 @@ func TestFind(t *testing.T) {
2525
want []string
2626
}{
2727
{
28-
name: "Template in root",
28+
name: "Legacy templates ignored",
2929
prepare: []string{
3030
"README.md",
3131
"ISSUE_TEMPLATE",
3232
"issue_template.md",
3333
"issue_template.txt",
3434
"pull_request_template.md",
35+
".github/issue_template.md",
36+
"docs/issue_template.md",
37+
},
38+
args: args{
39+
rootDir: tmpdir,
40+
name: "ISSUE_TEMPLATE",
41+
},
42+
want: []string{},
43+
},
44+
{
45+
name: "Template folder in .github takes precedence",
46+
prepare: []string{
47+
"ISSUE_TEMPLATE.md",
48+
"docs/ISSUE_TEMPLATE/abc.md",
49+
"ISSUE_TEMPLATE/abc.md",
50+
".github/ISSUE_TEMPLATE/abc.md",
3551
},
3652
args: args{
3753
rootDir: tmpdir,
3854
name: "ISSUE_TEMPLATE",
3955
},
4056
want: []string{
41-
path.Join(tmpdir, "issue_template.md"),
57+
path.Join(tmpdir, ".github/ISSUE_TEMPLATE/abc.md"),
4258
},
4359
},
4460
{
45-
name: "Template in .github takes precedence",
61+
name: "Template folder in root",
4662
prepare: []string{
4763
"ISSUE_TEMPLATE.md",
48-
".github/issue_template.md",
64+
"docs/ISSUE_TEMPLATE/abc.md",
65+
"ISSUE_TEMPLATE/abc.md",
4966
},
5067
args: args{
5168
rootDir: tmpdir,
5269
name: "ISSUE_TEMPLATE",
5370
},
5471
want: []string{
55-
path.Join(tmpdir, ".github/issue_template.md"),
72+
path.Join(tmpdir, "ISSUE_TEMPLATE/abc.md"),
5673
},
5774
},
5875
{
59-
name: "Template in docs",
76+
name: "Template folder in docs",
6077
prepare: []string{
61-
"README.md",
62-
"docs/issue_template.md",
78+
"ISSUE_TEMPLATE.md",
79+
"docs/ISSUE_TEMPLATE/abc.md",
6380
},
6481
args: args{
6582
rootDir: tmpdir,
6683
name: "ISSUE_TEMPLATE",
6784
},
6885
want: []string{
69-
path.Join(tmpdir, "docs/issue_template.md"),
86+
path.Join(tmpdir, "docs/ISSUE_TEMPLATE/abc.md"),
7087
},
7188
},
7289
{
73-
name: "Multiple templates",
90+
name: "Multiple templates in template folder",
7491
prepare: []string{
7592
".github/ISSUE_TEMPLATE/nope.md",
7693
".github/PULL_REQUEST_TEMPLATE.md",
@@ -90,18 +107,109 @@ func TestFind(t *testing.T) {
90107
},
91108
},
92109
{
93-
name: "Empty multiple templates directory",
110+
name: "Empty template directories",
94111
prepare: []string{
112+
".github/ISSUE_TEMPLATE/.keep",
113+
".docs/ISSUE_TEMPLATE/.keep",
114+
"ISSUE_TEMPLATE/.keep",
115+
},
116+
args: args{
117+
rootDir: tmpdir,
118+
name: "ISSUE_TEMPLATE",
119+
},
120+
want: []string{},
121+
},
122+
}
123+
for _, tt := range tests {
124+
t.Run(tt.name, func(t *testing.T) {
125+
for _, p := range tt.prepare {
126+
fp := path.Join(tmpdir, p)
127+
_ = os.MkdirAll(path.Dir(fp), 0700)
128+
file, err := os.Create(fp)
129+
if err != nil {
130+
t.Fatal(err)
131+
}
132+
file.Close()
133+
}
134+
135+
if got := FindNonLegacy(tt.args.rootDir, tt.args.name); !reflect.DeepEqual(got, tt.want) {
136+
t.Errorf("Find() = %v, want %v", got, tt.want)
137+
}
138+
})
139+
os.RemoveAll(tmpdir)
140+
}
141+
}
142+
143+
func TestFindLegacy(t *testing.T) {
144+
tmpdir, err := ioutil.TempDir("", "gh-cli")
145+
if err != nil {
146+
t.Fatal(err)
147+
}
148+
149+
type args struct {
150+
rootDir string
151+
name string
152+
}
153+
tests := []struct {
154+
name string
155+
prepare []string
156+
args args
157+
want string
158+
}{
159+
{
160+
name: "Template in root",
161+
prepare: []string{
162+
"README.md",
163+
"ISSUE_TEMPLATE",
164+
"issue_template.md",
165+
"issue_template.txt",
166+
"pull_request_template.md",
167+
"docs/issue_template.md",
168+
},
169+
args: args{
170+
rootDir: tmpdir,
171+
name: "ISSUE_TEMPLATE",
172+
},
173+
want: path.Join(tmpdir, "issue_template.md"),
174+
},
175+
{
176+
name: "Template in .github takes precedence",
177+
prepare: []string{
178+
"ISSUE_TEMPLATE.md",
95179
".github/issue_template.md",
96-
".github/issue_template/.keep",
180+
"docs/issue_template.md",
97181
},
98182
args: args{
99183
rootDir: tmpdir,
100184
name: "ISSUE_TEMPLATE",
101185
},
102-
want: []string{
103-
path.Join(tmpdir, ".github/issue_template.md"),
186+
want: path.Join(tmpdir, ".github/issue_template.md"),
187+
},
188+
{
189+
name: "Template in docs",
190+
prepare: []string{
191+
"README.md",
192+
"docs/issue_template.md",
193+
},
194+
args: args{
195+
rootDir: tmpdir,
196+
name: "ISSUE_TEMPLATE",
197+
},
198+
want: path.Join(tmpdir, "docs/issue_template.md"),
199+
},
200+
{
201+
name: "Non legacy templates ignored",
202+
prepare: []string{
203+
".github/PULL_REQUEST_TEMPLATE/abc.md",
204+
"PULL_REQUEST_TEMPLATE/abc.md",
205+
"docs/PULL_REQUEST_TEMPLATE/abc.md",
206+
".github/PULL_REQUEST_TEMPLATE.md",
207+
},
208+
args: args{
209+
rootDir: tmpdir,
210+
name: "PuLl_ReQuEsT_TeMpLaTe",
104211
},
212+
want: path.Join(tmpdir, ".github/PULL_REQUEST_TEMPLATE.md"),
105213
},
106214
}
107215
for _, tt := range tests {
@@ -116,7 +224,7 @@ func TestFind(t *testing.T) {
116224
file.Close()
117225
}
118226

119-
if got := Find(tt.args.rootDir, tt.args.name); !reflect.DeepEqual(got, tt.want) {
227+
if got := FindLegacy(tt.args.rootDir, tt.args.name); *got != tt.want {
120228
t.Errorf("Find() = %v, want %v", got, tt.want)
121229
}
122230
})

0 commit comments

Comments
 (0)
X Tutup