X Tutup
Skip to content

Commit 33e11f0

Browse files
author
Nate Smith
authored
Merge pull request cli#1611 from dhleong/dhleong/pr-prepend-commit-bodies
Prepend PR body defaults to the selected template (if any)
2 parents adc01d0 + c7e7022 commit 33e11f0

File tree

2 files changed

+87
-5
lines changed

2 files changed

+87
-5
lines changed

pkg/cmd/pr/create/create_test.go

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import (
55
"encoding/json"
66
"io/ioutil"
77
"net/http"
8+
"os"
9+
"path"
810
"reflect"
911
"strings"
1012
"testing"
@@ -314,7 +316,7 @@ func TestPRCreate_nonLegacyTemplate(t *testing.T) {
314316

315317
eq(t, reqBody.Variables.Input.RepositoryID, "REPOID")
316318
eq(t, reqBody.Variables.Input.Title, "my title")
317-
eq(t, reqBody.Variables.Input.Body, "Fixes a bug and Closes an issue")
319+
eq(t, reqBody.Variables.Input.Body, "- commit 1\n- commit 0\n\nFixes a bug and Closes an issue")
318320
eq(t, reqBody.Variables.Input.BaseRefName, "master")
319321
eq(t, reqBody.Variables.Input.HeadRefName, "feature")
320322

@@ -851,6 +853,77 @@ func TestPRCreate_survey_defaults_monocommit(t *testing.T) {
851853
eq(t, output.String(), "https://github.com/OWNER/REPO/pull/12\n")
852854
}
853855

856+
func TestPRCreate_survey_defaults_monocommit_template(t *testing.T) {
857+
http := initFakeHTTP()
858+
defer http.Verify(t)
859+
860+
http.Register(httpmock.GraphQL(`query RepositoryNetwork\b`), httpmock.StringResponse(httpmock.RepoNetworkStubResponse("OWNER", "REPO", "master", "WRITE")))
861+
http.Register(httpmock.GraphQL(`query RepositoryFindFork\b`), httpmock.StringResponse(`
862+
{ "data": { "repository": { "forks": { "nodes": [
863+
] } } } }
864+
`))
865+
http.Register(httpmock.GraphQL(`query PullRequestForBranch\b`), httpmock.StringResponse(`
866+
{ "data": { "repository": { "pullRequests": { "nodes" : [
867+
] } } } }
868+
`))
869+
http.Register(httpmock.GraphQL(`mutation PullRequestCreate\b`), httpmock.GraphQLMutation(`
870+
{ "data": { "createPullRequest": { "pullRequest": {
871+
"URL": "https://github.com/OWNER/REPO/pull/12"
872+
} } } }
873+
`, func(inputs map[string]interface{}) {
874+
eq(t, inputs["repositoryId"], "REPOID")
875+
eq(t, inputs["title"], "the sky above the port")
876+
eq(t, inputs["body"], "was the color of a television\n\n... turned to a dead channel")
877+
eq(t, inputs["baseRefName"], "master")
878+
eq(t, inputs["headRefName"], "feature")
879+
}))
880+
881+
cs, cmdTeardown := test.InitCmdStubber()
882+
defer cmdTeardown()
883+
884+
tmpdir, err := ioutil.TempDir("", "gh-cli")
885+
if err != nil {
886+
t.Fatal(err)
887+
}
888+
defer os.RemoveAll(tmpdir)
889+
890+
templateFp := path.Join(tmpdir, ".github/PULL_REQUEST_TEMPLATE.md")
891+
_ = os.MkdirAll(path.Dir(templateFp), 0700)
892+
_ = ioutil.WriteFile(templateFp, []byte("... turned to a dead channel"), 0700)
893+
894+
cs.Stub("") // git config --get-regexp (determineTrackingBranch)
895+
cs.Stub("") // git show-ref --verify (determineTrackingBranch)
896+
cs.Stub("") // git status
897+
cs.Stub("1234567890,the sky above the port") // git log
898+
cs.Stub("was the color of a television") // git show
899+
cs.Stub(tmpdir) // git rev-parse
900+
cs.Stub("") // git push
901+
902+
as, surveyTeardown := prompt.InitAskStubber()
903+
defer surveyTeardown()
904+
905+
as.Stub([]*prompt.QuestionStub{
906+
{
907+
Name: "title",
908+
Default: true,
909+
},
910+
{
911+
Name: "body",
912+
Default: true,
913+
},
914+
})
915+
as.Stub([]*prompt.QuestionStub{
916+
{
917+
Name: "confirmation",
918+
Value: 0,
919+
},
920+
})
921+
922+
output, err := runCommand(http, nil, "feature", true, ``)
923+
eq(t, err, nil)
924+
eq(t, output.String(), "https://github.com/OWNER/REPO/pull/12\n")
925+
}
926+
854927
func TestPRCreate_survey_autofill_nontty(t *testing.T) {
855928
http := initFakeHTTP()
856929
defer http.Verify(t)

pkg/cmd/pr/shared/title_body_survey.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package shared
22

33
import (
44
"fmt"
5+
"strings"
56

67
"github.com/AlecAivazis/survey/v2"
78
"github.com/cli/cli/api"
@@ -154,18 +155,26 @@ func TitleBodySurvey(io *iostreams.IOStreams, editorCommand string, issueState *
154155
templateContents := ""
155156

156157
if providedBody == "" {
158+
issueState.Body = defs.Body
159+
157160
if len(nonLegacyTemplatePaths) > 0 {
158161
var err error
159162
templateContents, err = selectTemplate(nonLegacyTemplatePaths, legacyTemplatePath, issueState.Type)
160163
if err != nil {
161164
return err
162165
}
163-
issueState.Body = templateContents
166+
164167
} else if legacyTemplatePath != nil {
165168
templateContents = string(githubtemplate.ExtractContents(*legacyTemplatePath))
166-
issueState.Body = templateContents
167-
} else {
168-
issueState.Body = defs.Body
169+
}
170+
171+
if templateContents != "" {
172+
if issueState.Body != "" {
173+
// prevent excessive newlines between default body and template
174+
issueState.Body = strings.TrimRight(issueState.Body, "\n")
175+
issueState.Body += "\n\n"
176+
}
177+
issueState.Body += templateContents
169178
}
170179
}
171180

0 commit comments

Comments
 (0)
X Tutup