Skip to content

Commit 5726376

Browse files
committed
Guard against faulty affiliations GraphQL filter
This API was just fixed in github.com, but it will take a while for the change to propagate to GitHub Enterprise installs, so guard ourselves from false positives when querying forks.
1 parent 5b2e184 commit 5726376

File tree

3 files changed

+13
-3
lines changed

3 files changed

+13
-3
lines changed

api/queries_repo.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,7 @@ func RepoFindFork(client *Client, repo ghrepo.Interface) (*Repository, error) {
245245
name
246246
owner { login }
247247
url
248+
viewerPermission
248249
}
249250
}
250251
}
@@ -253,8 +254,12 @@ func RepoFindFork(client *Client, repo ghrepo.Interface) (*Repository, error) {
253254
return nil, err
254255
}
255256

256-
if len(result.Repository.Forks.Nodes) > 0 {
257-
return &result.Repository.Forks.Nodes[0], nil
257+
forks := result.Repository.Forks.Nodes
258+
// we check ViewerCanPush, even though we expect it to always be true per
259+
// `affiliations` condition, to guard against versions of GitHub with a
260+
// faulty `affiliations` implementation
261+
if len(forks) > 0 && forks[0].ViewerCanPush() {
262+
return &forks[0], nil
258263
}
259264
return nil, &NotFoundError{errors.New("no fork found")}
260265
}

command/pr_create_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,10 @@ func TestPRCreate_alreadyExistsDifferentBase(t *testing.T) {
9696
initBlankContext("OWNER/REPO", "feature")
9797
http := initFakeHTTP()
9898
http.StubRepoResponse("OWNER", "REPO")
99+
http.StubResponse(200, bytes.NewBufferString(`
100+
{ "data": { "repository": { "forks": { "nodes": [
101+
] } } } }
102+
`))
99103
http.StubResponse(200, bytes.NewBufferString(`
100104
{ "data": { "repository": { "pullRequests": { "nodes": [
101105
{ "url": "https://github.com/OWNER/REPO/pull/123",

context/remote_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,8 @@ func Test_resolvedRemotes_forkLookup(t *testing.T) {
137137
{ "id": "FORKID",
138138
"url": "https://github.com/FORKOWNER/REPO",
139139
"name": "REPO",
140-
"owner": { "login": "FORKOWNER" }
140+
"owner": { "login": "FORKOWNER" },
141+
"viewerPermission": "WRITE"
141142
}
142143
] } } } }
143144
`))

0 commit comments

Comments
 (0)