...

Source file src/github.com/google/go-github/v33/github/git_blobs.go

Documentation: github.com/google/go-github/v33/github

     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/free-pro-team@latest/rest/reference/git/#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  	return blob, resp, err
    37  }
    38  
    39  // GetBlobRaw fetches a blob's contents from a repo.
    40  // Unlike GetBlob, it returns the raw bytes rather than the base64-encoded data.
    41  //
    42  // GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/git/#get-a-blob
    43  func (s *GitService) GetBlobRaw(ctx context.Context, owner, repo, sha string) ([]byte, *Response, error) {
    44  	u := fmt.Sprintf("repos/%v/%v/git/blobs/%v", owner, repo, sha)
    45  	req, err := s.client.NewRequest("GET", u, nil)
    46  	if err != nil {
    47  		return nil, nil, err
    48  	}
    49  	req.Header.Set("Accept", "application/vnd.github.v3.raw")
    50  
    51  	var buf bytes.Buffer
    52  	resp, err := s.client.Do(ctx, req, &buf)
    53  	return buf.Bytes(), resp, err
    54  }
    55  
    56  // CreateBlob creates a blob object.
    57  //
    58  // GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/git/#create-a-blob
    59  func (s *GitService) CreateBlob(ctx context.Context, owner string, repo string, blob *Blob) (*Blob, *Response, error) {
    60  	u := fmt.Sprintf("repos/%v/%v/git/blobs", owner, repo)
    61  	req, err := s.client.NewRequest("POST", u, blob)
    62  	if err != nil {
    63  		return nil, nil, err
    64  	}
    65  
    66  	t := new(Blob)
    67  	resp, err := s.client.Do(ctx, req, t)
    68  	return t, resp, err
    69  }
    70  

View as plain text