Skip to content

Commit a454fb1

Browse files
committed
Tree traversal from commit & its associated tests
1 parent b5386a3 commit a454fb1

3 files changed

Lines changed: 32 additions & 0 deletions

File tree

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@ public static class ShortInfo {
3737

3838
private int comment_count;
3939

40+
static class Tree {
41+
String sha;
42+
}
43+
44+
private Tree tree;
45+
4046
@WithBridgeMethods(value = GHAuthor.class, castRequired = true)
4147
public GitUser getAuthor() {
4248
return author;
@@ -224,6 +230,13 @@ public int getLinesDeleted() throws IOException {
224230
return stats.deletions;
225231
}
226232

233+
/**
234+
* Use this method to walk the tree
235+
*/
236+
public GHTree getTree() throws IOException {
237+
return owner.getTree(getCommitShortInfo().tree.sha);
238+
}
239+
227240
/**
228241
* URL of this commit like "https://github.com/kohsuke/sandbox-ant/commit/8ae38db0ea5837313ab5f39d43a6f73de3bd9000"
229242
*/

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
* https://developer.github.com/v3/git/trees/
1111
*
1212
* @author Daniel Teixeira - https://github.com/ddtxra
13+
* @see GHCommit#getTree()
1314
* @see GHRepository#getTree(String)
1415
* @see GHTreeEntry#asTree()
1516
*/
@@ -35,6 +36,19 @@ public List<GHTreeEntry> getTree() {
3536
return Collections.unmodifiableList(Arrays.asList(tree));
3637
}
3738

39+
/**
40+
* Finds a tree entry by its name.
41+
*
42+
* IOW, find a directory entry by a file name.
43+
*/
44+
public GHTreeEntry getEntry(String path) {
45+
for (GHTreeEntry e : tree) {
46+
if (e.getPath().equals(path))
47+
return e;
48+
}
49+
return null;
50+
}
51+
3852
/**
3953
* Returns true if the number of items in the tree array exceeded the GitHub maximum limit.
4054
* @return true true if the number of items in the tree array exceeded the GitHub maximum limit otherwise false.

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,11 @@ public void testCommit() throws Exception {
363363
assertEquals(48,f.getLinesChanged());
364364
assertEquals("modified",f.getStatus());
365365
assertEquals("changelog.html", f.getFileName());
366+
367+
// walk the tree
368+
GHTree t = commit.getTree();
369+
assertThat(IOUtils.toString(t.getEntry("todo.txt").readAsBlob()), containsString("executor rendering"));
370+
assertNotNull(t.getEntry("war").asTree());
366371
}
367372

368373
@Test

0 commit comments

Comments
 (0)