From 45a6841772e1be158f88f358227259a43d9c2037 Mon Sep 17 00:00:00 2001 From: Kohsuke Kawaguchi Date: Tue, 27 May 2014 20:57:42 -0700 Subject: [PATCH] added a method to visit sub-directory --- .../java/org/kohsuke/github/GHContent.java | 21 +++++++++++++++++++ src/test/java/org/kohsuke/github/AppTest.java | 13 ++++++++++++ 2 files changed, 34 insertions(+) diff --git a/src/main/java/org/kohsuke/github/GHContent.java b/src/main/java/org/kohsuke/github/GHContent.java index efb5447ae5..443dd5918d 100644 --- a/src/main/java/org/kohsuke/github/GHContent.java +++ b/src/main/java/org/kohsuke/github/GHContent.java @@ -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; @@ -107,6 +109,25 @@ public boolean isDirectory() { return "dir".equals(type); } + /** + * List immediate children of this directory. + */ + public PagedIterable listDirectoryContent() throws IOException { + if (!isDirectory()) + throw new IllegalStateException(path+" is not a directory"); + + return new PagedIterable() { + public PagedIterator iterator() { + return new PagedIterator(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); } diff --git a/src/test/java/org/kohsuke/github/AppTest.java b/src/test/java/org/kohsuke/github/AppTest.java index d12b44683f..f333d26204 100644 --- a/src/test/java/org/kohsuke/github/AppTest.java +++ b/src/test/java/org/kohsuke/github/AppTest.java @@ -529,6 +529,19 @@ public void testCreateRelease() throws Exception { } } + @Test + public void directoryListing() throws IOException { + List 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"));