github.com/google/go-github/v49@v49.1.0/github/git_blobs.go (about) 1 // Copyright 2013 The go-github AUTHORS. All rights reserved. 2 // 3 // Use of this source code is governed by a BSD-style 4 // license that can be found in the LICENSE file. 5 6 package github 7 8 import ( 9 "bytes" 10 "context" 11 "fmt" 12 ) 13 14 // Blob represents a blob object. 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 // GetBlob fetches a blob from a repo given a SHA. 25 // 26 // GitHub API docs: https://docs.github.com/en/rest/git/blobs#get-a-blob 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 // GetBlobRaw fetches a blob's contents from a repo. 44 // Unlike GetBlob, it returns the raw bytes rather than the base64-encoded data. 45 // 46 // GitHub API docs: https://docs.github.com/en/rest/git/blobs#get-a-blob 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 // CreateBlob creates a blob object. 66 // 67 // GitHub API docs: https://docs.github.com/en/rest/git/blobs#create-a-blob 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 }