mirror of
https://github.com/google/go-github.git
synced 2026-10-28 16:34:37 +08:00
89 lines
2.4 KiB
Go
89 lines
2.4 KiB
Go
// Copyright 2013 The go-github AUTHORS. All rights reserved.
|
|
//
|
|
// Use of this source code is governed by a BSD-style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package github
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"fmt"
|
|
)
|
|
|
|
// Blob represents a blob object.
|
|
type Blob struct {
|
|
Content string `json:"content"`
|
|
Encoding *string `json:"encoding,omitempty"`
|
|
SHA *string `json:"sha,omitempty"`
|
|
Size *int `json:"size,omitempty"`
|
|
URL *string `json:"url,omitempty"`
|
|
NodeID *string `json:"node_id,omitempty"`
|
|
}
|
|
|
|
// GetBlob fetches a blob from a repo given a SHA.
|
|
//
|
|
// GitHub API docs: https://docs.github.com/rest/git/blobs?apiVersion=2022-11-28#get-a-blob
|
|
//
|
|
//meta:operation GET /repos/{owner}/{repo}/git/blobs/{file_sha}
|
|
func (s *GitService) GetBlob(ctx context.Context, owner, repo, sha string) (*Blob, *Response, error) {
|
|
u := fmt.Sprintf("repos/%v/%v/git/blobs/%v", owner, repo, sha)
|
|
req, err := s.client.NewRequest(ctx, "GET", u, nil)
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
|
|
var blob *Blob
|
|
resp, err := s.client.Do(req, &blob)
|
|
if err != nil {
|
|
return nil, resp, err
|
|
}
|
|
|
|
return blob, resp, nil
|
|
}
|
|
|
|
// GetBlobRaw fetches a blob's contents from a repo.
|
|
// Unlike GetBlob, it returns the raw bytes rather than the base64-encoded data.
|
|
//
|
|
// GitHub API docs: https://docs.github.com/rest/git/blobs?apiVersion=2022-11-28#get-a-blob
|
|
//
|
|
//meta:operation GET /repos/{owner}/{repo}/git/blobs/{file_sha}
|
|
func (s *GitService) GetBlobRaw(ctx context.Context, owner, repo, sha string) ([]byte, *Response, error) {
|
|
u := fmt.Sprintf("repos/%v/%v/git/blobs/%v", owner, repo, sha)
|
|
req, err := s.client.NewRequest(ctx, "GET", u, nil)
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
|
|
req.Header.Set("Accept", "application/vnd.github.v3.raw")
|
|
|
|
var buf bytes.Buffer
|
|
resp, err := s.client.Do(req, &buf)
|
|
if err != nil {
|
|
return nil, resp, err
|
|
}
|
|
|
|
return buf.Bytes(), resp, nil
|
|
}
|
|
|
|
// CreateBlob creates a blob object.
|
|
//
|
|
// GitHub API docs: https://docs.github.com/rest/git/blobs?apiVersion=2022-11-28#create-a-blob
|
|
//
|
|
//meta:operation POST /repos/{owner}/{repo}/git/blobs
|
|
func (s *GitService) CreateBlob(ctx context.Context, owner, repo string, body Blob) (*Blob, *Response, error) {
|
|
u := fmt.Sprintf("repos/%v/%v/git/blobs", owner, repo)
|
|
req, err := s.client.NewRequest(ctx, "POST", u, body)
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
|
|
var v *Blob
|
|
resp, err := s.client.Do(req, &v)
|
|
if err != nil {
|
|
return nil, resp, err
|
|
}
|
|
|
|
return v, resp, nil
|
|
}
|