Skip to content

Commit

Permalink
support on-demand data population for the use in code search API.
Browse files Browse the repository at this point in the history
  • Loading branch information
kohsuke committed Mar 22, 2015
1 parent 1ee2ec3 commit 7b436ff
Showing 1 changed file with 40 additions and 13 deletions.
53 changes: 40 additions & 13 deletions src/main/java/org/kohsuke/github/GHContent.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,13 @@
*/
@SuppressWarnings({"UnusedDeclaration"})
public class GHContent {
private GHRepository owner;
/*
In normal use of this class, repository field is set via wrap(),
but in the code search API, there's a nested 'repository' field that gets populated from JSON.
*/
private GHRepository repository;

private GitHub root;

private String type;
private String encoding;
Expand All @@ -31,7 +37,7 @@ public class GHContent {
private String download_url;

public GHRepository getOwner() {
return owner;
return repository;
}

public String getType() {
Expand Down Expand Up @@ -107,13 +113,16 @@ public String getHtmlUrl() {
* Retrieves the actual content stored here.
*/
public InputStream read() throws IOException {
return new Requester(owner.root).read(getDownloadUrl());
return new Requester(root).read(getDownloadUrl());
}

/**
* URL to retrieve the raw content of the file. Null if this is a directory.
*/
public String getDownloadUrl() { return download_url; }
public String getDownloadUrl() throws IOException {
populate();
return download_url;
}

public boolean isFile() {
return "file".equals(type);
Expand All @@ -123,6 +132,16 @@ public boolean isDirectory() {
return "dir".equals(type);
}

/**
* Fully populate the data by retrieving missing data.
*
* Depending on the original API call where this object is created, it may not contain everything.
*/
protected synchronized void populate() throws IOException {
if (download_url!=null) return; // already populated
root.retrieve().to(url, this);
}

/**
* List immediate children of this directory.
*/
Expand All @@ -132,10 +151,10 @@ public PagedIterable<GHContent> listDirectoryContent() throws IOException {

return new PagedIterable<GHContent>() {
public PagedIterator<GHContent> iterator() {
return new PagedIterator<GHContent>(owner.root.retrieve().asIterator(url, GHContent[].class)) {
return new PagedIterator<GHContent>(root.retrieve().asIterator(url, GHContent[].class)) {
@Override
protected void wrapUp(GHContent[] page) {
GHContent.wrap(page,owner);
GHContent.wrap(page, repository);
}
};
}
Expand All @@ -157,7 +176,7 @@ public GHContentUpdateResponse update(byte[] newContentBytes, String commitMessa
public GHContentUpdateResponse update(byte[] newContentBytes, String commitMessage, String branch) throws IOException {
String encodedContent = DatatypeConverter.printBase64Binary(newContentBytes);

Requester requester = new Requester(owner.root)
Requester requester = new Requester(root)
.with("path", path)
.with("message", commitMessage)
.with("sha", sha)
Expand All @@ -170,8 +189,8 @@ public GHContentUpdateResponse update(byte[] newContentBytes, String commitMessa

GHContentUpdateResponse response = requester.to(getApiRoute(), GHContentUpdateResponse.class);

response.getContent().wrap(owner);
response.getCommit().wrapUp(owner);
response.getContent().wrap(repository);
response.getCommit().wrapUp(repository);

this.content = encodedContent;
return response;
Expand All @@ -182,7 +201,7 @@ public GHContentUpdateResponse delete(String message) throws IOException {
}

public GHContentUpdateResponse delete(String commitMessage, String branch) throws IOException {
Requester requester = new Requester(owner.root)
Requester requester = new Requester(root)
.with("path", path)
.with("message", commitMessage)
.with("sha", sha)
Expand All @@ -194,18 +213,26 @@ public GHContentUpdateResponse delete(String commitMessage, String branch) throw

GHContentUpdateResponse response = requester.to(getApiRoute(), GHContentUpdateResponse.class);

response.getCommit().wrapUp(owner);
response.getCommit().wrapUp(repository);
return response;
}

private String getApiRoute() {
return "/repos/" + owner.getOwnerName() + "/" + owner.getName() + "/contents/" + path;
return "/repos/" + repository.getOwnerName() + "/" + repository.getName() + "/contents/" + path;
}

GHContent wrap(GHRepository owner) {
this.owner = owner;
this.repository = owner;
this.root = owner.root;
return this;
}
GHContent wrap(GitHub root) {
this.root = root;
if (repository!=null)
repository.wrap(root);
return this;
}


public static GHContent[] wrap(GHContent[] contents, GHRepository repository) {
for (GHContent unwrappedContent : contents) {
Expand Down

0 comments on commit 7b436ff

Please sign in to comment.