Skip to content

Commit

Permalink
GHCommit might be only partially populated.
Browse files Browse the repository at this point in the history
This fixes issue #230
  • Loading branch information
kohsuke committed Dec 10, 2015
1 parent 2603b5a commit 9149b6b
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
22 changes: 17 additions & 5 deletions src/main/java/org/kohsuke/github/GHCommit.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -223,7 +226,8 @@ public String getSHA1() {
* @return
* Can be empty but never null.
*/
public List<File> getFiles() {
public List<File> getFiles() throws IOException {
populate();
return files!=null ? Collections.unmodifiableList(files) : Collections.<File>emptyList();
}

Expand Down Expand Up @@ -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);
}

/**
Expand All @@ -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;
Expand Down
12 changes: 12 additions & 0 deletions src/test/java/org/kohsuke/github/CommitTest.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<GHCommit> 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());
}
}
}

0 comments on commit 9149b6b

Please sign in to comment.