Skip to content

Commit

Permalink
Fix GHFileNotFoundException when getting commits from PR found in s…
Browse files Browse the repository at this point in the history
…earch (#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]>
  • Loading branch information
NlightNFotis and bitwiseman authored Jan 25, 2024
1 parent 5b30e46 commit be00e51
Show file tree
Hide file tree
Showing 42 changed files with 34,669 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/main/java/org/kohsuke/github/GHPullRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,12 @@ protected String getApiRoute() {
if (owner == null) {
// Issues returned from search to do not have an owner. Attempt to use url.
final URL url = Objects.requireNonNull(getUrl(), "Missing instance URL!");
return StringUtils.prependIfMissing(url.toString().replace(root().getApiUrl(), ""), "/");
// The url sourced above is of the form '/repos/<owner>/<reponame>/issues/', which
// subsequently issues requests against the `/issues/` handler, causing a 404 when
// asking for, say, a list of commits associated with a PR. Replace the `/issues/`
// with `/pulls/` to avoid that.
return StringUtils.prependIfMissing(url.toString().replace(root().getApiUrl(), ""), "/")
.replace("/issues/", "/pulls/");
}
return "/repos/" + owner.getOwnerName() + "/" + owner.getName() + "/pulls/" + number;
}
Expand Down
26 changes: 26 additions & 0 deletions src/test/java/org/kohsuke/github/GHPullRequestTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,32 @@ public void pullRequestComment() throws Exception {
assertThat(comments, contains(hasProperty("body", equalTo("Second comment"))));
}

/**
* Get list of commits from searched PR.
*
* This would result in a wrong API URL used, resulting in a GHFileNotFoundException.
*
* For more details, please have a look at the bug description in https://github.com/hub4j/github-api/issues/1778.
*
* @throws Exception
* the exception
*/
@Test
public void getListOfCommits() throws Exception {
String name = "getListOfCommits";
GHPullRequestSearchBuilder builder = getRepository().searchPullRequests().isClosed();
Optional<GHPullRequest> firstPR = builder.list().toList().stream().findFirst();

try {
String val = firstPR.get().listCommits().toArray()[0].getApiUrl().toString();
assertThat(val, notNullValue());
} catch (GHFileNotFoundException e) {
if (e.getMessage().contains("/issues/")) {
fail("Issued a request against the wrong path");
}
}
}

/**
* Close pull request.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"login": "NlightNFotis",
"id": 1859274,
"node_id": "MDQ6VXNlcjE4NTkyNzQ=",
"avatar_url": "https://avatars.githubusercontent.com/u/1859274?v=4",
"gravatar_id": "",
"url": "https://api.github.com/users/NlightNFotis",
"html_url": "https://github.com/NlightNFotis",
"followers_url": "https://api.github.com/users/NlightNFotis/followers",
"following_url": "https://api.github.com/users/NlightNFotis/following{/other_user}",
"gists_url": "https://api.github.com/users/NlightNFotis/gists{/gist_id}",
"starred_url": "https://api.github.com/users/NlightNFotis/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/NlightNFotis/subscriptions",
"organizations_url": "https://api.github.com/users/NlightNFotis/orgs",
"repos_url": "https://api.github.com/users/NlightNFotis/repos",
"events_url": "https://api.github.com/users/NlightNFotis/events{/privacy}",
"received_events_url": "https://api.github.com/users/NlightNFotis/received_events",
"type": "User",
"site_admin": false,
"name": "Fotis Koutoulakis",
"company": "@diffblue ",
"blog": "https://nlightnfotis.github.io",
"location": "Oxford, United Kingdom",
"email": "[email protected]",
"hireable": null,
"bio": "Interests in Mathematics, Computer Science, Biology and Economics.",
"twitter_username": "NlightNFotis",
"public_repos": 27,
"public_gists": 8,
"followers": 38,
"following": 51,
"created_at": "2012-06-17T17:34:11Z",
"updated_at": "2023-09-18T09:30:51Z"
}
Loading

0 comments on commit be00e51

Please sign in to comment.