Skip to content
This repository was archived by the owner on Jul 31, 2025. It is now read-only.

Commit bad0d1b

Browse files
committed
1 parent aa43e26 commit bad0d1b

3 files changed

Lines changed: 172 additions & 0 deletions

File tree

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -641,6 +641,35 @@ public GHRef getRef(String refName) throws IOException {
641641
return root.retrieve().to(String.format("/repos/%s/%s/git/refs/%s", owner.login, name, refName), GHRef.class).wrap(root);
642642
}
643643
/**
644+
* Retrive a tree of the given type for the current GitHub repository.
645+
*
646+
* @param sha - sha number or branch name ex: "master"
647+
* @return refs matching the request type
648+
* @throws IOException
649+
* on failure communicating with GitHub, potentially due to an
650+
* invalid tree type being requested
651+
*/
652+
public GHTree getTree(String sha) throws IOException {
653+
String url = String.format("/repos/%s/%s/git/trees/%s", owner.login, name, sha);
654+
return root.retrieve().to(url, GHTree.class).wrap(root);
655+
}
656+
657+
/**
658+
* Retrieves the tree for the current GitHub repository, recursively as described in here:
659+
* https://developer.github.com/v3/git/trees/#get-a-tree-recursively
660+
*
661+
* @param sha - sha number or branch name ex: "master"
662+
* @param recursive use 1
663+
* @throws IOException
664+
* on failure communicating with GitHub, potentially due to an
665+
* invalid tree type being requested
666+
*/
667+
public GHTree getTreeRecursive(String sha, int recursive) throws IOException {
668+
String url = String.format("/repos/%s/%s/git/trees/%s?recursive=%d", owner.login, name, sha, recursive);
669+
return root.retrieve().to(url, GHTree.class).wrap(root);
670+
}
671+
672+
/**
644673
* Gets a commit object in this repository.
645674
*/
646675
public GHCommit getCommit(String sha1) throws IOException {
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
package org.kohsuke.github;
2+
3+
import java.net.URL;
4+
5+
/**
6+
* Provides information for Git Trees
7+
* https://developer.github.com/v3/git/trees/
8+
*
9+
* @author Daniel Teixeira - https://github.com/ddtxra
10+
*/
11+
public class GHTree {
12+
/* package almost final */GitHub root;
13+
14+
private boolean truncated;
15+
private String sha, url;
16+
private GHTreeEntry[] tree;
17+
18+
/**
19+
* The SHA for this trees
20+
*/
21+
public String getSha() {
22+
return sha;
23+
}
24+
25+
/**
26+
* Return an array of entries of the trees
27+
* @return
28+
*/
29+
public GHTreeEntry[] getTree() {
30+
return tree;
31+
}
32+
33+
/**
34+
* Returns true if the number of items in the tree array exceeded the GitHub maximum limit.
35+
* @return true true if the number of items in the tree array exceeded the GitHub maximum limit otherwise false.
36+
*/
37+
public boolean isTruncated() {
38+
return truncated;
39+
}
40+
41+
/**
42+
* The API URL of this tag, such as
43+
* "url": "https://api.github.com/repos/octocat/Hello-World/trees/fc6274d15fa3ae2ab983129fb037999f264ba9a7",
44+
*/
45+
public URL getUrl() {
46+
return GitHub.parseURL(url);
47+
}
48+
49+
/* package */GHTree wrap(GitHub root) {
50+
this.root = root;
51+
return this;
52+
}
53+
54+
public static class GHTreeEntry {
55+
private String path, mode, type, sha, url;
56+
private long size;
57+
58+
/**
59+
* Get the path such as
60+
* "subdir/file.txt"
61+
*
62+
* @return the path
63+
*/
64+
public String getPath() {
65+
return path;
66+
}
67+
68+
/**
69+
* Get mode such as
70+
* 100644
71+
*
72+
* @return the mode
73+
*/
74+
public String getMode() {
75+
return mode;
76+
}
77+
78+
/**
79+
* Gets the size of the file, such as
80+
* 132
81+
* @return The size of the path or 0 if it is a directory
82+
*/
83+
public long getSize() {
84+
return size;
85+
}
86+
87+
/**
88+
* Gets the type such as:
89+
* "blob"
90+
*
91+
* @return The type
92+
*/
93+
public String getType() {
94+
return type;
95+
}
96+
97+
98+
/**
99+
* SHA1 of this object.
100+
*/
101+
public String getSha() {
102+
return sha;
103+
}
104+
105+
/**
106+
* API URL to this Git data, such as
107+
* https://api.github.com/repos/jenkinsci
108+
* /jenkins/git/commits/b72322675eb0114363a9a86e9ad5a170d1d07ac0
109+
*/
110+
public URL getUrl() {
111+
return GitHub.parseURL(url);
112+
}
113+
}
114+
}

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@
33
import com.google.common.base.Predicate;
44
import com.google.common.collect.Iterables;
55
import com.google.common.collect.Lists;
6+
67
import org.junit.Assume;
78
import org.junit.Test;
89
import org.kohsuke.github.GHCommit.File;
910
import org.kohsuke.github.GHOrganization.Permission;
11+
import org.kohsuke.github.GHTree.GHTreeEntry;
1012

1113
import java.io.IOException;
1214
import java.net.URL;
@@ -665,6 +667,33 @@ public void testReadme() throws IOException {
665667
assertEquals(readme.getName(),"README.md");
666668
assertEquals(readme.getContent(),"This is a markdown readme.\n");
667669
}
670+
671+
672+
@Test
673+
public void testTrees() throws IOException {
674+
GHTree masterTree = gitHub.getRepository("kohsuke/github-api").getTree("master");
675+
boolean foundReadme = false;
676+
for(GHTreeEntry e : masterTree.getTree()){
677+
if("readme".equalsIgnoreCase(e.getPath().replaceAll(".md", ""))){
678+
foundReadme = true;
679+
break;
680+
}
681+
}
682+
assertTrue(foundReadme);
683+
}
684+
685+
@Test
686+
public void testTreesRecursive() throws IOException {
687+
GHTree masterTree = gitHub.getRepository("kohsuke/github-api").getTreeRecursive("master", 1);
688+
boolean foundThisFile = false;
689+
for(GHTreeEntry e : masterTree.getTree()){
690+
if(e.getPath().endsWith(AppTest.class.getSimpleName() + ".java")){
691+
foundThisFile = true;
692+
break;
693+
}
694+
}
695+
assertTrue(foundThisFile);
696+
}
668697

669698
@Test
670699
public void testRepoLabel() throws IOException {

0 commit comments

Comments
 (0)