Add GitHub Contents API read methods

This commit is contained in:
Alexandre COLLIGNON
2013-09-18 11:29:04 +02:00
committed by Matt Farmer
parent 1283006b53
commit 925d26e54c
3 changed files with 129 additions and 1 deletions

View File

@@ -0,0 +1,72 @@
package org.kohsuke.github;
/**
* A Content of a repository.
*
* @author Alexandre COLLIGNON
*/
public final class GHContent {
private GHRepository owner;
private String type;
private String encoding;
private long size;
private String name;
private String path;
private String content;
private String url; // this is the API url
private String git_url; // this is the Blob url
private String html_url; // this is the UI
public GHRepository getOwner() {
return owner;
}
public String getType() {
return type;
}
public String getEncoding() {
return encoding;
}
public long getSize() {
return size;
}
public String getName() {
return name;
}
public String getPath() {
return path;
}
public String getContent() {
return new String(javax.xml.bind.DatatypeConverter.parseBase64Binary(getEncodedContent()));
}
public String getEncodedContent() {
return content;
}
public String getUrl() {
return url;
}
public String getGitUrl() {
return git_url;
}
public String getHtmlUrl() {
return html_url;
}
public boolean isFile() {
return "file".equals(type);
}
public boolean isDirectory() {
return "dir".equals(type);
}
}

View File

@@ -733,7 +733,21 @@ public class GHRepository {
}
return m;
}
public GHContent getFileContent(String path) throws IOException {
return root.retrieve().to(String.format("/repos/%s/%s/contents/%s", owner.login, name, path), GHContent.class);
}
public List<GHContent> getDirectoryContent(String path) throws IOException {
GHContent[] files = root.retrieve().to(String.format("/repos/%s/%s/contents/%s", owner.login, name, path), GHContent[].class);
return Arrays.asList(files);
}
public GHContent getReadme() throws Exception {
return getFileContent("readme");
}
public GHMilestone createMilestone(String title, String description) throws IOException {
return new Requester(root)
.with("title", title).with("description", description).method("POST").to(getApiTailUrl("milestones"), GHMilestone.class).wrap(this);