@@ -2,6 +2,8 @@ package command
22
33import (
44 "bytes"
5+ "encoding/json"
6+ "io/ioutil"
57 "os/exec"
68 "strings"
79 "testing"
@@ -21,6 +23,7 @@ func TestPRCheckout_sameRepo(t *testing.T) {
2123 return ctx
2224 }
2325 http := initFakeHTTP ()
26+ http .StubRepoResponse ("OWNER" , "REPO" )
2427
2528 http .StubResponse (200 , bytes .NewBufferString (`
2629 { "data": { "repository": { "pullRequest": {
@@ -112,6 +115,68 @@ func TestPRCheckout_urlArg(t *testing.T) {
112115 eq (t , strings .Join (ranCommands [1 ], " " ), "git checkout -b feature --no-track origin/feature" )
113116}
114117
118+ func TestPRCheckout_urlArg_differentBase (t * testing.T ) {
119+ ctx := context .NewBlank ()
120+ ctx .SetBranch ("master" )
121+ ctx .SetRemotes (map [string ]string {
122+ "origin" : "OWNER/REPO" ,
123+ })
124+ initContext = func () context.Context {
125+ return ctx
126+ }
127+ http := initFakeHTTP ()
128+
129+ http .StubResponse (200 , bytes .NewBufferString (`
130+ { "data": { "repository": { "pullRequest": {
131+ "number": 123,
132+ "headRefName": "feature",
133+ "headRepositoryOwner": {
134+ "login": "hubot"
135+ },
136+ "headRepository": {
137+ "name": "POE",
138+ "defaultBranchRef": {
139+ "name": "master"
140+ }
141+ },
142+ "isCrossRepository": false,
143+ "maintainerCanModify": false
144+ } } } }
145+ ` ))
146+
147+ ranCommands := [][]string {}
148+ restoreCmd := utils .SetPrepareCmd (func (cmd * exec.Cmd ) utils.Runnable {
149+ switch strings .Join (cmd .Args , " " ) {
150+ case "git show-ref --verify --quiet refs/heads/feature" :
151+ return & errorStub {"exit status: 1" }
152+ default :
153+ ranCommands = append (ranCommands , cmd .Args )
154+ return & test.OutputStub {}
155+ }
156+ })
157+ defer restoreCmd ()
158+
159+ output , err := RunCommand (prCheckoutCmd , `pr checkout https://github.com/OTHER/POE/pull/123/files` )
160+ eq (t , err , nil )
161+ eq (t , output .String (), "" )
162+
163+ bodyBytes , _ := ioutil .ReadAll (http .Requests [0 ].Body )
164+ reqBody := struct {
165+ Variables struct {
166+ Owner string
167+ Repo string
168+ }
169+ }{}
170+ json .Unmarshal (bodyBytes , & reqBody )
171+
172+ eq (t , reqBody .Variables .Owner , "OTHER" )
173+ eq (t , reqBody .Variables .Repo , "POE" )
174+
175+ eq (t , len (ranCommands ), 5 )
176+ eq (t , strings .Join (ranCommands [1 ], " " ), "git fetch https://github.com/OTHER/POE.git refs/pull/123/head:feature" )
177+ eq (t , strings .Join (ranCommands [3 ], " " ), "git config branch.feature.remote https://github.com/OTHER/POE.git" )
178+ }
179+
115180func TestPRCheckout_branchArg (t * testing.T ) {
116181 ctx := context .NewBlank ()
117182 ctx .SetBranch ("master" )
@@ -122,6 +187,7 @@ func TestPRCheckout_branchArg(t *testing.T) {
122187 return ctx
123188 }
124189 http := initFakeHTTP ()
190+ http .StubRepoResponse ("OWNER" , "REPO" )
125191
126192 http .StubResponse (200 , bytes .NewBufferString (`
127193 { "data": { "repository": { "pullRequests": { "nodes": [
@@ -171,6 +237,7 @@ func TestPRCheckout_existingBranch(t *testing.T) {
171237 return ctx
172238 }
173239 http := initFakeHTTP ()
240+ http .StubRepoResponse ("OWNER" , "REPO" )
174241
175242 http .StubResponse (200 , bytes .NewBufferString (`
176243 { "data": { "repository": { "pullRequest": {
@@ -223,6 +290,7 @@ func TestPRCheckout_differentRepo_remoteExists(t *testing.T) {
223290 return ctx
224291 }
225292 http := initFakeHTTP ()
293+ http .StubRepoResponse ("OWNER" , "REPO" )
226294
227295 http .StubResponse (200 , bytes .NewBufferString (`
228296 { "data": { "repository": { "pullRequest": {
@@ -275,6 +343,7 @@ func TestPRCheckout_differentRepo(t *testing.T) {
275343 return ctx
276344 }
277345 http := initFakeHTTP ()
346+ http .StubRepoResponse ("OWNER" , "REPO" )
278347
279348 http .StubResponse (200 , bytes .NewBufferString (`
280349 { "data": { "repository": { "pullRequest": {
@@ -327,6 +396,7 @@ func TestPRCheckout_differentRepo_existingBranch(t *testing.T) {
327396 return ctx
328397 }
329398 http := initFakeHTTP ()
399+ http .StubRepoResponse ("OWNER" , "REPO" )
330400
331401 http .StubResponse (200 , bytes .NewBufferString (`
332402 { "data": { "repository": { "pullRequest": {
@@ -377,6 +447,7 @@ func TestPRCheckout_differentRepo_currentBranch(t *testing.T) {
377447 return ctx
378448 }
379449 http := initFakeHTTP ()
450+ http .StubRepoResponse ("OWNER" , "REPO" )
380451
381452 http .StubResponse (200 , bytes .NewBufferString (`
382453 { "data": { "repository": { "pullRequest": {
@@ -427,6 +498,7 @@ func TestPRCheckout_maintainerCanModify(t *testing.T) {
427498 return ctx
428499 }
429500 http := initFakeHTTP ()
501+ http .StubRepoResponse ("OWNER" , "REPO" )
430502
431503 http .StubResponse (200 , bytes .NewBufferString (`
432504 { "data": { "repository": { "pullRequest": {
0 commit comments