From e341d7b49f6e14cd6ac67055131febf9109b2e05 Mon Sep 17 00:00:00 2001 From: Corey Johnson Date: Wed, 27 May 2020 08:38:30 -0700 Subject: [PATCH] Fix test --- command/pr.go | 10 +++++----- command/pr_checkout.go | 2 +- command/pr_diff.go | 2 +- command/pr_diff_test.go | 5 +++++ command/pr_lookup.go | 33 +++++++++------------------------ command/pr_review.go | 2 +- 6 files changed, 22 insertions(+), 32 deletions(-) diff --git a/command/pr.go b/command/pr.go index e40e8af44..c9cc095d5 100644 --- a/command/pr.go +++ b/command/pr.go @@ -315,7 +315,7 @@ func prView(cmd *cobra.Command, args []string) error { return err } - pr, err := prFromArgs(ctx, baseRepo, args...) + pr, err := prFromArgs(ctx, apiClient, baseRepo, args...) if err != nil { return err } @@ -342,7 +342,7 @@ func prClose(cmd *cobra.Command, args []string) error { return err } - pr, err := prFromArgs(ctx, baseRepo, args...) + pr, err := prFromArgs(ctx, apiClient, baseRepo, args...) if err != nil { return err } @@ -377,7 +377,7 @@ func prReopen(cmd *cobra.Command, args []string) error { return err } - pr, err := prFromArgs(ctx, baseRepo, args...) + pr, err := prFromArgs(ctx, apiClient, baseRepo, args...) if err != nil { return err } @@ -414,7 +414,7 @@ func prMerge(cmd *cobra.Command, args []string) error { return err } - pr, err := prFromArgs(ctx, baseRepo, args...) + pr, err := prFromArgs(ctx, apiClient, baseRepo, args...) if err != nil { return err } @@ -657,7 +657,7 @@ func prReady(cmd *cobra.Command, args []string) error { return err } - pr, err := prFromArgs(ctx, baseRepo, args...) + pr, err := prFromArgs(ctx, apiClient, baseRepo, args...) if err != nil { return err } diff --git a/command/pr_checkout.go b/command/pr_checkout.go index dd5caf6db..a8bf710b9 100644 --- a/command/pr_checkout.go +++ b/command/pr_checkout.go @@ -42,7 +42,7 @@ func prCheckout(cmd *cobra.Command, args []string) error { } } - pr, err := prFromArgs(ctx, baseRepo, prString) + pr, err := prFromArgs(ctx, apiClient, baseRepo, prString) if err != nil { return err } diff --git a/command/pr_diff.go b/command/pr_diff.go index 22217b35b..48ca42bbb 100644 --- a/command/pr_diff.go +++ b/command/pr_diff.go @@ -41,7 +41,7 @@ func prDiff(cmd *cobra.Command, args []string) error { return fmt.Errorf("could not determine base repo: %w", err) } - pr, err := prFromArgs(ctx, baseRepo, args...) + pr, err := prFromArgs(ctx, apiClient, baseRepo, args...) if err != nil { return fmt.Errorf("could not find pull request: %w", err) } diff --git a/command/pr_diff_test.go b/command/pr_diff_test.go index ab883dcab..725b21f03 100644 --- a/command/pr_diff_test.go +++ b/command/pr_diff_test.go @@ -37,6 +37,11 @@ func TestPRDiff_argument_not_found(t *testing.T) { initBlankContext("", "OWNER/REPO", "master") http := initFakeHTTP() http.StubRepoResponse("OWNER", "REPO") + http.StubResponse(200, bytes.NewBufferString(` + { "data": { "repository": { + "pullRequest": { "number": 123 } + } } } +`)) http.StubResponse(404, bytes.NewBufferString("")) _, err := RunCommand("pr diff 123") if err == nil { diff --git a/command/pr_lookup.go b/command/pr_lookup.go index 7f169ff77..df97930c6 100644 --- a/command/pr_lookup.go +++ b/command/pr_lookup.go @@ -12,25 +12,20 @@ import ( "github.com/cli/cli/internal/ghrepo" ) -func prFromArgs(ctx context.Context, repo ghrepo.Interface, args ...string) (*api.PullRequest, error) { - apiClient, err := apiClientForContext(ctx) - if err != nil { - return nil, err - } - +func prFromArgs(ctx context.Context, apiClient *api.Client, repo ghrepo.Interface, args ...string) (*api.PullRequest, error) { if len(args) == 0 { - return prForCurrentBranch(ctx, repo) + return prForCurrentBranch(ctx, apiClient, repo) } // First check to see if the prString is a url prString := args[0] - pr, err := prFromURL(ctx, repo, prString) + pr, err := prFromURL(ctx, apiClient, repo, prString) if pr != nil || err != nil { return pr, err } // Next see if the prString is a number and use that to look up the url - pr, err = prFromNumberString(ctx, repo, prString) + pr, err = prFromNumberString(ctx, apiClient, repo, prString) if pr != nil || err != nil { return pr, err } @@ -39,12 +34,7 @@ func prFromArgs(ctx context.Context, repo ghrepo.Interface, args ...string) (*ap return api.PullRequestForBranch(apiClient, repo, "", prString) } -func prFromNumberString(ctx context.Context, repo ghrepo.Interface, s string) (*api.PullRequest, error) { - apiClient, err := apiClientForContext(ctx) - if err != nil { - return nil, err - } - +func prFromNumberString(ctx context.Context, apiClient *api.Client, repo ghrepo.Interface, s string) (*api.PullRequest, error) { if prNumber, err := strconv.Atoi(strings.TrimPrefix(s, "#")); err == nil { return api.PullRequestByNumber(apiClient, repo, prNumber) } @@ -52,22 +42,17 @@ func prFromNumberString(ctx context.Context, repo ghrepo.Interface, s string) (* return nil, nil } -func prFromURL(ctx context.Context, repo ghrepo.Interface, s string) (*api.PullRequest, error) { +func prFromURL(ctx context.Context, apiClient *api.Client, repo ghrepo.Interface, s string) (*api.PullRequest, error) { r := regexp.MustCompile(`^https://github\.com/([^/]+)/([^/]+)/pull/(\d+)`) if m := r.FindStringSubmatch(s); m != nil { prNumberString := m[3] - return prFromNumberString(ctx, repo, prNumberString) + return prFromNumberString(ctx, apiClient, repo, prNumberString) } return nil, nil } -func prForCurrentBranch(ctx context.Context, repo ghrepo.Interface) (*api.PullRequest, error) { - apiClient, err := apiClientForContext(ctx) - if err != nil { - return nil, err - } - +func prForCurrentBranch(ctx context.Context, apiClient *api.Client, repo ghrepo.Interface) (*api.PullRequest, error) { prHeadRef, err := ctx.Branch() if err != nil { return nil, err @@ -78,7 +63,7 @@ func prForCurrentBranch(ctx context.Context, repo ghrepo.Interface) (*api.PullRe // the branch is configured to merge a special PR head ref prHeadRE := regexp.MustCompile(`^refs/pull/(\d+)/head$`) if m := prHeadRE.FindStringSubmatch(branchConfig.MergeRef); m != nil { - return prFromNumberString(ctx, repo, m[1]) + return prFromNumberString(ctx, apiClient, repo, m[1]) } var branchOwner string diff --git a/command/pr_review.go b/command/pr_review.go index 5e3b122d3..3d2d17558 100644 --- a/command/pr_review.go +++ b/command/pr_review.go @@ -94,7 +94,7 @@ func prReview(cmd *cobra.Command, args []string) error { return fmt.Errorf("could not determine base repo: %w", err) } - pr, err := prFromArgs(ctx, baseRepo, args...) + pr, err := prFromArgs(ctx, apiClient, baseRepo, args...) if err != nil { return err }