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 2d7d4bb

Browse filesBrowse files
committed
Merge pull request hub4j#320 with some additional changes
2 parents 2e78dc5 + e6ee278 commit 2d7d4bb
Copy full SHA for 2d7d4bb

File tree

Expand file treeCollapse file tree

4 files changed

+112
-2
lines changed
Filter options
Expand file treeCollapse file tree

4 files changed

+112
-2
lines changed
+63Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package org.kohsuke.github;
2+
3+
import org.apache.commons.codec.binary.Base64InputStream;
4+
5+
import java.io.ByteArrayInputStream;
6+
import java.io.InputStream;
7+
import java.io.UnsupportedEncodingException;
8+
import java.net.URL;
9+
10+
/**
11+
* @author Kanstantsin Shautsou
12+
* @author Kohsuke Kawaguchi
13+
* @see GHRepository#getBlob(String)
14+
* @see <a href="https://developer.github.com/v3/git/blobs/#get-a-blob">Get a blob</a>
15+
*/
16+
public class GHBlob {
17+
private String content, encoding, url, sha;
18+
private long size;
19+
20+
/**
21+
* API URL of this blob.
22+
*/
23+
public URL getUrl() {
24+
return GitHub.parseURL(url);
25+
}
26+
27+
public String getSha() {
28+
return sha;
29+
}
30+
31+
/**
32+
* Number of bytes in this blob.
33+
*/
34+
public long getSize() {
35+
return size;
36+
}
37+
38+
public String getEncoding() {
39+
return encoding;
40+
}
41+
42+
/**
43+
* Encoded content. You probably want {@link #read()}
44+
*/
45+
public String getContent() {
46+
return content;
47+
}
48+
49+
/**
50+
* Retrieves the actual bytes of the blob.
51+
*/
52+
public InputStream read() {
53+
if (encoding.equals("base64")) {
54+
try {
55+
return new Base64InputStream(new ByteArrayInputStream(content.getBytes("US-ASCII")), false);
56+
} catch (UnsupportedEncodingException e) {
57+
throw new AssertionError(e); // US-ASCII is mandatory
58+
}
59+
}
60+
61+
throw new UnsupportedOperationException("Unrecognized encoding: "+encoding);
62+
}
63+
}

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

Copy file name to clipboardExpand all lines: src/main/java/org/kohsuke/github/GHRepository.java
+26Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131

3232
import java.io.FileNotFoundException;
3333
import java.io.IOException;
34+
import java.io.InputStream;
3435
import java.io.InputStreamReader;
3536
import java.io.InterruptedIOException;
3637
import java.io.Reader;
@@ -817,6 +818,31 @@ public GHTree getTreeRecursive(String sha, int recursive) throws IOException {
817818
return root.retrieve().to(url, GHTree.class).wrap(root);
818819
}
819820

821+
/**
822+
* Obtains the metadata & the content of a blob.
823+
*
824+
* <p>
825+
* This method retrieves the whole content in memory, so beware when you are dealing with large BLOB.
826+
*
827+
* @see <a href="https://developer.github.com/v3/git/blobs/#get-a-blob">Get a blob</a>
828+
* @see #readBlob(String)
829+
*/
830+
public GHBlob getBlob(String blobSha) throws IOException {
831+
String target = getApiTailUrl("git/blobs/" + blobSha);
832+
return root.retrieve().to(target, GHBlob.class);
833+
}
834+
835+
/**
836+
* Reads the content of a blob as a stream for better efficiency.
837+
*
838+
* @see <a href="https://developer.github.com/v3/git/blobs/#get-a-blob">Get a blob</a>
839+
* @see #getBlob(String)
840+
*/
841+
public InputStream readBlob(String blobSha) throws IOException {
842+
String target = getApiTailUrl("git/blobs/" + blobSha);
843+
return root.retrieve().withHeader("Accept","application/vnd.github.VERSION.raw").asStream(target);
844+
}
845+
820846
/**
821847
* Gets a commit object in this repository.
822848
*/

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

Copy file name to clipboardExpand all lines: src/main/java/org/kohsuke/github/GHTreeEntry.java
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,14 @@ public String getType() {
5454

5555

5656
/**
57-
* SHA1 of this object.
57+
* SHA1 of this blob object.
5858
*/
5959
public String getSha() {
6060
return sha;
6161
}
6262

6363
/**
64-
* API URL to this Git data, such as
64+
* API URL to this Git blob data, such as
6565
* https://api.github.com/repos/jenkinsci
6666
* /jenkins/git/commits/b72322675eb0114363a9a86e9ad5a170d1d07ac0
6767
*/

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

Copy file name to clipboardExpand all lines: src/test/java/org/kohsuke/github/AppTest.java
+21Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import org.kohsuke.github.GHOrganization.Permission;
1313

1414
import java.io.IOException;
15+
import java.io.InputStream;
1516
import java.net.URL;
1617
import java.util.*;
1718
import java.util.Map.Entry;
@@ -903,6 +904,26 @@ public void listOrgMemberships() throws Exception {
903904
}
904905
}
905906

907+
@Test
908+
public void blob() throws Exception {
909+
GHRepository r = gitHub.getRepository("kohsuke/github-api");
910+
String sha1 = "a12243f2fc5b8c2ba47dd677d0b0c7583539584d";
911+
912+
assertBlobContent(r.readBlob(sha1));
913+
914+
GHBlob blob = r.getBlob(sha1);
915+
assertBlobContent(blob.read());
916+
assertThat(blob.getSha(),is("a12243f2fc5b8c2ba47dd677d0b0c7583539584d"));
917+
assertThat(blob.getSize(),is(1104L));
918+
}
919+
920+
private void assertBlobContent(InputStream is) throws Exception {
921+
String content = new String(IOUtils.toByteArray(is),"UTF-8");
922+
assertThat(content,containsString("Copyright (c) 2011- Kohsuke Kawaguchi and other contributors"));
923+
assertThat(content,containsString("FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR"));
924+
assertThat(content.length(),is(1104));
925+
}
926+
906927
private void kohsuke() {
907928
String login = getUser().getLogin();
908929
Assume.assumeTrue(login.equals("kohsuke") || login.equals("kohsuke2"));

0 commit comments

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