Skip to content

Commit 2e7fedd

Browse files
author
Nate Smith
authored
Merge pull request cli#362 from cli/cross-repo-create
Cross repo pr create
2 parents 15d6d4b + 212dc1d commit 2e7fedd

File tree

2 files changed

+89
-1
lines changed

2 files changed

+89
-1
lines changed

command/pr_create.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,12 +176,17 @@ func prCreate(cmd *cobra.Command, _ []string) error {
176176
return fmt.Errorf("pull request title must not be blank")
177177
}
178178

179+
headRefName := headBranch
180+
if !ghrepo.IsSame(headRemote, baseRepo) {
181+
headRefName = fmt.Sprintf("%s:%s", headRemote.RepoOwner(), headBranch)
182+
}
183+
179184
params := map[string]interface{}{
180185
"title": title,
181186
"body": body,
182187
"draft": isDraft,
183188
"baseRefName": baseBranch,
184-
"headRefName": headBranch,
189+
"headRefName": headRefName,
185190
}
186191

187192
pr, err := api.CreatePullRequest(client, baseRepo, params)

command/pr_create_test.go

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"strings"
1111
"testing"
1212

13+
"github.com/cli/cli/context"
1314
"github.com/cli/cli/git"
1415
"github.com/cli/cli/test"
1516
"github.com/cli/cli/utils"
@@ -131,3 +132,85 @@ Creating pull request for feature into master in OWNER/REPO
131132
132133
`)
133134
}
135+
func TestPRCreate_cross_repo_same_branch(t *testing.T) {
136+
ctx := context.NewBlank()
137+
ctx.SetBranch("default")
138+
ctx.SetRemotes(map[string]string{
139+
"origin": "OWNER/REPO",
140+
"fork": "MYSELF/REPO",
141+
})
142+
initContext = func() context.Context {
143+
return ctx
144+
}
145+
http := initFakeHTTP()
146+
http.StubResponse(200, bytes.NewBufferString(`
147+
{ "data": { "repo_000": {
148+
"id": "REPOID0",
149+
"name": "REPO",
150+
"owner": {"login": "OWNER"},
151+
"defaultBranchRef": {
152+
"name": "default",
153+
"target": {"oid": "deadbeef"}
154+
},
155+
"viewerPermission": "READ"
156+
},
157+
"repo_001" : {
158+
"parent": {
159+
"id": "REPOID0",
160+
"name": "REPO",
161+
"owner": {"login": "OWNER"},
162+
"defaultBranchRef": {
163+
"name": "default",
164+
"target": {"oid": "deadbeef"}
165+
},
166+
"viewerPermission": "READ"
167+
},
168+
"id": "REPOID1",
169+
"name": "REPO",
170+
"owner": {"login": "MYSELF"},
171+
"defaultBranchRef": {
172+
"name": "default",
173+
"target": {"oid": "deadbeef"}
174+
},
175+
"viewerPermission": "WRITE"
176+
} } }
177+
`))
178+
http.StubResponse(200, bytes.NewBufferString(`
179+
{ "data": { "createPullRequest": { "pullRequest": {
180+
"URL": "https://github.com/OWNER/REPO/pull/12"
181+
} } } }
182+
`))
183+
184+
origGitCommand := git.GitCommand
185+
git.GitCommand = test.StubExecCommand("TestPrCreateHelperProcess", "clean")
186+
defer func() {
187+
git.GitCommand = origGitCommand
188+
}()
189+
190+
output, err := RunCommand(prCreateCmd, `pr create -t "cross repo" -b "same branch"`)
191+
eq(t, err, nil)
192+
193+
bodyBytes, _ := ioutil.ReadAll(http.Requests[1].Body)
194+
reqBody := struct {
195+
Variables struct {
196+
Input struct {
197+
RepositoryID string
198+
Title string
199+
Body string
200+
BaseRefName string
201+
HeadRefName string
202+
}
203+
}
204+
}{}
205+
json.Unmarshal(bodyBytes, &reqBody)
206+
207+
eq(t, reqBody.Variables.Input.RepositoryID, "REPOID0")
208+
eq(t, reqBody.Variables.Input.Title, "cross repo")
209+
eq(t, reqBody.Variables.Input.Body, "same branch")
210+
eq(t, reqBody.Variables.Input.BaseRefName, "default")
211+
eq(t, reqBody.Variables.Input.HeadRefName, "MYSELF:default")
212+
213+
eq(t, output.String(), "https://github.com/OWNER/REPO/pull/12\n")
214+
215+
// goal: only care that gql is formatted properly
216+
}

0 commit comments

Comments
 (0)