X Tutup
Skip to content

Commit f5277e4

Browse files
vilmibmvilmibm
authored andcommitted
get everything working
1 parent 0ed7879 commit f5277e4

File tree

7 files changed

+114
-146
lines changed

7 files changed

+114
-146
lines changed

pkg/cmd/issue/create/create.go

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -154,25 +154,29 @@ func createRun(opts *CreateOptions) error {
154154
return err
155155
}
156156

157-
err = prShared.TitleSurvey(&tb)
158-
if err != nil {
159-
return err
157+
if tb.Title == "" {
158+
err = prShared.TitleSurvey(&tb)
159+
if err != nil {
160+
return err
161+
}
160162
}
161163

162-
templateContent := ""
164+
if tb.Body == "" {
165+
templateContent := ""
163166

164-
templateContent, err = prShared.TemplateSurvey(templateFiles, legacyTemplate, tb)
165-
if err != nil {
166-
return err
167-
}
167+
templateContent, err = prShared.TemplateSurvey(templateFiles, legacyTemplate, tb)
168+
if err != nil {
169+
return err
170+
}
168171

169-
err = prShared.BodySurvey(&tb, templateContent, editorCommand)
170-
if err != nil {
171-
return err
172-
}
172+
err = prShared.BodySurvey(&tb, templateContent, editorCommand)
173+
if err != nil {
174+
return err
175+
}
173176

174-
if tb.Body == "" {
175-
tb.Body = templateContent
177+
if tb.Body == "" {
178+
tb.Body = templateContent
179+
}
176180
}
177181

178182
action, err := prShared.ConfirmSubmission(!tb.HasMetadata(), repo.ViewerCanTriage())

pkg/cmd/issue/create/create_test.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -144,18 +144,17 @@ func TestIssueCreate_nonLegacyTemplate(t *testing.T) {
144144

145145
as, teardown := prompt.InitAskStubber()
146146
defer teardown()
147+
148+
// tmeplate
147149
as.Stub([]*prompt.QuestionStub{
148150
{
149151
Name: "index",
150152
Value: 1,
151153
},
152154
})
153-
as.Stub([]*prompt.QuestionStub{
154-
{
155-
Name: "body",
156-
Default: true,
157-
},
158-
})
155+
// body
156+
as.StubOneDefault()
157+
// confirm
159158
as.Stub([]*prompt.QuestionStub{
160159
{
161160
Name: "confirmation",

pkg/cmd/pr/create/create.go

Lines changed: 52 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,6 @@ type CreateOptions struct {
3232
Remotes func() (context.Remotes, error)
3333
Branch func() (string, error)
3434

35-
Interactive bool
36-
3735
TitleProvided bool
3836
BodyProvided bool
3937

@@ -60,14 +58,15 @@ type CreateContext struct {
6058
// This struct stores contextual data about the creation process and is for building up enough
6159
// data to create a pull request
6260
RepoContext *context.ResolvedRemotes
63-
BaseRepo *api.Repository
61+
BaseRepo ghrepo.Interface
6462
HeadRepo ghrepo.Interface
6563
BaseTrackingBranch string
6664
BaseBranch string
6765
HeadBranch string
6866
HeadBranchLabel string
6967
HeadRemote *context.Remote
7068
IsPushEnabled bool
69+
Client *api.Client
7170
}
7271

7372
func NewCmdCreate(f *cmdutil.Factory, runF func(*CreateOptions) error) *cobra.Command {
@@ -104,18 +103,10 @@ func NewCmdCreate(f *cmdutil.Factory, runF func(*CreateOptions) error) *cobra.Co
104103
opts.BodyProvided = cmd.Flags().Changed("body")
105104
opts.RepoOverride, _ = cmd.Flags().GetString("repo")
106105

107-
opts.Interactive = !(opts.TitleProvided && opts.BodyProvided)
108-
109-
// TODO check on edge cases around title/body provision
110-
111106
if !opts.IO.CanPrompt() && !opts.WebMode && !opts.TitleProvided && !opts.Autofill {
112107
return &cmdutil.FlagError{Err: errors.New("--title or --fill required when not running interactively")}
113108
}
114109

115-
if !opts.IO.CanPrompt() {
116-
opts.Interactive = false
117-
}
118-
119110
if opts.IsDraft && opts.WebMode {
120111
return errors.New("the --draft flag is not supported with --web")
121112
}
@@ -148,17 +139,13 @@ func NewCmdCreate(f *cmdutil.Factory, runF func(*CreateOptions) error) *cobra.Co
148139
}
149140

150141
func createRun(opts *CreateOptions) (err error) {
151-
httpClient, err := opts.HttpClient()
152-
if err != nil {
153-
return err
154-
}
155-
client := api.NewClientFromHTTP(httpClient)
156-
157142
ctx, err := NewCreateContext(opts)
158143
if err != nil {
159144
return err
160145
}
161146

147+
client := ctx.Client
148+
162149
var milestoneTitles []string
163150
if opts.Milestone != "" {
164151
milestoneTitles = []string{opts.Milestone}
@@ -173,29 +160,32 @@ func createRun(opts *CreateOptions) (err error) {
173160
Milestones: milestoneTitles,
174161
}
175162

176-
defaultsErr := computeDefaults(*ctx, &state)
177-
if defaultsErr != nil && (opts.Autofill || opts.WebMode || !opts.Interactive) {
178-
return fmt.Errorf("could not compute title or body defaults: %w", defaultsErr)
179-
}
180-
181-
if opts.TitleProvided {
182-
state.Title = opts.Title
183-
}
184-
185-
if opts.BodyProvided {
186-
state.Body = opts.Body
163+
var defaultsErr error
164+
if opts.Autofill || !opts.TitleProvided || !opts.BodyProvided {
165+
defaultsErr = computeDefaults(*ctx, &state)
166+
if defaultsErr != nil {
167+
return fmt.Errorf("could not compute title or body defaults: %w", defaultsErr)
168+
}
187169
}
188170

189171
if opts.WebMode {
172+
if !opts.Autofill {
173+
state.Title = opts.Title
174+
state.Body = opts.Body
175+
}
190176
err := handlePush(*opts, *ctx)
191177
if err != nil {
192178
return err
193179
}
194180
return previewPR(*opts, *ctx, state)
195181
}
196182

197-
if opts.Autofill || !opts.Interactive {
198-
return submitPR(*opts, *ctx, state)
183+
if opts.TitleProvided {
184+
state.Title = opts.Title
185+
}
186+
187+
if opts.BodyProvided {
188+
state.Body = opts.Body
199189
}
200190

201191
existingPR, err := api.PullRequestForBranch(
@@ -216,13 +206,23 @@ func createRun(opts *CreateOptions) (err error) {
216206

217207
cs := opts.IO.ColorScheme()
218208

219-
fmt.Fprintf(opts.IO.ErrOut, message,
220-
cs.Cyan(ctx.HeadBranchLabel),
221-
cs.Cyan(ctx.BaseBranch),
222-
ghrepo.FullName(ctx.BaseRepo))
223-
if (state.Title == "" || state.Body == "") && defaultsErr != nil {
224-
fmt.Fprintf(opts.IO.ErrOut,
225-
"%s warning: could not compute title or body defaults: %s\n", cs.Yellow("!"), defaultsErr)
209+
if opts.IO.CanPrompt() {
210+
fmt.Fprintf(opts.IO.ErrOut, message,
211+
cs.Cyan(ctx.HeadBranchLabel),
212+
cs.Cyan(ctx.BaseBranch),
213+
ghrepo.FullName(ctx.BaseRepo))
214+
if (state.Title == "" || state.Body == "") && defaultsErr != nil {
215+
fmt.Fprintf(opts.IO.ErrOut,
216+
"%s warning: could not compute title or body defaults: %s\n", cs.Yellow("!"), defaultsErr)
217+
}
218+
}
219+
220+
if opts.Autofill || (opts.TitleProvided && opts.BodyProvided) {
221+
err = handlePush(*opts, *ctx)
222+
if err != nil {
223+
return err
224+
}
225+
return submitPR(*opts, *ctx, state)
226226
}
227227

228228
if !opts.TitleProvided {
@@ -256,7 +256,7 @@ func createRun(opts *CreateOptions) (err error) {
256256
}
257257
}
258258

259-
allowMetadata := ctx.BaseRepo.ViewerCanTriage()
259+
allowMetadata := ctx.BaseRepo.(*api.Repository).ViewerCanTriage()
260260
action, err := shared.ConfirmSubmission(!state.HasMetadata(), allowMetadata)
261261
if err != nil {
262262
return fmt.Errorf("unable to confirm: %w", err)
@@ -523,35 +523,32 @@ func NewCreateContext(opts *CreateOptions) (*CreateContext, error) {
523523
HeadRemote: headRemote,
524524
IsPushEnabled: isPushEnabled,
525525
RepoContext: repoContext,
526+
Client: client,
526527
}, nil
527528

528529
}
529530

530-
func submitPR(opts CreateOptions, createCtx CreateContext, state shared.IssueMetadataState) error {
531-
httpClient, err := opts.HttpClient()
532-
if err != nil {
533-
return nil
534-
}
535-
client := api.NewClientFromHTTP(httpClient)
531+
func submitPR(opts CreateOptions, ctx CreateContext, state shared.IssueMetadataState) error {
532+
client := ctx.Client
536533

537534
params := map[string]interface{}{
538535
"title": state.Title,
539536
"body": state.Body,
540537
"draft": opts.IsDraft,
541-
"baseRefName": createCtx.BaseBranch,
542-
"headRefName": createCtx.HeadBranchLabel,
538+
"baseRefName": ctx.BaseBranch,
539+
"headRefName": ctx.HeadBranchLabel,
543540
}
544541

545542
if params["title"] == "" {
546543
return errors.New("pull request title must not be blank")
547544
}
548545

549-
err = shared.AddMetadataToIssueParams(client, createCtx.BaseRepo, params, &state)
546+
err := shared.AddMetadataToIssueParams(client, ctx.BaseRepo, params, &state)
550547
if err != nil {
551548
return err
552549
}
553550

554-
pr, err := api.CreatePullRequest(client, createCtx.BaseRepo, params)
551+
pr, err := api.CreatePullRequest(client, ctx.BaseRepo.(*api.Repository), params)
555552
if pr != nil {
556553
fmt.Fprintln(opts.IO.Out, pr.URL)
557554
}
@@ -564,8 +561,8 @@ func submitPR(opts CreateOptions, createCtx CreateContext, state shared.IssueMet
564561
return nil
565562
}
566563

567-
func previewPR(opts CreateOptions, createCtx CreateContext, state shared.IssueMetadataState) error {
568-
openURL, err := generateCompareURL(createCtx, state)
564+
func previewPR(opts CreateOptions, ctx CreateContext, state shared.IssueMetadataState) error {
565+
openURL, err := generateCompareURL(ctx, state)
569566
if err != nil {
570567
return err
571568
}
@@ -581,13 +578,9 @@ func handlePush(opts CreateOptions, ctx CreateContext) error {
581578
didForkRepo := false
582579
headRepo := ctx.HeadRepo
583580
headRemote := ctx.HeadRemote
581+
client := ctx.Client
584582

585-
httpClient, err := opts.HttpClient()
586-
if err != nil {
587-
return err
588-
}
589-
client := api.NewClientFromHTTP(httpClient)
590-
583+
var err error
591584
// if a head repository could not be determined so far, automatically create
592585
// one by forking the base repository
593586
if headRepo == nil && ctx.IsPushEnabled {
@@ -664,11 +657,11 @@ func handlePush(opts CreateOptions, ctx CreateContext) error {
664657
return nil
665658
}
666659

667-
func generateCompareURL(createCtx CreateContext, state shared.IssueMetadataState) (string, error) {
660+
func generateCompareURL(ctx CreateContext, state shared.IssueMetadataState) (string, error) {
668661
u := ghrepo.GenerateRepoURL(
669-
createCtx.BaseRepo,
662+
ctx.BaseRepo,
670663
"compare/%s...%s?expand=1",
671-
url.QueryEscape(createCtx.BaseBranch), url.QueryEscape(createCtx.HeadBranch))
664+
url.QueryEscape(ctx.BaseBranch), url.QueryEscape(ctx.HeadBranch))
672665
url, err := shared.WithPrAndIssueQueryParams(u, state)
673666
if err != nil {
674667
return "", err

0 commit comments

Comments
 (0)
X Tutup