Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e066e98

Browse files
authored
Merge pull request cli#726 from cli/pr-create-crash
Fix auto-forking scenario in `pr create`
2 parents 44a37cb + f0e6c98 commit e066e98

File tree

3 files changed

+57
-11
lines changed

3 files changed

+57
-11
lines changed

api/fake_http.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ func (f *FakeHTTP) StubWithFixture(status int, fixtureFileName string) func() {
4848
}
4949

5050
func (f *FakeHTTP) StubRepoResponse(owner, repo string) {
51+
f.StubRepoResponseWithPermission(owner, repo, "WRITE")
52+
}
53+
54+
func (f *FakeHTTP) StubRepoResponseWithPermission(owner, repo, permission string) {
5155
body := bytes.NewBufferString(fmt.Sprintf(`
5256
{ "data": { "repo_000": {
5357
"id": "REPOID",
@@ -56,9 +60,9 @@ func (f *FakeHTTP) StubRepoResponse(owner, repo string) {
5660
"defaultBranchRef": {
5761
"name": "master"
5862
},
59-
"viewerPermission": "WRITE"
63+
"viewerPermission": "%s"
6064
} } }
61-
`, repo, owner))
65+
`, repo, owner, permission))
6266
resp := &http.Response{
6367
StatusCode: 200,
6468
Body: ioutil.NopCloser(body),

command/pr_create.go

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,9 @@ func prCreate(cmd *cobra.Command, _ []string) error {
9595

9696
// otherwise, determine the head repository with info obtained from the API
9797
if headRepo == nil {
98-
headRepo, _ = repoContext.HeadRepo()
98+
if r, err := repoContext.HeadRepo(); err == nil {
99+
headRepo = r
100+
}
99101
}
100102

101103
baseBranch, err := cmd.Flags().GetString("base")
@@ -236,14 +238,18 @@ func prCreate(cmd *cobra.Command, _ []string) error {
236238
headBranchLabel = fmt.Sprintf("%s:%s", headRepo.RepoOwner(), headBranch)
237239
}
238240

241+
if headRemote == nil {
242+
headRemote, _ = repoContext.RemoteForRepo(headRepo)
243+
}
244+
239245
// There are two cases when an existing remote for the head repo will be
240246
// missing:
241247
// 1. the head repo was just created by auto-forking;
242248
// 2. an existing fork was discovered by quering the API.
243249
//
244250
// In either case, we want to add the head repo as a new git remote so we
245251
// can push to it.
246-
if err != nil {
252+
if headRemote == nil {
247253
// TODO: support non-HTTPS git remote URLs
248254
headRepoURL := fmt.Sprintf("https://github.com/%s.git", ghrepo.FullName(headRepo))
249255
// TODO: prevent clashes with another remote of a same name
@@ -260,13 +266,6 @@ func prCreate(cmd *cobra.Command, _ []string) error {
260266

261267
// automatically push the branch if it hasn't been pushed anywhere yet
262268
if headBranchPushedTo == nil {
263-
if headRemote == nil {
264-
headRemote, err = repoContext.RemoteForRepo(headRepo)
265-
if err != nil {
266-
return fmt.Errorf("git remote not found for head repository: %w", err)
267-
}
268-
}
269-
270269
pushTries := 0
271270
maxPushTries := 3
272271
for {

command/pr_create_test.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,49 @@ func TestPRCreate(t *testing.T) {
6565
eq(t, output.String(), "https://github.com/OWNER/REPO/pull/12\n")
6666
}
6767

68+
func TestPRCreate_withForking(t *testing.T) {
69+
initBlankContext("OWNER/REPO", "feature")
70+
http := initFakeHTTP()
71+
http.StubRepoResponseWithPermission("OWNER", "REPO", "READ")
72+
http.StubResponse(200, bytes.NewBufferString(`
73+
{ "data": { "repository": { "forks": { "nodes": [
74+
] } } } }
75+
`))
76+
http.StubResponse(200, bytes.NewBufferString(`
77+
{ "data": { "repository": { "pullRequests": { "nodes" : [
78+
] } } } }
79+
`))
80+
http.StubResponse(200, bytes.NewBufferString(`
81+
{ "node_id": "NODEID",
82+
"name": "REPO",
83+
"owner": {"login": "myself"},
84+
"clone_url": "http://example.com",
85+
"created_at": "2008-02-25T20:21:40Z"
86+
}
87+
`))
88+
http.StubResponse(200, bytes.NewBufferString(`
89+
{ "data": { "createPullRequest": { "pullRequest": {
90+
"URL": "https://github.com/OWNER/REPO/pull/12"
91+
} } } }
92+
`))
93+
94+
cs, cmdTeardown := test.InitCmdStubber()
95+
defer cmdTeardown()
96+
97+
cs.Stub("") // git config --get-regexp (determineTrackingBranch)
98+
cs.Stub("") // git show-ref --verify (determineTrackingBranch)
99+
cs.Stub("") // git status
100+
cs.Stub("1234567890,commit 0\n2345678901,commit 1") // git log
101+
cs.Stub("") // git remote add
102+
cs.Stub("") // git push
103+
104+
output, err := RunCommand(prCreateCmd, `pr create -t title -b body`)
105+
eq(t, err, nil)
106+
107+
eq(t, http.Requests[3].URL.Path, "/repos/OWNER/REPO/forks")
108+
eq(t, output.String(), "https://github.com/OWNER/REPO/pull/12\n")
109+
}
110+
68111
func TestPRCreate_alreadyExists(t *testing.T) {
69112
initBlankContext("OWNER/REPO", "feature")
70113
http := initFakeHTTP()

0 commit comments

Comments
 (0)