Skip to content

Commit be00e51

Browse files
Fix GHFileNotFoundException when getting commits from PR found in search (#1779)
* Replace /issues/ with /pulls/ in sourced URL for Pull Requests. Previously, when a pull request had been fetched, it lacked an `owner`, and thus was following a specific codepath inside `GHPullRequest.GetApiRoute` that was returning a URL that corresponded to an `issue`. When that URL was then appended on for further queries, say, with `/commits`, then Github would return a 404. This fixes the issue by manipulating the URL manually and replacing the wrong reference to `/issues/` with `/pulls/`. * Add test for issue with List of Commits returning 404. --------- Co-authored-by: Liam Newman <[email protected]>
1 parent 5b30e46 commit be00e51

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+34669
-1
lines changed

src/main/java/org/kohsuke/github/GHPullRequest.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,12 @@ protected String getApiRoute() {
100100
if (owner == null) {
101101
// Issues returned from search to do not have an owner. Attempt to use url.
102102
final URL url = Objects.requireNonNull(getUrl(), "Missing instance URL!");
103-
return StringUtils.prependIfMissing(url.toString().replace(root().getApiUrl(), ""), "/");
103+
// The url sourced above is of the form '/repos/<owner>/<reponame>/issues/', which
104+
// subsequently issues requests against the `/issues/` handler, causing a 404 when
105+
// asking for, say, a list of commits associated with a PR. Replace the `/issues/`
106+
// with `/pulls/` to avoid that.
107+
return StringUtils.prependIfMissing(url.toString().replace(root().getApiUrl(), ""), "/")
108+
.replace("/issues/", "/pulls/");
104109
}
105110
return "/repos/" + owner.getOwnerName() + "/" + owner.getName() + "/pulls/" + number;
106111
}

src/test/java/org/kohsuke/github/GHPullRequestTest.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,32 @@ public void pullRequestComment() throws Exception {
178178
assertThat(comments, contains(hasProperty("body", equalTo("Second comment"))));
179179
}
180180

181+
/**
182+
* Get list of commits from searched PR.
183+
*
184+
* This would result in a wrong API URL used, resulting in a GHFileNotFoundException.
185+
*
186+
* For more details, please have a look at the bug description in https://github.com/hub4j/github-api/issues/1778.
187+
*
188+
* @throws Exception
189+
* the exception
190+
*/
191+
@Test
192+
public void getListOfCommits() throws Exception {
193+
String name = "getListOfCommits";
194+
GHPullRequestSearchBuilder builder = getRepository().searchPullRequests().isClosed();
195+
Optional<GHPullRequest> firstPR = builder.list().toList().stream().findFirst();
196+
197+
try {
198+
String val = firstPR.get().listCommits().toArray()[0].getApiUrl().toString();
199+
assertThat(val, notNullValue());
200+
} catch (GHFileNotFoundException e) {
201+
if (e.getMessage().contains("/issues/")) {
202+
fail("Issued a request against the wrong path");
203+
}
204+
}
205+
}
206+
181207
/**
182208
* Close pull request.
183209
*
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
"login": "NlightNFotis",
3+
"id": 1859274,
4+
"node_id": "MDQ6VXNlcjE4NTkyNzQ=",
5+
"avatar_url": "https://avatars.githubusercontent.com/u/1859274?v=4",
6+
"gravatar_id": "",
7+
"url": "https://api.github.com/users/NlightNFotis",
8+
"html_url": "https://github.com/NlightNFotis",
9+
"followers_url": "https://api.github.com/users/NlightNFotis/followers",
10+
"following_url": "https://api.github.com/users/NlightNFotis/following{/other_user}",
11+
"gists_url": "https://api.github.com/users/NlightNFotis/gists{/gist_id}",
12+
"starred_url": "https://api.github.com/users/NlightNFotis/starred{/owner}{/repo}",
13+
"subscriptions_url": "https://api.github.com/users/NlightNFotis/subscriptions",
14+
"organizations_url": "https://api.github.com/users/NlightNFotis/orgs",
15+
"repos_url": "https://api.github.com/users/NlightNFotis/repos",
16+
"events_url": "https://api.github.com/users/NlightNFotis/events{/privacy}",
17+
"received_events_url": "https://api.github.com/users/NlightNFotis/received_events",
18+
"type": "User",
19+
"site_admin": false,
20+
"name": "Fotis Koutoulakis",
21+
"company": "@diffblue ",
22+
"blog": "https://nlightnfotis.github.io",
23+
"location": "Oxford, United Kingdom",
24+
"email": "[email protected]",
25+
"hireable": null,
26+
"bio": "Interests in Mathematics, Computer Science, Biology and Economics.",
27+
"twitter_username": "NlightNFotis",
28+
"public_repos": 27,
29+
"public_gists": 8,
30+
"followers": 38,
31+
"following": 51,
32+
"created_at": "2012-06-17T17:34:11Z",
33+
"updated_at": "2023-09-18T09:30:51Z"
34+
}

0 commit comments

Comments
 (0)