Skip to content

Commit c9b5074

Browse files
committed
Fix the wrapping of retrieved commits (owner/repository)
1 parent 64af13f commit c9b5074

2 files changed

Lines changed: 26 additions & 0 deletions

File tree

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package org.kohsuke.github;
22

3+
import java.io.IOException;
34
import java.util.Locale;
45

6+
import org.apache.commons.lang.StringUtils;
7+
58
/**
69
* Search commits.
710
*
@@ -104,9 +107,29 @@ private static class CommitSearchResult extends SearchResult<GHCommit> {
104107

105108
@Override
106109
/*package*/ GHCommit[] getItems(GitHub root) {
110+
for (GHCommit commit : items) {
111+
String repoName = getRepoName(commit.url);
112+
try {
113+
GHRepository repo = root.getRepository(repoName);
114+
commit.wrapUp(repo);
115+
} catch (IOException ioe) {}
116+
}
107117
return items;
108118
}
109119
}
120+
121+
/**
122+
* @param commitUrl a commit URL
123+
* @return the repo name ("username/reponame")
124+
*/
125+
private static String getRepoName(String commitUrl) {
126+
if (StringUtils.isBlank(commitUrl)) {
127+
return null;
128+
}
129+
int indexOfUsername = (GitHub.GITHUB_URL + "/repos/").length();
130+
String[] tokens = commitUrl.substring(indexOfUsername).split("/", 3);
131+
return tokens[0] + '/' + tokens[1];
132+
}
110133

111134
@Override
112135
protected String getApiUrl() {

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -689,6 +689,9 @@ public void testMemberPagenation() throws IOException {
689689
public void testCommitSearch() throws IOException {
690690
PagedSearchIterable<GHCommit> r = gitHub.searchCommits().author("kohsuke").list();
691691
assertTrue(r.getTotalCount() > 0);
692+
693+
GHCommit firstCommit = r.iterator().next();
694+
assertTrue(firstCommit.getFiles().size() > 0);
692695
}
693696

694697
@Test

0 commit comments

Comments
 (0)