diff --git a/src/main/java/org/kohsuke/github/GHCommit.java b/src/main/java/org/kohsuke/github/GHCommit.java index 2b68ff9a95..a350eeb535 100644 --- a/src/main/java/org/kohsuke/github/GHCommit.java +++ b/src/main/java/org/kohsuke/github/GHCommit.java @@ -192,21 +192,24 @@ public GHRepository getOwner() { /** * Number of lines added + removed. */ - public int getLinesChanged() { + public int getLinesChanged() throws IOException { + populate(); return stats.total; } /** * Number of lines added. */ - public int getLinesAdded() { + public int getLinesAdded() throws IOException { + populate(); return stats.additions; } /** * Number of lines removed. */ - public int getLinesDeleted() { + public int getLinesDeleted() throws IOException { + populate(); return stats.deletions; } @@ -223,7 +226,8 @@ public String getSHA1() { * @return * Can be empty but never null. */ - public List getFiles() { + public List getFiles() throws IOException { + populate(); return files!=null ? Collections.unmodifiableList(files) : Collections.emptyList(); } @@ -301,7 +305,7 @@ public GHCommitComment createComment(String body, String path, Integer line, Int } public GHCommitComment createComment(String body) throws IOException { - return createComment(body,null,null,null); + return createComment(body, null, null, null); } /** @@ -318,6 +322,14 @@ public GHCommitStatus getLastStatus() throws IOException { return owner.getLastCommitStatus(sha); } + /** + * Some of the fields are not always filled in when this object is retrieved as a part of another API call. + */ + void populate() throws IOException { + if (files==null && stats==null) + owner.root.retrieve().to(owner.getApiTailUrl("commits/" + sha), this); + } + GHCommit wrapUp(GHRepository owner) { this.owner = owner; return this; diff --git a/src/test/java/org/kohsuke/github/CommitTest.java b/src/test/java/org/kohsuke/github/CommitTest.java index fff9084ebd..c42fceef41 100644 --- a/src/test/java/org/kohsuke/github/CommitTest.java +++ b/src/test/java/org/kohsuke/github/CommitTest.java @@ -1,5 +1,7 @@ package org.kohsuke.github; +import com.google.common.collect.Iterables; +import com.google.common.collect.Iterators; import org.junit.Test; import java.io.IOException; @@ -13,4 +15,14 @@ public void lastStatus() throws IOException { GHTag t = gitHub.getRepository("stapler/stapler").listTags().iterator().next(); t.getCommit().getLastStatus(); } + + @Test // issue 230 + public void listFiles() throws Exception { + GHRepository repo = gitHub.getRepository("stapler/stapler"); + PagedIterable commits = repo.queryCommits().path("pom.xml").list(); + for (GHCommit commit : Iterables.limit(commits, 10)) { + GHCommit expected = repo.getCommit( commit.getSHA1() ); + assertEquals(expected.getFiles().size(), commit.getFiles().size()); + } + } }