Skip to content

Commit

Permalink
Merge pull request #320 with some additional changes
Browse files Browse the repository at this point in the history
  • Loading branch information
kohsuke committed Dec 17, 2016
2 parents 2e78dc5 + e6ee278 commit 2d7d4bb
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 2 deletions.
63 changes: 63 additions & 0 deletions src/main/java/org/kohsuke/github/GHBlob.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package org.kohsuke.github;

import org.apache.commons.codec.binary.Base64InputStream;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URL;

/**
* @author Kanstantsin Shautsou
* @author Kohsuke Kawaguchi
* @see GHRepository#getBlob(String)
* @see <a href="https://developer.github.com/v3/git/blobs/#get-a-blob">Get a blob</a>
*/
public class GHBlob {
private String content, encoding, url, sha;
private long size;

/**
* API URL of this blob.
*/
public URL getUrl() {
return GitHub.parseURL(url);
}

public String getSha() {
return sha;
}

/**
* Number of bytes in this blob.
*/
public long getSize() {
return size;
}

public String getEncoding() {
return encoding;
}

/**
* Encoded content. You probably want {@link #read()}
*/
public String getContent() {
return content;
}

/**
* Retrieves the actual bytes of the blob.
*/
public InputStream read() {
if (encoding.equals("base64")) {
try {
return new Base64InputStream(new ByteArrayInputStream(content.getBytes("US-ASCII")), false);
} catch (UnsupportedEncodingException e) {
throw new AssertionError(e); // US-ASCII is mandatory
}
}

throw new UnsupportedOperationException("Unrecognized encoding: "+encoding);
}
}
26 changes: 26 additions & 0 deletions src/main/java/org/kohsuke/github/GHRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.InterruptedIOException;
import java.io.Reader;
Expand Down Expand Up @@ -817,6 +818,31 @@ public GHTree getTreeRecursive(String sha, int recursive) throws IOException {
return root.retrieve().to(url, GHTree.class).wrap(root);
}

/**
* Obtains the metadata & the content of a blob.
*
* <p>
* This method retrieves the whole content in memory, so beware when you are dealing with large BLOB.
*
* @see <a href="https://developer.github.com/v3/git/blobs/#get-a-blob">Get a blob</a>
* @see #readBlob(String)
*/
public GHBlob getBlob(String blobSha) throws IOException {
String target = getApiTailUrl("git/blobs/" + blobSha);
return root.retrieve().to(target, GHBlob.class);
}

/**
* Reads the content of a blob as a stream for better efficiency.
*
* @see <a href="https://developer.github.com/v3/git/blobs/#get-a-blob">Get a blob</a>
* @see #getBlob(String)
*/
public InputStream readBlob(String blobSha) throws IOException {
String target = getApiTailUrl("git/blobs/" + blobSha);
return root.retrieve().withHeader("Accept","application/vnd.github.VERSION.raw").asStream(target);
}

/**
* Gets a commit object in this repository.
*/
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/kohsuke/github/GHTreeEntry.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,14 @@ public String getType() {


/**
* SHA1 of this object.
* SHA1 of this blob object.
*/
public String getSha() {
return sha;
}

/**
* API URL to this Git data, such as
* API URL to this Git blob data, such as
* https://api.github.com/repos/jenkinsci
* /jenkins/git/commits/b72322675eb0114363a9a86e9ad5a170d1d07ac0
*/
Expand Down
21 changes: 21 additions & 0 deletions src/test/java/org/kohsuke/github/AppTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.kohsuke.github.GHOrganization.Permission;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.*;
import java.util.Map.Entry;
Expand Down Expand Up @@ -903,6 +904,26 @@ public void listOrgMemberships() throws Exception {
}
}

@Test
public void blob() throws Exception {
GHRepository r = gitHub.getRepository("kohsuke/github-api");
String sha1 = "a12243f2fc5b8c2ba47dd677d0b0c7583539584d";

assertBlobContent(r.readBlob(sha1));

GHBlob blob = r.getBlob(sha1);
assertBlobContent(blob.read());
assertThat(blob.getSha(),is("a12243f2fc5b8c2ba47dd677d0b0c7583539584d"));
assertThat(blob.getSize(),is(1104L));
}

private void assertBlobContent(InputStream is) throws Exception {
String content = new String(IOUtils.toByteArray(is),"UTF-8");
assertThat(content,containsString("Copyright (c) 2011- Kohsuke Kawaguchi and other contributors"));
assertThat(content,containsString("FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR"));
assertThat(content.length(),is(1104));
}

private void kohsuke() {
String login = getUser().getLogin();
Assume.assumeTrue(login.equals("kohsuke") || login.equals("kohsuke2"));
Expand Down

0 comments on commit 2d7d4bb

Please sign in to comment.