From 7b98e27de3230438f294aa9587f91279bccbf1be Mon Sep 17 00:00:00 2001 From: wilso199 Date: Sun, 2 Aug 2020 21:36:56 -0400 Subject: [PATCH 1/4] Adding logic and documentation for a recurse submodules flag --- command/pr.go | 2 ++ command/pr_checkout.go | 15 +++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/command/pr.go b/command/pr.go index 3f8e53481..70594b8d0 100644 --- a/command/pr.go +++ b/command/pr.go @@ -28,6 +28,8 @@ func init() { RootCmd.AddCommand(prCmd) prCmd.AddCommand(prCheckoutCmd) + prCheckoutCmd.Flags().BoolP("recurse-submodules", "", false, "Update all active submodules (recursively)") + prCmd.AddCommand(prCreateCmd) prCmd.AddCommand(prStatusCmd) prCmd.AddCommand(prCloseCmd) diff --git a/command/pr_checkout.go b/command/pr_checkout.go index 256a3dfe8..0ce94cc55 100644 --- a/command/pr_checkout.go +++ b/command/pr_checkout.go @@ -7,6 +7,7 @@ import ( "os/exec" "strings" + "github.com/MakeNowJust/heredoc" "github.com/spf13/cobra" "github.com/cli/cli/api" @@ -104,6 +105,16 @@ func prCheckout(cmd *cobra.Command, args []string) error { } } + recurseSubmodules, err := cmd.Flags().GetBool("recurse-submodules") + if err != nil { + return err + } + + if recurseSubmodules { + cmdQueue = append(cmdQueue, []string{"git", "submodule", "sync", "--recursive"}) + cmdQueue = append(cmdQueue, []string{"git", "submodule", "update", "--init", "--recursive"}) + } + for _, args := range cmdQueue { cmd := exec.Command(args[0], args[1:]...) cmd.Stdout = os.Stdout @@ -119,6 +130,10 @@ func prCheckout(cmd *cobra.Command, args []string) error { var prCheckoutCmd = &cobra.Command{ Use: "checkout { | | }", Short: "Check out a pull request in Git", + Example: heredoc.Doc(` + $ gh pr checkout 353 + $ gh pr checkout 353 --recurse-submodules + `), Args: func(cmd *cobra.Command, args []string) error { if len(args) < 1 { return errors.New("requires a pull request number as an argument") From 4250906c82f3e21ca98ced085d0c47fee61f2623 Mon Sep 17 00:00:00 2001 From: wilso199 Date: Sun, 2 Aug 2020 21:37:21 -0400 Subject: [PATCH 2/4] Adding test to ensure submodule commands are executed --- command/pr_checkout_test.go | 54 +++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/command/pr_checkout_test.go b/command/pr_checkout_test.go index 525225802..8e5f0f6b7 100644 --- a/command/pr_checkout_test.go +++ b/command/pr_checkout_test.go @@ -565,3 +565,57 @@ func TestPRCheckout_maintainerCanModify(t *testing.T) { eq(t, strings.Join(ranCommands[2], " "), "git config branch.feature.remote https://github.com/hubot/REPO.git") eq(t, strings.Join(ranCommands[3], " "), "git config branch.feature.merge refs/heads/feature") } + +func TestPRCheckout_recurseSubmodules(t *testing.T) { + ctx := context.NewBlank() + ctx.SetBranch("master") + ctx.SetRemotes(map[string]string{ + "origin": "OWNER/REPO", + }) + initContext = func() context.Context { + return ctx + } + http := initFakeHTTP() + http.StubRepoResponse("OWNER", "REPO") + + http.StubResponse(200, bytes.NewBufferString(` + { "data": { "repository": { "pullRequest": { + "number": 123, + "headRefName": "feature", + "headRepositoryOwner": { + "login": "hubot" + }, + "headRepository": { + "name": "REPO", + "defaultBranchRef": { + "name": "master" + } + }, + "isCrossRepository": false, + "maintainerCanModify": false + } } } } + `)) + + ranCommands := [][]string{} + restoreCmd := run.SetPrepareCmd(func(cmd *exec.Cmd) run.Runnable { + switch strings.Join(cmd.Args, " ") { + case "git show-ref --verify -- refs/heads/feature": + return &test.OutputStub{} + default: + ranCommands = append(ranCommands, cmd.Args) + return &test.OutputStub{} + } + }) + defer restoreCmd() + + output, err := RunCommand(`pr checkout 123 --recurse-submodules`) + eq(t, err, nil) + eq(t, output.String(), "") + + eq(t, len(ranCommands), 5) + eq(t, strings.Join(ranCommands[0], " "), "git fetch origin +refs/heads/feature:refs/remotes/origin/feature") + eq(t, strings.Join(ranCommands[1], " "), "git checkout feature") + eq(t, strings.Join(ranCommands[2], " "), "git merge --ff-only refs/remotes/origin/feature") + eq(t, strings.Join(ranCommands[3], " "), "git submodule sync --recursive") + eq(t, strings.Join(ranCommands[4], " "), "git submodule update --init --recursive") +} From 267400a3a5125fb32aaa50361222bbef01bb886d Mon Sep 17 00:00:00 2001 From: wilso199 Date: Sun, 2 Aug 2020 22:37:47 -0400 Subject: [PATCH 3/4] Cleaning up formatting errors and updating test to use named api call --- command/pr.go | 2 +- command/pr_checkout_test.go | 7 ++----- 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/command/pr.go b/command/pr.go index 70594b8d0..96e0a365c 100644 --- a/command/pr.go +++ b/command/pr.go @@ -29,7 +29,7 @@ func init() { RootCmd.AddCommand(prCmd) prCmd.AddCommand(prCheckoutCmd) prCheckoutCmd.Flags().BoolP("recurse-submodules", "", false, "Update all active submodules (recursively)") - + prCmd.AddCommand(prCreateCmd) prCmd.AddCommand(prStatusCmd) prCmd.AddCommand(prCloseCmd) diff --git a/command/pr_checkout_test.go b/command/pr_checkout_test.go index 8e5f0f6b7..b9c3fd34b 100644 --- a/command/pr_checkout_test.go +++ b/command/pr_checkout_test.go @@ -578,7 +578,7 @@ func TestPRCheckout_recurseSubmodules(t *testing.T) { http := initFakeHTTP() http.StubRepoResponse("OWNER", "REPO") - http.StubResponse(200, bytes.NewBufferString(` + http.Register(httpmock.GraphQL(`query PullRequestByNumber\b`), httpmock.StringResponse(` { "data": { "repository": { "pullRequest": { "number": 123, "headRefName": "feature", @@ -586,10 +586,7 @@ func TestPRCheckout_recurseSubmodules(t *testing.T) { "login": "hubot" }, "headRepository": { - "name": "REPO", - "defaultBranchRef": { - "name": "master" - } + "name": "REPO" }, "isCrossRepository": false, "maintainerCanModify": false From 16f69e9c1233b73316c18b481e16e5e3ecfee7c1 Mon Sep 17 00:00:00 2001 From: vilmibm Date: Wed, 19 Aug 2020 16:40:13 -0500 Subject: [PATCH 4/4] fix tests --- pkg/cmd/pr/checkout/checkout_test.go | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-) diff --git a/pkg/cmd/pr/checkout/checkout_test.go b/pkg/cmd/pr/checkout/checkout_test.go index 948de4491..c49fef7e0 100644 --- a/pkg/cmd/pr/checkout/checkout_test.go +++ b/pkg/cmd/pr/checkout/checkout_test.go @@ -567,16 +567,7 @@ func TestPRCheckout_maintainerCanModify(t *testing.T) { } func TestPRCheckout_recurseSubmodules(t *testing.T) { - ctx := context.NewBlank() - ctx.SetBranch("master") - ctx.SetRemotes(map[string]string{ - "origin": "OWNER/REPO", - }) - initContext = func() context.Context { - return ctx - } - http := initFakeHTTP() - http.StubRepoResponse("OWNER", "REPO") + http := &httpmock.Registry{} http.Register(httpmock.GraphQL(`query PullRequestByNumber\b`), httpmock.StringResponse(` { "data": { "repository": { "pullRequest": { @@ -605,7 +596,7 @@ func TestPRCheckout_recurseSubmodules(t *testing.T) { }) defer restoreCmd() - output, err := RunCommand(`pr checkout 123 --recurse-submodules`) + output, err := runCommand(http, nil, "master", `123 --recurse-submodules`) eq(t, err, nil) eq(t, output.String(), "")