Port pr create to new templates implementation

This commit is contained in:
Mislav Marohnić 2021-02-10 18:18:41 +01:00
parent 3ddd93793c
commit 83bb1bfd9d
4 changed files with 15 additions and 64 deletions

View file

@ -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

View file

@ -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)

View file

@ -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",

View file

@ -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 != "" {