Skip to content

Commit 4fd051b

Browse files
Issue/PR template selection flow mirrors that of GitHub
1 parent 864d74d commit 4fd051b

File tree

3 files changed

+32
-17
lines changed

3 files changed

+32
-17
lines changed

command/issue.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -352,11 +352,11 @@ func issueCreate(cmd *cobra.Command, args []string) error {
352352
return err
353353
}
354354

355-
var templateFiles []string
355+
var nonLegacyTemplateFiles []string
356356
if baseOverride == "" {
357357
if rootDir, err := git.ToplevelDir(); err == nil {
358358
// TODO: figure out how to stub this in tests
359-
templateFiles = githubtemplate.FindNonLegacy(rootDir, "ISSUE_TEMPLATE")
359+
nonLegacyTemplateFiles = githubtemplate.FindNonLegacy(rootDir, "ISSUE_TEMPLATE")
360360
}
361361
}
362362

@@ -397,7 +397,7 @@ func issueCreate(cmd *cobra.Command, args []string) error {
397397
url.QueryEscape(title),
398398
url.QueryEscape(body),
399399
)
400-
} else if len(templateFiles) > 1 {
400+
} else if len(nonLegacyTemplateFiles) > 1 {
401401
openURL += "/choose"
402402
}
403403
cmd.Printf("Opening %s in your browser.\n", displayURL(openURL))
@@ -431,7 +431,14 @@ func issueCreate(cmd *cobra.Command, args []string) error {
431431
interactive := !(cmd.Flags().Changed("title") && cmd.Flags().Changed("body"))
432432

433433
if interactive {
434-
err := titleBodySurvey(cmd, &tb, apiClient, baseRepo, title, body, defaults{}, templateFiles, false, repo.ViewerCanTriage())
434+
var legacyTemplateFile *string
435+
if baseOverride == "" {
436+
if rootDir, err := git.ToplevelDir(); err == nil {
437+
// TODO: figure out how to stub this in tests
438+
legacyTemplateFile = githubtemplate.FindLegacy(rootDir, "ISSUE_TEMPLATE")
439+
}
440+
}
441+
err := titleBodySurvey(cmd, &tb, apiClient, baseRepo, title, body, defaults{}, nonLegacyTemplateFiles, legacyTemplateFile, false, repo.ViewerCanTriage())
435442
if err != nil {
436443
return fmt.Errorf("could not collect title and/or body: %w", err)
437444
}

command/pr_create.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -214,13 +214,14 @@ func prCreate(cmd *cobra.Command, _ []string) error {
214214
interactive := !(cmd.Flags().Changed("title") && cmd.Flags().Changed("body"))
215215

216216
if !isWeb && !autofill && interactive {
217-
var templateFiles []string
217+
var nonLegacyTemplateFiles []string
218+
var legacyTemplateFile *string
218219
if rootDir, err := git.ToplevelDir(); err == nil {
219220
// TODO: figure out how to stub this in tests
220-
templateFiles = githubtemplate.FindNonLegacy(rootDir, "PULL_REQUEST_TEMPLATE")
221+
nonLegacyTemplateFiles = githubtemplate.FindNonLegacy(rootDir, "PULL_REQUEST_TEMPLATE")
222+
legacyTemplateFile = githubtemplate.FindLegacy(rootDir, "ISSUE_TEMPLATE")
221223
}
222-
223-
err := titleBodySurvey(cmd, &tb, client, baseRepo, title, body, defs, templateFiles, true, baseRepo.ViewerCanTriage())
224+
err := titleBodySurvey(cmd, &tb, client, baseRepo, title, body, defs, nonLegacyTemplateFiles, legacyTemplateFile, true, baseRepo.ViewerCanTriage())
224225
if err != nil {
225226
return fmt.Errorf("could not collect title and/or body: %w", err)
226227
}

command/title_body_survey.go

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -107,12 +107,12 @@ func confirmSubmission(allowPreview bool, allowMetadata bool) (Action, error) {
107107
}
108108
}
109109

110-
func selectTemplate(templatePaths []string, metadataType metadataStateType) (string, error) {
110+
func selectTemplate(nonLegacyTemplatePaths []string, legacyTemplatePath *string, metadataType metadataStateType) (string, error) {
111111
templateResponse := struct {
112112
Index int
113113
}{}
114-
templateNames := make([]string, 0, len(templatePaths))
115-
for _, p := range templatePaths {
114+
templateNames := make([]string, 0, len(nonLegacyTemplatePaths))
115+
for _, p := range nonLegacyTemplatePaths {
116116
templateNames = append(templateNames, githubtemplate.ExtractName(p))
117117
}
118118
if metadataType == issueMetadata {
@@ -134,14 +134,19 @@ func selectTemplate(templatePaths []string, metadataType metadataStateType) (str
134134
return "", fmt.Errorf("could not prompt: %w", err)
135135
}
136136

137-
if templateResponse.Index == len(templatePaths) { // the user has selected the blank template
138-
return "", nil
137+
if templateResponse.Index == len(nonLegacyTemplatePaths) { // the user has selected the blank template
138+
if legacyTemplatePath != nil {
139+
templateContents := githubtemplate.ExtractContents(*legacyTemplatePath)
140+
return string(templateContents), nil
141+
} else {
142+
return "", nil
143+
}
139144
}
140-
templateContents := githubtemplate.ExtractContents(templatePaths[templateResponse.Index])
145+
templateContents := githubtemplate.ExtractContents(nonLegacyTemplatePaths[templateResponse.Index])
141146
return string(templateContents), nil
142147
}
143148

144-
func titleBodySurvey(cmd *cobra.Command, issueState *issueMetadataState, apiClient *api.Client, repo ghrepo.Interface, providedTitle, providedBody string, defs defaults, templatePaths []string, allowReviewers, allowMetadata bool) error {
149+
func titleBodySurvey(cmd *cobra.Command, issueState *issueMetadataState, apiClient *api.Client, repo ghrepo.Interface, providedTitle, providedBody string, defs defaults, nonLegacyTemplatePaths []string, legacyTemplatePath *string, allowReviewers, allowMetadata bool) error {
145150
editorCommand, err := determineEditor(cmd)
146151
if err != nil {
147152
return err
@@ -151,13 +156,15 @@ func titleBodySurvey(cmd *cobra.Command, issueState *issueMetadataState, apiClie
151156
templateContents := ""
152157

153158
if providedBody == "" {
154-
if len(templatePaths) > 0 {
159+
if len(nonLegacyTemplatePaths) > 0 {
155160
var err error
156-
templateContents, err = selectTemplate(templatePaths, issueState.Type)
161+
templateContents, err = selectTemplate(nonLegacyTemplatePaths, legacyTemplatePath, issueState.Type)
157162
if err != nil {
158163
return err
159164
}
160165
issueState.Body = templateContents
166+
} else if legacyTemplatePath != nil {
167+
issueState.Body = string(githubtemplate.ExtractContents(*legacyTemplatePath))
161168
} else {
162169
issueState.Body = defs.Body
163170
}

0 commit comments

Comments
 (0)