From 6effcd4261f7574811e4a74e9381f1a9fd531543 Mon Sep 17 00:00:00 2001 From: AliabbasMerchant Date: Mon, 18 May 2020 00:37:28 +0530 Subject: [PATCH] Allow choosing a blank issue/pr template --- command/issue.go | 1 + command/pr_create.go | 1 + command/title_body_survey.go | 54 +++++++++++++++++++++++------------- 3 files changed, 36 insertions(+), 20 deletions(-) diff --git a/command/issue.go b/command/issue.go index 5eed13a13..af3a03fd3 100644 --- a/command/issue.go +++ b/command/issue.go @@ -421,6 +421,7 @@ func issueCreate(cmd *cobra.Command, args []string) error { action := SubmitAction tb := issueMetadataState{ + Type: issueMetadata, Assignees: assignees, Labels: labelNames, Projects: projectNames, diff --git a/command/pr_create.go b/command/pr_create.go index 96dc08791..1100bf9a0 100644 --- a/command/pr_create.go +++ b/command/pr_create.go @@ -203,6 +203,7 @@ func prCreate(cmd *cobra.Command, _ []string) error { } tb := issueMetadataState{ + Type: prMetadata, Reviewers: reviewers, Assignees: assignees, Labels: labelNames, diff --git a/command/title_body_survey.go b/command/title_body_survey.go index 17f7af9fd..311c816ea 100644 --- a/command/title_body_survey.go +++ b/command/title_body_survey.go @@ -13,8 +13,16 @@ import ( ) type Action int +type metadataStateType int + +const ( + issueMetadata metadataStateType = 0 + prMetadata metadataStateType = 1 +) type issueMetadataState struct { + Type metadataStateType + Body string Title string Action Action @@ -99,30 +107,36 @@ func confirmSubmission(allowPreview bool, allowMetadata bool) (Action, error) { } } -func selectTemplate(templatePaths []string) (string, error) { +func selectTemplate(templatePaths []string, metadataType metadataStateType) (string, error) { templateResponse := struct { Index int }{} - if len(templatePaths) > 1 { - templateNames := make([]string, 0, len(templatePaths)) - for _, p := range templatePaths { - templateNames = append(templateNames, githubtemplate.ExtractName(p)) - } - - selectQs := []*survey.Question{ - { - Name: "index", - Prompt: &survey.Select{ - Message: "Choose a template", - Options: templateNames, - }, - }, - } - if err := SurveyAsk(selectQs, &templateResponse); err != nil { - return "", fmt.Errorf("could not prompt: %w", err) - } + templateNames := make([]string, 0, len(templatePaths)) + for _, p := range templatePaths { + templateNames = append(templateNames, githubtemplate.ExtractName(p)) + } + if metadataType == issueMetadata { + templateNames = append(templateNames, "Open a blank issue") + } else if metadataType == prMetadata { + templateNames = append(templateNames, "Open a blank PR") } + selectQs := []*survey.Question{ + { + Name: "index", + Prompt: &survey.Select{ + Message: "Choose a template", + Options: templateNames, + }, + }, + } + if err := SurveyAsk(selectQs, &templateResponse); err != nil { + return "", fmt.Errorf("could not prompt: %w", err) + } + + if templateResponse.Index == len(templatePaths) { // the user has selected the blank template + return "", nil + } templateContents := githubtemplate.ExtractContents(templatePaths[templateResponse.Index]) return string(templateContents), nil } @@ -139,7 +153,7 @@ func titleBodySurvey(cmd *cobra.Command, issueState *issueMetadataState, apiClie if providedBody == "" { if len(templatePaths) > 0 { var err error - templateContents, err = selectTemplate(templatePaths) + templateContents, err = selectTemplate(templatePaths, issueState.Type) if err != nil { return err }