Skip to content

Commit 201217d

Browse files
committed
Fix detecting PR for current branch pushed to fork
Now passing base repo as reference to `prSelectorForCurrentBranch()` so that it doesn't have to (inexactly) determine the base repo itself and risk it being different than the base repo determined during `pr status/view`.
1 parent 704ce31 commit 201217d

File tree

4 files changed

+100
-9
lines changed

4 files changed

+100
-9
lines changed

api/fake_http.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"io"
77
"io/ioutil"
88
"net/http"
9+
"strings"
910
)
1011

1112
// FakeHTTP provides a mechanism by which to stub HTTP responses through
@@ -56,3 +57,35 @@ func (f *FakeHTTP) StubRepoResponse(owner, repo string) {
5657
}
5758
f.responseStubs = append(f.responseStubs, resp)
5859
}
60+
61+
func (f *FakeHTTP) StubForkedRepoResponse(forkFullName, parentFullName string) {
62+
forkRepo := strings.SplitN(forkFullName, "/", 2)
63+
parentRepo := strings.SplitN(parentFullName, "/", 2)
64+
body := bytes.NewBufferString(fmt.Sprintf(`
65+
{ "data": { "repo_000": {
66+
"id": "REPOID2",
67+
"name": "%s",
68+
"owner": {"login": "%s"},
69+
"defaultBranchRef": {
70+
"name": "master",
71+
"target": {"oid": "deadbeef"}
72+
},
73+
"viewerPermission": "ADMIN",
74+
"parent": {
75+
"id": "REPOID1",
76+
"name": "%s",
77+
"owner": {"login": "%s"},
78+
"defaultBranchRef": {
79+
"name": "master",
80+
"target": {"oid": "deadbeef"}
81+
},
82+
"viewerPermission": "READ"
83+
}
84+
} } }
85+
`, forkRepo[1], forkRepo[0], parentRepo[1], parentRepo[0]))
86+
resp := &http.Response{
87+
StatusCode: 200,
88+
Body: ioutil.NopCloser(body),
89+
}
90+
f.responseStubs = append(f.responseStubs, resp)
91+
}

command/pr.go

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -71,16 +71,17 @@ func prStatus(cmd *cobra.Command, args []string) error {
7171
return err
7272
}
7373

74-
currentPRNumber, currentPRHeadRef, err := prSelectorForCurrentBranch(ctx)
74+
currentUser, err := ctx.AuthLogin()
7575
if err != nil {
7676
return err
7777
}
78-
currentUser, err := ctx.AuthLogin()
78+
79+
baseRepo, err := determineBaseRepo(cmd, ctx)
7980
if err != nil {
8081
return err
8182
}
8283

83-
baseRepo, err := determineBaseRepo(cmd, ctx)
84+
currentPRNumber, currentPRHeadRef, err := prSelectorForCurrentBranch(ctx, baseRepo)
8485
if err != nil {
8586
return err
8687
}
@@ -275,7 +276,7 @@ func prView(cmd *cobra.Command, args []string) error {
275276
}
276277
openURL = pr.URL
277278
} else {
278-
prNumber, branchWithOwner, err := prSelectorForCurrentBranch(ctx)
279+
prNumber, branchWithOwner, err := prSelectorForCurrentBranch(ctx, baseRepo)
279280
if err != nil {
280281
return err
281282
}
@@ -345,11 +346,7 @@ func prFromArg(apiClient *api.Client, baseRepo ghrepo.Interface, arg string) (*a
345346
return api.PullRequestForBranch(apiClient, baseRepo, arg)
346347
}
347348

348-
func prSelectorForCurrentBranch(ctx context.Context) (prNumber int, prHeadRef string, err error) {
349-
baseRepo, err := ctx.BaseRepo()
350-
if err != nil {
351-
return
352-
}
349+
func prSelectorForCurrentBranch(ctx context.Context, baseRepo ghrepo.Interface) (prNumber int, prHeadRef string, err error) {
353350
prHeadRef, err = ctx.Branch()
354351
if err != nil {
355352
return

command/pr_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,36 @@ func TestPRStatus(t *testing.T) {
9898
}
9999
}
100100

101+
func TestPRStatus_fork(t *testing.T) {
102+
initBlankContext("OWNER/REPO", "blueberries")
103+
http := initFakeHTTP()
104+
http.StubForkedRepoResponse("OWNER/REPO", "PARENT/REPO")
105+
106+
jsonFile, _ := os.Open("../test/fixtures/prStatusFork.json")
107+
defer jsonFile.Close()
108+
http.StubResponse(200, jsonFile)
109+
110+
defer utils.SetPrepareCmd(func(cmd *exec.Cmd) utils.Runnable {
111+
switch strings.Join(cmd.Args, " ") {
112+
case `git config --get-regexp ^branch\.blueberries\.(remote|merge)$`:
113+
return &outputStub{[]byte(`branch.blueberries.remote origin
114+
branch.blueberries.merge refs/heads/blueberries`)}
115+
default:
116+
panic("not implemented")
117+
}
118+
})()
119+
120+
output, err := RunCommand(prStatusCmd, "pr status")
121+
if err != nil {
122+
t.Fatalf("error running command `pr status`: %v", err)
123+
}
124+
125+
branchRE := regexp.MustCompile(`#10.*\[OWNER:blueberries\]`)
126+
if !branchRE.MatchString(output.String()) {
127+
t.Errorf("did not match current branch:\n%v", output.String())
128+
}
129+
}
130+
101131
func TestPRStatus_reviewsAndChecks(t *testing.T) {
102132
initBlankContext("OWNER/REPO", "blueberries")
103133
http := initFakeHTTP()

test/fixtures/prStatusFork.json

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"data": {
3+
"repository": {
4+
"pullRequests": {
5+
"totalCount": 1,
6+
"edges": [
7+
{
8+
"node": {
9+
"number": 10,
10+
"title": "Blueberries are a good fruit",
11+
"url": "https://github.com/PARENT/REPO/pull/10",
12+
"headRefName": "blueberries",
13+
"headRepositoryOwner": {
14+
"login": "OWNER"
15+
},
16+
"isCrossRepository": true
17+
}
18+
}
19+
]
20+
}
21+
},
22+
"viewerCreated": {
23+
"totalCount": 0,
24+
"edges": []
25+
},
26+
"reviewRequested": {
27+
"totalCount": 0,
28+
"edges": []
29+
}
30+
}
31+
}

0 commit comments

Comments
 (0)