Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix GHFileNotFoundException when getting commits from PR found in search #1779

Merged
merged 3 commits into from
Jan 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading