X Tutup
Skip to content

Commit cc1ffb0

Browse files
committed
pass apiClient to determineBaseRepo
Our code had an unspoken assumption that only one apiClient is created during the course of a command. Violating this assumption is fine in almost all cases, but not when we need to do a re-auth to add a new oauth scope to a user's token. There is likely a more elegant solution to the problem but until then this changes determineBaseRepo to use an existing apiClient.
1 parent 3a7f564 commit cc1ffb0

File tree

7 files changed

+28
-35
lines changed

7 files changed

+28
-35
lines changed

api/client.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,6 @@ func CheckScopes(wantedScope string, cb func(string, string) error) ClientOption
9999
if err := cb(wantedScope, appID); err != nil {
100100
return res, err
101101
}
102-
// TODO seems like we should call RoundTrip again? I tried but it didn't work
103102
issuedScopesWarning = true
104103
}
105104

command/issue.go

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ func issueList(cmd *cobra.Command, args []string) error {
106106
return err
107107
}
108108

109-
baseRepo, err := determineBaseRepo(cmd, ctx)
109+
baseRepo, err := determineBaseRepo(apiClient, cmd, ctx)
110110
if err != nil {
111111
return err
112112
}
@@ -167,7 +167,7 @@ func issueStatus(cmd *cobra.Command, args []string) error {
167167
return err
168168
}
169169

170-
baseRepo, err := determineBaseRepo(cmd, ctx)
170+
baseRepo, err := determineBaseRepo(apiClient, cmd, ctx)
171171
if err != nil {
172172
return err
173173
}
@@ -224,7 +224,7 @@ func issueView(cmd *cobra.Command, args []string) error {
224224
return err
225225
}
226226

227-
baseRepo, err := determineBaseRepo(cmd, ctx)
227+
baseRepo, err := determineBaseRepo(apiClient, cmd, ctx)
228228
if err != nil {
229229
return err
230230
}
@@ -340,9 +340,13 @@ func issueFromArg(apiClient *api.Client, baseRepo ghrepo.Interface, arg string)
340340

341341
func issueCreate(cmd *cobra.Command, args []string) error {
342342
ctx := contextForCommand(cmd)
343+
apiClient, err := apiClientForContext(ctx)
344+
if err != nil {
345+
return err
346+
}
343347

344348
// NB no auto forking like over in pr create
345-
baseRepo, err := determineBaseRepo(cmd, ctx)
349+
baseRepo, err := determineBaseRepo(apiClient, cmd, ctx)
346350
if err != nil {
347351
return err
348352
}
@@ -406,11 +410,6 @@ func issueCreate(cmd *cobra.Command, args []string) error {
406410

407411
fmt.Fprintf(colorableErr(cmd), "\nCreating issue in %s\n\n", ghrepo.FullName(baseRepo))
408412

409-
apiClient, err := apiClientForContext(ctx)
410-
if err != nil {
411-
return err
412-
}
413-
414413
repo, err := api.GitHubRepo(apiClient, baseRepo)
415414
if err != nil {
416415
return err
@@ -654,7 +653,7 @@ func issueClose(cmd *cobra.Command, args []string) error {
654653
return err
655654
}
656655

657-
baseRepo, err := determineBaseRepo(cmd, ctx)
656+
baseRepo, err := determineBaseRepo(apiClient, cmd, ctx)
658657
if err != nil {
659658
return err
660659
}
@@ -689,7 +688,7 @@ func issueReopen(cmd *cobra.Command, args []string) error {
689688
return err
690689
}
691690

692-
baseRepo, err := determineBaseRepo(cmd, ctx)
691+
baseRepo, err := determineBaseRepo(apiClient, cmd, ctx)
693692
if err != nil {
694693
return err
695694
}

command/pr.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ func prStatus(cmd *cobra.Command, args []string) error {
104104
return err
105105
}
106106

107-
baseRepo, err := determineBaseRepo(cmd, ctx)
107+
baseRepo, err := determineBaseRepo(apiClient, cmd, ctx)
108108
if err != nil {
109109
return err
110110
}
@@ -168,7 +168,7 @@ func prList(cmd *cobra.Command, args []string) error {
168168
return err
169169
}
170170

171-
baseRepo, err := determineBaseRepo(cmd, ctx)
171+
baseRepo, err := determineBaseRepo(apiClient, cmd, ctx)
172172
if err != nil {
173173
return err
174174
}
@@ -307,7 +307,7 @@ func prView(cmd *cobra.Command, args []string) error {
307307
}
308308

309309
if baseRepo == nil {
310-
baseRepo, err = determineBaseRepo(cmd, ctx)
310+
baseRepo, err = determineBaseRepo(apiClient, cmd, ctx)
311311
if err != nil {
312312
return err
313313
}
@@ -366,7 +366,7 @@ func prClose(cmd *cobra.Command, args []string) error {
366366
return err
367367
}
368368

369-
baseRepo, err := determineBaseRepo(cmd, ctx)
369+
baseRepo, err := determineBaseRepo(apiClient, cmd, ctx)
370370
if err != nil {
371371
return err
372372
}
@@ -401,7 +401,7 @@ func prReopen(cmd *cobra.Command, args []string) error {
401401
return err
402402
}
403403

404-
baseRepo, err := determineBaseRepo(cmd, ctx)
404+
baseRepo, err := determineBaseRepo(apiClient, cmd, ctx)
405405
if err != nil {
406406
return err
407407
}
@@ -438,7 +438,7 @@ func prMerge(cmd *cobra.Command, args []string) error {
438438
return err
439439
}
440440

441-
baseRepo, err := determineBaseRepo(cmd, ctx)
441+
baseRepo, err := determineBaseRepo(apiClient, cmd, ctx)
442442
if err != nil {
443443
return err
444444
}

command/pr_checkout.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func prCheckout(cmd *cobra.Command, args []string) error {
3434
}
3535

3636
if baseRepo == nil {
37-
baseRepo, err = determineBaseRepo(cmd, ctx)
37+
baseRepo, err = determineBaseRepo(apiClient, cmd, ctx)
3838
if err != nil {
3939
return err
4040
}

command/pr_review.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,14 +86,14 @@ func processReviewOpt(cmd *cobra.Command) (*api.PullRequestReviewInput, error) {
8686

8787
func prReview(cmd *cobra.Command, args []string) error {
8888
ctx := contextForCommand(cmd)
89-
baseRepo, err := determineBaseRepo(cmd, ctx)
89+
apiClient, err := apiClientForContext(ctx)
9090
if err != nil {
91-
return fmt.Errorf("could not determine base repo: %w", err)
91+
return err
9292
}
9393

94-
apiClient, err := apiClientForContext(ctx)
94+
baseRepo, err := determineBaseRepo(apiClient, cmd, ctx)
9595
if err != nil {
96-
return err
96+
return fmt.Errorf("could not determine base repo: %w", err)
9797
}
9898

9999
var prNum int

command/repo.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ func repoFork(cmd *cobra.Command, args []string) error {
336336
var repoToFork ghrepo.Interface
337337
inParent := false // whether or not we're forking the repo we're currently "in"
338338
if len(args) == 0 {
339-
baseRepo, err := determineBaseRepo(cmd, ctx)
339+
baseRepo, err := determineBaseRepo(apiClient, cmd, ctx)
340340
if err != nil {
341341
return fmt.Errorf("unable to determine base repository: %w", err)
342342
}
@@ -487,11 +487,15 @@ var Confirm = func(prompt string, result *bool) error {
487487

488488
func repoView(cmd *cobra.Command, args []string) error {
489489
ctx := contextForCommand(cmd)
490+
apiClient, err := apiClientForContext(ctx)
491+
if err != nil {
492+
return err
493+
}
490494

491495
var toView ghrepo.Interface
492496
if len(args) == 0 {
493497
var err error
494-
toView, err = determineBaseRepo(cmd, ctx)
498+
toView, err = determineBaseRepo(apiClient, cmd, ctx)
495499
if err != nil {
496500
return err
497501
}
@@ -512,10 +516,6 @@ func repoView(cmd *cobra.Command, args []string) error {
512516
}
513517
}
514518

515-
apiClient, err := apiClientForContext(ctx)
516-
if err != nil {
517-
return err
518-
}
519519
repo, err := api.GitHubRepo(apiClient, toView)
520520
if err != nil {
521521
return err

command/root.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -218,17 +218,12 @@ func changelogURL(version string) string {
218218
return url
219219
}
220220

221-
func determineBaseRepo(cmd *cobra.Command, ctx context.Context) (ghrepo.Interface, error) {
221+
func determineBaseRepo(apiClient *api.Client, cmd *cobra.Command, ctx context.Context) (ghrepo.Interface, error) {
222222
repo, err := cmd.Flags().GetString("repo")
223223
if err == nil && repo != "" {
224224
return ghrepo.FromFullName(repo), nil
225225
}
226226

227-
apiClient, err := apiClientForContext(ctx)
228-
if err != nil {
229-
return nil, err
230-
}
231-
232227
baseOverride, err := cmd.Flags().GetString("repo")
233228
if err != nil {
234229
return nil, err

0 commit comments

Comments
 (0)
X Tutup