Skip to content

Commit 83bb1bf

Browse files
committed
Port pr create to new templates implementation
1 parent 3ddd937 commit 83bb1bf

File tree

4 files changed

+15
-64
lines changed

4 files changed

+15
-64
lines changed

api/client.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,10 @@ type Client struct {
111111
http *http.Client
112112
}
113113

114+
func (c *Client) HTTP() *http.Client {
115+
return c.http
116+
}
117+
114118
type graphQLResponse struct {
115119
Data interface{}
116120
Errors []GraphQLError

pkg/cmd/pr/create/create.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -246,15 +246,21 @@ func createRun(opts *CreateOptions) (err error) {
246246

247247
defer shared.PreserveInput(opts.IO, state, &err)()
248248

249-
templateContent := ""
250249
if !opts.BodyProvided {
250+
templateContent := ""
251251
if opts.RecoverFile == "" {
252-
templateFiles, legacyTemplate := shared.FindTemplates(opts.RootDirOverride, "PULL_REQUEST_TEMPLATE")
253-
254-
templateContent, err = shared.TemplateSurvey(templateFiles, legacyTemplate, *state)
252+
tpl := shared.NewTemplateManager(client.HTTP(), ctx.BaseRepo, opts.RootDirOverride, opts.RepoOverride == "", true)
253+
var template shared.Template
254+
template, err = tpl.Choose()
255255
if err != nil {
256256
return
257257
}
258+
259+
if template != nil {
260+
templateContent = string(template.Body())
261+
} else {
262+
templateContent = string(tpl.LegacyBody())
263+
}
258264
}
259265

260266
err = shared.BodySurvey(state, templateContent, editorCommand)

pkg/cmd/pr/create/create_test.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -542,12 +542,7 @@ func TestPRCreate_nonLegacyTemplate(t *testing.T) {
542542

543543
as, teardown := prompt.InitAskStubber()
544544
defer teardown()
545-
as.Stub([]*prompt.QuestionStub{
546-
{
547-
Name: "index",
548-
Value: 0,
549-
},
550-
}) // template
545+
as.StubOne(0) // template
551546
as.Stub([]*prompt.QuestionStub{
552547
{
553548
Name: "Body",

pkg/cmd/pr/shared/survey.go

Lines changed: 0 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -74,60 +74,6 @@ func ConfirmSubmission(allowPreview bool, allowMetadata bool) (Action, error) {
7474
}
7575
}
7676

77-
// Deprecated: use SelectTemplate instead
78-
func TemplateSurvey(templateFiles []string, legacyTemplate string, state IssueMetadataState) (templateContent string, err error) {
79-
if len(templateFiles) == 0 && legacyTemplate == "" {
80-
return
81-
}
82-
83-
if len(templateFiles) > 0 {
84-
templateContent, err = selectTemplate(templateFiles, legacyTemplate, state.Type)
85-
} else {
86-
templateContent = string(githubtemplate.ExtractContents(legacyTemplate))
87-
}
88-
89-
return
90-
}
91-
92-
func selectTemplate(nonLegacyTemplatePaths []string, legacyTemplatePath string, metadataType metadataStateType) (string, error) {
93-
templateResponse := struct {
94-
Index int
95-
}{}
96-
templateNames := make([]string, 0, len(nonLegacyTemplatePaths))
97-
for _, p := range nonLegacyTemplatePaths {
98-
templateNames = append(templateNames, githubtemplate.ExtractName(p))
99-
}
100-
if metadataType == IssueMetadata {
101-
templateNames = append(templateNames, "Open a blank issue")
102-
} else if metadataType == PRMetadata {
103-
templateNames = append(templateNames, "Open a blank pull request")
104-
}
105-
106-
selectQs := []*survey.Question{
107-
{
108-
Name: "index",
109-
Prompt: &survey.Select{
110-
Message: "Choose a template",
111-
Options: templateNames,
112-
},
113-
},
114-
}
115-
if err := prompt.SurveyAsk(selectQs, &templateResponse); err != nil {
116-
return "", fmt.Errorf("could not prompt: %w", err)
117-
}
118-
119-
if templateResponse.Index == len(nonLegacyTemplatePaths) { // the user has selected the blank template
120-
if legacyTemplatePath != "" {
121-
templateContents := githubtemplate.ExtractContents(legacyTemplatePath)
122-
return string(templateContents), nil
123-
} else {
124-
return "", nil
125-
}
126-
}
127-
templateContents := githubtemplate.ExtractContents(nonLegacyTemplatePaths[templateResponse.Index])
128-
return string(templateContents), nil
129-
}
130-
13177
func BodySurvey(state *IssueMetadataState, templateContent, editorCommand string) error {
13278
if templateContent != "" {
13379
if state.Body != "" {

0 commit comments

Comments
 (0)