Skip to content

Commit

Permalink
added a method to visit sub-directory
Browse files Browse the repository at this point in the history
  • Loading branch information
kohsuke committed May 28, 2014
1 parent 80b93a6 commit 45a6841
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/main/java/org/kohsuke/github/GHContent.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package org.kohsuke.github;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;

import javax.xml.bind.DatatypeConverter;

Expand Down Expand Up @@ -107,6 +109,25 @@ public boolean isDirectory() {
return "dir".equals(type);
}

/**
* List immediate children of this directory.
*/
public PagedIterable<GHContent> listDirectoryContent() throws IOException {
if (!isDirectory())
throw new IllegalStateException(path+" is not a directory");

return new PagedIterable<GHContent>() {
public PagedIterator<GHContent> iterator() {
return new PagedIterator<GHContent>(owner.root.retrieve().asIterator(url, GHContent[].class)) {
@Override
protected void wrapUp(GHContent[] page) {
GHContent.wrap(page,owner);
}
};
}
};
}

public GHContentUpdateResponse update(String newContent, String commitMessage) throws IOException {
return update(newContent, commitMessage, null);
}
Expand Down
13 changes: 13 additions & 0 deletions src/test/java/org/kohsuke/github/AppTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,19 @@ public void testCreateRelease() throws Exception {
}
}

@Test
public void directoryListing() throws IOException {
List<GHContent> children = gitHub.getRepository("jenkinsci/jenkins").getDirectoryContent("core");
for (GHContent c : children) {
System.out.println(c.getName());
if (c.isDirectory()) {
for (GHContent d : c.listDirectoryContent()) {
System.out.println(" "+d.getName());
}
}
}
}

private void kohsuke() {
String login = getUser().getLogin();
Assume.assumeTrue(login.equals("kohsuke") || login.equals("kohsuke2"));
Expand Down

0 comments on commit 45a6841

Please sign in to comment.