From 83bb1bfd9d359b235b27e3b4606683eae09bac57 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mislav=20Marohni=C4=87?= Date: Wed, 10 Feb 2021 18:18:41 +0100 Subject: [PATCH] Port `pr create` to new templates implementation --- api/client.go | 4 +++ pkg/cmd/pr/create/create.go | 14 ++++++--- pkg/cmd/pr/create/create_test.go | 7 +---- pkg/cmd/pr/shared/survey.go | 54 -------------------------------- 4 files changed, 15 insertions(+), 64 deletions(-) diff --git a/api/client.go b/api/client.go index 09195181b..59056c506 100644 --- a/api/client.go +++ b/api/client.go @@ -111,6 +111,10 @@ type Client struct { http *http.Client } +func (c *Client) HTTP() *http.Client { + return c.http +} + type graphQLResponse struct { Data interface{} Errors []GraphQLError diff --git a/pkg/cmd/pr/create/create.go b/pkg/cmd/pr/create/create.go index 3818249a1..52539b8f5 100644 --- a/pkg/cmd/pr/create/create.go +++ b/pkg/cmd/pr/create/create.go @@ -246,15 +246,21 @@ func createRun(opts *CreateOptions) (err error) { defer shared.PreserveInput(opts.IO, state, &err)() - templateContent := "" if !opts.BodyProvided { + templateContent := "" if opts.RecoverFile == "" { - templateFiles, legacyTemplate := shared.FindTemplates(opts.RootDirOverride, "PULL_REQUEST_TEMPLATE") - - templateContent, err = shared.TemplateSurvey(templateFiles, legacyTemplate, *state) + tpl := shared.NewTemplateManager(client.HTTP(), ctx.BaseRepo, opts.RootDirOverride, opts.RepoOverride == "", true) + var template shared.Template + template, err = tpl.Choose() if err != nil { return } + + if template != nil { + templateContent = string(template.Body()) + } else { + templateContent = string(tpl.LegacyBody()) + } } err = shared.BodySurvey(state, templateContent, editorCommand) diff --git a/pkg/cmd/pr/create/create_test.go b/pkg/cmd/pr/create/create_test.go index 1dbf4c68a..c95b86368 100644 --- a/pkg/cmd/pr/create/create_test.go +++ b/pkg/cmd/pr/create/create_test.go @@ -542,12 +542,7 @@ func TestPRCreate_nonLegacyTemplate(t *testing.T) { as, teardown := prompt.InitAskStubber() defer teardown() - as.Stub([]*prompt.QuestionStub{ - { - Name: "index", - Value: 0, - }, - }) // template + as.StubOne(0) // template as.Stub([]*prompt.QuestionStub{ { Name: "Body", diff --git a/pkg/cmd/pr/shared/survey.go b/pkg/cmd/pr/shared/survey.go index 6c3bdfbe9..74a316262 100644 --- a/pkg/cmd/pr/shared/survey.go +++ b/pkg/cmd/pr/shared/survey.go @@ -74,60 +74,6 @@ func ConfirmSubmission(allowPreview bool, allowMetadata bool) (Action, error) { } } -// Deprecated: use SelectTemplate instead -func TemplateSurvey(templateFiles []string, legacyTemplate string, state IssueMetadataState) (templateContent string, err error) { - if len(templateFiles) == 0 && legacyTemplate == "" { - return - } - - if len(templateFiles) > 0 { - templateContent, err = selectTemplate(templateFiles, legacyTemplate, state.Type) - } else { - templateContent = string(githubtemplate.ExtractContents(legacyTemplate)) - } - - return -} - -func selectTemplate(nonLegacyTemplatePaths []string, legacyTemplatePath string, metadataType metadataStateType) (string, error) { - templateResponse := struct { - Index int - }{} - templateNames := make([]string, 0, len(nonLegacyTemplatePaths)) - for _, p := range nonLegacyTemplatePaths { - 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 pull request") - } - - selectQs := []*survey.Question{ - { - Name: "index", - Prompt: &survey.Select{ - Message: "Choose a template", - Options: templateNames, - }, - }, - } - if err := prompt.SurveyAsk(selectQs, &templateResponse); err != nil { - return "", fmt.Errorf("could not prompt: %w", err) - } - - if templateResponse.Index == len(nonLegacyTemplatePaths) { // the user has selected the blank template - if legacyTemplatePath != "" { - templateContents := githubtemplate.ExtractContents(legacyTemplatePath) - return string(templateContents), nil - } else { - return "", nil - } - } - templateContents := githubtemplate.ExtractContents(nonLegacyTemplatePaths[templateResponse.Index]) - return string(templateContents), nil -} - func BodySurvey(state *IssueMetadataState, templateContent, editorCommand string) error { if templateContent != "" { if state.Body != "" {