github.com/google/go-github/v64@v64.0.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/rest/git/blobs#get-a-blob
    27  //
    28  //meta:operation GET /repos/{owner}/{repo}/git/blobs/{file_sha}
    29  func (s *GitService) GetBlob(ctx context.Context, owner string, repo string, sha string) (*Blob, *Response, error) {
    30  	u := fmt.Sprintf("repos/%v/%v/git/blobs/%v", owner, repo, sha)
    31  	req, err := s.client.NewRequest("GET", u, nil)
    32  	if err != nil {
    33  		return nil, nil, err
    34  	}
    35  
    36  	blob := new(Blob)
    37  	resp, err := s.client.Do(ctx, req, blob)
    38  	if err != nil {
    39  		return nil, resp, err
    40  	}
    41  
    42  	return blob, resp, nil
    43  }
    44  
    45  // GetBlobRaw fetches a blob's contents from a repo.
    46  // Unlike GetBlob, it returns the raw bytes rather than the base64-encoded data.
    47  //
    48  // GitHub API docs: https://docs.github.com/rest/git/blobs#get-a-blob
    49  //
    50  //meta:operation GET /repos/{owner}/{repo}/git/blobs/{file_sha}
    51  func (s *GitService) GetBlobRaw(ctx context.Context, owner, repo, sha string) ([]byte, *Response, error) {
    52  	u := fmt.Sprintf("repos/%v/%v/git/blobs/%v", owner, repo, sha)
    53  	req, err := s.client.NewRequest("GET", u, nil)
    54  	if err != nil {
    55  		return nil, nil, err
    56  	}
    57  
    58  	req.Header.Set("Accept", "application/vnd.github.v3.raw")
    59  
    60  	var buf bytes.Buffer
    61  	resp, err := s.client.Do(ctx, req, &buf)
    62  	if err != nil {
    63  		return nil, resp, err
    64  	}
    65  
    66  	return buf.Bytes(), resp, nil
    67  }
    68  
    69  // CreateBlob creates a blob object.
    70  //
    71  // GitHub API docs: https://docs.github.com/rest/git/blobs#create-a-blob
    72  //
    73  //meta:operation POST /repos/{owner}/{repo}/git/blobs
    74  func (s *GitService) CreateBlob(ctx context.Context, owner string, repo string, blob *Blob) (*Blob, *Response, error) {
    75  	u := fmt.Sprintf("repos/%v/%v/git/blobs", owner, repo)
    76  	req, err := s.client.NewRequest("POST", u, blob)
    77  	if err != nil {
    78  		return nil, nil, err
    79  	}
    80  
    81  	t := new(Blob)
    82  	resp, err := s.client.Do(ctx, req, t)
    83  	if err != nil {
    84  		return nil, resp, err
    85  	}
    86  
    87  	return t, resp, nil
    88  }