forked from cli/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemplates.go
More file actions
320 lines (266 loc) · 7.1 KB
/
templates.go
File metadata and controls
320 lines (266 loc) · 7.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
package shared
import (
"context"
"fmt"
"net/http"
"time"
"github.com/AlecAivazis/survey/v2"
"github.com/cli/cli/v2/api"
"github.com/cli/cli/v2/git"
"github.com/cli/cli/v2/internal/ghinstance"
"github.com/cli/cli/v2/internal/ghrepo"
"github.com/cli/cli/v2/pkg/githubtemplate"
"github.com/cli/cli/v2/pkg/prompt"
graphql "github.com/cli/shurcooL-graphql"
"github.com/shurcooL/githubv4"
)
type issueTemplate struct {
// I would have un-exported these fields, except `cli/shurcool-graphql` then cannot unmarshal them :/
Gname string `graphql:"name"`
Gbody string `graphql:"body"`
}
type pullRequestTemplate struct {
// I would have un-exported these fields, except `cli/shurcool-graphql` then cannot unmarshal them :/
Gname string `graphql:"filename"`
Gbody string `graphql:"body"`
}
func (t *issueTemplate) Name() string {
return t.Gname
}
func (t *issueTemplate) NameForSubmit() string {
return t.Gname
}
func (t *issueTemplate) Body() []byte {
return []byte(t.Gbody)
}
func (t *pullRequestTemplate) Name() string {
return t.Gname
}
func (t *pullRequestTemplate) NameForSubmit() string {
return ""
}
func (t *pullRequestTemplate) Body() []byte {
return []byte(t.Gbody)
}
func listIssueTemplates(httpClient *http.Client, repo ghrepo.Interface) ([]Template, error) {
var query struct {
Repository struct {
IssueTemplates []issueTemplate
} `graphql:"repository(owner: $owner, name: $name)"`
}
variables := map[string]interface{}{
"owner": githubv4.String(repo.RepoOwner()),
"name": githubv4.String(repo.RepoName()),
}
gql := graphql.NewClient(ghinstance.GraphQLEndpoint(repo.RepoHost()), httpClient)
err := gql.QueryNamed(context.Background(), "IssueTemplates", &query, variables)
if err != nil {
return nil, err
}
ts := query.Repository.IssueTemplates
templates := make([]Template, len(ts))
for i := range templates {
templates[i] = &ts[i]
}
return templates, nil
}
func listPullRequestTemplates(httpClient *http.Client, repo ghrepo.Interface) ([]Template, error) {
var query struct {
Repository struct {
PullRequestTemplates []pullRequestTemplate
} `graphql:"repository(owner: $owner, name: $name)"`
}
variables := map[string]interface{}{
"owner": githubv4.String(repo.RepoOwner()),
"name": githubv4.String(repo.RepoName()),
}
gql := graphql.NewClient(ghinstance.GraphQLEndpoint(repo.RepoHost()), httpClient)
err := gql.QueryNamed(context.Background(), "PullRequestTemplates", &query, variables)
if err != nil {
return nil, err
}
ts := query.Repository.PullRequestTemplates
templates := make([]Template, len(ts))
for i := range templates {
templates[i] = &ts[i]
}
return templates, nil
}
func hasTemplateSupport(httpClient *http.Client, hostname string, isPR bool) (bool, error) {
if !ghinstance.IsEnterprise(hostname) {
return true, nil
}
var featureDetection struct {
Repository struct {
Fields []struct {
Name string
} `graphql:"fields(includeDeprecated: true)"`
} `graphql:"Repository: __type(name: \"Repository\")"`
CreateIssueInput struct {
InputFields []struct {
Name string
}
} `graphql:"CreateIssueInput: __type(name: \"CreateIssueInput\")"`
}
gql := graphql.NewClient(ghinstance.GraphQLEndpoint(hostname), httpClient)
err := gql.QueryNamed(context.Background(), "IssueTemplates_fields", &featureDetection, nil)
if err != nil {
return false, err
}
var hasIssueQuerySupport bool
var hasIssueMutationSupport bool
var hasPullRequestQuerySupport bool
for _, field := range featureDetection.Repository.Fields {
if field.Name == "issueTemplates" {
hasIssueQuerySupport = true
}
if field.Name == "pullRequestTemplates" {
hasPullRequestQuerySupport = true
}
}
for _, field := range featureDetection.CreateIssueInput.InputFields {
if field.Name == "issueTemplate" {
hasIssueMutationSupport = true
}
}
if isPR {
return hasPullRequestQuerySupport, nil
} else {
return hasIssueQuerySupport && hasIssueMutationSupport, nil
}
}
type Template interface {
Name() string
NameForSubmit() string
Body() []byte
}
type templateManager struct {
repo ghrepo.Interface
rootDir string
allowFS bool
isPR bool
httpClient *http.Client
cachedClient *http.Client
templates []Template
legacyTemplate Template
didFetch bool
fetchError error
}
func NewTemplateManager(httpClient *http.Client, repo ghrepo.Interface, dir string, allowFS bool, isPR bool) *templateManager {
return &templateManager{
repo: repo,
rootDir: dir,
allowFS: allowFS,
isPR: isPR,
httpClient: httpClient,
}
}
func (m *templateManager) hasAPI() (bool, error) {
if m.cachedClient == nil {
m.cachedClient = api.NewCachedClient(m.httpClient, time.Hour*24)
}
return hasTemplateSupport(m.cachedClient, m.repo.RepoHost(), m.isPR)
}
func (m *templateManager) HasTemplates() (bool, error) {
if err := m.memoizedFetch(); err != nil {
return false, err
}
return len(m.templates) > 0, nil
}
func (m *templateManager) LegacyBody() []byte {
if m.legacyTemplate == nil {
return nil
}
return m.legacyTemplate.Body()
}
func (m *templateManager) Choose() (Template, error) {
if err := m.memoizedFetch(); err != nil {
return nil, err
}
if len(m.templates) == 0 {
return nil, nil
}
names := make([]string, len(m.templates))
for i, t := range m.templates {
names[i] = t.Name()
}
blankOption := "Open a blank issue"
if m.isPR {
blankOption = "Open a blank pull request"
}
var selectedOption int
err := prompt.SurveyAskOne(&survey.Select{
Message: "Choose a template",
Options: append(names, blankOption),
}, &selectedOption)
if err != nil {
return nil, fmt.Errorf("could not prompt: %w", err)
}
if selectedOption == len(names) {
return nil, nil
}
return m.templates[selectedOption], nil
}
func (m *templateManager) memoizedFetch() error {
if m.didFetch {
return m.fetchError
}
m.fetchError = m.fetch()
m.didFetch = true
return m.fetchError
}
func (m *templateManager) fetch() error {
hasAPI, err := m.hasAPI()
if err != nil {
return err
}
if hasAPI {
lister := listIssueTemplates
if m.isPR {
lister = listPullRequestTemplates
}
templates, err := lister(m.httpClient, m.repo)
if err != nil {
return err
}
m.templates = templates
}
if !m.allowFS {
return nil
}
dir := m.rootDir
if dir == "" {
var err error
dir, err = git.ToplevelDir()
if err != nil {
return nil // abort silently
}
}
filePattern := "ISSUE_TEMPLATE"
if m.isPR {
filePattern = "PULL_REQUEST_TEMPLATE"
}
if !hasAPI {
issueTemplates := githubtemplate.FindNonLegacy(dir, filePattern)
m.templates = make([]Template, len(issueTemplates))
for i, t := range issueTemplates {
m.templates[i] = &filesystemTemplate{path: t}
}
}
if legacyTemplate := githubtemplate.FindLegacy(dir, filePattern); legacyTemplate != "" {
m.legacyTemplate = &filesystemTemplate{path: legacyTemplate}
}
return nil
}
type filesystemTemplate struct {
path string
}
func (t *filesystemTemplate) Name() string {
return githubtemplate.ExtractName(t.path)
}
func (t *filesystemTemplate) NameForSubmit() string {
return ""
}
func (t *filesystemTemplate) Body() []byte {
return githubtemplate.ExtractContents(t.path)
}