Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 925d26e

Browse filesBrowse files
acollignfarmdawgnation
authored andcommitted
Add GitHub Contents API read methods
1 parent 1283006 commit 925d26e
Copy full SHA for 925d26e

File tree

Expand file treeCollapse file tree

3 files changed

+129
-1
lines changed
Filter options
Expand file treeCollapse file tree

3 files changed

+129
-1
lines changed
+72Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package org.kohsuke.github;
2+
3+
/**
4+
* A Content of a repository.
5+
*
6+
* @author Alexandre COLLIGNON
7+
*/
8+
public final class GHContent {
9+
private GHRepository owner;
10+
11+
private String type;
12+
private String encoding;
13+
private long size;
14+
private String name;
15+
private String path;
16+
private String content;
17+
private String url; // this is the API url
18+
private String git_url; // this is the Blob url
19+
private String html_url; // this is the UI
20+
21+
public GHRepository getOwner() {
22+
return owner;
23+
}
24+
25+
public String getType() {
26+
return type;
27+
}
28+
29+
public String getEncoding() {
30+
return encoding;
31+
}
32+
33+
public long getSize() {
34+
return size;
35+
}
36+
37+
public String getName() {
38+
return name;
39+
}
40+
41+
public String getPath() {
42+
return path;
43+
}
44+
45+
public String getContent() {
46+
return new String(javax.xml.bind.DatatypeConverter.parseBase64Binary(getEncodedContent()));
47+
}
48+
49+
public String getEncodedContent() {
50+
return content;
51+
}
52+
53+
public String getUrl() {
54+
return url;
55+
}
56+
57+
public String getGitUrl() {
58+
return git_url;
59+
}
60+
61+
public String getHtmlUrl() {
62+
return html_url;
63+
}
64+
65+
public boolean isFile() {
66+
return "file".equals(type);
67+
}
68+
69+
public boolean isDirectory() {
70+
return "dir".equals(type);
71+
}
72+
}

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

Copy file name to clipboardExpand all lines: src/main/java/org/kohsuke/github/GHRepository.java
+15-1Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -733,7 +733,21 @@ public GHMilestone getMilestone(int number) throws IOException {
733733
}
734734
return m;
735735
}
736-
736+
737+
public GHContent getFileContent(String path) throws IOException {
738+
return root.retrieve().to(String.format("/repos/%s/%s/contents/%s", owner.login, name, path), GHContent.class);
739+
}
740+
741+
public List<GHContent> getDirectoryContent(String path) throws IOException {
742+
GHContent[] files = root.retrieve().to(String.format("/repos/%s/%s/contents/%s", owner.login, name, path), GHContent[].class);
743+
744+
return Arrays.asList(files);
745+
}
746+
747+
public GHContent getReadme() throws Exception {
748+
return getFileContent("readme");
749+
}
750+
737751
public GHMilestone createMilestone(String title, String description) throws IOException {
738752
return new Requester(root)
739753
.with("title", title).with("description", description).method("POST").to(getApiTailUrl("milestones"), GHMilestone.class).wrap(this);
+42Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package org.kohsuke.github;
2+
3+
import junit.framework.TestCase;
4+
5+
import java.util.List;
6+
7+
/**
8+
* Unit test for {@link GHContent}.
9+
*/
10+
public class GHContentIntegrationTest extends TestCase {
11+
12+
private GitHub gitHub;
13+
private GHRepository repo;
14+
15+
@Override
16+
public void setUp() throws Exception {
17+
super.setUp();
18+
// we just read at the moment
19+
gitHub = GitHub.connectAnonymously();
20+
repo = gitHub.getUser("acollign").getRepository("github-api-test");
21+
}
22+
23+
public void testGetFileContent() throws Exception {
24+
GHContent content = repo.getFileContent("ghcontent-ro/a-file-with-content");
25+
26+
assertTrue(content.isFile());
27+
assertEquals("thanks for reading me\n", content.getContent());
28+
}
29+
30+
public void testGetEmptyFileContent() throws Exception {
31+
GHContent content = repo.getFileContent("ghcontent-ro/an-empty-file");
32+
33+
assertTrue(content.isFile());
34+
assertEquals("", content.getContent());
35+
}
36+
37+
public void testGetDirectoryContent() throws Exception {
38+
List<GHContent> entries = repo.getDirectoryContent("ghcontent-ro/a-dir-with-3-entries");
39+
40+
assertTrue(entries.size() == 3);
41+
}
42+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.