1
2
3
4
5
6 package github
7
8 import (
9 "bytes"
10 "context"
11 "fmt"
12 )
13
14
15 type Blob struct {
16 Content *string `json:"content,omitempty"`
17 Encoding *string `json:"encoding,omitempty"`
18 SHA *string `json:"sha,omitempty"`
19 Size *int `json:"size,omitempty"`
20 URL *string `json:"url,omitempty"`
21 NodeID *string `json:"node_id,omitempty"`
22 }
23
24
25
26
27 func (s *GitService) GetBlob(ctx context.Context, owner string, repo string, sha string) (*Blob, *Response, error) {
28 u := fmt.Sprintf("repos/%v/%v/git/blobs/%v", owner, repo, sha)
29 req, err := s.client.NewRequest("GET", u, nil)
30 if err != nil {
31 return nil, nil, err
32 }
33
34 blob := new(Blob)
35 resp, err := s.client.Do(ctx, req, blob)
36 if err != nil {
37 return nil, resp, err
38 }
39
40 return blob, resp, nil
41 }
42
43
44
45
46
47 func (s *GitService) GetBlobRaw(ctx context.Context, owner, repo, sha string) ([]byte, *Response, error) {
48 u := fmt.Sprintf("repos/%v/%v/git/blobs/%v", owner, repo, sha)
49 req, err := s.client.NewRequest("GET", u, nil)
50 if err != nil {
51 return nil, nil, err
52 }
53
54 req.Header.Set("Accept", "application/vnd.github.v3.raw")
55
56 var buf bytes.Buffer
57 resp, err := s.client.Do(ctx, req, &buf)
58 if err != nil {
59 return nil, resp, err
60 }
61
62 return buf.Bytes(), resp, nil
63 }
64
65
66
67
68 func (s *GitService) CreateBlob(ctx context.Context, owner string, repo string, blob *Blob) (*Blob, *Response, error) {
69 u := fmt.Sprintf("repos/%v/%v/git/blobs", owner, repo)
70 req, err := s.client.NewRequest("POST", u, blob)
71 if err != nil {
72 return nil, nil, err
73 }
74
75 t := new(Blob)
76 resp, err := s.client.Do(ctx, req, t)
77 if err != nil {
78 return nil, resp, err
79 }
80
81 return t, resp, nil
82 }
83
View as plain text