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

Commit ce7ca59

Browse files
committed
Merge pull request hub4j#155
2 parents d95c8a4 + 76610b2 commit ce7ca59

4 files changed

Lines changed: 186 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
@@ -653,6 +653,35 @@ public GHRef getRef(String refName) throws IOException {
653653
return root.retrieve().to(String.format("/repos/%s/%s/git/refs/%s", owner.login, name, refName), GHRef.class).wrap(root);
654654
}
655655
/**
656+
* Retrive a tree of the given type for the current GitHub repository.
657+
*
658+
* @param sha - sha number or branch name ex: "master"
659+
* @return refs matching the request type
660+
* @throws IOException
661+
* on failure communicating with GitHub, potentially due to an
662+
* invalid tree type being requested
663+
*/
664+
public GHTree getTree(String sha) throws IOException {
665+
String url = String.format("/repos/%s/%s/git/trees/%s", owner.login, name, sha);
666+
return root.retrieve().to(url, GHTree.class).wrap(root);
667+
}
668+
669+
/**
670+
* Retrieves the tree for the current GitHub repository, recursively as described in here:
671+
* https://developer.github.com/v3/git/trees/#get-a-tree-recursively
672+
*
673+
* @param sha - sha number or branch name ex: "master"
674+
* @param recursive use 1
675+
* @throws IOException
676+
* on failure communicating with GitHub, potentially due to an
677+
* invalid tree type being requested
678+
*/
679+
public GHTree getTreeRecursive(String sha, int recursive) throws IOException {
680+
String url = String.format("/repos/%s/%s/git/trees/%s?recursive=%d", owner.login, name, sha, recursive);
681+
return root.retrieve().to(url, GHTree.class).wrap(root);
682+
}
683+
684+
/**
656685
* Gets a commit object in this repository.
657686
*/
658687
public GHCommit getCommit(String sha1) throws IOException {
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package org.kohsuke.github;
2+
3+
import java.net.URL;
4+
import java.util.Arrays;
5+
import java.util.Collections;
6+
import java.util.List;
7+
8+
/**
9+
* Provides information for Git Trees
10+
* https://developer.github.com/v3/git/trees/
11+
*
12+
* @author Daniel Teixeira - https://github.com/ddtxra
13+
* @see GHRepository#getTree(String)
14+
*/
15+
public class GHTree {
16+
/* package almost final */GitHub root;
17+
18+
private boolean truncated;
19+
private String sha, url;
20+
private GHTreeEntry[] tree;
21+
22+
/**
23+
* The SHA for this trees
24+
*/
25+
public String getSha() {
26+
return sha;
27+
}
28+
29+
/**
30+
* Return an array of entries of the trees
31+
* @return
32+
*/
33+
public List<GHTreeEntry> getTree() {
34+
return Collections.unmodifiableList(Arrays.asList(tree));
35+
}
36+
37+
/**
38+
* Returns true if the number of items in the tree array exceeded the GitHub maximum limit.
39+
* @return true true if the number of items in the tree array exceeded the GitHub maximum limit otherwise false.
40+
*/
41+
public boolean isTruncated() {
42+
return truncated;
43+
}
44+
45+
/**
46+
* The API URL of this tag, such as
47+
* "url": "https://api.github.com/repos/octocat/Hello-World/trees/fc6274d15fa3ae2ab983129fb037999f264ba9a7",
48+
*/
49+
public URL getUrl() {
50+
return GitHub.parseURL(url);
51+
}
52+
53+
/* package */GHTree wrap(GitHub root) {
54+
this.root = root;
55+
return this;
56+
}
57+
58+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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+
* @see GHTree
11+
*/
12+
public class GHTreeEntry {
13+
private String path, mode, type, sha, url;
14+
private long size;
15+
16+
/**
17+
* Get the path such as
18+
* "subdir/file.txt"
19+
*
20+
* @return the path
21+
*/
22+
public String getPath() {
23+
return path;
24+
}
25+
26+
/**
27+
* Get mode such as
28+
* 100644
29+
*
30+
* @return the mode
31+
*/
32+
public String getMode() {
33+
return mode;
34+
}
35+
36+
/**
37+
* Gets the size of the file, such as
38+
* 132
39+
* @return The size of the path or 0 if it is a directory
40+
*/
41+
public long getSize() {
42+
return size;
43+
}
44+
45+
/**
46+
* Gets the type such as:
47+
* "blob"
48+
*
49+
* @return The type
50+
*/
51+
public String getType() {
52+
return type;
53+
}
54+
55+
56+
/**
57+
* SHA1 of this object.
58+
*/
59+
public String getSha() {
60+
return sha;
61+
}
62+
63+
/**
64+
* API URL to this Git data, such as
65+
* https://api.github.com/repos/jenkinsci
66+
* /jenkins/git/commits/b72322675eb0114363a9a86e9ad5a170d1d07ac0
67+
*/
68+
public URL getUrl() {
69+
return GitHub.parseURL(url);
70+
}
71+
}

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
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;
@@ -665,6 +666,33 @@ public void testReadme() throws IOException {
665666
assertEquals(readme.getName(),"README.md");
666667
assertEquals(readme.getContent(),"This is a markdown readme.\n");
667668
}
669+
670+
671+
@Test
672+
public void testTrees() throws IOException {
673+
GHTree masterTree = gitHub.getRepository("kohsuke/github-api").getTree("master");
674+
boolean foundReadme = false;
675+
for(GHTreeEntry e : masterTree.getTree()){
676+
if("readme".equalsIgnoreCase(e.getPath().replaceAll(".md", ""))){
677+
foundReadme = true;
678+
break;
679+
}
680+
}
681+
assertTrue(foundReadme);
682+
}
683+
684+
@Test
685+
public void testTreesRecursive() throws IOException {
686+
GHTree masterTree = gitHub.getRepository("kohsuke/github-api").getTreeRecursive("master", 1);
687+
boolean foundThisFile = false;
688+
for(GHTreeEntry e : masterTree.getTree()){
689+
if(e.getPath().endsWith(AppTest.class.getSimpleName() + ".java")){
690+
foundThisFile = true;
691+
break;
692+
}
693+
}
694+
assertTrue(foundThisFile);
695+
}
668696

669697
@Test
670698
public void testRepoLabel() throws IOException {

0 commit comments

Comments
 (0)