Skip to content

Commit 89a446e

Browse files
dsymondsdmitshur
authored andcommitted
Add GitService.GetBlobRaw method. (#891)
This method is similar to GitService.GetBlob, except it uses the "application/vnd.github.v3.raw" media type to fetch the blob content as raw bytes (rather than a JSON object containing a base64-encoded string and other information). GitHub API docs: - https://developer.github.com/v3/git/blobs/#get-a-blob - https://developer.github.com/v3/git/blobs/#custom-media-types Fixes #658.
1 parent 88eb4e9 commit 89a446e

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

github/git_blobs.go

+19-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
package github
77

88
import (
9+
"bytes"
910
"context"
1011
"fmt"
1112
)
@@ -20,7 +21,7 @@ type Blob struct {
2021
NodeID *string `json:"node_id,omitempty"`
2122
}
2223

23-
// GetBlob fetchs a blob from a repo given a SHA.
24+
// GetBlob fetches a blob from a repo given a SHA.
2425
//
2526
// GitHub API docs: https://developer.github.com/v3/git/blobs/#get-a-blob
2627
func (s *GitService) GetBlob(ctx context.Context, owner string, repo string, sha string) (*Blob, *Response, error) {
@@ -38,6 +39,23 @@ func (s *GitService) GetBlob(ctx context.Context, owner string, repo string, sha
3839
return blob, resp, err
3940
}
4041

42+
// GetBlobRaw fetches a blob's contents from a repo.
43+
// Unlike GetBlob, it returns the raw bytes rather than the base64-encoded data.
44+
//
45+
// GitHub API docs: https://developer.github.com/v3/git/blobs/#get-a-blob
46+
func (s *GitService) GetBlobRaw(ctx context.Context, owner, repo, sha string) ([]byte, *Response, error) {
47+
u := fmt.Sprintf("repos/%v/%v/git/blobs/%v", owner, repo, sha)
48+
req, err := s.client.NewRequest("GET", u, nil)
49+
if err != nil {
50+
return nil, nil, err
51+
}
52+
req.Header.Set("Accept", "application/vnd.github.v3.raw")
53+
54+
var buf bytes.Buffer
55+
resp, err := s.client.Do(ctx, req, &buf)
56+
return buf.Bytes(), resp, err
57+
}
58+
4159
// CreateBlob creates a blob object.
4260
//
4361
// GitHub API docs: https://developer.github.com/v3/git/blobs/#create-a-blob

github/git_blobs_test.go

+23
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
package github
77

88
import (
9+
"bytes"
910
"context"
1011
"encoding/json"
1112
"fmt"
@@ -51,6 +52,28 @@ func TestGitService_GetBlob_invalidOwner(t *testing.T) {
5152
testURLParseError(t, err)
5253
}
5354

55+
func TestGitService_GetBlobRaw(t *testing.T) {
56+
client, mux, _, teardown := setup()
57+
defer teardown()
58+
59+
mux.HandleFunc("/repos/o/r/git/blobs/s", func(w http.ResponseWriter, r *http.Request) {
60+
testMethod(t, r, "GET")
61+
testHeader(t, r, "Accept", "application/vnd.github.v3.raw")
62+
63+
fmt.Fprint(w, `raw contents here`)
64+
})
65+
66+
blob, _, err := client.Git.GetBlobRaw(context.Background(), "o", "r", "s")
67+
if err != nil {
68+
t.Errorf("Git.GetBlobRaw returned error: %v", err)
69+
}
70+
71+
want := []byte("raw contents here")
72+
if !bytes.Equal(blob, want) {
73+
t.Errorf("GetBlobRaw returned %q, want %q", blob, want)
74+
}
75+
}
76+
5477
func TestGitService_CreateBlob(t *testing.T) {
5578
client, mux, _, teardown := setup()
5679
defer teardown()

0 commit comments

Comments
 (0)