From af46fcb1ff037aa46eb9a876b629dfbd788336c0 Mon Sep 17 00:00:00 2001 From: vilmibm Date: Mon, 16 Mar 2020 15:32:47 -0500 Subject: [PATCH] --fill for pr create --- command/pr_create.go | 12 +++++++++- command/pr_create_test.go | 48 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+), 1 deletion(-) diff --git a/command/pr_create.go b/command/pr_create.go index 10cc25716..2330386ee 100644 --- a/command/pr_create.go +++ b/command/pr_create.go @@ -111,9 +111,18 @@ func prCreate(cmd *cobra.Command, _ []string) error { return fmt.Errorf("could not parse web: %q", err) } + autofill, err := cmd.Flags().GetBool("fill") + if err != nil { + return fmt.Errorf("could not parse fill: %q", err) + } + action := SubmitAction if isWeb { action = PreviewAction + } else if autofill { + action = SubmitAction + title = defs.Title + body = defs.Body } else { fmt.Fprintf(colorableErr(cmd), "\nCreating pull request for %s into %s in %s\n\n", utils.Cyan(headBranch), @@ -122,7 +131,7 @@ func prCreate(cmd *cobra.Command, _ []string) error { } // TODO: only drop into interactive mode if stdin & stdout are a tty - if !isWeb && (title == "" || body == "") { + if !isWeb && !autofill && (title == "" || body == "") { var templateFiles []string if rootDir, err := git.ToplevelDir(); err == nil { // TODO: figure out how to stub this in tests @@ -276,4 +285,5 @@ func init() { prCreateCmd.Flags().StringP("base", "B", "", "The branch into which you want your code merged") prCreateCmd.Flags().BoolP("web", "w", false, "Open the web browser to create a pull request") + prCreateCmd.Flags().BoolP("fill", "f", false, "Do not prompt for title/body and just use commit info") } diff --git a/command/pr_create_test.go b/command/pr_create_test.go index 2a1aa20a3..ba2c3a802 100644 --- a/command/pr_create_test.go +++ b/command/pr_create_test.go @@ -322,3 +322,51 @@ func TestPRCreate_survey_defaults_monocommit(t *testing.T) { eq(t, output.String(), "https://github.com/OWNER/REPO/pull/12\n") } + +func TestPRCreate_survey_autofill(t *testing.T) { + initBlankContext("OWNER/REPO", "feature") + http := initFakeHTTP() + http.StubRepoResponse("OWNER", "REPO") + http.StubResponse(200, bytes.NewBufferString(` + { "data": { "createPullRequest": { "pullRequest": { + "URL": "https://github.com/OWNER/REPO/pull/12" + } } } } + `)) + + cs, cmdTeardown := initCmdStubber() + defer cmdTeardown() + + cs.Stub("") // git status + cs.Stub("1234567890,the sky above the port") // git log + cs.Stub("was the color of a television, turned to a dead channel") // git show + cs.Stub("") // git rev-parse + cs.Stub("") // git push + cs.Stub("") // browser open + + output, err := RunCommand(prCreateCmd, `pr create -f`) + eq(t, err, nil) + + bodyBytes, _ := ioutil.ReadAll(http.Requests[1].Body) + reqBody := struct { + Variables struct { + Input struct { + RepositoryID string + Title string + Body string + BaseRefName string + HeadRefName string + } + } + }{} + json.Unmarshal(bodyBytes, &reqBody) + + expectedBody := "was the color of a television, turned to a dead channel" + + eq(t, reqBody.Variables.Input.RepositoryID, "REPOID") + eq(t, reqBody.Variables.Input.Title, "the sky above the port") + eq(t, reqBody.Variables.Input.Body, expectedBody) + eq(t, reqBody.Variables.Input.BaseRefName, "master") + eq(t, reqBody.Variables.Input.HeadRefName, "feature") + + eq(t, output.String(), "https://github.com/OWNER/REPO/pull/12\n") +}