diff --git a/command/issue.go b/command/issue.go index 39a8528d0..4ab95e822 100644 --- a/command/issue.go +++ b/command/issue.go @@ -352,11 +352,11 @@ func issueCreate(cmd *cobra.Command, args []string) error { return err } - var templateFiles []string + var nonLegacyTemplateFiles []string if baseOverride == "" { if rootDir, err := git.ToplevelDir(); err == nil { // TODO: figure out how to stub this in tests - templateFiles = githubtemplate.FindNonLegacy(rootDir, "ISSUE_TEMPLATE") + nonLegacyTemplateFiles = githubtemplate.FindNonLegacy(rootDir, "ISSUE_TEMPLATE") } } @@ -397,7 +397,7 @@ func issueCreate(cmd *cobra.Command, args []string) error { url.QueryEscape(title), url.QueryEscape(body), ) - } else if len(templateFiles) > 1 { + } else if len(nonLegacyTemplateFiles) > 1 { openURL += "/choose" } cmd.Printf("Opening %s in your browser.\n", displayURL(openURL)) @@ -431,7 +431,14 @@ func issueCreate(cmd *cobra.Command, args []string) error { interactive := !(cmd.Flags().Changed("title") && cmd.Flags().Changed("body")) if interactive { - err := titleBodySurvey(cmd, &tb, apiClient, baseRepo, title, body, defaults{}, templateFiles, false, repo.ViewerCanTriage()) + var legacyTemplateFile *string + if baseOverride == "" { + if rootDir, err := git.ToplevelDir(); err == nil { + // TODO: figure out how to stub this in tests + legacyTemplateFile = githubtemplate.FindLegacy(rootDir, "ISSUE_TEMPLATE") + } + } + err := titleBodySurvey(cmd, &tb, apiClient, baseRepo, title, body, defaults{}, nonLegacyTemplateFiles, legacyTemplateFile, false, repo.ViewerCanTriage()) if err != nil { return fmt.Errorf("could not collect title and/or body: %w", err) } diff --git a/command/pr_create.go b/command/pr_create.go index 820e516c2..3eea8ec71 100644 --- a/command/pr_create.go +++ b/command/pr_create.go @@ -214,13 +214,14 @@ func prCreate(cmd *cobra.Command, _ []string) error { interactive := !(cmd.Flags().Changed("title") && cmd.Flags().Changed("body")) if !isWeb && !autofill && interactive { - var templateFiles []string + var nonLegacyTemplateFiles []string + var legacyTemplateFile *string if rootDir, err := git.ToplevelDir(); err == nil { // TODO: figure out how to stub this in tests - templateFiles = githubtemplate.FindNonLegacy(rootDir, "PULL_REQUEST_TEMPLATE") + nonLegacyTemplateFiles = githubtemplate.FindNonLegacy(rootDir, "PULL_REQUEST_TEMPLATE") + legacyTemplateFile = githubtemplate.FindLegacy(rootDir, "ISSUE_TEMPLATE") } - - err := titleBodySurvey(cmd, &tb, client, baseRepo, title, body, defs, templateFiles, true, baseRepo.ViewerCanTriage()) + err := titleBodySurvey(cmd, &tb, client, baseRepo, title, body, defs, nonLegacyTemplateFiles, legacyTemplateFile, true, baseRepo.ViewerCanTriage()) if err != nil { return fmt.Errorf("could not collect title and/or body: %w", err) } diff --git a/command/title_body_survey.go b/command/title_body_survey.go index f0ef0e764..a6ac2c538 100644 --- a/command/title_body_survey.go +++ b/command/title_body_survey.go @@ -107,12 +107,12 @@ func confirmSubmission(allowPreview bool, allowMetadata bool) (Action, error) { } } -func selectTemplate(templatePaths []string, metadataType metadataStateType) (string, error) { +func selectTemplate(nonLegacyTemplatePaths []string, legacyTemplatePath *string, metadataType metadataStateType) (string, error) { templateResponse := struct { Index int }{} - templateNames := make([]string, 0, len(templatePaths)) - for _, p := range templatePaths { + templateNames := make([]string, 0, len(nonLegacyTemplatePaths)) + for _, p := range nonLegacyTemplatePaths { templateNames = append(templateNames, githubtemplate.ExtractName(p)) } if metadataType == issueMetadata { @@ -134,14 +134,19 @@ func selectTemplate(templatePaths []string, metadataType metadataStateType) (str return "", fmt.Errorf("could not prompt: %w", err) } - if templateResponse.Index == len(templatePaths) { // the user has selected the blank template - return "", nil + if templateResponse.Index == len(nonLegacyTemplatePaths) { // the user has selected the blank template + if legacyTemplatePath != nil { + templateContents := githubtemplate.ExtractContents(*legacyTemplatePath) + return string(templateContents), nil + } else { + return "", nil + } } - templateContents := githubtemplate.ExtractContents(templatePaths[templateResponse.Index]) + templateContents := githubtemplate.ExtractContents(nonLegacyTemplatePaths[templateResponse.Index]) return string(templateContents), nil } -func titleBodySurvey(cmd *cobra.Command, issueState *issueMetadataState, apiClient *api.Client, repo ghrepo.Interface, providedTitle, providedBody string, defs defaults, templatePaths []string, allowReviewers, allowMetadata bool) error { +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 { editorCommand, err := determineEditor(cmd) if err != nil { return err @@ -151,13 +156,15 @@ func titleBodySurvey(cmd *cobra.Command, issueState *issueMetadataState, apiClie templateContents := "" if providedBody == "" { - if len(templatePaths) > 0 { + if len(nonLegacyTemplatePaths) > 0 { var err error - templateContents, err = selectTemplate(templatePaths, issueState.Type) + templateContents, err = selectTemplate(nonLegacyTemplatePaths, legacyTemplatePath, issueState.Type) if err != nil { return err } issueState.Body = templateContents + } else if legacyTemplatePath != nil { + issueState.Body = string(githubtemplate.ExtractContents(*legacyTemplatePath)) } else { issueState.Body = defs.Body }